ofp-util: Add OFPUTIL_P_OF12 and NXFF_OPENFLOW12
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OFP_UTIL_H
18 #define OFP_UTIL_H 1
19
20 #include <assert.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 #include "classifier.h"
25 #include "compiler.h"
26 #include "flow.h"
27 #include "netdev.h"
28 #include "openflow/nicira-ext.h"
29 #include "openvswitch/types.h"
30
31 struct cls_rule;
32 struct ofpbuf;
33
34 /* Basic decoding and length validation of OpenFlow messages. */
35 enum ofputil_msg_code {
36     OFPUTIL_MSG_INVALID,
37
38     /* OFPT_* messages. */
39     OFPUTIL_OFPT_HELLO,
40     OFPUTIL_OFPT_ERROR,
41     OFPUTIL_OFPT_ECHO_REQUEST,
42     OFPUTIL_OFPT_ECHO_REPLY,
43     OFPUTIL_OFPT_FEATURES_REQUEST,
44     OFPUTIL_OFPT_FEATURES_REPLY,
45     OFPUTIL_OFPT_GET_CONFIG_REQUEST,
46     OFPUTIL_OFPT_GET_CONFIG_REPLY,
47     OFPUTIL_OFPT_SET_CONFIG,
48     OFPUTIL_OFPT_PACKET_IN,
49     OFPUTIL_OFPT_FLOW_REMOVED,
50     OFPUTIL_OFPT_PORT_STATUS,
51     OFPUTIL_OFPT_PACKET_OUT,
52     OFPUTIL_OFPT_FLOW_MOD,
53     OFPUTIL_OFPT_PORT_MOD,
54     OFPUTIL_OFPT_BARRIER_REQUEST,
55     OFPUTIL_OFPT_BARRIER_REPLY,
56     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST,
57     OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY,
58
59     /* OFPST_* stat requests. */
60     OFPUTIL_OFPST_DESC_REQUEST,
61     OFPUTIL_OFPST_FLOW_REQUEST,
62     OFPUTIL_OFPST_AGGREGATE_REQUEST,
63     OFPUTIL_OFPST_TABLE_REQUEST,
64     OFPUTIL_OFPST_PORT_REQUEST,
65     OFPUTIL_OFPST_QUEUE_REQUEST,
66     OFPUTIL_OFPST_PORT_DESC_REQUEST,
67
68     /* OFPST_* stat replies. */
69     OFPUTIL_OFPST_DESC_REPLY,
70     OFPUTIL_OFPST_FLOW_REPLY,
71     OFPUTIL_OFPST_QUEUE_REPLY,
72     OFPUTIL_OFPST_PORT_REPLY,
73     OFPUTIL_OFPST_TABLE_REPLY,
74     OFPUTIL_OFPST_AGGREGATE_REPLY,
75     OFPUTIL_OFPST_PORT_DESC_REPLY,
76
77     /* NXT_* messages. */
78     OFPUTIL_NXT_ROLE_REQUEST,
79     OFPUTIL_NXT_ROLE_REPLY,
80     OFPUTIL_NXT_SET_FLOW_FORMAT,
81     OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
82     OFPUTIL_NXT_FLOW_MOD,
83     OFPUTIL_NXT_FLOW_REMOVED,
84     OFPUTIL_NXT_SET_PACKET_IN_FORMAT,
85     OFPUTIL_NXT_PACKET_IN,
86     OFPUTIL_NXT_FLOW_AGE,
87     OFPUTIL_NXT_SET_ASYNC_CONFIG,
88     OFPUTIL_NXT_SET_CONTROLLER_ID,
89     OFPUTIL_NXT_FLOW_MONITOR_CANCEL,
90     OFPUTIL_NXT_FLOW_MONITOR_PAUSED,
91     OFPUTIL_NXT_FLOW_MONITOR_RESUMED,
92
93     /* NXST_* stat requests. */
94     OFPUTIL_NXST_FLOW_REQUEST,
95     OFPUTIL_NXST_AGGREGATE_REQUEST,
96     OFPUTIL_NXST_FLOW_MONITOR_REQUEST,
97
98     /* NXST_* stat replies. */
99     OFPUTIL_NXST_FLOW_REPLY,
100     OFPUTIL_NXST_AGGREGATE_REPLY,
101     OFPUTIL_NXST_FLOW_MONITOR_REPLY,
102 };
103
104 struct ofputil_msg_type;
105 enum ofperr ofputil_decode_msg_type(const struct ofp_header *,
106                                     const struct ofputil_msg_type **);
107 enum ofperr ofputil_decode_msg_type_partial(const struct ofp_header *,
108                                             size_t length,
109                                             const struct ofputil_msg_type **);
110 enum ofputil_msg_code ofputil_msg_type_code(const struct ofputil_msg_type *);
111 const char *ofputil_msg_type_name(const struct ofputil_msg_type *);
112
113 /* Port numbers. */
114 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port);
115 ovs_be32 ofputil_port_to_ofp11(uint16_t ofp10_port);
116
117 enum ofperr ofputil_check_output_port(uint16_t ofp_port, int max_ports);
118 bool ofputil_port_from_string(const char *, uint16_t *port);
119 void ofputil_format_port(uint16_t port, struct ds *);
120
121 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
122  * to and from IP bitmasks. */
123 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
124 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
125
126 /* Protocols.
127  *
128  * These are arranged from most portable to least portable, or alternatively
129  * from least powerful to most powerful.  Formats earlier on the list are more
130  * likely to be understood for the purpose of making requests, but formats
131  * later on the list are more likely to accurately describe a flow within a
132  * switch.
133  *
134  * On any given OpenFlow connection, a single protocol is in effect at any
135  * given time.  These values use separate bits only because that makes it easy
136  * to test whether a particular protocol is within a given set of protocols and
137  * to implement set union and intersection.
138  */
139 enum ofputil_protocol {
140     /* OpenFlow 1.0-based protocols. */
141     OFPUTIL_P_OF10     = 1 << 0, /* OpenFlow 1.0 flow format. */
142     OFPUTIL_P_OF10_TID = 1 << 1, /* OF1.0 + flow_mod_table_id extension. */
143 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10 | OFPUTIL_P_OF10_TID)
144
145     /* OpenFlow 1.0 with NXM-based flow formats. */
146     OFPUTIL_P_NXM      = 1 << 2, /* Nicira extended match. */
147     OFPUTIL_P_NXM_TID  = 1 << 3, /* NXM + flow_mod_table_id extension. */
148 #define OFPUTIL_P_NXM_ANY (OFPUTIL_P_NXM | OFPUTIL_P_NXM_TID)
149
150     /* OpenFlow 1.2 */
151     OFPUTIL_P_OF12      = 1 << 4, /* OpenFlow 1.2 flow format. */
152
153     /* All protocols. */
154 #define OFPUTIL_P_ANY (OFPUTIL_P_OF10_ANY | OFPUTIL_P_NXM_ANY)
155
156     /* Protocols in which a specific table may be specified in flow_mods. */
157 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_TID | OFPUTIL_P_NXM_TID)
158 };
159
160 /* Protocols to use for flow dumps, from most to least preferred. */
161 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
162 extern size_t ofputil_n_flow_dump_protocols;
163
164 enum ofputil_protocol ofputil_protocol_from_ofp_version(int version);
165 uint8_t ofputil_protocol_to_ofp_version(enum ofputil_protocol);
166
167 bool ofputil_protocol_is_valid(enum ofputil_protocol);
168 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
169                                                bool enable);
170 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
171 enum ofputil_protocol ofputil_protocol_set_base(
172     enum ofputil_protocol cur, enum ofputil_protocol new_base);
173
174 const char *ofputil_protocol_to_string(enum ofputil_protocol);
175 char *ofputil_protocols_to_string(enum ofputil_protocol);
176 enum ofputil_protocol ofputil_protocols_from_string(const char *);
177 enum ofputil_protocol ofputil_usable_protocols(const struct cls_rule *);
178
179 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
180                                            enum ofputil_protocol want,
181                                            enum ofputil_protocol *next);
182
183 /* nx_flow_format */
184 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
185 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
186 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
187 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
188
189 /* Work with ofp10_match. */
190 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
191 void ofputil_cls_rule_from_ofp10_match(const struct ofp10_match *,
192                                        unsigned int priority,
193                                        struct cls_rule *);
194 void ofputil_normalize_rule(struct cls_rule *);
195 void ofputil_cls_rule_to_ofp10_match(const struct cls_rule *,
196                                      struct ofp10_match *);
197
198 /* Work with ofp11_match. */
199 enum ofperr ofputil_cls_rule_from_ofp11_match(const struct ofp11_match *,
200                                               unsigned int priority,
201                                               struct cls_rule *);
202 void ofputil_cls_rule_to_ofp11_match(const struct cls_rule *,
203                                      struct ofp11_match *);
204
205 /* dl_type translation between OpenFlow and 'struct flow' format. */
206 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
207 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
208
209 /* PACKET_IN. */
210 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
211 int ofputil_packet_in_format_from_string(const char *);
212 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
213 struct ofpbuf *ofputil_make_set_packet_in_format(enum nx_packet_in_format);
214
215 /* NXT_FLOW_MOD_TABLE_ID extension. */
216 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
217
218 /* Protocol-independent flow_mod.
219  *
220  * The handling of cookies across multiple versions of OpenFlow is a bit
221  * confusing.  A full description of Open vSwitch's cookie handling is
222  * in the DESIGN file.  The following table shows the expected values of
223  * the cookie-related fields for the different flow_mod commands in
224  * OpenFlow 1.0 ("OF10") and NXM.  "<used>" and "-" indicate a value
225  * that may be populated and an ignored field, respectively.
226  *
227  *               cookie  cookie_mask  new_cookie
228  *               ======  ===========  ==========
229  * OF10 Add        -          0         <used>
230  * OF10 Modify     -          0         <used>
231  * OF10 Delete     -          0           -
232  * NXM Add         -          0         <used>
233  * NXM Modify    <used>     <used>      <used>
234  * NXM Delete    <used>     <used>        -
235  */
236 struct ofputil_flow_mod {
237     struct cls_rule cr;
238     ovs_be64 cookie;         /* Cookie bits to match. */
239     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
240     ovs_be64 new_cookie;     /* New cookie to install or -1. */
241     uint8_t table_id;
242     uint16_t command;
243     uint16_t idle_timeout;
244     uint16_t hard_timeout;
245     uint32_t buffer_id;
246     uint16_t out_port;
247     uint16_t flags;
248     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
249     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
250 };
251
252 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
253                                     const struct ofp_header *,
254                                     enum ofputil_protocol,
255                                     struct ofpbuf *ofpacts);
256 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
257                                        enum ofputil_protocol);
258
259 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
260     const struct ofputil_flow_mod *fms, size_t n_fms);
261
262 /* Flow stats or aggregate stats request, independent of protocol. */
263 struct ofputil_flow_stats_request {
264     bool aggregate;             /* Aggregate results? */
265     struct cls_rule match;
266     ovs_be64 cookie;
267     ovs_be64 cookie_mask;
268     uint16_t out_port;
269     uint8_t table_id;
270 };
271
272 enum ofperr ofputil_decode_flow_stats_request(
273     struct ofputil_flow_stats_request *, const struct ofp_header *);
274 struct ofpbuf *ofputil_encode_flow_stats_request(
275     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
276 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
277     const struct ofputil_flow_stats_request *);
278
279 /* Flow stats reply, independent of protocol. */
280 struct ofputil_flow_stats {
281     struct cls_rule rule;
282     ovs_be64 cookie;
283     uint8_t table_id;
284     uint32_t duration_sec;
285     uint32_t duration_nsec;
286     uint16_t idle_timeout;
287     uint16_t hard_timeout;
288     int idle_age;               /* Seconds since last packet, -1 if unknown. */
289     int hard_age;               /* Seconds since last change, -1 if unknown. */
290     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
291     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
292     struct ofpact *ofpacts;
293     size_t ofpacts_len;
294 };
295
296 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
297                                     struct ofpbuf *msg,
298                                     bool flow_age_extension,
299                                     struct ofpbuf *ofpacts);
300 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
301                                      struct list *replies);
302
303 /* Aggregate stats reply, independent of protocol. */
304 struct ofputil_aggregate_stats {
305     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
306     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
307     uint32_t flow_count;
308 };
309
310 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
311     const struct ofputil_aggregate_stats *stats,
312     const struct ofp_stats_msg *request);
313
314 /* Flow removed message, independent of protocol. */
315 struct ofputil_flow_removed {
316     struct cls_rule rule;
317     ovs_be64 cookie;
318     uint8_t reason;             /* One of OFPRR_*. */
319     uint32_t duration_sec;
320     uint32_t duration_nsec;
321     uint16_t idle_timeout;
322     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
323     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
324 };
325
326 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
327                                         const struct ofp_header *);
328 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
329                                            enum ofputil_protocol);
330
331 /* Abstract packet-in message. */
332 struct ofputil_packet_in {
333     const void *packet;
334     size_t packet_len;
335
336     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
337     uint16_t controller_id;              /* Controller ID to send to. */
338     uint8_t table_id;
339     ovs_be64 cookie;
340
341     uint32_t buffer_id;
342     int send_len;
343     uint16_t total_len;         /* Full length of frame. */
344
345     struct flow_metadata fmd;   /* Metadata at creation time. */
346 };
347
348 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
349                                      const struct ofp_header *);
350 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
351                                         enum nx_packet_in_format);
352
353 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
354 bool ofputil_packet_in_reason_from_string(const char *,
355                                           enum ofp_packet_in_reason *);
356
357 /* Abstract packet-out message.
358  *
359  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
360  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
361 struct ofputil_packet_out {
362     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
363     size_t packet_len;          /* Length of packet data in bytes. */
364     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
365     uint16_t in_port;           /* Packet's input port. */
366     struct ofpact *ofpacts;     /* Actions. */
367     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
368 };
369
370 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
371                                       const struct ofp_packet_out *,
372                                       struct ofpbuf *ofpacts);
373 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *);
374
375 enum ofputil_port_config {
376     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
377     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
378     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
379     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
380     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
381     /* OpenFlow 1.0 only. */
382     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
383     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
384     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
385     /* There are no OpenFlow 1.1-only bits. */
386 };
387
388 enum ofputil_port_state {
389     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
390     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
391     /* OpenFlow 1.1 only. */
392     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
393     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
394     /* OpenFlow 1.0 only. */
395     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
396     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
397     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
398     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
399     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
400 };
401
402 /* Abstract ofp10_phy_port or ofp11_port. */
403 struct ofputil_phy_port {
404     uint16_t port_no;
405     uint8_t hw_addr[OFP_ETH_ALEN];
406     char name[OFP_MAX_PORT_NAME_LEN];
407     enum ofputil_port_config config;
408     enum ofputil_port_state state;
409
410     /* NETDEV_F_* feature bitmasks. */
411     enum netdev_features curr;       /* Current features. */
412     enum netdev_features advertised; /* Features advertised by the port. */
413     enum netdev_features supported;  /* Features supported by the port. */
414     enum netdev_features peer;       /* Features advertised by peer. */
415
416     /* Speed. */
417     uint32_t curr_speed;        /* Current speed, in kbps. */
418     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
419 };
420
421 enum ofputil_capabilities {
422     /* OpenFlow 1.0 and 1.1 share these values for these capabilities. */
423     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
424     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
425     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
426     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
427     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
428     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
429
430     /* OpenFlow 1.0 only. */
431     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
432
433     /* OpenFlow 1.1 only. */
434     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
435 };
436
437 enum ofputil_action_bitmap {
438     OFPUTIL_A_OUTPUT         = 1 << 0,
439     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
440     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
441     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
442     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
443     OFPUTIL_A_SET_DL_DST     = 1 << 5,
444     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
445     OFPUTIL_A_SET_NW_DST     = 1 << 7,
446     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
447     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
448     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
449     OFPUTIL_A_SET_TP_DST     = 1 << 11,
450     OFPUTIL_A_ENQUEUE        = 1 << 12,
451     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
452     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
453     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
454     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
455     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
456     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
457     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
458     OFPUTIL_A_POP_VLAN       = 1 << 20,
459     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
460     OFPUTIL_A_POP_MPLS       = 1 << 22,
461     OFPUTIL_A_SET_QUEUE      = 1 << 23,
462     OFPUTIL_A_GROUP          = 1 << 24,
463     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
464     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
465 };
466
467 /* Abstract ofp_switch_features. */
468 struct ofputil_switch_features {
469     uint64_t datapath_id;       /* Datapath unique ID. */
470     uint32_t n_buffers;         /* Max packets buffered at once. */
471     uint8_t n_tables;           /* Number of tables supported by datapath. */
472     enum ofputil_capabilities capabilities;
473     enum ofputil_action_bitmap actions;
474 };
475
476 enum ofperr ofputil_decode_switch_features(const struct ofp_switch_features *,
477                                            struct ofputil_switch_features *,
478                                            struct ofpbuf *);
479
480 struct ofpbuf *ofputil_encode_switch_features(
481     const struct ofputil_switch_features *, enum ofputil_protocol,
482     ovs_be32 xid);
483 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
484                                       struct ofpbuf *);
485 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
486
487 /* phy_port helper functions. */
488 int ofputil_pull_phy_port(uint8_t ofp_version, struct ofpbuf *,
489                           struct ofputil_phy_port *);
490 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
491
492 /* Abstract ofp_port_status. */
493 struct ofputil_port_status {
494     enum ofp_port_reason reason;
495     struct ofputil_phy_port desc;
496 };
497
498 enum ofperr ofputil_decode_port_status(const struct ofp_port_status *,
499                                        struct ofputil_port_status *);
500 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
501                                           enum ofputil_protocol);
502
503 /* Abstract ofp_port_mod. */
504 struct ofputil_port_mod {
505     uint16_t port_no;
506     uint8_t hw_addr[OFP_ETH_ALEN];
507     enum ofputil_port_config config;
508     enum ofputil_port_config mask;
509     enum netdev_features advertise;
510 };
511
512 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
513                                     struct ofputil_port_mod *);
514 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
515                                        enum ofputil_protocol);
516
517 /* Abstract nx_flow_monitor_request. */
518 struct ofputil_flow_monitor_request {
519     uint32_t id;
520     enum nx_flow_monitor_flags flags;
521     uint16_t out_port;
522     uint8_t table_id;
523     struct cls_rule match;
524 };
525
526 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
527                                         struct ofpbuf *msg);
528 void ofputil_append_flow_monitor_request(
529     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
530
531 /* Abstract nx_flow_update. */
532 struct ofputil_flow_update {
533     enum nx_flow_update_event event;
534
535     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
536     enum ofp_flow_removed_reason reason;
537     uint16_t idle_timeout;
538     uint16_t hard_timeout;
539     uint8_t table_id;
540     ovs_be64 cookie;
541     struct cls_rule *match;
542     struct ofpact *ofpacts;
543     size_t ofpacts_len;
544
545     /* Used only for NXFME_ABBREV. */
546     ovs_be32 xid;
547 };
548
549 int ofputil_decode_flow_update(struct ofputil_flow_update *,
550                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
551 void ofputil_start_flow_update(struct list *replies);
552 void ofputil_append_flow_update(const struct ofputil_flow_update *,
553                                 struct list *replies);
554
555 /* Abstract nx_flow_monitor_cancel. */
556 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
557 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
558
559 /* OpenFlow protocol utility functions. */
560 void *make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **);
561 void *make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **);
562
563 void *make_openflow_xid(size_t openflow_len, uint8_t type,
564                         ovs_be32 xid, struct ofpbuf **);
565 void *make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
566                      struct ofpbuf **);
567
568 void *put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *);
569 void *put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
570                        struct ofpbuf *);
571
572 void *put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *);
573 void *put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
574                     struct ofpbuf *);
575
576 void update_openflow_length(struct ofpbuf *);
577
578 void *ofputil_make_stats_request(size_t openflow_len, uint16_t type,
579                                  uint32_t subtype, struct ofpbuf **);
580 void *ofputil_make_stats_reply(size_t openflow_len,
581                                const struct ofp_stats_msg *request,
582                                struct ofpbuf **);
583
584 void ofputil_put_stats_header(ovs_be32 xid, uint8_t ofp_type,
585                               ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
586                               struct ofpbuf *);
587
588 void ofputil_start_stats_reply(const struct ofp_stats_msg *request,
589                                struct list *);
590 struct ofpbuf *ofputil_reserve_stats_reply(size_t len, struct list *);
591 void *ofputil_append_stats_reply(size_t len, struct list *);
592 void ofputil_postappend_stats_reply(size_t start_ofs, struct list *);
593
594 void ofputil_append_port_desc_stats_reply(uint8_t ofp_version,
595                                           const struct ofputil_phy_port *pp,
596                                           struct list *replies);
597
598 const void *ofputil_stats_body(const struct ofp_header *);
599 size_t ofputil_stats_body_len(const struct ofp_header *);
600
601 const void *ofputil_nxstats_body(const struct ofp_header *);
602 size_t ofputil_nxstats_body_len(const struct ofp_header *);
603
604 /*  */
605 struct ofpbuf *make_echo_request(void);
606 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
607
608 struct ofpbuf *ofputil_encode_barrier_request(void);
609
610 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
611 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
612
613 \f
614 /* Actions. */
615
616 /* The type of an action.
617  *
618  * For each implemented OFPAT10_* and NXAST_* action type, there is a
619  * corresponding constant prefixed with OFPUTIL_, e.g.:
620  *
621  * OFPUTIL_OFPAT10_OUTPUT
622  * OFPUTIL_OFPAT10_SET_VLAN_VID
623  * OFPUTIL_OFPAT10_SET_VLAN_PCP
624  * OFPUTIL_OFPAT10_STRIP_VLAN
625  * OFPUTIL_OFPAT10_SET_DL_SRC
626  * OFPUTIL_OFPAT10_SET_DL_DST
627  * OFPUTIL_OFPAT10_SET_NW_SRC
628  * OFPUTIL_OFPAT10_SET_NW_DST
629  * OFPUTIL_OFPAT10_SET_NW_TOS
630  * OFPUTIL_OFPAT10_SET_TP_SRC
631  * OFPUTIL_OFPAT10_SET_TP_DST
632  * OFPUTIL_OFPAT10_ENQUEUE
633  * OFPUTIL_NXAST_RESUBMIT
634  * OFPUTIL_NXAST_SET_TUNNEL
635  * OFPUTIL_NXAST_SET_QUEUE
636  * OFPUTIL_NXAST_POP_QUEUE
637  * OFPUTIL_NXAST_REG_MOVE
638  * OFPUTIL_NXAST_REG_LOAD
639  * OFPUTIL_NXAST_NOTE
640  * OFPUTIL_NXAST_SET_TUNNEL64
641  * OFPUTIL_NXAST_MULTIPATH
642  * OFPUTIL_NXAST_AUTOPATH
643  * OFPUTIL_NXAST_BUNDLE
644  * OFPUTIL_NXAST_BUNDLE_LOAD
645  * OFPUTIL_NXAST_RESUBMIT_TABLE
646  * OFPUTIL_NXAST_OUTPUT_REG
647  * OFPUTIL_NXAST_LEARN
648  * OFPUTIL_NXAST_DEC_TTL
649  * OFPUTIL_NXAST_FIN_TIMEOUT
650  *
651  * (The above list helps developers who want to "grep" for these definitions.)
652  */
653 enum OVS_PACKED_ENUM ofputil_action_code {
654     OFPUTIL_ACTION_INVALID,
655 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)           OFPUTIL_##ENUM,
656 #define OFPAT11_ACTION(ENUM, STRUCT, NAME)           OFPUTIL_##ENUM,
657 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
658 #include "ofp-util.def"
659 };
660
661 /* The number of values of "enum ofputil_action_code". */
662 enum {
663 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)           + 1
664 #define OFPAT11_ACTION(ENUM, STRUCT, NAME)           + 1
665 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
666     OFPUTIL_N_ACTIONS = 1
667 #include "ofp-util.def"
668 };
669
670 int ofputil_action_code_from_name(const char *);
671
672 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
673
674 /* For each OpenFlow action <ENUM> that has a corresponding action structure
675  * struct <STRUCT>, this defines two functions:
676  *
677  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
678  *
679  *     Initializes the parts of 'action' that identify it as having type <ENUM>
680  *     and length 'sizeof *action' and zeros the rest.  For actions that have
681  *     variable length, the length used and cleared is that of struct <STRUCT>.
682  *
683  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
684  *
685  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
686  *     initializes it with ofputil_init_<ENUM>(), and returns it.
687  */
688 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
689     void ofputil_init_##ENUM(struct STRUCT *);          \
690     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
691 #define OFPAT11_ACTION(ENUM, STRUCT, NAME)              \
692     void ofputil_init_##ENUM(struct STRUCT *);          \
693     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
694 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
695     void ofputil_init_##ENUM(struct STRUCT *);          \
696     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
697 #include "ofp-util.def"
698
699 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
700
701 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
702                              const struct flow *, int max_ports);
703 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
704
705 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
706                                  union ofp_action **, size_t *);
707
708 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
709                            const union ofp_action *b, size_t n_b);
710 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
711
712 /* Handy utility for parsing flows and actions. */
713 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
714
715 #endif /* ofp-util.h */