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