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