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