ofp-actions: Implement writing to metadata field
[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 "match.h"
28 #include "netdev.h"
29 #include "openflow/nicira-ext.h"
30 #include "openvswitch/types.h"
31
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 *portp);
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
86 ofputil_protocol_from_ofp_version(enum ofp_version version);
87 enum ofp_version  ofputil_protocol_to_ofp_version(enum ofputil_protocol);
88
89 bool ofputil_protocol_is_valid(enum ofputil_protocol);
90 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
91                                                bool enable);
92 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
93 enum ofputil_protocol ofputil_protocol_set_base(
94     enum ofputil_protocol cur, enum ofputil_protocol new_base);
95
96 const char *ofputil_protocol_to_string(enum ofputil_protocol);
97 char *ofputil_protocols_to_string(enum ofputil_protocol);
98 enum ofputil_protocol ofputil_protocols_from_string(const char *);
99 enum ofputil_protocol ofputil_usable_protocols(const struct match *);
100
101 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
102                                            enum ofputil_protocol want,
103                                            enum ofputil_protocol *next);
104
105 /* nx_flow_format */
106 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
107 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
108 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
109 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
110
111 /* Work with ofp10_match. */
112 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
113 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
114                                     struct match *);
115 void ofputil_normalize_match(struct match *);
116 void ofputil_normalize_match_quiet(struct match *);
117 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
118
119 /* Work with ofp11_match. */
120 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
121                                      uint16_t *padded_match_len);
122 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
123                                            struct match *);
124 void ofputil_match_to_ofp11_match(const struct match *, 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 ofp_version,
135                                                  enum nx_packet_in_format);
136
137 /* NXT_FLOW_MOD_TABLE_ID extension. */
138 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
139
140 /* Protocol-independent flow_mod.
141  *
142  * The handling of cookies across multiple versions of OpenFlow is a bit
143  * confusing.  A full description of Open vSwitch's cookie handling is
144  * in the DESIGN file.  The following table shows the expected values of
145  * the cookie-related fields for the different flow_mod commands in
146  * OpenFlow 1.0 ("OF10") and NXM.  "<used>" and "-" indicate a value
147  * that may be populated and an ignored field, respectively.
148  *
149  *               cookie  cookie_mask  new_cookie
150  *               ======  ===========  ==========
151  * OF10 Add        -          0         <used>
152  * OF10 Modify     -          0         <used>
153  * OF10 Delete     -          0           -
154  * NXM Add         -          0         <used>
155  * NXM Modify    <used>     <used>      <used>
156  * NXM Delete    <used>     <used>        -
157  */
158 struct ofputil_flow_mod {
159     struct match match;
160     unsigned int priority;
161     ovs_be64 cookie;         /* Cookie bits to match. */
162     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
163     ovs_be64 new_cookie;     /* New cookie to install or -1. */
164     uint8_t table_id;
165     uint16_t command;
166     uint16_t idle_timeout;
167     uint16_t hard_timeout;
168     uint32_t buffer_id;
169     uint16_t out_port;
170     uint16_t flags;
171     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
172     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
173 };
174
175 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
176                                     const struct ofp_header *,
177                                     enum ofputil_protocol,
178                                     struct ofpbuf *ofpacts);
179 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
180                                        enum ofputil_protocol);
181
182 enum ofputil_protocol ofputil_flow_mod_usable_protocols(
183     const struct ofputil_flow_mod *fms, size_t n_fms);
184
185 /* Flow stats or aggregate stats request, independent of protocol. */
186 struct ofputil_flow_stats_request {
187     bool aggregate;             /* Aggregate results? */
188     struct match match;
189     ovs_be64 cookie;
190     ovs_be64 cookie_mask;
191     uint16_t out_port;
192     uint8_t table_id;
193 };
194
195 enum ofperr ofputil_decode_flow_stats_request(
196     struct ofputil_flow_stats_request *, const struct ofp_header *);
197 struct ofpbuf *ofputil_encode_flow_stats_request(
198     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
199 enum ofputil_protocol ofputil_flow_stats_request_usable_protocols(
200     const struct ofputil_flow_stats_request *);
201
202 /* Flow stats reply, independent of protocol. */
203 struct ofputil_flow_stats {
204     struct match match;
205     ovs_be64 cookie;
206     uint8_t table_id;
207     uint32_t duration_sec;
208     uint32_t duration_nsec;
209     uint16_t priority;
210     uint16_t idle_timeout;
211     uint16_t hard_timeout;
212     int idle_age;               /* Seconds since last packet, -1 if unknown. */
213     int hard_age;               /* Seconds since last change, -1 if unknown. */
214     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
215     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
216     struct ofpact *ofpacts;
217     size_t ofpacts_len;
218 };
219
220 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
221                                     struct ofpbuf *msg,
222                                     bool flow_age_extension,
223                                     struct ofpbuf *ofpacts);
224 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
225                                      struct list *replies);
226
227 /* Aggregate stats reply, independent of protocol. */
228 struct ofputil_aggregate_stats {
229     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
230     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
231     uint32_t flow_count;
232 };
233
234 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
235     const struct ofputil_aggregate_stats *stats,
236     const struct ofp_header *request);
237 enum ofperr ofputil_decode_aggregate_stats_reply(
238     struct ofputil_aggregate_stats *,
239     const struct ofp_header *reply);
240
241 /* Flow removed message, independent of protocol. */
242 struct ofputil_flow_removed {
243     struct match match;
244     uint16_t priority;
245     ovs_be64 cookie;
246     uint8_t reason;             /* One of OFPRR_*. */
247     uint8_t table_id;           /* 255 if message didn't include table ID. */
248     uint32_t duration_sec;
249     uint32_t duration_nsec;
250     uint16_t idle_timeout;
251     uint16_t hard_timeout;
252     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
253     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
254 };
255
256 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
257                                         const struct ofp_header *);
258 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
259                                            enum ofputil_protocol);
260
261 /* Abstract packet-in message. */
262 struct ofputil_packet_in {
263     const void *packet;
264     size_t packet_len;
265
266     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
267     uint16_t controller_id;              /* Controller ID to send to. */
268     uint8_t table_id;
269     ovs_be64 cookie;
270
271     uint32_t buffer_id;
272     int send_len;
273     uint16_t total_len;         /* Full length of frame. */
274
275     struct flow_metadata fmd;   /* Metadata at creation time. */
276 };
277
278 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
279                                      const struct ofp_header *);
280 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
281                                         enum ofputil_protocol protocol,
282                                         enum nx_packet_in_format);
283
284 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason);
285 bool ofputil_packet_in_reason_from_string(const char *,
286                                           enum ofp_packet_in_reason *);
287
288 /* Abstract packet-out message.
289  *
290  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
291  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
292 struct ofputil_packet_out {
293     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
294     size_t packet_len;          /* Length of packet data in bytes. */
295     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
296     uint16_t in_port;           /* Packet's input port. */
297     struct ofpact *ofpacts;     /* Actions. */
298     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
299 };
300
301 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
302                                       const struct ofp_header *,
303                                       struct ofpbuf *ofpacts);
304 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
305                                          enum ofputil_protocol protocol);
306
307 enum ofputil_port_config {
308     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
309     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
310     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
311     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
312     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
313     /* OpenFlow 1.0 only. */
314     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
315     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
316     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
317     /* There are no OpenFlow 1.1-only bits. */
318 };
319
320 enum ofputil_port_state {
321     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
322     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
323     /* OpenFlow 1.1 only. */
324     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
325     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
326     /* OpenFlow 1.0 only. */
327     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
328     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
329     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
330     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
331     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
332 };
333
334 /* Abstract ofp10_phy_port or ofp11_port. */
335 struct ofputil_phy_port {
336     uint16_t port_no;
337     uint8_t hw_addr[OFP_ETH_ALEN];
338     char name[OFP_MAX_PORT_NAME_LEN];
339     enum ofputil_port_config config;
340     enum ofputil_port_state state;
341
342     /* NETDEV_F_* feature bitmasks. */
343     enum netdev_features curr;       /* Current features. */
344     enum netdev_features advertised; /* Features advertised by the port. */
345     enum netdev_features supported;  /* Features supported by the port. */
346     enum netdev_features peer;       /* Features advertised by peer. */
347
348     /* Speed. */
349     uint32_t curr_speed;        /* Current speed, in kbps. */
350     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
351 };
352
353 enum ofputil_capabilities {
354     /* OpenFlow 1.0, 1.1 and 1.2 share these values for these capabilities. */
355     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
356     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
357     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
358     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
359     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
360
361     /* OpenFlow 1.0 and 1.1 share this capability. */
362     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
363
364     /* OpenFlow 1.0 only. */
365     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
366
367     /* OpenFlow 1.1 and 1.2 share this capability. */
368     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
369
370     /* OpenFlow 1.2 only */
371     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
372 };
373
374 enum ofputil_action_bitmap {
375     OFPUTIL_A_OUTPUT         = 1 << 0,
376     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
377     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
378     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
379     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
380     OFPUTIL_A_SET_DL_DST     = 1 << 5,
381     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
382     OFPUTIL_A_SET_NW_DST     = 1 << 7,
383     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
384     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
385     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
386     OFPUTIL_A_SET_TP_DST     = 1 << 11,
387     OFPUTIL_A_ENQUEUE        = 1 << 12,
388     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
389     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
390     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
391     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
392     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
393     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
394     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
395     OFPUTIL_A_POP_VLAN       = 1 << 20,
396     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
397     OFPUTIL_A_POP_MPLS       = 1 << 22,
398     OFPUTIL_A_SET_QUEUE      = 1 << 23,
399     OFPUTIL_A_GROUP          = 1 << 24,
400     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
401     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
402     OFPUTIL_A_SET_FIELD      = 1 << 27,
403 };
404
405 /* Abstract ofp_switch_features. */
406 struct ofputil_switch_features {
407     uint64_t datapath_id;       /* Datapath unique ID. */
408     uint32_t n_buffers;         /* Max packets buffered at once. */
409     uint8_t n_tables;           /* Number of tables supported by datapath. */
410     enum ofputil_capabilities capabilities;
411     enum ofputil_action_bitmap actions;
412 };
413
414 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
415                                            struct ofputil_switch_features *,
416                                            struct ofpbuf *);
417
418 struct ofpbuf *ofputil_encode_switch_features(
419     const struct ofputil_switch_features *, enum ofputil_protocol,
420     ovs_be32 xid);
421 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
422                                       struct ofpbuf *);
423 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
424
425 /* phy_port helper functions. */
426 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
427                           struct ofputil_phy_port *);
428 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
429
430 /* Abstract ofp_port_status. */
431 struct ofputil_port_status {
432     enum ofp_port_reason reason;
433     struct ofputil_phy_port desc;
434 };
435
436 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
437                                        struct ofputil_port_status *);
438 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
439                                           enum ofputil_protocol);
440
441 /* Abstract ofp_port_mod. */
442 struct ofputil_port_mod {
443     uint16_t port_no;
444     uint8_t hw_addr[OFP_ETH_ALEN];
445     enum ofputil_port_config config;
446     enum ofputil_port_config mask;
447     enum netdev_features advertise;
448 };
449
450 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
451                                     struct ofputil_port_mod *);
452 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
453                                        enum ofputil_protocol);
454
455 /* Abstract table stats.
456  *
457  * For now we use ofp12_table_stats as a superset of the other protocol
458  * versions' table stats. */
459
460 struct ofpbuf *ofputil_encode_table_stats_reply(
461     const struct ofp12_table_stats[], int n,
462     const struct ofp_header *request);
463
464 /* Abstract nx_flow_monitor_request. */
465 struct ofputil_flow_monitor_request {
466     uint32_t id;
467     enum nx_flow_monitor_flags flags;
468     uint16_t out_port;
469     uint8_t table_id;
470     struct match match;
471 };
472
473 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
474                                         struct ofpbuf *msg);
475 void ofputil_append_flow_monitor_request(
476     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
477
478 /* Abstract nx_flow_update. */
479 struct ofputil_flow_update {
480     enum nx_flow_update_event event;
481
482     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
483     enum ofp_flow_removed_reason reason;
484     uint16_t idle_timeout;
485     uint16_t hard_timeout;
486     uint8_t table_id;
487     ovs_be64 cookie;
488     struct match *match;
489     uint16_t priority;
490     struct ofpact *ofpacts;
491     size_t ofpacts_len;
492
493     /* Used only for NXFME_ABBREV. */
494     ovs_be32 xid;
495 };
496
497 int ofputil_decode_flow_update(struct ofputil_flow_update *,
498                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
499 void ofputil_start_flow_update(struct list *replies);
500 void ofputil_append_flow_update(const struct ofputil_flow_update *,
501                                 struct list *replies);
502
503 /* Abstract nx_flow_monitor_cancel. */
504 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
505 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
506
507 /* Encoding OpenFlow stats messages. */
508 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
509                                           const struct ofputil_phy_port *pp,
510                                           struct list *replies);
511
512 /* Encoding simple OpenFlow messages. */
513 struct ofpbuf *make_echo_request(enum ofp_version);
514 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
515
516 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
517
518 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
519 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
520
521 \f
522 /* Actions. */
523
524 /* The type of an action.
525  *
526  * For each implemented OFPAT10_* and NXAST_* action type, there is a
527  * corresponding constant prefixed with OFPUTIL_, e.g.:
528  *
529  * OFPUTIL_OFPAT10_OUTPUT
530  * OFPUTIL_OFPAT10_SET_VLAN_VID
531  * OFPUTIL_OFPAT10_SET_VLAN_PCP
532  * OFPUTIL_OFPAT10_STRIP_VLAN
533  * OFPUTIL_OFPAT10_SET_DL_SRC
534  * OFPUTIL_OFPAT10_SET_DL_DST
535  * OFPUTIL_OFPAT10_SET_NW_SRC
536  * OFPUTIL_OFPAT10_SET_NW_DST
537  * OFPUTIL_OFPAT10_SET_NW_TOS
538  * OFPUTIL_OFPAT10_SET_TP_SRC
539  * OFPUTIL_OFPAT10_SET_TP_DST
540  * OFPUTIL_OFPAT10_ENQUEUE
541  * OFPUTIL_NXAST_RESUBMIT
542  * OFPUTIL_NXAST_SET_TUNNEL
543  * OFPUTIL_NXAST_SET_METADATA
544  * OFPUTIL_NXAST_SET_QUEUE
545  * OFPUTIL_NXAST_POP_QUEUE
546  * OFPUTIL_NXAST_REG_MOVE
547  * OFPUTIL_NXAST_REG_LOAD
548  * OFPUTIL_NXAST_NOTE
549  * OFPUTIL_NXAST_SET_TUNNEL64
550  * OFPUTIL_NXAST_MULTIPATH
551  * OFPUTIL_NXAST_AUTOPATH
552  * OFPUTIL_NXAST_BUNDLE
553  * OFPUTIL_NXAST_BUNDLE_LOAD
554  * OFPUTIL_NXAST_RESUBMIT_TABLE
555  * OFPUTIL_NXAST_OUTPUT_REG
556  * OFPUTIL_NXAST_LEARN
557  * OFPUTIL_NXAST_DEC_TTL
558  * OFPUTIL_NXAST_FIN_TIMEOUT
559  *
560  * (The above list helps developers who want to "grep" for these definitions.)
561  */
562 enum OVS_PACKED_ENUM ofputil_action_code {
563     OFPUTIL_ACTION_INVALID,
564 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
565 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
566 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
567 #include "ofp-util.def"
568 };
569
570 /* The number of values of "enum ofputil_action_code". */
571 enum {
572 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
573 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
574 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
575     OFPUTIL_N_ACTIONS = 1
576 #include "ofp-util.def"
577 };
578
579 int ofputil_action_code_from_name(const char *);
580
581 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
582
583 /* For each OpenFlow action <ENUM> that has a corresponding action structure
584  * struct <STRUCT>, this defines two functions:
585  *
586  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
587  *
588  *     Initializes the parts of 'action' that identify it as having type <ENUM>
589  *     and length 'sizeof *action' and zeros the rest.  For actions that have
590  *     variable length, the length used and cleared is that of struct <STRUCT>.
591  *
592  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
593  *
594  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
595  *     initializes it with ofputil_init_<ENUM>(), and returns it.
596  */
597 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
598     void ofputil_init_##ENUM(struct STRUCT *);          \
599     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
600 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
601     void ofputil_init_##ENUM(struct STRUCT *);          \
602     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
603 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
604     void ofputil_init_##ENUM(struct STRUCT *);          \
605     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
606 #include "ofp-util.def"
607
608 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
609
610 enum ofperr validate_actions(const union ofp_action *, size_t n_actions,
611                              const struct flow *, int max_ports);
612 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
613
614 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
615                                  union ofp_action **, size_t *);
616
617 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
618                            const union ofp_action *b, size_t n_b);
619 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
620
621 /* Handy utility for parsing flows and actions. */
622 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
623
624 struct ofpbuf *ofputlil_dump_ports(enum ofp_version ofp_version, int16_t port);
625
626 struct ofputil_port_stats {
627     uint16_t port_no;
628     struct netdev_stats stats;
629 };
630
631 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
632                                                  int16_t port);
633 void ofputil_append_port_stat(struct list *replies,
634                               const struct ofputil_port_stats *ops);
635 size_t ofputil_count_port_stats(const struct ofp_header *);
636 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
637 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
638                                               uint16_t *ofp10_port);
639
640 struct ofputil_queue_stats_request {
641     uint16_t port_no;
642     uint32_t queue_id;
643 };
644
645 enum ofperr
646 ofputil_decode_queue_stats_request(const struct ofp_header *request,
647                                    struct ofputil_queue_stats_request *oqsr);
648 struct ofpbuf *
649 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
650                                    const struct ofputil_queue_stats_request *oqsr);
651
652 struct ofputil_queue_stats {
653     uint16_t port_no;
654     uint32_t queue_id;
655     struct netdev_queue_stats stats;
656 };
657
658 size_t ofputil_count_queue_stats(const struct ofp_header *);
659 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
660 void ofputil_append_queue_stat(struct list *replies,
661                                const struct ofputil_queue_stats *oqs);
662
663 #endif /* ofp-util.h */