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