lib/ofp-actions: Enforce action consistency.
[sliver-openvswitch.git] / lib / ofp-util.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 <stdbool.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include "classifier.h"
24 #include "compiler.h"
25 #include "flow.h"
26 #include "match.h"
27 #include "netdev.h"
28 #include "openflow/nicira-ext.h"
29 #include "openvswitch/types.h"
30 #include "type-props.h"
31
32 struct ofpbuf;
33 union ofp_action;
34
35 /* Port numbers. */
36 enum ofperr ofputil_port_from_ofp11(ovs_be32 ofp11_port,
37                                     ofp_port_t *ofp10_port);
38 ovs_be32 ofputil_port_to_ofp11(ofp_port_t ofp10_port);
39
40 enum ofperr ofputil_check_output_port(ofp_port_t ofp_port,
41                                       ofp_port_t max_ports);
42 bool ofputil_port_from_string(const char *, ofp_port_t *portp);
43 void ofputil_format_port(ofp_port_t port, struct ds *);
44 void ofputil_port_to_string(ofp_port_t, char namebuf[OFP_MAX_PORT_NAME_LEN],
45                             size_t bufsize);
46
47 /* Group numbers. */
48 enum { MAX_GROUP_NAME_LEN = INT_STRLEN(uint32_t) };
49 bool ofputil_group_from_string(const char *, uint32_t *group_id);
50 void ofputil_format_group(uint32_t group_id, struct ds *);
51 void ofputil_group_to_string(uint32_t group_id,
52                              char namebuf[MAX_GROUP_NAME_LEN + 1],
53                              size_t bufsize);
54
55 /* Converting OFPFW10_NW_SRC_MASK and OFPFW10_NW_DST_MASK wildcard bit counts
56  * to and from IP bitmasks. */
57 ovs_be32 ofputil_wcbits_to_netmask(int wcbits);
58 int ofputil_netmask_to_wcbits(ovs_be32 netmask);
59
60 /* Protocols.
61  *
62  * A "protocol" is an OpenFlow version plus, for some OpenFlow versions,
63  * a bit extra about the flow match format in use.
64  *
65  * These are arranged from most portable to least portable, or alternatively
66  * from least powerful to most powerful.  Protocols earlier on the list are
67  * more likely to be understood for the purpose of making requests, but
68  * protocol later on the list are more likely to accurately describe a flow
69  * within a switch.
70  *
71  * On any given OpenFlow connection, a single protocol is in effect at any
72  * given time.  These values use separate bits only because that makes it easy
73  * to test whether a particular protocol is within a given set of protocols and
74  * to implement set union and intersection.
75  */
76 enum ofputil_protocol {
77     /* OpenFlow 1.0 protocols.
78      *
79      * The "STD" protocols use the standard OpenFlow 1.0 flow format.
80      * The "NXM" protocols use the Nicira Extensible Match (NXM) flow format.
81      *
82      * The protocols with "TID" mean that the nx_flow_mod_table_id Nicira
83      * extension has been enabled.  The other protocols have it disabled.
84      */
85 #define OFPUTIL_P_NONE 0
86     OFPUTIL_P_OF10_STD     = 1 << 0,
87     OFPUTIL_P_OF10_STD_TID = 1 << 1,
88     OFPUTIL_P_OF10_NXM     = 1 << 2,
89     OFPUTIL_P_OF10_NXM_TID = 1 << 3,
90 #define OFPUTIL_P_OF10_STD_ANY (OFPUTIL_P_OF10_STD | OFPUTIL_P_OF10_STD_TID)
91 #define OFPUTIL_P_OF10_NXM_ANY (OFPUTIL_P_OF10_NXM | OFPUTIL_P_OF10_NXM_TID)
92 #define OFPUTIL_P_OF10_ANY (OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY)
93
94     /* OpenFlow 1.1 protocol.
95      *
96      * We only support the standard OpenFlow 1.1 flow format.
97      *
98      * OpenFlow 1.1 always operates with an equivalent of the
99      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
100      * variant. */
101     OFPUTIL_P_OF11_STD     = 1 << 4,
102
103     /* OpenFlow 1.2+ protocols (only one variant each).
104      *
105      * These use the standard OpenFlow Extensible Match (OXM) flow format.
106      *
107      * OpenFlow 1.2+ always operates with an equivalent of the
108      * nx_flow_mod_table_id Nicira extension enabled, so there is no "TID"
109      * variant. */
110     OFPUTIL_P_OF12_OXM      = 1 << 5,
111     OFPUTIL_P_OF13_OXM      = 1 << 6,
112 #define OFPUTIL_P_ANY_OXM (OFPUTIL_P_OF12_OXM | OFPUTIL_P_OF13_OXM)
113
114 #define OFPUTIL_P_NXM_OF11_UP (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF11_STD | \
115                                OFPUTIL_P_ANY_OXM)
116
117 #define OFPUTIL_P_NXM_OXM_ANY (OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_ANY_OXM)
118
119 #define OFPUTIL_P_OF11_UP (OFPUTIL_P_OF11_STD | OFPUTIL_P_ANY_OXM)
120
121 #define OFPUTIL_P_OF12_UP (OFPUTIL_P_ANY_OXM)
122
123 #define OFPUTIL_P_OF13_UP (OFPUTIL_P_OF13_OXM)
124
125     /* All protocols. */
126 #define OFPUTIL_P_ANY ((1 << 7) - 1)
127
128     /* Protocols in which a specific table may be specified in flow_mods. */
129 #define OFPUTIL_P_TID (OFPUTIL_P_OF10_STD_TID | \
130                        OFPUTIL_P_OF10_NXM_TID | \
131                        OFPUTIL_P_OF11_STD |     \
132                        OFPUTIL_P_ANY_OXM)
133 };
134
135 /* Protocols to use for flow dumps, from most to least preferred. */
136 extern enum ofputil_protocol ofputil_flow_dump_protocols[];
137 extern size_t ofputil_n_flow_dump_protocols;
138
139 enum ofputil_protocol ofputil_protocol_from_ofp_version(enum ofp_version);
140 enum ofputil_protocol ofputil_protocols_from_ofp_version(enum ofp_version);
141 enum ofp_version ofputil_protocol_to_ofp_version(enum ofputil_protocol);
142
143 bool ofputil_protocol_is_valid(enum ofputil_protocol);
144 enum ofputil_protocol ofputil_protocol_set_tid(enum ofputil_protocol,
145                                                bool enable);
146 enum ofputil_protocol ofputil_protocol_to_base(enum ofputil_protocol);
147 enum ofputil_protocol ofputil_protocol_set_base(
148     enum ofputil_protocol cur, enum ofputil_protocol new_base);
149
150 const char *ofputil_protocol_to_string(enum ofputil_protocol);
151 char *ofputil_protocols_to_string(enum ofputil_protocol);
152 enum ofputil_protocol ofputil_protocols_from_string(const char *);
153
154 void ofputil_format_version(struct ds *, enum ofp_version);
155 void ofputil_format_version_name(struct ds *, enum ofp_version);
156
157 /* A bitmap of version numbers
158  *
159  * Bit offsets correspond to ofp_version numbers which in turn correspond to
160  * wire-protocol numbers for Open Flow versions..  E.g. (1u << OFP11_VERSION)
161  * is the mask for Open Flow 1.1.  If the bit for a version is set then it is
162  * allowed, otherwise it is disallowed. */
163
164 void ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap);
165 void ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap);
166
167 uint32_t ofputil_protocols_to_version_bitmap(enum ofputil_protocol);
168 enum ofputil_protocol ofputil_protocols_from_version_bitmap(uint32_t bitmap);
169
170 /* Bitmap of OpenFlow versions that Open vSwitch supports. */
171 #define OFPUTIL_SUPPORTED_VERSIONS \
172     ((1u << OFP10_VERSION) | (1u << OFP12_VERSION) | (1u << OFP13_VERSION))
173
174 /* Bitmap of OpenFlow versions to enable by default (a subset of
175  * OFPUTIL_SUPPORTED_VERSIONS). */
176 #define OFPUTIL_DEFAULT_VERSIONS (1u << OFP10_VERSION)
177
178 enum ofputil_protocol ofputil_protocols_from_string(const char *s);
179
180 const char *ofputil_version_to_string(enum ofp_version ofp_version);
181 uint32_t ofputil_versions_from_string(const char *s);
182 uint32_t ofputil_versions_from_strings(char ** const s, size_t count);
183
184 bool ofputil_decode_hello(const struct ofp_header *,
185                           uint32_t *allowed_versions);
186 struct ofpbuf *ofputil_encode_hello(uint32_t version_bitmap);
187
188 struct ofpbuf *ofputil_encode_set_protocol(enum ofputil_protocol current,
189                                            enum ofputil_protocol want,
190                                            enum ofputil_protocol *next);
191
192 /* nx_flow_format */
193 struct ofpbuf *ofputil_encode_nx_set_flow_format(enum nx_flow_format);
194 enum ofputil_protocol ofputil_nx_flow_format_to_protocol(enum nx_flow_format);
195 bool ofputil_nx_flow_format_is_valid(enum nx_flow_format);
196 const char *ofputil_nx_flow_format_to_string(enum nx_flow_format);
197
198 /* Work with ofp10_match. */
199 void ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *);
200 void ofputil_match_from_ofp10_match(const struct ofp10_match *,
201                                     struct match *);
202 void ofputil_normalize_match(struct match *);
203 void ofputil_normalize_match_quiet(struct match *);
204 void ofputil_match_to_ofp10_match(const struct match *, struct ofp10_match *);
205
206 /* Work with ofp11_match. */
207 enum ofperr ofputil_pull_ofp11_match(struct ofpbuf *, struct match *,
208                                      uint16_t *padded_match_len);
209 enum ofperr ofputil_match_from_ofp11_match(const struct ofp11_match *,
210                                            struct match *);
211 int ofputil_put_ofp11_match(struct ofpbuf *, const struct match *,
212                             enum ofputil_protocol);
213 void ofputil_match_to_ofp11_match(const struct match *, struct ofp11_match *);
214 int ofputil_match_typical_len(enum ofputil_protocol);
215
216 /* dl_type translation between OpenFlow and 'struct flow' format. */
217 ovs_be16 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type);
218 ovs_be16 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type);
219
220 /* PACKET_IN. */
221 bool ofputil_packet_in_format_is_valid(enum nx_packet_in_format);
222 int ofputil_packet_in_format_from_string(const char *);
223 const char *ofputil_packet_in_format_to_string(enum nx_packet_in_format);
224 struct ofpbuf *ofputil_make_set_packet_in_format(enum ofp_version,
225                                                  enum nx_packet_in_format);
226
227 /* NXT_FLOW_MOD_TABLE_ID extension. */
228 struct ofpbuf *ofputil_make_flow_mod_table_id(bool flow_mod_table_id);
229
230 /* Protocol-independent flow_mod flags. */
231 enum ofputil_flow_mod_flags {
232     /* Flags that are maintained with a flow as part of its state.
233      *
234      * (OFPUTIL_FF_EMERG would be here too, if OVS supported it.) */
235     OFPUTIL_FF_SEND_FLOW_REM = 1 << 0, /* All versions. */
236     OFPUTIL_FF_NO_PKT_COUNTS = 1 << 1, /* OpenFlow 1.3+. */
237     OFPUTIL_FF_NO_BYT_COUNTS = 1 << 2, /* OpenFlow 1.3+. */
238 #define OFPUTIL_FF_STATE (OFPUTIL_FF_SEND_FLOW_REM      \
239                           | OFPUTIL_FF_NO_PKT_COUNTS    \
240                           | OFPUTIL_FF_NO_BYT_COUNTS)
241
242     /* Flags that affect flow_mod behavior but are not part of flow state. */
243     OFPUTIL_FF_CHECK_OVERLAP = 1 << 3, /* All versions. */
244     OFPUTIL_FF_EMERG         = 1 << 4, /* OpenFlow 1.0 only. */
245     OFPUTIL_FF_RESET_COUNTS  = 1 << 5, /* OpenFlow 1.2+. */
246 };
247
248 /* Protocol-independent flow_mod.
249  *
250  * The handling of cookies across multiple versions of OpenFlow is a bit
251  * confusing.  See DESIGN for the details. */
252 struct ofputil_flow_mod {
253     struct list list_node;    /* For queuing flow_mods. */
254
255     struct match match;
256     unsigned int priority;
257
258     /* Cookie matching.  The flow_mod affects only flows that have cookies that
259      * bitwise match 'cookie' bits in positions where 'cookie_mask has 1-bits.
260      *
261      * 'cookie_mask' should be zero for OFPFC_ADD flow_mods. */
262     ovs_be64 cookie;         /* Cookie bits to match. */
263     ovs_be64 cookie_mask;    /* 1-bit in each 'cookie' bit to match. */
264
265     /* Cookie changes.
266      *
267      * OFPFC_ADD uses 'new_cookie' as the new flow's cookie.  'new_cookie'
268      * should not be UINT64_MAX.
269      *
270      * OFPFC_MODIFY and OFPFC_MODIFY_STRICT have two cases:
271      *
272      *   - If one or more matching flows exist and 'modify_cookie' is true,
273      *     then the flow_mod changes the existing flows' cookies to
274      *     'new_cookie'.  'new_cookie' should not be UINT64_MAX.
275      *
276      *   - If no matching flow exists, 'new_cookie' is not UINT64_MAX, and
277      *     'cookie_mask' is 0, then the flow_mod adds a new flow with
278      *     'new_cookie' as its cookie.
279      */
280     ovs_be64 new_cookie;     /* New cookie to install or UINT64_MAX. */
281     bool modify_cookie;      /* Set cookie of existing flow to 'new_cookie'? */
282
283     uint8_t table_id;
284     uint16_t command;
285     uint16_t idle_timeout;
286     uint16_t hard_timeout;
287     uint32_t buffer_id;
288     ofp_port_t out_port;
289     uint32_t out_group;
290     enum ofputil_flow_mod_flags flags;
291     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
292     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
293 };
294
295 enum ofperr ofputil_decode_flow_mod(struct ofputil_flow_mod *,
296                                     const struct ofp_header *,
297                                     enum ofputil_protocol,
298                                     struct ofpbuf *ofpacts);
299 struct ofpbuf *ofputil_encode_flow_mod(const struct ofputil_flow_mod *,
300                                        enum ofputil_protocol);
301
302 /* Flow stats or aggregate stats request, independent of protocol. */
303 struct ofputil_flow_stats_request {
304     bool aggregate;             /* Aggregate results? */
305     struct match match;
306     ovs_be64 cookie;
307     ovs_be64 cookie_mask;
308     ofp_port_t out_port;
309     uint32_t out_group;
310     uint8_t table_id;
311 };
312
313 enum ofperr ofputil_decode_flow_stats_request(
314     struct ofputil_flow_stats_request *, const struct ofp_header *);
315 struct ofpbuf *ofputil_encode_flow_stats_request(
316     const struct ofputil_flow_stats_request *, enum ofputil_protocol);
317
318 /* Flow stats reply, independent of protocol. */
319 struct ofputil_flow_stats {
320     struct match match;
321     ovs_be64 cookie;
322     uint8_t table_id;
323     uint32_t duration_sec;
324     uint32_t duration_nsec;
325     uint16_t priority;
326     uint16_t idle_timeout;
327     uint16_t hard_timeout;
328     int idle_age;               /* Seconds since last packet, -1 if unknown. */
329     int hard_age;               /* Seconds since last change, -1 if unknown. */
330     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
331     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
332     struct ofpact *ofpacts;
333     size_t ofpacts_len;
334     enum ofputil_flow_mod_flags flags;
335 };
336
337 int ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *,
338                                     struct ofpbuf *msg,
339                                     bool flow_age_extension,
340                                     struct ofpbuf *ofpacts);
341 void ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *,
342                                      struct list *replies);
343
344 /* Aggregate stats reply, independent of protocol. */
345 struct ofputil_aggregate_stats {
346     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
347     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
348     uint32_t flow_count;
349 };
350
351 struct ofpbuf *ofputil_encode_aggregate_stats_reply(
352     const struct ofputil_aggregate_stats *stats,
353     const struct ofp_header *request);
354 enum ofperr ofputil_decode_aggregate_stats_reply(
355     struct ofputil_aggregate_stats *,
356     const struct ofp_header *reply);
357
358 /* Flow removed message, independent of protocol. */
359 struct ofputil_flow_removed {
360     struct match match;
361     uint16_t priority;
362     ovs_be64 cookie;
363     uint8_t reason;             /* One of OFPRR_*. */
364     uint8_t table_id;           /* 255 if message didn't include table ID. */
365     uint32_t duration_sec;
366     uint32_t duration_nsec;
367     uint16_t idle_timeout;
368     uint16_t hard_timeout;
369     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
370     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
371 };
372
373 enum ofperr ofputil_decode_flow_removed(struct ofputil_flow_removed *,
374                                         const struct ofp_header *);
375 struct ofpbuf *ofputil_encode_flow_removed(const struct ofputil_flow_removed *,
376                                            enum ofputil_protocol);
377
378 /* Abstract packet-in message. */
379 struct ofputil_packet_in {
380     /* Packet data and metadata.
381      *
382      * To save bandwidth, in some cases a switch may send only the first
383      * several bytes of a packet, indicated by 'packet_len < total_len'.  When
384      * the full packet is included, 'packet_len == total_len'. */
385     const void *packet;
386     size_t packet_len;          /* Number of bytes in 'packet'. */
387     size_t total_len;           /* Size of packet, pre-truncation. */
388     struct flow_metadata fmd;
389
390     /* Identifies a buffer in the switch that contains the full packet, to
391      * allow the controller to reference it later without having to send the
392      * entire packet back to the switch.
393      *
394      * UINT32_MAX indicates that the packet is not buffered in the switch.  A
395      * switch should only use UINT32_MAX when it sends the entire packet. */
396     uint32_t buffer_id;
397
398     /* Reason that the packet-in is being sent. */
399     enum ofp_packet_in_reason reason;    /* One of OFPR_*. */
400
401     /* Information about the OpenFlow flow that triggered the packet-in.
402      *
403      * A packet-in triggered by a flow table miss has no associated flow.  In
404      * that case, 'cookie' is UINT64_MAX. */
405     uint8_t table_id;                    /* OpenFlow table ID. */
406     ovs_be64 cookie;                     /* Flow's cookie. */
407 };
408
409 enum ofperr ofputil_decode_packet_in(struct ofputil_packet_in *,
410                                      const struct ofp_header *);
411 struct ofpbuf *ofputil_encode_packet_in(const struct ofputil_packet_in *,
412                                         enum ofputil_protocol protocol,
413                                         enum nx_packet_in_format);
414
415 enum { OFPUTIL_PACKET_IN_REASON_BUFSIZE = INT_STRLEN(int) + 1 };
416 const char *ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason,
417                                                char *reasonbuf,
418                                                size_t bufsize);
419 bool ofputil_packet_in_reason_from_string(const char *,
420                                           enum ofp_packet_in_reason *);
421
422 /* Abstract packet-out message.
423  *
424  * ofputil_decode_packet_out() will ensure that 'in_port' is a physical port
425  * (OFPP_MAX or less) or one of OFPP_LOCAL, OFPP_NONE, or OFPP_CONTROLLER. */
426 struct ofputil_packet_out {
427     const void *packet;         /* Packet data, if buffer_id == UINT32_MAX. */
428     size_t packet_len;          /* Length of packet data in bytes. */
429     uint32_t buffer_id;         /* Buffer id or UINT32_MAX if no buffer. */
430     ofp_port_t in_port;         /* Packet's input port. */
431     struct ofpact *ofpacts;     /* Actions. */
432     size_t ofpacts_len;         /* Size of ofpacts in bytes. */
433 };
434
435 enum ofperr ofputil_decode_packet_out(struct ofputil_packet_out *,
436                                       const struct ofp_header *,
437                                       struct ofpbuf *ofpacts);
438 struct ofpbuf *ofputil_encode_packet_out(const struct ofputil_packet_out *,
439                                          enum ofputil_protocol protocol);
440
441 enum ofputil_port_config {
442     /* OpenFlow 1.0 and 1.1 share these values for these port config bits. */
443     OFPUTIL_PC_PORT_DOWN    = 1 << 0, /* Port is administratively down. */
444     OFPUTIL_PC_NO_RECV      = 1 << 2, /* Drop all packets received by port. */
445     OFPUTIL_PC_NO_FWD       = 1 << 5, /* Drop packets forwarded to port. */
446     OFPUTIL_PC_NO_PACKET_IN = 1 << 6, /* No send packet-in msgs for port. */
447     /* OpenFlow 1.0 only. */
448     OFPUTIL_PC_NO_STP       = 1 << 1, /* No 802.1D spanning tree for port. */
449     OFPUTIL_PC_NO_RECV_STP  = 1 << 3, /* Drop received 802.1D STP packets. */
450     OFPUTIL_PC_NO_FLOOD     = 1 << 4, /* Do not include port when flooding. */
451     /* There are no OpenFlow 1.1-only bits. */
452 };
453
454 enum ofputil_port_state {
455     /* OpenFlow 1.0 and 1.1 share this values for these port state bits. */
456     OFPUTIL_PS_LINK_DOWN   = 1 << 0, /* No physical link present. */
457     /* OpenFlow 1.1 only. */
458     OFPUTIL_PS_BLOCKED     = 1 << 1, /* Port is blocked */
459     OFPUTIL_PS_LIVE        = 1 << 2, /* Live for Fast Failover Group. */
460     /* OpenFlow 1.0 only. */
461     OFPUTIL_PS_STP_LISTEN  = 0 << 8, /* Not learning or relaying frames. */
462     OFPUTIL_PS_STP_LEARN   = 1 << 8, /* Learning but not relaying frames. */
463     OFPUTIL_PS_STP_FORWARD = 2 << 8, /* Learning and relaying frames. */
464     OFPUTIL_PS_STP_BLOCK   = 3 << 8, /* Not part of spanning tree. */
465     OFPUTIL_PS_STP_MASK    = 3 << 8  /* Bit mask for OFPPS10_STP_* values. */
466 };
467
468 /* Abstract ofp10_phy_port or ofp11_port. */
469 struct ofputil_phy_port {
470     ofp_port_t port_no;
471     uint8_t hw_addr[OFP_ETH_ALEN];
472     char name[OFP_MAX_PORT_NAME_LEN];
473     enum ofputil_port_config config;
474     enum ofputil_port_state state;
475
476     /* NETDEV_F_* feature bitmasks. */
477     enum netdev_features curr;       /* Current features. */
478     enum netdev_features advertised; /* Features advertised by the port. */
479     enum netdev_features supported;  /* Features supported by the port. */
480     enum netdev_features peer;       /* Features advertised by peer. */
481
482     /* Speed. */
483     uint32_t curr_speed;        /* Current speed, in kbps. */
484     uint32_t max_speed;         /* Maximum supported speed, in kbps. */
485 };
486
487 enum ofputil_capabilities {
488     /* OpenFlow 1.0, 1.1, 1.2, and 1.3 share these capability values. */
489     OFPUTIL_C_FLOW_STATS     = 1 << 0,  /* Flow statistics. */
490     OFPUTIL_C_TABLE_STATS    = 1 << 1,  /* Table statistics. */
491     OFPUTIL_C_PORT_STATS     = 1 << 2,  /* Port statistics. */
492     OFPUTIL_C_IP_REASM       = 1 << 5,  /* Can reassemble IP fragments. */
493     OFPUTIL_C_QUEUE_STATS    = 1 << 6,  /* Queue statistics. */
494
495     /* OpenFlow 1.0 and 1.1 share this capability. */
496     OFPUTIL_C_ARP_MATCH_IP   = 1 << 7,  /* Match IP addresses in ARP pkts. */
497
498     /* OpenFlow 1.0 only. */
499     OFPUTIL_C_STP            = 1 << 3,  /* 802.1d spanning tree. */
500
501     /* OpenFlow 1.1, 1.2, and 1.3 share this capability. */
502     OFPUTIL_C_GROUP_STATS    = 1 << 4,  /* Group statistics. */
503
504     /* OpenFlow 1.2 and 1.3 share this capability */
505     OFPUTIL_C_PORT_BLOCKED   = 1 << 8,  /* Switch will block looping ports */
506 };
507
508 enum ofputil_action_bitmap {
509     OFPUTIL_A_OUTPUT         = 1 << 0,
510     OFPUTIL_A_SET_VLAN_VID   = 1 << 1,
511     OFPUTIL_A_SET_VLAN_PCP   = 1 << 2,
512     OFPUTIL_A_STRIP_VLAN     = 1 << 3,
513     OFPUTIL_A_SET_DL_SRC     = 1 << 4,
514     OFPUTIL_A_SET_DL_DST     = 1 << 5,
515     OFPUTIL_A_SET_NW_SRC     = 1 << 6,
516     OFPUTIL_A_SET_NW_DST     = 1 << 7,
517     OFPUTIL_A_SET_NW_ECN     = 1 << 8,
518     OFPUTIL_A_SET_NW_TOS     = 1 << 9,
519     OFPUTIL_A_SET_TP_SRC     = 1 << 10,
520     OFPUTIL_A_SET_TP_DST     = 1 << 11,
521     OFPUTIL_A_ENQUEUE        = 1 << 12,
522     OFPUTIL_A_COPY_TTL_OUT   = 1 << 13,
523     OFPUTIL_A_COPY_TTL_IN    = 1 << 14,
524     OFPUTIL_A_SET_MPLS_LABEL = 1 << 15,
525     OFPUTIL_A_SET_MPLS_TC    = 1 << 16,
526     OFPUTIL_A_SET_MPLS_TTL   = 1 << 17,
527     OFPUTIL_A_DEC_MPLS_TTL   = 1 << 18,
528     OFPUTIL_A_PUSH_VLAN      = 1 << 19,
529     OFPUTIL_A_POP_VLAN       = 1 << 20,
530     OFPUTIL_A_PUSH_MPLS      = 1 << 21,
531     OFPUTIL_A_POP_MPLS       = 1 << 22,
532     OFPUTIL_A_SET_QUEUE      = 1 << 23,
533     OFPUTIL_A_GROUP          = 1 << 24,
534     OFPUTIL_A_SET_NW_TTL     = 1 << 25,
535     OFPUTIL_A_DEC_NW_TTL     = 1 << 26,
536     OFPUTIL_A_SET_FIELD      = 1 << 27,
537 };
538
539 /* Abstract ofp_switch_features. */
540 struct ofputil_switch_features {
541     uint64_t datapath_id;       /* Datapath unique ID. */
542     uint32_t n_buffers;         /* Max packets buffered at once. */
543     uint8_t n_tables;           /* Number of tables supported by datapath. */
544     uint8_t auxiliary_id;       /* Identify auxiliary connections */
545     enum ofputil_capabilities capabilities;
546     enum ofputil_action_bitmap actions;
547 };
548
549 enum ofperr ofputil_decode_switch_features(const struct ofp_header *,
550                                            struct ofputil_switch_features *,
551                                            struct ofpbuf *);
552
553 struct ofpbuf *ofputil_encode_switch_features(
554     const struct ofputil_switch_features *, enum ofputil_protocol,
555     ovs_be32 xid);
556 void ofputil_put_switch_features_port(const struct ofputil_phy_port *,
557                                       struct ofpbuf *);
558 bool ofputil_switch_features_ports_trunc(struct ofpbuf *b);
559
560 /* phy_port helper functions. */
561 int ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *,
562                           struct ofputil_phy_port *);
563 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *);
564
565 /* Abstract ofp_port_status. */
566 struct ofputil_port_status {
567     enum ofp_port_reason reason;
568     struct ofputil_phy_port desc;
569 };
570
571 enum ofperr ofputil_decode_port_status(const struct ofp_header *,
572                                        struct ofputil_port_status *);
573 struct ofpbuf *ofputil_encode_port_status(const struct ofputil_port_status *,
574                                           enum ofputil_protocol);
575
576 /* Abstract ofp_port_mod. */
577 struct ofputil_port_mod {
578     ofp_port_t port_no;
579     uint8_t hw_addr[OFP_ETH_ALEN];
580     enum ofputil_port_config config;
581     enum ofputil_port_config mask;
582     enum netdev_features advertise;
583 };
584
585 enum ofperr ofputil_decode_port_mod(const struct ofp_header *,
586                                     struct ofputil_port_mod *);
587 struct ofpbuf *ofputil_encode_port_mod(const struct ofputil_port_mod *,
588                                        enum ofputil_protocol);
589
590 /* Abstract ofp_table_mod. */
591 struct ofputil_table_mod {
592     uint8_t table_id;         /* ID of the table, 0xff indicates all tables. */
593     uint32_t config;
594 };
595
596 enum ofperr ofputil_decode_table_mod(const struct ofp_header *,
597                                     struct ofputil_table_mod *);
598 struct ofpbuf *ofputil_encode_table_mod(const struct ofputil_table_mod *,
599                                        enum ofputil_protocol);
600
601 /* Meter band configuration for all supported band types. */
602 struct ofputil_meter_band {
603     uint16_t type;
604     uint8_t prec_level;         /* Non-zero if type == OFPMBT_DSCP_REMARK. */
605     uint32_t rate;
606     uint32_t burst_size;
607 };
608
609 struct ofputil_meter_band_stats {
610     uint64_t packet_count;
611     uint64_t byte_count;
612 };
613
614 struct ofputil_meter_config {
615     uint32_t meter_id;
616     uint16_t flags;
617     uint16_t n_bands;
618     struct ofputil_meter_band *bands;
619 };
620
621 /* Abstract ofp_meter_mod. */
622 struct ofputil_meter_mod {
623     uint16_t command;
624     struct ofputil_meter_config meter;
625 };
626
627 struct ofputil_meter_stats {
628     uint32_t meter_id;
629     uint32_t flow_count;
630     uint64_t packet_in_count;
631     uint64_t byte_in_count;
632     uint32_t duration_sec;
633     uint32_t duration_nsec;
634     uint16_t n_bands;
635     struct ofputil_meter_band_stats *bands;
636 };
637
638 struct ofputil_meter_features {
639     uint32_t max_meters;        /* Maximum number of meters. */
640     uint32_t band_types;        /* Can support max 32 band types. */
641     uint32_t capabilities;      /* Supported flags. */
642     uint8_t  max_bands;
643     uint8_t  max_color;
644 };
645
646 enum ofperr ofputil_decode_meter_mod(const struct ofp_header *,
647                                      struct ofputil_meter_mod *,
648                                      struct ofpbuf *bands);
649 struct ofpbuf *ofputil_encode_meter_mod(enum ofp_version,
650                                         const struct ofputil_meter_mod *);
651
652 void ofputil_decode_meter_features(const struct ofp_header *,
653                                    struct ofputil_meter_features *);
654 struct ofpbuf *ofputil_encode_meter_features_reply(const struct
655                                                    ofputil_meter_features *,
656                                                    const struct ofp_header *
657                                                    request);
658 void ofputil_decode_meter_request(const struct ofp_header *,
659                                   uint32_t *meter_id);
660
661 void ofputil_append_meter_config(struct list *replies,
662                                  const struct ofputil_meter_config *);
663
664 void ofputil_append_meter_stats(struct list *replies,
665                                 const struct ofputil_meter_stats *);
666
667 enum ofputil_meter_request_type {
668     OFPUTIL_METER_FEATURES,
669     OFPUTIL_METER_CONFIG,
670     OFPUTIL_METER_STATS
671 };
672
673 struct ofpbuf *ofputil_encode_meter_request(enum ofp_version,
674                                             enum ofputil_meter_request_type,
675                                             uint32_t meter_id);
676
677 int ofputil_decode_meter_stats(struct ofpbuf *,
678                                struct ofputil_meter_stats *,
679                                struct ofpbuf *bands);
680
681 int ofputil_decode_meter_config(struct ofpbuf *,
682                                 struct ofputil_meter_config *,
683                                 struct ofpbuf *bands);
684
685 /* Type for meter_id in ofproto provider interface, UINT32_MAX if invalid. */
686 typedef struct { uint32_t uint32; } ofproto_meter_id;
687
688 /* Abstract ofp_role_request and reply. */
689 struct ofputil_role_request {
690     enum ofp12_controller_role role;
691     bool have_generation_id;
692     uint64_t generation_id;
693 };
694
695 enum ofperr ofputil_decode_role_message(const struct ofp_header *,
696                                         struct ofputil_role_request *);
697 struct ofpbuf *ofputil_encode_role_reply(const struct ofp_header *,
698                                          const struct ofputil_role_request *);
699
700 /* Abstract table stats.
701  *
702  * For now we use ofp12_table_stats as a superset of the other protocol
703  * versions' table stats. */
704
705 struct ofpbuf *ofputil_encode_table_stats_reply(
706     const struct ofp12_table_stats[], int n,
707     const struct ofp_header *request);
708
709 /* Abstract nx_flow_monitor_request. */
710 struct ofputil_flow_monitor_request {
711     uint32_t id;
712     enum nx_flow_monitor_flags flags;
713     ofp_port_t out_port;
714     uint8_t table_id;
715     struct match match;
716 };
717
718 int ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *,
719                                         struct ofpbuf *msg);
720 void ofputil_append_flow_monitor_request(
721     const struct ofputil_flow_monitor_request *, struct ofpbuf *msg);
722
723 /* Abstract nx_flow_update. */
724 struct ofputil_flow_update {
725     enum nx_flow_update_event event;
726
727     /* Used only for NXFME_ADDED, NXFME_DELETED, NXFME_MODIFIED. */
728     enum ofp_flow_removed_reason reason;
729     uint16_t idle_timeout;
730     uint16_t hard_timeout;
731     uint8_t table_id;
732     ovs_be64 cookie;
733     struct match *match;
734     uint16_t priority;
735     struct ofpact *ofpacts;
736     size_t ofpacts_len;
737
738     /* Used only for NXFME_ABBREV. */
739     ovs_be32 xid;
740 };
741
742 int ofputil_decode_flow_update(struct ofputil_flow_update *,
743                                struct ofpbuf *msg, struct ofpbuf *ofpacts);
744 void ofputil_start_flow_update(struct list *replies);
745 void ofputil_append_flow_update(const struct ofputil_flow_update *,
746                                 struct list *replies);
747
748 /* Abstract nx_flow_monitor_cancel. */
749 uint32_t ofputil_decode_flow_monitor_cancel(const struct ofp_header *);
750 struct ofpbuf *ofputil_encode_flow_monitor_cancel(uint32_t id);
751
752 /* Encoding OpenFlow stats messages. */
753 void ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
754                                           const struct ofputil_phy_port *pp,
755                                           struct list *replies);
756
757 /* Encoding simple OpenFlow messages. */
758 struct ofpbuf *make_echo_request(enum ofp_version);
759 struct ofpbuf *make_echo_reply(const struct ofp_header *rq);
760
761 struct ofpbuf *ofputil_encode_barrier_request(enum ofp_version);
762
763 const char *ofputil_frag_handling_to_string(enum ofp_config_flags);
764 bool ofputil_frag_handling_from_string(const char *, enum ofp_config_flags *);
765
766 \f
767 /* Actions. */
768
769 /* The type of an action.
770  *
771  * For each implemented OFPAT10_* and NXAST_* action type, there is a
772  * corresponding constant prefixed with OFPUTIL_, e.g.:
773  *
774  * OFPUTIL_OFPAT10_OUTPUT
775  * OFPUTIL_OFPAT10_SET_VLAN_VID
776  * OFPUTIL_OFPAT10_SET_VLAN_PCP
777  * OFPUTIL_OFPAT10_STRIP_VLAN
778  * OFPUTIL_OFPAT10_SET_DL_SRC
779  * OFPUTIL_OFPAT10_SET_DL_DST
780  * OFPUTIL_OFPAT10_SET_NW_SRC
781  * OFPUTIL_OFPAT10_SET_NW_DST
782  * OFPUTIL_OFPAT10_SET_NW_TOS
783  * OFPUTIL_OFPAT10_SET_TP_SRC
784  * OFPUTIL_OFPAT10_SET_TP_DST
785  * OFPUTIL_OFPAT10_ENQUEUE
786  * OFPUTIL_NXAST_RESUBMIT
787  * OFPUTIL_NXAST_SET_TUNNEL
788  * OFPUTIL_NXAST_SET_METADATA
789  * OFPUTIL_NXAST_SET_QUEUE
790  * OFPUTIL_NXAST_POP_QUEUE
791  * OFPUTIL_NXAST_REG_MOVE
792  * OFPUTIL_NXAST_REG_LOAD
793  * OFPUTIL_NXAST_NOTE
794  * OFPUTIL_NXAST_SET_TUNNEL64
795  * OFPUTIL_NXAST_MULTIPATH
796  * OFPUTIL_NXAST_BUNDLE
797  * OFPUTIL_NXAST_BUNDLE_LOAD
798  * OFPUTIL_NXAST_RESUBMIT_TABLE
799  * OFPUTIL_NXAST_OUTPUT_REG
800  * OFPUTIL_NXAST_LEARN
801  * OFPUTIL_NXAST_DEC_TTL
802  * OFPUTIL_NXAST_FIN_TIMEOUT
803  *
804  * (The above list helps developers who want to "grep" for these definitions.)
805  */
806 enum OVS_PACKED_ENUM ofputil_action_code {
807     OFPUTIL_ACTION_INVALID,
808 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             OFPUTIL_##ENUM,
809 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) OFPUTIL_##ENUM,
810 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   OFPUTIL_##ENUM,
811 #include "ofp-util.def"
812 };
813
814 /* The number of values of "enum ofputil_action_code". */
815 enum {
816 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)             + 1
817 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) + 1
818 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)   + 1
819     OFPUTIL_N_ACTIONS = 1
820 #include "ofp-util.def"
821 };
822
823 int ofputil_action_code_from_name(const char *);
824
825 void *ofputil_put_action(enum ofputil_action_code, struct ofpbuf *buf);
826
827 /* For each OpenFlow action <ENUM> that has a corresponding action structure
828  * struct <STRUCT>, this defines two functions:
829  *
830  *   void ofputil_init_<ENUM>(struct <STRUCT> *action);
831  *
832  *     Initializes the parts of 'action' that identify it as having type <ENUM>
833  *     and length 'sizeof *action' and zeros the rest.  For actions that have
834  *     variable length, the length used and cleared is that of struct <STRUCT>.
835  *
836  *  struct <STRUCT> *ofputil_put_<ENUM>(struct ofpbuf *buf);
837  *
838  *     Appends a new 'action', of length 'sizeof(struct <STRUCT>)', to 'buf',
839  *     initializes it with ofputil_init_<ENUM>(), and returns it.
840  */
841 #define OFPAT10_ACTION(ENUM, STRUCT, NAME)              \
842     void ofputil_init_##ENUM(struct STRUCT *);          \
843     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
844 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
845     void ofputil_init_##ENUM(struct STRUCT *);          \
846     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
847 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)    \
848     void ofputil_init_##ENUM(struct STRUCT *);          \
849     struct STRUCT *ofputil_put_##ENUM(struct ofpbuf *);
850 #include "ofp-util.def"
851
852 #define OFP_ACTION_ALIGN 8      /* Alignment of ofp_actions. */
853
854 bool action_outputs_to_port(const union ofp_action *, ovs_be16 port);
855
856 enum ofperr ofputil_pull_actions(struct ofpbuf *, unsigned int actions_len,
857                                  union ofp_action **, size_t *);
858
859 bool ofputil_actions_equal(const union ofp_action *a, size_t n_a,
860                            const union ofp_action *b, size_t n_b);
861 union ofp_action *ofputil_actions_clone(const union ofp_action *, size_t n);
862
863 /* Handy utility for parsing flows and actions. */
864 bool ofputil_parse_key_value(char **stringp, char **keyp, char **valuep);
865
866 struct ofputil_port_stats {
867     ofp_port_t port_no;
868     struct netdev_stats stats;
869     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
870     uint32_t duration_nsec;
871 };
872
873 struct ofpbuf *ofputil_encode_dump_ports_request(enum ofp_version ofp_version,
874                                                  ofp_port_t port);
875 void ofputil_append_port_stat(struct list *replies,
876                               const struct ofputil_port_stats *ops);
877 size_t ofputil_count_port_stats(const struct ofp_header *);
878 int ofputil_decode_port_stats(struct ofputil_port_stats *, struct ofpbuf *msg);
879 enum ofperr ofputil_decode_port_stats_request(const struct ofp_header *request,
880                                               ofp_port_t *ofp10_port);
881
882 struct ofputil_queue_stats_request {
883     ofp_port_t port_no;           /* OFPP_ANY means "all ports". */
884     uint32_t queue_id;
885 };
886
887 enum ofperr
888 ofputil_decode_queue_stats_request(const struct ofp_header *request,
889                                    struct ofputil_queue_stats_request *oqsr);
890 struct ofpbuf *
891 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
892                                    const struct ofputil_queue_stats_request *oqsr);
893
894 struct ofputil_queue_stats {
895     ofp_port_t port_no;
896     uint32_t queue_id;
897
898     /* Values of unsupported statistics are set to all-1-bits (UINT64_MAX). */
899     uint64_t tx_bytes;
900     uint64_t tx_packets;
901     uint64_t tx_errors;
902
903     /* UINT32_MAX if unknown. */
904     uint32_t duration_sec;
905     uint32_t duration_nsec;
906 };
907
908 size_t ofputil_count_queue_stats(const struct ofp_header *);
909 int ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg);
910 void ofputil_append_queue_stat(struct list *replies,
911                                const struct ofputil_queue_stats *oqs);
912
913 /* Bucket for use in groups. */
914 struct ofputil_bucket {
915     struct list list_node;
916     uint16_t weight;            /* Relative weight, for "select" groups. */
917     ofp_port_t watch_port;      /* Port whose state affects whether this bucket
918                                  * is live. Only required for fast failover
919                                  * groups. */
920     uint32_t watch_group;       /* Group whose state affects whether this
921                                  * bucket is live. Only required for fast
922                                  * failover groups. */
923     struct ofpact *ofpacts;     /* Series of "struct ofpact"s. */
924     size_t ofpacts_len;         /* Length of ofpacts, in bytes. */
925 };
926
927 /* Protocol-independent group_mod. */
928 struct ofputil_group_mod {
929     uint16_t command;             /* One of OFPGC11_*. */
930     uint8_t type;                 /* One of OFPGT11_*. */
931     uint32_t group_id;            /* Group identifier. */
932     struct list buckets;          /* Contains "struct ofputil_bucket"s. */
933 };
934
935 struct bucket_counter {
936     uint64_t packet_count;   /* Number of packets processed by bucket. */
937     uint64_t byte_count;     /* Number of bytes processed by bucket. */
938 };
939
940 /* Group stats reply, independent of protocol. */
941 struct ofputil_group_stats {
942     uint32_t group_id;    /* Group identifier. */
943     uint32_t ref_count;
944     uint64_t packet_count;      /* Packet count, UINT64_MAX if unknown. */
945     uint64_t byte_count;        /* Byte count, UINT64_MAX if unknown. */
946     uint32_t duration_sec;      /* UINT32_MAX if unknown. */
947     uint32_t duration_nsec;
948     uint32_t n_buckets;
949     struct bucket_counter *bucket_stats;
950 };
951
952 /* Group features reply, independent of protocol. */
953 struct ofputil_group_features {
954     uint32_t  types;           /* Bitmap of OFPGT_* values supported. */
955     uint32_t  capabilities;    /* Bitmap of OFPGFC12_* capability supported. */
956     uint32_t  max_groups[4];   /* Maximum number of groups for each type. */
957     uint32_t  actions[4];      /* Bitmaps of OFPAT_* that are supported. */
958 };
959
960 /* Group desc reply, independent of protocol. */
961 struct ofputil_group_desc {
962     uint8_t type;               /* One of OFPGT_*. */
963     uint32_t group_id;          /* Group identifier. */
964     struct list buckets;        /* Contains "struct ofputil_bucket"s. */
965 };
966
967 void ofputil_bucket_list_destroy(struct list *buckets);
968
969 struct ofpbuf *ofputil_encode_group_stats_request(enum ofp_version,
970                                                   uint32_t group_id);
971 enum ofperr ofputil_decode_group_stats_request(
972     const struct ofp_header *request, uint32_t *group_id);
973 void ofputil_append_group_stats(struct list *replies,
974                                 const struct ofputil_group_stats *);
975 struct ofpbuf *ofputil_encode_group_features_request(enum ofp_version);
976 struct ofpbuf *ofputil_encode_group_features_reply(
977     const struct ofputil_group_features *, const struct ofp_header *request);
978 void ofputil_decode_group_features_reply(const struct ofp_header *,
979                                          struct ofputil_group_features *);
980 struct ofpbuf *ofputil_encode_group_mod(enum ofp_version ofp_version,
981                                         const struct ofputil_group_mod *gm);
982
983 enum ofperr ofputil_decode_group_mod(const struct ofp_header *,
984                                      struct ofputil_group_mod *);
985
986 int ofputil_decode_group_stats_reply(struct ofpbuf *,
987                                      struct ofputil_group_stats *);
988
989 int ofputil_decode_group_desc_reply(struct ofputil_group_desc *,
990                                     struct ofpbuf *, enum ofp_version);
991
992 void ofputil_append_group_desc_reply(const struct ofputil_group_desc *,
993                                      struct list *buckets,
994                                      struct list *replies);
995 struct ofpbuf *ofputil_encode_group_desc_request(enum ofp_version);
996
997 #endif /* ofp-util.h */