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