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