]> git.proxmox.com Git - ovs.git/blame - lib/ofp-util.c
flow: Rearrange IPv6 addresses to remove potential hole in structure.
[ovs.git] / lib / ofp-util.c
CommitLineData
fa37b408 1/*
dc4762ed 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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#include <config.h>
18#include "ofp-print.h"
dc4762ed 19#include <errno.h>
fa37b408 20#include <inttypes.h>
6ca00f6f
ETN
21#include <sys/types.h>
22#include <netinet/in.h>
b459a924 23#include <netinet/icmp6.h>
fa37b408 24#include <stdlib.h>
3b6a2571 25#include "autopath.h"
daff3353 26#include "bundle.h"
10a24935 27#include "byte-order.h"
d8ae4d67 28#include "classifier.h"
dc4762ed 29#include "dynamic-string.h"
75a75043 30#include "learn.h"
53ddd40a 31#include "multipath.h"
b6c9e612 32#include "nx-match.h"
dc4762ed 33#include "ofp-errors.h"
fa37b408
BP
34#include "ofp-util.h"
35#include "ofpbuf.h"
36#include "packets.h"
37#include "random.h"
4ffd1b43 38#include "unaligned.h"
e41a9130 39#include "type-props.h"
5136ce49 40#include "vlog.h"
fa37b408 41
d98e6007 42VLOG_DEFINE_THIS_MODULE(ofp_util);
fa37b408
BP
43
44/* Rate limit for OpenFlow message parse errors. These always indicate a bug
45 * in the peer and so there's not much point in showing a lot of them. */
46static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
47
0596e897
BP
48/* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
49 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
50 * is wildcarded.
51 *
52 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
53 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
54 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
55 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
56 * wildcarded. */
57ovs_be32
58ofputil_wcbits_to_netmask(int wcbits)
59{
60 wcbits &= 0x3f;
61 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
62}
63
64/* Given the IP netmask 'netmask', returns the number of bits of the IP address
aad29cd1
BP
65 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
66 * must be a CIDR netmask (see ip_is_cidr()). */
0596e897
BP
67int
68ofputil_netmask_to_wcbits(ovs_be32 netmask)
69{
aad29cd1 70 return 32 - ip_count_cidr_bits(netmask);
0596e897
BP
71}
72
d8ae4d67
BP
73/* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
74 * name. */
75#define WC_INVARIANT_LIST \
76 WC_INVARIANT_BIT(IN_PORT) \
d8ae4d67
BP
77 WC_INVARIANT_BIT(DL_SRC) \
78 WC_INVARIANT_BIT(DL_DST) \
79 WC_INVARIANT_BIT(DL_TYPE) \
80 WC_INVARIANT_BIT(NW_PROTO) \
81 WC_INVARIANT_BIT(TP_SRC) \
82 WC_INVARIANT_BIT(TP_DST)
83
84/* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
85 * actually have the same names and values. */
86#define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
87 WC_INVARIANT_LIST
88#undef WC_INVARIANT_BIT
89
90/* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
91 * OR'd together. */
eeba8e4f 92static const flow_wildcards_t WC_INVARIANTS = 0
d8ae4d67
BP
93#define WC_INVARIANT_BIT(NAME) | FWW_##NAME
94 WC_INVARIANT_LIST
95#undef WC_INVARIANT_BIT
eeba8e4f 96;
d8ae4d67 97
eb6f28db
BP
98/* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
99 * struct cls_rule. It is the caller's responsibility to handle the special
100 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
7286b1e1 101void
eb6f28db 102ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
d8ae4d67 103{
7257b535 104 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 3);
a877206f 105
d8ae4d67 106 /* Initialize most of rule->wc. */
f9ba8dad 107 flow_wildcards_init_catchall(wc);
eeba8e4f 108 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
bad68a99
JP
109
110 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
685a51a5 111 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_ND_TARGET);
bad68a99 112
7257b535
BP
113 if (!(ofpfw & OFPFW_NW_TOS)) {
114 wc->tos_frag_mask |= IP_DSCP_MASK;
d8ae4d67 115 }
7257b535 116
d8ae4d67
BP
117 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
118 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
119
d8ae4d67
BP
120 if (ofpfw & OFPFW_DL_DST) {
121 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
122 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
123 * and FWW_ETH_MCAST. */
124 wc->wildcards |= FWW_ETH_MCAST;
125 }
126
eb6f28db
BP
127 /* VLAN TCI mask. */
128 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
129 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
130 }
131 if (!(ofpfw & OFPFW_DL_VLAN)) {
132 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
133 }
134}
135
136/* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
137 * 'priority'. */
138void
139ofputil_cls_rule_from_match(const struct ofp_match *match,
140 unsigned int priority, struct cls_rule *rule)
141{
142 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
143
144 /* Initialize rule->priority, rule->wc. */
145 rule->priority = !ofpfw ? UINT16_MAX : priority;
146 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
147
66642cb4 148 /* Initialize most of rule->flow. */
d8ae4d67
BP
149 rule->flow.nw_src = match->nw_src;
150 rule->flow.nw_dst = match->nw_dst;
abe529af 151 rule->flow.in_port = ntohs(match->in_port);
36956a7d 152 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
d8ae4d67
BP
153 rule->flow.tp_src = match->tp_src;
154 rule->flow.tp_dst = match->tp_dst;
155 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
156 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
7257b535 157 rule->flow.tos_frag = match->nw_tos & IP_DSCP_MASK;
d8ae4d67
BP
158 rule->flow.nw_proto = match->nw_proto;
159
66642cb4 160 /* Translate VLANs. */
47271d0d
BP
161 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
162 /* Match only packets without 802.1Q header.
163 *
164 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
165 *
166 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
167 * because we can't have a specific PCP without an 802.1Q header.
168 * However, older versions of OVS treated this as matching packets
169 * withut an 802.1Q header, so we do here too. */
66642cb4 170 rule->flow.vlan_tci = htons(0);
eb6f28db 171 rule->wc.vlan_tci_mask = htons(0xffff);
47271d0d
BP
172 } else {
173 ovs_be16 vid, pcp, tci;
174
47271d0d
BP
175 vid = match->dl_vlan & htons(VLAN_VID_MASK);
176 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
177 tci = vid | pcp | htons(VLAN_CFI);
eb6f28db 178 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
66642cb4
BP
179 }
180
d8ae4d67
BP
181 /* Clean up. */
182 cls_rule_zero_wildcarded_fields(rule);
183}
184
b78f6b77 185/* Convert 'rule' into the OpenFlow match structure 'match'. */
d8ae4d67 186void
b78f6b77 187ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
d8ae4d67
BP
188{
189 const struct flow_wildcards *wc = &rule->wc;
eeba8e4f 190 uint32_t ofpfw;
d8ae4d67 191
66642cb4 192 /* Figure out most OpenFlow wildcards. */
eeba8e4f 193 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
d8ae4d67
BP
194 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
195 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
7257b535 196 if (!(wc->tos_frag_mask & IP_DSCP_MASK)) {
d8ae4d67
BP
197 ofpfw |= OFPFW_NW_TOS;
198 }
ff9d3826 199
66642cb4
BP
200 /* Translate VLANs. */
201 match->dl_vlan = htons(0);
202 match->dl_vlan_pcp = 0;
203 if (rule->wc.vlan_tci_mask == htons(0)) {
204 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
205 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
206 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
207 match->dl_vlan = htons(OFP_VLAN_NONE);
208 } else {
209 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
210 ofpfw |= OFPFW_DL_VLAN;
211 } else {
212 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
213 }
214
215 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
216 ofpfw |= OFPFW_DL_VLAN_PCP;
217 } else {
218 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
219 }
220 }
221
222 /* Compose most of the match structure. */
d8ae4d67 223 match->wildcards = htonl(ofpfw);
abe529af 224 match->in_port = htons(rule->flow.in_port);
d8ae4d67
BP
225 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
226 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
36956a7d 227 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
d8ae4d67
BP
228 match->nw_src = rule->flow.nw_src;
229 match->nw_dst = rule->flow.nw_dst;
7257b535 230 match->nw_tos = rule->flow.tos_frag & IP_DSCP_MASK;
d8ae4d67
BP
231 match->nw_proto = rule->flow.nw_proto;
232 match->tp_src = rule->flow.tp_src;
233 match->tp_dst = rule->flow.tp_dst;
234 memset(match->pad1, '\0', sizeof match->pad1);
235 memset(match->pad2, '\0', sizeof match->pad2);
236}
237
36956a7d
BP
238/* Given a 'dl_type' value in the format used in struct flow, returns the
239 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
240ovs_be16
241ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
242{
243 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
244 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
245 : flow_dl_type);
246}
247
248/* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
249 * structure, returns the corresponding 'dl_type' value for use in struct
250 * flow. */
251ovs_be16
252ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
253{
254 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
255 ? htons(FLOW_DL_TYPE_NONE)
256 : ofp_dl_type);
257}
258
72fae175 259/* Returns a transaction ID to use for an outgoing OpenFlow message. */
44381c1b 260static ovs_be32
fa37b408
BP
261alloc_xid(void)
262{
72fae175 263 static uint32_t next_xid = 1;
44381c1b 264 return htonl(next_xid++);
fa37b408 265}
d1e2cf21
BP
266\f
267/* Basic parsing of OpenFlow messages. */
fa37b408 268
d1e2cf21
BP
269struct ofputil_msg_type {
270 enum ofputil_msg_code code; /* OFPUTIL_*. */
271 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
272 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
273 unsigned int min_size; /* Minimum total message size in bytes. */
274 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
275 * the message may exceed 'min_size' by an even multiple of this value. */
276 unsigned int extra_multiple;
277};
278
5a020ef3
BP
279/* Represents a malformed OpenFlow message. */
280static const struct ofputil_msg_type ofputil_invalid_type = {
281 OFPUTIL_MSG_INVALID, 0, "OFPUTIL_MSG_INVALID", 0, 0
282};
283
d1e2cf21
BP
284struct ofputil_msg_category {
285 const char *name; /* e.g. "OpenFlow message" */
286 const struct ofputil_msg_type *types;
287 size_t n_types;
288 int missing_error; /* ofp_mkerr() value for missing type. */
289};
290
5a020ef3
BP
291static int
292ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
d1e2cf21
BP
293{
294 switch (type->extra_multiple) {
295 case 0:
296 if (size != type->min_size) {
5a020ef3 297 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21 298 "length %u (expected length %u)",
5a020ef3
BP
299 type->name, size, type->min_size);
300 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
d1e2cf21 301 }
5a020ef3 302 return 0;
d1e2cf21
BP
303
304 case 1:
305 if (size < type->min_size) {
5a020ef3 306 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21 307 "length %u (expected length at least %u bytes)",
5a020ef3
BP
308 type->name, size, type->min_size);
309 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
d1e2cf21 310 }
5a020ef3 311 return 0;
d1e2cf21
BP
312
313 default:
314 if (size < type->min_size
315 || (size - type->min_size) % type->extra_multiple) {
5a020ef3 316 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
d1e2cf21
BP
317 "length %u (must be exactly %u bytes or longer "
318 "by an integer multiple of %u bytes)",
5a020ef3 319 type->name, size,
d1e2cf21 320 type->min_size, type->extra_multiple);
5a020ef3 321 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
d1e2cf21 322 }
5a020ef3 323 return 0;
d1e2cf21
BP
324 }
325}
326
327static int
328ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
5a020ef3 329 uint32_t value,
d1e2cf21
BP
330 const struct ofputil_msg_type **typep)
331{
332 const struct ofputil_msg_type *type;
333
334 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
335 if (type->value == value) {
d1e2cf21
BP
336 *typep = type;
337 return 0;
338 }
339 }
340
5c47debb 341 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
d1e2cf21
BP
342 cat->name, value);
343 return cat->missing_error;
344}
345
346static int
5a020ef3 347ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
348 const struct ofputil_msg_type **typep)
349{
350 static const struct ofputil_msg_type nxt_messages[] = {
d1e2cf21
BP
351 { OFPUTIL_NXT_ROLE_REQUEST,
352 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
353 sizeof(struct nx_role_request), 0 },
354
355 { OFPUTIL_NXT_ROLE_REPLY,
356 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
357 sizeof(struct nx_role_request), 0 },
358
359 { OFPUTIL_NXT_SET_FLOW_FORMAT,
360 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
361 sizeof(struct nxt_set_flow_format), 0 },
362
363 { OFPUTIL_NXT_FLOW_MOD,
364 NXT_FLOW_MOD, "NXT_FLOW_MOD",
365 sizeof(struct nx_flow_mod), 8 },
366
367 { OFPUTIL_NXT_FLOW_REMOVED,
368 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
369 sizeof(struct nx_flow_removed), 8 },
d1e9b9bf
BP
370
371 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
372 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
373 sizeof(struct nxt_flow_mod_table_id), 0 },
d1e2cf21
BP
374 };
375
376 static const struct ofputil_msg_category nxt_category = {
377 "Nicira extension message",
378 nxt_messages, ARRAY_SIZE(nxt_messages),
379 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
380 };
381
382 const struct ofp_vendor_header *ovh;
383 const struct nicira_header *nh;
384
5a020ef3
BP
385 if (length < sizeof(struct ofp_vendor_header)) {
386 if (length == ntohs(oh->length)) {
387 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
388 }
389 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
390 }
391
d1e2cf21
BP
392 ovh = (const struct ofp_vendor_header *) oh;
393 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
394 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
395 "vendor %"PRIx32, ntohl(ovh->vendor));
396 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
397 }
398
5a020ef3
BP
399 if (length < sizeof(struct nicira_header)) {
400 if (length == ntohs(oh->length)) {
401 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
402 "length %u (expected at least %zu)",
403 ntohs(ovh->header.length),
404 sizeof(struct nicira_header));
405 }
d1e2cf21
BP
406 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
407 }
408
409 nh = (const struct nicira_header *) oh;
410 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
5a020ef3 411 typep);
d1e2cf21
BP
412}
413
414static int
5a020ef3 415check_nxstats_msg(const struct ofp_header *oh, size_t length)
d1e2cf21 416{
28c8bad1 417 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
418 ovs_be32 vendor;
419
5a020ef3
BP
420 if (length < sizeof(struct ofp_vendor_stats_msg)) {
421 if (length == ntohs(oh->length)) {
422 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
423 }
424 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
425 }
426
03cd3493 427 memcpy(&vendor, osm + 1, sizeof vendor);
d1e2cf21
BP
428 if (vendor != htonl(NX_VENDOR_ID)) {
429 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
430 "unknown vendor %"PRIx32, ntohl(vendor));
431 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
432 }
433
5a020ef3
BP
434 if (length < sizeof(struct nicira_stats_msg)) {
435 if (length == ntohs(osm->header.length)) {
436 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
437 }
d1e2cf21
BP
438 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
439 }
440
441 return 0;
442}
443
444static int
5a020ef3 445ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
446 const struct ofputil_msg_type **typep)
447{
448 static const struct ofputil_msg_type nxst_requests[] = {
449 { OFPUTIL_NXST_FLOW_REQUEST,
450 NXST_FLOW, "NXST_FLOW request",
451 sizeof(struct nx_flow_stats_request), 8 },
452
453 { OFPUTIL_NXST_AGGREGATE_REQUEST,
454 NXST_AGGREGATE, "NXST_AGGREGATE request",
455 sizeof(struct nx_aggregate_stats_request), 8 },
456 };
457
458 static const struct ofputil_msg_category nxst_request_category = {
08717852 459 "Nicira extension statistics request",
d1e2cf21
BP
460 nxst_requests, ARRAY_SIZE(nxst_requests),
461 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
462 };
463
464 const struct nicira_stats_msg *nsm;
465 int error;
466
5a020ef3 467 error = check_nxstats_msg(oh, length);
d1e2cf21
BP
468 if (error) {
469 return error;
470 }
471
472 nsm = (struct nicira_stats_msg *) oh;
473 return ofputil_lookup_openflow_message(&nxst_request_category,
5a020ef3 474 ntohl(nsm->subtype), typep);
d1e2cf21
BP
475}
476
477static int
5a020ef3 478ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
479 const struct ofputil_msg_type **typep)
480{
481 static const struct ofputil_msg_type nxst_replies[] = {
482 { OFPUTIL_NXST_FLOW_REPLY,
483 NXST_FLOW, "NXST_FLOW reply",
484 sizeof(struct nicira_stats_msg), 8 },
485
486 { OFPUTIL_NXST_AGGREGATE_REPLY,
487 NXST_AGGREGATE, "NXST_AGGREGATE reply",
488 sizeof(struct nx_aggregate_stats_reply), 0 },
489 };
490
491 static const struct ofputil_msg_category nxst_reply_category = {
08717852 492 "Nicira extension statistics reply",
d1e2cf21
BP
493 nxst_replies, ARRAY_SIZE(nxst_replies),
494 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
495 };
496
497 const struct nicira_stats_msg *nsm;
498 int error;
499
5a020ef3 500 error = check_nxstats_msg(oh, length);
d1e2cf21
BP
501 if (error) {
502 return error;
503 }
504
505 nsm = (struct nicira_stats_msg *) oh;
506 return ofputil_lookup_openflow_message(&nxst_reply_category,
5a020ef3
BP
507 ntohl(nsm->subtype), typep);
508}
509
510static int
511check_stats_msg(const struct ofp_header *oh, size_t length)
512{
513 if (length < sizeof(struct ofp_stats_msg)) {
514 if (length == ntohs(oh->length)) {
515 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
516 }
517 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
518 }
519
520 return 0;
d1e2cf21
BP
521}
522
523static int
5a020ef3 524ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
525 const struct ofputil_msg_type **typep)
526{
d1e2cf21
BP
527 static const struct ofputil_msg_type ofpst_requests[] = {
528 { OFPUTIL_OFPST_DESC_REQUEST,
529 OFPST_DESC, "OFPST_DESC request",
63f2140a 530 sizeof(struct ofp_stats_msg), 0 },
d1e2cf21
BP
531
532 { OFPUTIL_OFPST_FLOW_REQUEST,
533 OFPST_FLOW, "OFPST_FLOW request",
63f2140a 534 sizeof(struct ofp_flow_stats_request), 0 },
d1e2cf21
BP
535
536 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
537 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
63f2140a 538 sizeof(struct ofp_flow_stats_request), 0 },
d1e2cf21
BP
539
540 { OFPUTIL_OFPST_TABLE_REQUEST,
541 OFPST_TABLE, "OFPST_TABLE request",
63f2140a 542 sizeof(struct ofp_stats_msg), 0 },
d1e2cf21
BP
543
544 { OFPUTIL_OFPST_PORT_REQUEST,
545 OFPST_PORT, "OFPST_PORT request",
63f2140a 546 sizeof(struct ofp_port_stats_request), 0 },
d1e2cf21
BP
547
548 { OFPUTIL_OFPST_QUEUE_REQUEST,
549 OFPST_QUEUE, "OFPST_QUEUE request",
63f2140a 550 sizeof(struct ofp_queue_stats_request), 0 },
d1e2cf21
BP
551
552 { 0,
553 OFPST_VENDOR, "OFPST_VENDOR request",
63f2140a 554 sizeof(struct ofp_vendor_stats_msg), 1 },
d1e2cf21
BP
555 };
556
557 static const struct ofputil_msg_category ofpst_request_category = {
558 "OpenFlow statistics",
559 ofpst_requests, ARRAY_SIZE(ofpst_requests),
560 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
561 };
562
28c8bad1 563 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
564 int error;
565
5a020ef3
BP
566 error = check_stats_msg(oh, length);
567 if (error) {
568 return error;
569 }
570
d1e2cf21 571 error = ofputil_lookup_openflow_message(&ofpst_request_category,
5a020ef3 572 ntohs(request->type), typep);
28c8bad1 573 if (!error && request->type == htons(OFPST_VENDOR)) {
5a020ef3 574 error = ofputil_decode_nxst_request(oh, length, typep);
d1e2cf21
BP
575 }
576 return error;
577}
578
579static int
5a020ef3 580ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
d1e2cf21
BP
581 const struct ofputil_msg_type **typep)
582{
d1e2cf21
BP
583 static const struct ofputil_msg_type ofpst_replies[] = {
584 { OFPUTIL_OFPST_DESC_REPLY,
585 OFPST_DESC, "OFPST_DESC reply",
63f2140a 586 sizeof(struct ofp_desc_stats), 0 },
d1e2cf21
BP
587
588 { OFPUTIL_OFPST_FLOW_REPLY,
589 OFPST_FLOW, "OFPST_FLOW reply",
63f2140a 590 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21
BP
591
592 { OFPUTIL_OFPST_AGGREGATE_REPLY,
593 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
63f2140a 594 sizeof(struct ofp_aggregate_stats_reply), 0 },
d1e2cf21
BP
595
596 { OFPUTIL_OFPST_TABLE_REPLY,
597 OFPST_TABLE, "OFPST_TABLE reply",
63f2140a 598 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
d1e2cf21
BP
599
600 { OFPUTIL_OFPST_PORT_REPLY,
601 OFPST_PORT, "OFPST_PORT reply",
63f2140a 602 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
d1e2cf21
BP
603
604 { OFPUTIL_OFPST_QUEUE_REPLY,
605 OFPST_QUEUE, "OFPST_QUEUE reply",
63f2140a 606 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
d1e2cf21
BP
607
608 { 0,
609 OFPST_VENDOR, "OFPST_VENDOR reply",
63f2140a 610 sizeof(struct ofp_vendor_stats_msg), 1 },
d1e2cf21
BP
611 };
612
613 static const struct ofputil_msg_category ofpst_reply_category = {
614 "OpenFlow statistics",
615 ofpst_replies, ARRAY_SIZE(ofpst_replies),
616 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
617 };
618
28c8bad1 619 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
d1e2cf21
BP
620 int error;
621
5a020ef3
BP
622 error = check_stats_msg(oh, length);
623 if (error) {
624 return error;
625 }
626
d1e2cf21 627 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
5a020ef3 628 ntohs(reply->type), typep);
28c8bad1 629 if (!error && reply->type == htons(OFPST_VENDOR)) {
5a020ef3 630 error = ofputil_decode_nxst_reply(oh, length, typep);
d1e2cf21
BP
631 }
632 return error;
633}
634
5a020ef3
BP
635static int
636ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
637 const struct ofputil_msg_type **typep)
d1e2cf21
BP
638{
639 static const struct ofputil_msg_type ofpt_messages[] = {
640 { OFPUTIL_OFPT_HELLO,
641 OFPT_HELLO, "OFPT_HELLO",
642 sizeof(struct ofp_hello), 1 },
643
644 { OFPUTIL_OFPT_ERROR,
645 OFPT_ERROR, "OFPT_ERROR",
646 sizeof(struct ofp_error_msg), 1 },
647
648 { OFPUTIL_OFPT_ECHO_REQUEST,
649 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
650 sizeof(struct ofp_header), 1 },
651
652 { OFPUTIL_OFPT_ECHO_REPLY,
653 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
654 sizeof(struct ofp_header), 1 },
655
656 { OFPUTIL_OFPT_FEATURES_REQUEST,
657 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
658 sizeof(struct ofp_header), 0 },
659
660 { OFPUTIL_OFPT_FEATURES_REPLY,
661 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
662 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
663
664 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
665 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
666 sizeof(struct ofp_header), 0 },
667
668 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
669 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
670 sizeof(struct ofp_switch_config), 0 },
671
672 { OFPUTIL_OFPT_SET_CONFIG,
673 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
674 sizeof(struct ofp_switch_config), 0 },
675
676 { OFPUTIL_OFPT_PACKET_IN,
677 OFPT_PACKET_IN, "OFPT_PACKET_IN",
678 offsetof(struct ofp_packet_in, data), 1 },
679
680 { OFPUTIL_OFPT_FLOW_REMOVED,
681 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
682 sizeof(struct ofp_flow_removed), 0 },
683
684 { OFPUTIL_OFPT_PORT_STATUS,
685 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
686 sizeof(struct ofp_port_status), 0 },
687
688 { OFPUTIL_OFPT_PACKET_OUT,
689 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
690 sizeof(struct ofp_packet_out), 1 },
691
692 { OFPUTIL_OFPT_FLOW_MOD,
693 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
694 sizeof(struct ofp_flow_mod), 1 },
695
696 { OFPUTIL_OFPT_PORT_MOD,
697 OFPT_PORT_MOD, "OFPT_PORT_MOD",
698 sizeof(struct ofp_port_mod), 0 },
699
700 { 0,
701 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
28c8bad1 702 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21
BP
703
704 { 0,
705 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
28c8bad1 706 sizeof(struct ofp_stats_msg), 1 },
d1e2cf21
BP
707
708 { OFPUTIL_OFPT_BARRIER_REQUEST,
709 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
710 sizeof(struct ofp_header), 0 },
711
712 { OFPUTIL_OFPT_BARRIER_REPLY,
713 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
714 sizeof(struct ofp_header), 0 },
715
716 { 0,
717 OFPT_VENDOR, "OFPT_VENDOR",
718 sizeof(struct ofp_vendor_header), 1 },
719 };
720
721 static const struct ofputil_msg_category ofpt_category = {
722 "OpenFlow message",
723 ofpt_messages, ARRAY_SIZE(ofpt_messages),
724 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
725 };
726
727 int error;
728
5a020ef3 729 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type, typep);
d1e2cf21
BP
730 if (!error) {
731 switch (oh->type) {
732 case OFPT_VENDOR:
5a020ef3 733 error = ofputil_decode_vendor(oh, length, typep);
d1e2cf21
BP
734 break;
735
736 case OFPT_STATS_REQUEST:
5a020ef3 737 error = ofputil_decode_ofpst_request(oh, length, typep);
d1e2cf21
BP
738 break;
739
740 case OFPT_STATS_REPLY:
5a020ef3 741 error = ofputil_decode_ofpst_reply(oh, length, typep);
d1e2cf21
BP
742
743 default:
744 break;
745 }
746 }
5a020ef3
BP
747 return error;
748}
749
750/* Decodes the message type represented by 'oh'. Returns 0 if successful or
751 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
752 * way, stores in '*typep' a type structure that can be inspected with the
753 * ofputil_msg_type_*() functions.
754 *
755 * oh->length must indicate the correct length of the message (and must be at
756 * least sizeof(struct ofp_header)).
757 *
758 * Success indicates that 'oh' is at least as long as the minimum-length
759 * message of its type. */
760int
761ofputil_decode_msg_type(const struct ofp_header *oh,
762 const struct ofputil_msg_type **typep)
763{
764 size_t length = ntohs(oh->length);
765 int error;
766
767 error = ofputil_decode_msg_type__(oh, length, typep);
768 if (!error) {
769 error = ofputil_check_length(*typep, length);
770 }
d1e2cf21 771 if (error) {
5a020ef3
BP
772 *typep = &ofputil_invalid_type;
773 }
774 return error;
775}
d1e2cf21 776
5a020ef3
BP
777/* Decodes the message type represented by 'oh', of which only the first
778 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
779 * code constructed with ofp_mkerr() on failure. Either way, stores in
780 * '*typep' a type structure that can be inspected with the
781 * ofputil_msg_type_*() functions. */
782int
783ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
784 const struct ofputil_msg_type **typep)
785{
786 int error;
787
788 error = (length >= sizeof *oh
789 ? ofputil_decode_msg_type__(oh, length, typep)
790 : ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN));
791 if (error) {
d1e2cf21
BP
792 *typep = &ofputil_invalid_type;
793 }
794 return error;
795}
796
797/* Returns an OFPUTIL_* message type code for 'type'. */
798enum ofputil_msg_code
799ofputil_msg_type_code(const struct ofputil_msg_type *type)
800{
801 return type->code;
802}
2e4f5fcf 803\f
7fa91113
BP
804/* Flow formats. */
805
806bool
807ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
808{
809 switch (flow_format) {
810 case NXFF_OPENFLOW10:
7fa91113
BP
811 case NXFF_NXM:
812 return true;
813 }
814
815 return false;
816}
817
818const char *
819ofputil_flow_format_to_string(enum nx_flow_format flow_format)
820{
821 switch (flow_format) {
822 case NXFF_OPENFLOW10:
823 return "openflow10";
7fa91113
BP
824 case NXFF_NXM:
825 return "nxm";
826 default:
827 NOT_REACHED();
828 }
829}
830
88ca35ee
BP
831int
832ofputil_flow_format_from_string(const char *s)
833{
834 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
88ca35ee
BP
835 : !strcmp(s, "nxm") ? NXFF_NXM
836 : -1);
837}
838
839static bool
840regs_fully_wildcarded(const struct flow_wildcards *wc)
841{
842 int i;
843
844 for (i = 0; i < FLOW_N_REGS; i++) {
845 if (wc->reg_masks[i] != 0) {
846 return false;
847 }
848 }
849 return true;
850}
851
b78f6b77
BP
852/* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
853 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
854 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
855 * NXFF_OPENFLOW10 for backward compatibility. */
856enum nx_flow_format
857ofputil_min_flow_format(const struct cls_rule *rule)
8368c090
BP
858{
859 const struct flow_wildcards *wc = &rule->wc;
8368c090 860
7257b535 861 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 3);
a877206f 862
8368c090
BP
863 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
864 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
b78f6b77 865 return NXFF_NXM;
8368c090
BP
866 }
867
bad68a99
JP
868 /* Only NXM supports matching ARP hardware addresses. */
869 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
b78f6b77 870 return NXFF_NXM;
bad68a99
JP
871 }
872
d31f1109
JP
873 /* Only NXM supports matching IPv6 traffic. */
874 if (!(wc->wildcards & FWW_DL_TYPE)
875 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
b78f6b77 876 return NXFF_NXM;
d31f1109
JP
877 }
878
8368c090
BP
879 /* Only NXM supports matching registers. */
880 if (!regs_fully_wildcarded(wc)) {
b78f6b77 881 return NXFF_NXM;
8368c090
BP
882 }
883
b78f6b77
BP
884 /* Only NXM supports matching tun_id. */
885 if (wc->tun_id_mask != htonll(0)) {
886 return NXFF_NXM;
8368c090
BP
887 }
888
7257b535
BP
889 /* Only NXM supports matching fragments. */
890 if (wc->tos_frag_mask & FLOW_FRAG_MASK) {
891 return NXFF_NXM;
892 }
893
8368c090 894 /* Other formats can express this rule. */
b78f6b77 895 return NXFF_OPENFLOW10;
88ca35ee
BP
896}
897
898/* Returns an OpenFlow message that can be used to set the flow format to
899 * 'flow_format'. */
900struct ofpbuf *
901ofputil_make_set_flow_format(enum nx_flow_format flow_format)
902{
b78f6b77 903 struct nxt_set_flow_format *sff;
88ca35ee
BP
904 struct ofpbuf *msg;
905
b78f6b77
BP
906 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
907 sff->format = htonl(flow_format);
88ca35ee
BP
908
909 return msg;
910}
911
6c1491fb
BP
912/* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
913 * extension on or off (according to 'flow_mod_table_id'). */
914struct ofpbuf *
915ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
916{
917 struct nxt_flow_mod_table_id *nfmti;
918 struct ofpbuf *msg;
919
920 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
921 nfmti->set = flow_mod_table_id;
922 return msg;
923}
924
7fa91113
BP
925/* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
926 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
927 * code.
928 *
6c1491fb
BP
929 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
930 * enabled, false otherwise.
931 *
2e4f5fcf
BP
932 * Does not validate the flow_mod actions. */
933int
a9a2da38
BP
934ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
935 const struct ofp_header *oh, bool flow_mod_table_id)
2e4f5fcf
BP
936{
937 const struct ofputil_msg_type *type;
6c1491fb 938 uint16_t command;
2e4f5fcf
BP
939 struct ofpbuf b;
940
2013493b 941 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
942
943 ofputil_decode_msg_type(oh, &type);
944 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
945 /* Standard OpenFlow flow_mod. */
2e4f5fcf 946 const struct ofp_flow_mod *ofm;
1c0b7503 947 uint16_t priority;
2e4f5fcf
BP
948 int error;
949
950 /* Dissect the message. */
bbc32a88 951 ofm = ofpbuf_pull(&b, sizeof *ofm);
2e4f5fcf
BP
952 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
953 if (error) {
954 return error;
955 }
956
1c0b7503
BP
957 /* Set priority based on original wildcards. Normally we'd allow
958 * ofputil_cls_rule_from_match() to do this for us, but
b459a924 959 * ofputil_normalize_rule() can put wildcards where the original flow
1c0b7503
BP
960 * didn't have them. */
961 priority = ntohs(ofm->priority);
962 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
963 priority = UINT16_MAX;
964 }
965
b459a924
BP
966 /* Translate the rule. */
967 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
968 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
2e4f5fcf
BP
969
970 /* Translate the message. */
2e4f5fcf 971 fm->cookie = ofm->cookie;
6c1491fb 972 command = ntohs(ofm->command);
2e4f5fcf
BP
973 fm->idle_timeout = ntohs(ofm->idle_timeout);
974 fm->hard_timeout = ntohs(ofm->hard_timeout);
975 fm->buffer_id = ntohl(ofm->buffer_id);
976 fm->out_port = ntohs(ofm->out_port);
977 fm->flags = ntohs(ofm->flags);
978 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
979 /* Nicira extended flow_mod. */
980 const struct nx_flow_mod *nfm;
981 int error;
982
983 /* Dissect the message. */
bbc32a88 984 nfm = ofpbuf_pull(&b, sizeof *nfm);
2e4f5fcf
BP
985 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
986 &fm->cr);
987 if (error) {
988 return error;
989 }
990 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
991 if (error) {
992 return error;
993 }
994
995 /* Translate the message. */
996 fm->cookie = nfm->cookie;
6c1491fb 997 command = ntohs(nfm->command);
2e4f5fcf
BP
998 fm->idle_timeout = ntohs(nfm->idle_timeout);
999 fm->hard_timeout = ntohs(nfm->hard_timeout);
1000 fm->buffer_id = ntohl(nfm->buffer_id);
1001 fm->out_port = ntohs(nfm->out_port);
1002 fm->flags = ntohs(nfm->flags);
1003 } else {
1004 NOT_REACHED();
1005 }
1006
6c1491fb
BP
1007 if (flow_mod_table_id) {
1008 fm->command = command & 0xff;
1009 fm->table_id = command >> 8;
1010 } else {
1011 fm->command = command;
1012 fm->table_id = 0xff;
1013 }
1014
2e4f5fcf
BP
1015 return 0;
1016}
1017
1018/* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
6c1491fb
BP
1019 * 'flow_format' and returns the message.
1020 *
1021 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1022 * enabled, false otherwise. */
2e4f5fcf 1023struct ofpbuf *
a9a2da38 1024ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
6c1491fb
BP
1025 enum nx_flow_format flow_format,
1026 bool flow_mod_table_id)
2e4f5fcf
BP
1027{
1028 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1029 struct ofpbuf *msg;
6c1491fb
BP
1030 uint16_t command;
1031
1032 command = (flow_mod_table_id
1033 ? (fm->command & 0xff) | (fm->table_id << 8)
1034 : fm->command);
2e4f5fcf 1035
b78f6b77 1036 if (flow_format == NXFF_OPENFLOW10) {
2e4f5fcf
BP
1037 struct ofp_flow_mod *ofm;
1038
1039 msg = ofpbuf_new(sizeof *ofm + actions_len);
1040 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
b78f6b77
BP
1041 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1042 ofm->cookie = fm->cookie;
05411977 1043 ofm->command = htons(command);
2e4f5fcf
BP
1044 ofm->idle_timeout = htons(fm->idle_timeout);
1045 ofm->hard_timeout = htons(fm->hard_timeout);
1046 ofm->priority = htons(fm->cr.priority);
1047 ofm->buffer_id = htonl(fm->buffer_id);
1048 ofm->out_port = htons(fm->out_port);
1049 ofm->flags = htons(fm->flags);
1050 } else if (flow_format == NXFF_NXM) {
1051 struct nx_flow_mod *nfm;
1052 int match_len;
1053
1054 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1055 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1056 match_len = nx_put_match(msg, &fm->cr);
1057
1058 nfm = msg->data;
1059 nfm->cookie = fm->cookie;
6c1491fb 1060 nfm->command = htons(command);
2e4f5fcf
BP
1061 nfm->idle_timeout = htons(fm->idle_timeout);
1062 nfm->hard_timeout = htons(fm->hard_timeout);
1063 nfm->priority = htons(fm->cr.priority);
1064 nfm->buffer_id = htonl(fm->buffer_id);
1065 nfm->out_port = htons(fm->out_port);
1066 nfm->flags = htons(fm->flags);
1067 nfm->match_len = htons(match_len);
1068 } else {
1069 NOT_REACHED();
1070 }
1071
1072 ofpbuf_put(msg, fm->actions, actions_len);
1073 update_openflow_length(msg);
1074 return msg;
1075}
1076
1077static int
81d1ea94 1078ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
2e4f5fcf 1079 const struct ofp_header *oh,
2e4f5fcf
BP
1080 bool aggregate)
1081{
63f2140a
BP
1082 const struct ofp_flow_stats_request *ofsr =
1083 (const struct ofp_flow_stats_request *) oh;
2e4f5fcf
BP
1084
1085 fsr->aggregate = aggregate;
b78f6b77 1086 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
2e4f5fcf
BP
1087 fsr->out_port = ntohs(ofsr->out_port);
1088 fsr->table_id = ofsr->table_id;
1089
1090 return 0;
1091}
1092
1093static int
81d1ea94 1094ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2e4f5fcf
BP
1095 const struct ofp_header *oh,
1096 bool aggregate)
1097{
1098 const struct nx_flow_stats_request *nfsr;
1099 struct ofpbuf b;
1100 int error;
1101
2013493b 1102 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf 1103
bbc32a88 1104 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
2e4f5fcf
BP
1105 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1106 if (error) {
1107 return error;
1108 }
1109 if (b.size) {
1110 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1111 }
1112
1113 fsr->aggregate = aggregate;
1114 fsr->out_port = ntohs(nfsr->out_port);
1115 fsr->table_id = nfsr->table_id;
1116
1117 return 0;
1118}
1119
1120/* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
b78f6b77
BP
1121 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1122 * successful, otherwise an OpenFlow error code. */
2e4f5fcf 1123int
81d1ea94 1124ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
b78f6b77 1125 const struct ofp_header *oh)
2e4f5fcf
BP
1126{
1127 const struct ofputil_msg_type *type;
1128 struct ofpbuf b;
1129 int code;
1130
2013493b 1131 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2e4f5fcf
BP
1132
1133 ofputil_decode_msg_type(oh, &type);
1134 code = ofputil_msg_type_code(type);
1135 switch (code) {
1136 case OFPUTIL_OFPST_FLOW_REQUEST:
b78f6b77 1137 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
2e4f5fcf
BP
1138
1139 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
b78f6b77 1140 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
2e4f5fcf
BP
1141
1142 case OFPUTIL_NXST_FLOW_REQUEST:
1143 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1144
1145 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1146 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1147
1148 default:
1149 /* Hey, the caller lied. */
1150 NOT_REACHED();
1151 }
1152}
1153
1154/* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
4ffd1b43 1155 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2e4f5fcf
BP
1156 * 'flow_format', and returns the message. */
1157struct ofpbuf *
81d1ea94 1158ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2e4f5fcf
BP
1159 enum nx_flow_format flow_format)
1160{
1161 struct ofpbuf *msg;
1162
b78f6b77 1163 if (flow_format == NXFF_OPENFLOW10) {
2e4f5fcf
BP
1164 struct ofp_flow_stats_request *ofsr;
1165 int type;
1166
2e4f5fcf 1167 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
63f2140a 1168 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
b78f6b77 1169 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
2e4f5fcf
BP
1170 ofsr->table_id = fsr->table_id;
1171 ofsr->out_port = htons(fsr->out_port);
1172 } else if (flow_format == NXFF_NXM) {
1173 struct nx_flow_stats_request *nfsr;
1174 int match_len;
9fb7fa87 1175 int subtype;
2e4f5fcf 1176
9fb7fa87 1177 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
63f2140a 1178 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
2e4f5fcf
BP
1179 match_len = nx_put_match(msg, &fsr->match);
1180
1181 nfsr = msg->data;
1182 nfsr->out_port = htons(fsr->out_port);
1183 nfsr->match_len = htons(match_len);
1184 nfsr->table_id = fsr->table_id;
1185 } else {
1186 NOT_REACHED();
1187 }
1188
1189 return msg;
1190}
d1e2cf21 1191
4ffd1b43 1192/* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
b78f6b77 1193 * ofputil_flow_stats in 'fs'.
4ffd1b43
BP
1194 *
1195 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1196 * OpenFlow message. Calling this function multiple times for a single 'msg'
1197 * iterates through the replies. The caller must initially leave 'msg''s layer
1198 * pointers null and not modify them between calls.
1199 *
1200 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1201 * otherwise a positive errno value. */
1202int
1203ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
b78f6b77 1204 struct ofpbuf *msg)
4ffd1b43
BP
1205{
1206 const struct ofputil_msg_type *type;
1207 int code;
1208
1209 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1210 code = ofputil_msg_type_code(type);
1211 if (!msg->l2) {
1212 msg->l2 = msg->data;
1213 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
28c8bad1 1214 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
4ffd1b43
BP
1215 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1216 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1217 } else {
1218 NOT_REACHED();
1219 }
1220 }
1221
1222 if (!msg->size) {
1223 return EOF;
1224 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1225 const struct ofp_flow_stats *ofs;
1226 size_t length;
1227
1228 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1229 if (!ofs) {
1230 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1231 "bytes at end", msg->size);
1232 return EINVAL;
1233 }
1234
1235 length = ntohs(ofs->length);
1236 if (length < sizeof *ofs) {
1237 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1238 "length %zu", length);
1239 return EINVAL;
1240 }
1241
1242 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1243 &fs->actions, &fs->n_actions)) {
1244 return EINVAL;
1245 }
1246
1247 fs->cookie = get_32aligned_be64(&ofs->cookie);
1248 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
b78f6b77 1249 &fs->rule);
4ffd1b43
BP
1250 fs->table_id = ofs->table_id;
1251 fs->duration_sec = ntohl(ofs->duration_sec);
1252 fs->duration_nsec = ntohl(ofs->duration_nsec);
1253 fs->idle_timeout = ntohs(ofs->idle_timeout);
1254 fs->hard_timeout = ntohs(ofs->hard_timeout);
1255 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1256 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1257 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1258 const struct nx_flow_stats *nfs;
1259 size_t match_len, length;
1260
1261 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1262 if (!nfs) {
1263 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1264 "bytes at end", msg->size);
1265 return EINVAL;
1266 }
1267
1268 length = ntohs(nfs->length);
1269 match_len = ntohs(nfs->match_len);
1270 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1271 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1272 "claims invalid length %zu", match_len, length);
1273 return EINVAL;
1274 }
1275 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1276 return EINVAL;
1277 }
1278
1279 if (ofputil_pull_actions(msg,
1280 length - sizeof *nfs - ROUND_UP(match_len, 8),
1281 &fs->actions, &fs->n_actions)) {
1282 return EINVAL;
1283 }
1284
1285 fs->cookie = nfs->cookie;
1286 fs->table_id = nfs->table_id;
1287 fs->duration_sec = ntohl(nfs->duration_sec);
1288 fs->duration_nsec = ntohl(nfs->duration_nsec);
1289 fs->idle_timeout = ntohs(nfs->idle_timeout);
1290 fs->hard_timeout = ntohs(nfs->hard_timeout);
1291 fs->packet_count = ntohll(nfs->packet_count);
1292 fs->byte_count = ntohll(nfs->byte_count);
1293 } else {
1294 NOT_REACHED();
1295 }
1296
1297 return 0;
1298}
1299
5e9d0469
BP
1300/* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1301 *
1302 * We use this in situations where OVS internally uses UINT64_MAX to mean
1303 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1304static uint64_t
1305unknown_to_zero(uint64_t count)
1306{
1307 return count != UINT64_MAX ? count : 0;
1308}
1309
349adfb2
BP
1310/* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1311 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1312 * have been initialized with ofputil_start_stats_reply(). */
1313void
1314ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1315 struct list *replies)
1316{
1317 size_t act_len = fs->n_actions * sizeof *fs->actions;
1318 const struct ofp_stats_msg *osm;
1319
1320 osm = ofpbuf_from_list(list_back(replies))->data;
1321 if (osm->type == htons(OFPST_FLOW)) {
1322 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1323 struct ofp_flow_stats *ofs;
1324
1325 ofs = ofputil_append_stats_reply(len, replies);
1326 ofs->length = htons(len);
1327 ofs->table_id = fs->table_id;
1328 ofs->pad = 0;
1329 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1330 ofs->duration_sec = htonl(fs->duration_sec);
1331 ofs->duration_nsec = htonl(fs->duration_nsec);
1332 ofs->priority = htons(fs->rule.priority);
1333 ofs->idle_timeout = htons(fs->idle_timeout);
1334 ofs->hard_timeout = htons(fs->hard_timeout);
1335 memset(ofs->pad2, 0, sizeof ofs->pad2);
1336 put_32aligned_be64(&ofs->cookie, fs->cookie);
5e9d0469
BP
1337 put_32aligned_be64(&ofs->packet_count,
1338 htonll(unknown_to_zero(fs->packet_count)));
1339 put_32aligned_be64(&ofs->byte_count,
1340 htonll(unknown_to_zero(fs->byte_count)));
349adfb2
BP
1341 memcpy(ofs->actions, fs->actions, act_len);
1342 } else if (osm->type == htons(OFPST_VENDOR)) {
1343 struct nx_flow_stats *nfs;
1344 struct ofpbuf *msg;
1345 size_t start_len;
1346
1347 msg = ofputil_reserve_stats_reply(
1348 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1349 start_len = msg->size;
1350
1351 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1352 nfs->table_id = fs->table_id;
1353 nfs->pad = 0;
1354 nfs->duration_sec = htonl(fs->duration_sec);
1355 nfs->duration_nsec = htonl(fs->duration_nsec);
1356 nfs->priority = htons(fs->rule.priority);
1357 nfs->idle_timeout = htons(fs->idle_timeout);
1358 nfs->hard_timeout = htons(fs->hard_timeout);
1359 nfs->match_len = htons(nx_put_match(msg, &fs->rule));
1360 memset(nfs->pad2, 0, sizeof nfs->pad2);
1361 nfs->cookie = fs->cookie;
1362 nfs->packet_count = htonll(fs->packet_count);
1363 nfs->byte_count = htonll(fs->byte_count);
1364 ofpbuf_put(msg, fs->actions, act_len);
1365 nfs->length = htons(msg->size - start_len);
1366 } else {
1367 NOT_REACHED();
1368 }
1369}
1370
76c93b22
BP
1371/* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1372 * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */
1373struct ofpbuf *
1374ofputil_encode_aggregate_stats_reply(
1375 const struct ofputil_aggregate_stats *stats,
1376 const struct ofp_stats_msg *request)
1377{
1378 struct ofpbuf *msg;
1379
1380 if (request->type == htons(OFPST_AGGREGATE)) {
1381 struct ofp_aggregate_stats_reply *asr;
1382
1383 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
5e9d0469
BP
1384 put_32aligned_be64(&asr->packet_count,
1385 htonll(unknown_to_zero(stats->packet_count)));
1386 put_32aligned_be64(&asr->byte_count,
1387 htonll(unknown_to_zero(stats->byte_count)));
76c93b22
BP
1388 asr->flow_count = htonl(stats->flow_count);
1389 } else if (request->type == htons(OFPST_VENDOR)) {
1390 struct nx_aggregate_stats_reply *nasr;
1391
1392 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1393 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1394 nasr->packet_count = htonll(stats->packet_count);
1395 nasr->byte_count = htonll(stats->byte_count);
1396 nasr->flow_count = htonl(stats->flow_count);
1397 } else {
1398 NOT_REACHED();
1399 }
1400
1401 return msg;
1402}
1403
b78f6b77
BP
1404/* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1405 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1406 * an OpenFlow error code. */
9b045a0c
BP
1407int
1408ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
b78f6b77 1409 const struct ofp_header *oh)
9b045a0c
BP
1410{
1411 const struct ofputil_msg_type *type;
1412 enum ofputil_msg_code code;
1413
1414 ofputil_decode_msg_type(oh, &type);
1415 code = ofputil_msg_type_code(type);
1416 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1417 const struct ofp_flow_removed *ofr;
1418
1419 ofr = (const struct ofp_flow_removed *) oh;
1420 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
b78f6b77 1421 &fr->rule);
9b045a0c
BP
1422 fr->cookie = ofr->cookie;
1423 fr->reason = ofr->reason;
1424 fr->duration_sec = ntohl(ofr->duration_sec);
1425 fr->duration_nsec = ntohl(ofr->duration_nsec);
1426 fr->idle_timeout = ntohs(ofr->idle_timeout);
1427 fr->packet_count = ntohll(ofr->packet_count);
1428 fr->byte_count = ntohll(ofr->byte_count);
1429 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1430 struct nx_flow_removed *nfr;
1431 struct ofpbuf b;
1432 int error;
1433
1434 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1435
1436 nfr = ofpbuf_pull(&b, sizeof *nfr);
1437 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1438 &fr->rule);
1439 if (error) {
1440 return error;
1441 }
1442 if (b.size) {
1443 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1444 }
1445
1446 fr->cookie = nfr->cookie;
1447 fr->reason = nfr->reason;
1448 fr->duration_sec = ntohl(nfr->duration_sec);
1449 fr->duration_nsec = ntohl(nfr->duration_nsec);
1450 fr->idle_timeout = ntohs(nfr->idle_timeout);
1451 fr->packet_count = ntohll(nfr->packet_count);
1452 fr->byte_count = ntohll(nfr->byte_count);
1453 } else {
1454 NOT_REACHED();
1455 }
1456
1457 return 0;
1458}
1459
588cd7b5
BP
1460/* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1461 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1462 * message. */
1463struct ofpbuf *
1464ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1465 enum nx_flow_format flow_format)
1466{
1467 struct ofpbuf *msg;
1468
b78f6b77 1469 if (flow_format == NXFF_OPENFLOW10) {
588cd7b5
BP
1470 struct ofp_flow_removed *ofr;
1471
1472 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1473 &msg);
b78f6b77 1474 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
7fb563b9 1475 ofr->cookie = fr->cookie;
588cd7b5
BP
1476 ofr->priority = htons(fr->rule.priority);
1477 ofr->reason = fr->reason;
1478 ofr->duration_sec = htonl(fr->duration_sec);
1479 ofr->duration_nsec = htonl(fr->duration_nsec);
1480 ofr->idle_timeout = htons(fr->idle_timeout);
5e9d0469
BP
1481 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1482 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
588cd7b5
BP
1483 } else if (flow_format == NXFF_NXM) {
1484 struct nx_flow_removed *nfr;
1485 int match_len;
1486
1487 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1488 match_len = nx_put_match(msg, &fr->rule);
1489
1490 nfr = msg->data;
1491 nfr->cookie = fr->cookie;
1492 nfr->priority = htons(fr->rule.priority);
1493 nfr->reason = fr->reason;
1494 nfr->duration_sec = htonl(fr->duration_sec);
1495 nfr->duration_nsec = htonl(fr->duration_nsec);
1496 nfr->idle_timeout = htons(fr->idle_timeout);
1497 nfr->match_len = htons(match_len);
1498 nfr->packet_count = htonll(fr->packet_count);
1499 nfr->byte_count = htonll(fr->byte_count);
1500 } else {
1501 NOT_REACHED();
1502 }
1503
1504 return msg;
1505}
1506
ebb57021
BP
1507/* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1508 * and returns the message.
1509 *
1510 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1511 * returned ofpbuf.
1512 *
1513 * If 'rw_packet' is nonnull, then it must contain the same data as
1514 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1515 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1516 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1517 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1518 * than ofputil_encode_packet_in() because it does not copy the packet
1519 * payload. */
1520struct ofpbuf *
1521ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1522 struct ofpbuf *rw_packet)
1523{
1524 int total_len = pin->packet->size;
62698c77 1525 struct ofp_packet_in opi;
ebb57021
BP
1526
1527 if (rw_packet) {
1528 if (pin->send_len < rw_packet->size) {
1529 rw_packet->size = pin->send_len;
1530 }
1531 } else {
1532 rw_packet = ofpbuf_clone_data_with_headroom(
1533 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1534 offsetof(struct ofp_packet_in, data));
1535 }
1536
1537 /* Add OFPT_PACKET_IN. */
62698c77
BP
1538 memset(&opi, 0, sizeof opi);
1539 opi.header.version = OFP_VERSION;
1540 opi.header.type = OFPT_PACKET_IN;
1541 opi.total_len = htons(total_len);
1542 opi.in_port = htons(pin->in_port);
1543 opi.reason = pin->reason;
1544 opi.buffer_id = htonl(pin->buffer_id);
1545 ofpbuf_push(rw_packet, &opi, offsetof(struct ofp_packet_in, data));
ebb57021
BP
1546 update_openflow_length(rw_packet);
1547
1548 return rw_packet;
1549}
1550
d1e2cf21
BP
1551/* Returns a string representing the message type of 'type'. The string is the
1552 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1553 * messages, the constant is followed by "request" or "reply",
1554 * e.g. "OFPST_AGGREGATE reply". */
1555const char *
1556ofputil_msg_type_name(const struct ofputil_msg_type *type)
1557{
1558 return type->name;
1559}
1560\f
fa37b408
BP
1561/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1562 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1563 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1564 * zeroed.
1565 *
1566 * The caller is responsible for freeing '*bufferp' when it is no longer
1567 * needed.
1568 *
1569 * The OpenFlow header length is initially set to 'openflow_len'; if the
1570 * message is later extended, the length should be updated with
1571 * update_openflow_length() before sending.
1572 *
1573 * Returns the header. */
1574void *
1575make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1576{
1577 *bufferp = ofpbuf_new(openflow_len);
1578 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1579}
1580
0bd0c660
BP
1581/* Similar to make_openflow() but creates a Nicira vendor extension message
1582 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1583void *
1584make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1585{
1586 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1587}
1588
fa37b408
BP
1589/* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1590 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1591 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1592 * zeroed.
1593 *
1594 * The caller is responsible for freeing '*bufferp' when it is no longer
1595 * needed.
1596 *
1597 * The OpenFlow header length is initially set to 'openflow_len'; if the
1598 * message is later extended, the length should be updated with
1599 * update_openflow_length() before sending.
1600 *
1601 * Returns the header. */
1602void *
44381c1b 1603make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
1604 struct ofpbuf **bufferp)
1605{
1606 *bufferp = ofpbuf_new(openflow_len);
1607 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1608}
1609
0bd0c660
BP
1610/* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1611 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1612void *
44381c1b 1613make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
0bd0c660
BP
1614 struct ofpbuf **bufferp)
1615{
dfdfc8d4
BP
1616 *bufferp = ofpbuf_new(openflow_len);
1617 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
0bd0c660
BP
1618}
1619
fa37b408
BP
1620/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1621 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1622 * beyond the header, if any, are zeroed.
1623 *
1624 * The OpenFlow header length is initially set to 'openflow_len'; if the
1625 * message is later extended, the length should be updated with
1626 * update_openflow_length() before sending.
1627 *
1628 * Returns the header. */
1629void *
1630put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1631{
1632 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1633}
1634
1635/* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1636 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1637 * the header, if any, are zeroed.
1638 *
1639 * The OpenFlow header length is initially set to 'openflow_len'; if the
1640 * message is later extended, the length should be updated with
1641 * update_openflow_length() before sending.
1642 *
1643 * Returns the header. */
1644void *
44381c1b 1645put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
fa37b408
BP
1646 struct ofpbuf *buffer)
1647{
1648 struct ofp_header *oh;
1649
1650 assert(openflow_len >= sizeof *oh);
1651 assert(openflow_len <= UINT16_MAX);
1652
1653 oh = ofpbuf_put_uninit(buffer, openflow_len);
1654 oh->version = OFP_VERSION;
1655 oh->type = type;
1656 oh->length = htons(openflow_len);
1657 oh->xid = xid;
1658 memset(oh + 1, 0, openflow_len - sizeof *oh);
1659 return oh;
1660}
1661
dfdfc8d4
BP
1662/* Similar to put_openflow() but append a Nicira vendor extension message with
1663 * the specific 'subtype'. 'subtype' should be in host byte order. */
1664void *
1665put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1666{
1667 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1668}
1669
1670/* Similar to put_openflow_xid() but append a Nicira vendor extension message
1671 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1672void *
1673put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1674 struct ofpbuf *buffer)
1675{
1676 struct nicira_header *nxh;
1677
1678 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1679 nxh->vendor = htonl(NX_VENDOR_ID);
1680 nxh->subtype = htonl(subtype);
1681 return nxh;
1682}
1683
fa37b408
BP
1684/* Updates the 'length' field of the OpenFlow message in 'buffer' to
1685 * 'buffer->size'. */
1686void
d295e8e9 1687update_openflow_length(struct ofpbuf *buffer)
fa37b408
BP
1688{
1689 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
d295e8e9 1690 oh->length = htons(buffer->size);
fa37b408
BP
1691}
1692
63f2140a
BP
1693static void
1694put_stats__(ovs_be32 xid, uint8_t ofp_type,
1695 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
1696 struct ofpbuf *msg)
1697{
1698 if (ofpst_type == htons(OFPST_VENDOR)) {
1699 struct nicira_stats_msg *nsm;
1700
1701 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
1702 nsm->vsm.osm.type = ofpst_type;
1703 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
1704 nsm->subtype = nxst_subtype;
1705 } else {
1706 struct ofp_stats_msg *osm;
1707
1708 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
1709 osm->type = ofpst_type;
1710 }
1711}
1712
1713/* Creates a statistics request message with total length 'openflow_len'
1714 * (including all headers) and the given 'ofpst_type', and stores the buffer
1715 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
1716 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
1717 * subtype (otherwise 'nxst_subtype' is ignored).
1718 *
1719 * Initializes bytes following the headers to all-bits-zero.
1720 *
1721 * Returns the first byte of the new message. */
dfdfc8d4 1722void *
63f2140a
BP
1723ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
1724 uint32_t nxst_subtype, struct ofpbuf **bufferp)
dfdfc8d4 1725{
63f2140a
BP
1726 struct ofpbuf *msg;
1727
1728 msg = *bufferp = ofpbuf_new(openflow_len);
1729 put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
1730 htons(ofpst_type), htonl(nxst_subtype), msg);
1731 ofpbuf_padto(msg, openflow_len);
1732
1733 return msg->data;
1734}
1735
1736static void
1737put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
1738{
1739 assert(request->header.type == OFPT_STATS_REQUEST ||
1740 request->header.type == OFPT_STATS_REPLY);
1741 put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
1742 (request->type != htons(OFPST_VENDOR)
1743 ? htonl(0)
1744 : ((const struct nicira_stats_msg *) request)->subtype),
1745 msg);
1746}
1747
1748/* Creates a statistics reply message with total length 'openflow_len'
1749 * (including all headers) and the same type (either a standard OpenFlow
1750 * statistics type or a Nicira extension type and subtype) as 'request', and
1751 * stores the buffer containing the new message in '*bufferp'.
1752 *
1753 * Initializes bytes following the headers to all-bits-zero.
1754 *
1755 * Returns the first byte of the new message. */
1756void *
1757ofputil_make_stats_reply(size_t openflow_len,
1758 const struct ofp_stats_msg *request,
1759 struct ofpbuf **bufferp)
1760{
1761 struct ofpbuf *msg;
1762
1763 msg = *bufferp = ofpbuf_new(openflow_len);
1764 put_stats_reply__(request, msg);
1765 ofpbuf_padto(msg, openflow_len);
1766
1767 return msg->data;
1768}
1769
1770/* Initializes 'replies' as a list of ofpbufs that will contain a series of
1771 * replies to 'request', which should be an OpenFlow or Nicira extension
1772 * statistics request. Initially 'replies' will have a single reply message
1773 * that has only a header. The functions ofputil_reserve_stats_reply() and
1774 * ofputil_append_stats_reply() may be used to add to the reply. */
1775void
1776ofputil_start_stats_reply(const struct ofp_stats_msg *request,
1777 struct list *replies)
1778{
1779 struct ofpbuf *msg;
1780
1781 msg = ofpbuf_new(1024);
1782 put_stats_reply__(request, msg);
1783
1784 list_init(replies);
1785 list_push_back(replies, &msg->list_node);
1786}
1787
1788/* Prepares to append up to 'len' bytes to the series of statistics replies in
1789 * 'replies', which should have been initialized with
1790 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
1791 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
1792 * do so with e.g. ofpbuf_put_uninit().) */
1793struct ofpbuf *
1794ofputil_reserve_stats_reply(size_t len, struct list *replies)
1795{
1796 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1797 struct ofp_stats_msg *osm = msg->data;
1798
1799 if (msg->size + len <= UINT16_MAX) {
1800 ofpbuf_prealloc_tailroom(msg, len);
1801 } else {
1802 osm->flags |= htons(OFPSF_REPLY_MORE);
1803
1804 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
1805 put_stats_reply__(osm, msg);
1806 list_push_back(replies, &msg->list_node);
1807 }
1808 return msg;
dfdfc8d4
BP
1809}
1810
63f2140a
BP
1811/* Appends 'len' bytes to the series of statistics replies in 'replies', and
1812 * returns the first byte. */
dfdfc8d4 1813void *
63f2140a 1814ofputil_append_stats_reply(size_t len, struct list *replies)
dfdfc8d4 1815{
63f2140a 1816 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
dfdfc8d4
BP
1817}
1818
03cd3493 1819/* Returns the first byte past the ofp_stats_msg header in 'oh'. */
d1e2cf21
BP
1820const void *
1821ofputil_stats_body(const struct ofp_header *oh)
1822{
1823 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
28c8bad1 1824 return (const struct ofp_stats_msg *) oh + 1;
d1e2cf21
BP
1825}
1826
03cd3493 1827/* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
d1e2cf21
BP
1828size_t
1829ofputil_stats_body_len(const struct ofp_header *oh)
1830{
1831 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
28c8bad1 1832 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
d1e2cf21
BP
1833}
1834
03cd3493 1835/* Returns the first byte past the nicira_stats_msg header in 'oh'. */
c6430da5
BP
1836const void *
1837ofputil_nxstats_body(const struct ofp_header *oh)
1838{
1839 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1840 return ((const struct nicira_stats_msg *) oh) + 1;
1841}
1842
03cd3493 1843/* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
c6430da5
BP
1844size_t
1845ofputil_nxstats_body_len(const struct ofp_header *oh)
1846{
1847 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1848 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1849}
1850
fa37b408 1851struct ofpbuf *
daa68e9f
BP
1852make_flow_mod(uint16_t command, const struct cls_rule *rule,
1853 size_t actions_len)
fa37b408
BP
1854{
1855 struct ofp_flow_mod *ofm;
1856 size_t size = sizeof *ofm + actions_len;
1857 struct ofpbuf *out = ofpbuf_new(size);
1858 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1859 ofm->header.version = OFP_VERSION;
1860 ofm->header.type = OFPT_FLOW_MOD;
1861 ofm->header.length = htons(size);
1862 ofm->cookie = 0;
daa68e9f 1863 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
b78f6b77 1864 ofputil_cls_rule_to_match(rule, &ofm->match);
fa37b408
BP
1865 ofm->command = htons(command);
1866 return out;
1867}
1868
1869struct ofpbuf *
daa68e9f 1870make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
fa37b408
BP
1871 uint16_t idle_timeout, size_t actions_len)
1872{
daa68e9f 1873 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
fa37b408
BP
1874 struct ofp_flow_mod *ofm = out->data;
1875 ofm->idle_timeout = htons(idle_timeout);
1876 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1877 ofm->buffer_id = htonl(buffer_id);
1878 return out;
1879}
1880
1881struct ofpbuf *
daa68e9f 1882make_del_flow(const struct cls_rule *rule)
fa37b408 1883{
daa68e9f 1884 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
fa37b408
BP
1885 struct ofp_flow_mod *ofm = out->data;
1886 ofm->out_port = htons(OFPP_NONE);
1887 return out;
1888}
1889
1890struct ofpbuf *
daa68e9f 1891make_add_simple_flow(const struct cls_rule *rule,
fa37b408
BP
1892 uint32_t buffer_id, uint16_t out_port,
1893 uint16_t idle_timeout)
1894{
81f3cad4
BP
1895 if (out_port != OFPP_NONE) {
1896 struct ofp_action_output *oao;
1897 struct ofpbuf *buffer;
1898
daa68e9f 1899 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
93996add 1900 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
81f3cad4
BP
1901 return buffer;
1902 } else {
daa68e9f 1903 return make_add_flow(rule, buffer_id, idle_timeout, 0);
81f3cad4 1904 }
fa37b408
BP
1905}
1906
1907struct ofpbuf *
1908make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1909 const struct ofpbuf *payload, int max_send_len)
1910{
1911 struct ofp_packet_in *opi;
1912 struct ofpbuf *buf;
1913 int send_len;
1914
1915 send_len = MIN(max_send_len, payload->size);
1916 buf = ofpbuf_new(sizeof *opi + send_len);
1917 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1918 OFPT_PACKET_IN, 0, buf);
1919 opi->buffer_id = htonl(buffer_id);
1920 opi->total_len = htons(payload->size);
1921 opi->in_port = htons(in_port);
1922 opi->reason = reason;
1923 ofpbuf_put(buf, payload->data, send_len);
1924 update_openflow_length(buf);
1925
1926 return buf;
1927}
1928
1929struct ofpbuf *
1930make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1931 uint16_t in_port,
1932 const struct ofp_action_header *actions, size_t n_actions)
1933{
1934 size_t actions_len = n_actions * sizeof *actions;
1935 struct ofp_packet_out *opo;
1936 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1937 struct ofpbuf *out = ofpbuf_new(size);
1938
1939 opo = ofpbuf_put_uninit(out, sizeof *opo);
1940 opo->header.version = OFP_VERSION;
1941 opo->header.type = OFPT_PACKET_OUT;
1942 opo->header.length = htons(size);
1943 opo->header.xid = htonl(0);
1944 opo->buffer_id = htonl(buffer_id);
6c24d402 1945 opo->in_port = htons(in_port);
fa37b408
BP
1946 opo->actions_len = htons(actions_len);
1947 ofpbuf_put(out, actions, actions_len);
1948 if (packet) {
1949 ofpbuf_put(out, packet->data, packet->size);
1950 }
1951 return out;
1952}
1953
1954struct ofpbuf *
1955make_unbuffered_packet_out(const struct ofpbuf *packet,
1956 uint16_t in_port, uint16_t out_port)
1957{
1958 struct ofp_action_output action;
1959 action.type = htons(OFPAT_OUTPUT);
1960 action.len = htons(sizeof action);
1961 action.port = htons(out_port);
1962 return make_packet_out(packet, UINT32_MAX, in_port,
1963 (struct ofp_action_header *) &action, 1);
1964}
1965
1966struct ofpbuf *
1967make_buffered_packet_out(uint32_t buffer_id,
1968 uint16_t in_port, uint16_t out_port)
1969{
81f3cad4
BP
1970 if (out_port != OFPP_NONE) {
1971 struct ofp_action_output action;
1972 action.type = htons(OFPAT_OUTPUT);
1973 action.len = htons(sizeof action);
1974 action.port = htons(out_port);
1975 return make_packet_out(NULL, buffer_id, in_port,
1976 (struct ofp_action_header *) &action, 1);
1977 } else {
1978 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1979 }
fa37b408
BP
1980}
1981
1982/* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1983struct ofpbuf *
1984make_echo_request(void)
1985{
1986 struct ofp_header *rq;
1987 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1988 rq = ofpbuf_put_uninit(out, sizeof *rq);
1989 rq->version = OFP_VERSION;
1990 rq->type = OFPT_ECHO_REQUEST;
1991 rq->length = htons(sizeof *rq);
44381c1b 1992 rq->xid = htonl(0);
fa37b408
BP
1993 return out;
1994}
1995
1996/* Creates and returns an OFPT_ECHO_REPLY message matching the
1997 * OFPT_ECHO_REQUEST message in 'rq'. */
1998struct ofpbuf *
1999make_echo_reply(const struct ofp_header *rq)
2000{
2001 size_t size = ntohs(rq->length);
2002 struct ofpbuf *out = ofpbuf_new(size);
2003 struct ofp_header *reply = ofpbuf_put(out, rq, size);
2004 reply->type = OFPT_ECHO_REPLY;
2005 return out;
2006}
2007
7257b535
BP
2008const char *
2009ofputil_frag_handling_to_string(enum ofp_config_flags flags)
2010{
2011 switch (flags & OFPC_FRAG_MASK) {
2012 case OFPC_FRAG_NORMAL: return "normal";
2013 case OFPC_FRAG_DROP: return "drop";
2014 case OFPC_FRAG_REASM: return "reassemble";
2015 case OFPC_FRAG_NX_MATCH: return "nx-match";
2016 }
2017
2018 NOT_REACHED();
2019}
2020
2021bool
2022ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
2023{
2024 if (!strcasecmp(s, "normal")) {
2025 *flags = OFPC_FRAG_NORMAL;
2026 } else if (!strcasecmp(s, "drop")) {
2027 *flags = OFPC_FRAG_DROP;
2028 } else if (!strcasecmp(s, "reassemble")) {
2029 *flags = OFPC_FRAG_REASM;
2030 } else if (!strcasecmp(s, "nx-match")) {
2031 *flags = OFPC_FRAG_NX_MATCH;
2032 } else {
2033 return false;
2034 }
2035 return true;
2036}
2037
c1c9c9c4
BP
2038/* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
2039 * that the switch will never have more than 'max_ports' ports. Returns 0 if
2040 * 'port' is valid, otherwise an ofp_mkerr() return code. */
77410139
EJ
2041int
2042ofputil_check_output_port(uint16_t port, int max_ports)
fa37b408
BP
2043{
2044 switch (port) {
2045 case OFPP_IN_PORT:
2046 case OFPP_TABLE:
2047 case OFPP_NORMAL:
2048 case OFPP_FLOOD:
2049 case OFPP_ALL:
2050 case OFPP_CONTROLLER:
2051 case OFPP_LOCAL:
2052 return 0;
2053
2054 default:
c1c9c9c4 2055 if (port < max_ports) {
fa37b408
BP
2056 return 0;
2057 }
fa37b408
BP
2058 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
2059 }
2060}
2061
39dc9082
BP
2062#define OFPUTIL_NAMED_PORTS \
2063 OFPUTIL_NAMED_PORT(IN_PORT) \
2064 OFPUTIL_NAMED_PORT(TABLE) \
2065 OFPUTIL_NAMED_PORT(NORMAL) \
2066 OFPUTIL_NAMED_PORT(FLOOD) \
2067 OFPUTIL_NAMED_PORT(ALL) \
2068 OFPUTIL_NAMED_PORT(CONTROLLER) \
2069 OFPUTIL_NAMED_PORT(LOCAL) \
2070 OFPUTIL_NAMED_PORT(NONE)
2071
2072/* Checks whether 's' is the string representation of an OpenFlow port number,
2073 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
2074 * number in '*port' and returns true. Otherwise, returns false. */
2075bool
2076ofputil_port_from_string(const char *name, uint16_t *port)
2077{
2078 struct pair {
2079 const char *name;
2080 uint16_t value;
2081 };
2082 static const struct pair pairs[] = {
2083#define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2084 OFPUTIL_NAMED_PORTS
2085#undef OFPUTIL_NAMED_PORT
2086 };
2087 static const int n_pairs = ARRAY_SIZE(pairs);
2088 int i;
2089
2090 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2091 *port = i;
2092 return true;
2093 }
2094
2095 for (i = 0; i < n_pairs; i++) {
2096 if (!strcasecmp(name, pairs[i].name)) {
2097 *port = pairs[i].value;
2098 return true;
2099 }
2100 }
2101 return false;
2102}
2103
2104/* Appends to 's' a string representation of the OpenFlow port number 'port'.
2105 * Most ports' string representation is just the port number, but for special
2106 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
2107void
2108ofputil_format_port(uint16_t port, struct ds *s)
2109{
2110 const char *name;
2111
2112 switch (port) {
2113#define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2114 OFPUTIL_NAMED_PORTS
2115#undef OFPUTIL_NAMED_PORT
2116
2117 default:
2118 ds_put_format(s, "%"PRIu16, port);
2119 return;
2120 }
2121 ds_put_cstr(s, name);
2122}
2123
29901626
BP
2124static int
2125check_resubmit_table(const struct nx_action_resubmit *nar)
2126{
2127 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2128 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2129 }
2130 return 0;
2131}
2132
f694937d
EJ
2133static int
2134check_output_reg(const struct nx_action_output_reg *naor,
2135 const struct flow *flow)
2136{
2137 size_t i;
2138
2139 for (i = 0; i < sizeof naor->zero; i++) {
2140 if (naor->zero[i]) {
2141 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2142 }
2143 }
2144
2145 return nxm_src_check(naor->src, nxm_decode_ofs(naor->ofs_nbits),
2146 nxm_decode_n_bits(naor->ofs_nbits), flow);
2147}
2148
38f2e360
BP
2149int
2150validate_actions(const union ofp_action *actions, size_t n_actions,
2151 const struct flow *flow, int max_ports)
c1c9c9c4 2152{
38f2e360
BP
2153 const union ofp_action *a;
2154 size_t left;
c1c9c9c4 2155
38f2e360
BP
2156 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2157 uint16_t port;
2158 int error;
2159 int code;
c1c9c9c4 2160
38f2e360
BP
2161 code = ofputil_decode_action(a);
2162 if (code < 0) {
2163 char *msg;
c1c9c9c4 2164
38f2e360
BP
2165 error = -code;
2166 msg = ofputil_error_to_string(error);
2167 VLOG_WARN_RL(&bad_ofmsg_rl,
2168 "action decoding error at offset %td (%s)",
2169 (a - actions) * sizeof *a, msg);
2170 free(msg);
fa37b408 2171
38f2e360
BP
2172 return error;
2173 }
fa37b408 2174
38f2e360
BP
2175 error = 0;
2176 switch ((enum ofputil_action_code) code) {
2177 case OFPUTIL_OFPAT_OUTPUT:
77410139
EJ
2178 error = ofputil_check_output_port(ntohs(a->output.port),
2179 max_ports);
38f2e360 2180 break;
e41a9130 2181
38f2e360
BP
2182 case OFPUTIL_OFPAT_SET_VLAN_VID:
2183 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2184 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2185 }
2186 break;
96fc46e8 2187
38f2e360
BP
2188 case OFPUTIL_OFPAT_SET_VLAN_PCP:
2189 if (a->vlan_pcp.vlan_pcp & ~7) {
2190 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2191 }
2192 break;
96fc46e8 2193
38f2e360
BP
2194 case OFPUTIL_OFPAT_ENQUEUE:
2195 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
2196 if (port >= max_ports && port != OFPP_IN_PORT) {
2197 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
2198 }
2199 break;
96fc46e8 2200
38f2e360
BP
2201 case OFPUTIL_NXAST_REG_MOVE:
2202 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2203 flow);
2204 break;
96fc46e8 2205
38f2e360
BP
2206 case OFPUTIL_NXAST_REG_LOAD:
2207 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2208 flow);
2209 break;
b9298d3f 2210
38f2e360 2211 case OFPUTIL_NXAST_MULTIPATH:
43edca57
EJ
2212 error = multipath_check((const struct nx_action_multipath *) a,
2213 flow);
38f2e360
BP
2214 break;
2215
2216 case OFPUTIL_NXAST_AUTOPATH:
43edca57
EJ
2217 error = autopath_check((const struct nx_action_autopath *) a,
2218 flow);
38f2e360
BP
2219 break;
2220
daff3353 2221 case OFPUTIL_NXAST_BUNDLE:
a368bb53 2222 case OFPUTIL_NXAST_BUNDLE_LOAD:
daff3353 2223 error = bundle_check((const struct nx_action_bundle *) a,
a368bb53 2224 max_ports, flow);
daff3353
EJ
2225 break;
2226
f694937d
EJ
2227 case OFPUTIL_NXAST_OUTPUT_REG:
2228 error = check_output_reg((const struct nx_action_output_reg *) a,
2229 flow);
2230 break;
2231
29901626
BP
2232 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2233 error = check_resubmit_table(
2234 (const struct nx_action_resubmit *) a);
2235 break;
2236
75a75043
BP
2237 case OFPUTIL_NXAST_LEARN:
2238 error = learn_check((const struct nx_action_learn *) a, flow);
2239 break;
2240
38f2e360
BP
2241 case OFPUTIL_OFPAT_STRIP_VLAN:
2242 case OFPUTIL_OFPAT_SET_NW_SRC:
2243 case OFPUTIL_OFPAT_SET_NW_DST:
2244 case OFPUTIL_OFPAT_SET_NW_TOS:
2245 case OFPUTIL_OFPAT_SET_TP_SRC:
2246 case OFPUTIL_OFPAT_SET_TP_DST:
2247 case OFPUTIL_OFPAT_SET_DL_SRC:
2248 case OFPUTIL_OFPAT_SET_DL_DST:
2249 case OFPUTIL_NXAST_RESUBMIT:
2250 case OFPUTIL_NXAST_SET_TUNNEL:
2251 case OFPUTIL_NXAST_SET_QUEUE:
2252 case OFPUTIL_NXAST_POP_QUEUE:
2253 case OFPUTIL_NXAST_NOTE:
2254 case OFPUTIL_NXAST_SET_TUNNEL64:
848e8809 2255 case OFPUTIL_NXAST_EXIT:
38f2e360 2256 break;
53ddd40a 2257 }
53ddd40a 2258
3b6a2571 2259 if (error) {
38f2e360
BP
2260 char *msg = ofputil_error_to_string(error);
2261 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
2262 (a - actions) * sizeof *a, msg);
2263 free(msg);
3b6a2571
EJ
2264 return error;
2265 }
fa37b408 2266 }
38f2e360
BP
2267 if (left) {
2268 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
2269 (n_actions - left) * sizeof *a);
2270 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2271 }
2272 return 0;
fa37b408
BP
2273}
2274
51cb6e0c
BP
2275struct ofputil_action {
2276 int code;
38f2e360
BP
2277 unsigned int min_len;
2278 unsigned int max_len;
2279};
27bcf966 2280
51cb6e0c
BP
2281static const struct ofputil_action action_bad_type
2282 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE), 0, UINT_MAX };
2283static const struct ofputil_action action_bad_len
2284 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_LEN), 0, UINT_MAX };
2285static const struct ofputil_action action_bad_vendor
2286 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR), 0, UINT_MAX };
27bcf966 2287
51cb6e0c 2288static const struct ofputil_action *
38f2e360
BP
2289ofputil_decode_ofpat_action(const union ofp_action *a)
2290{
51cb6e0c 2291 enum ofp_action_type type = ntohs(a->type);
fa37b408 2292
51cb6e0c 2293 switch (type) {
e23ae585 2294#define OFPAT_ACTION(ENUM, STRUCT, NAME) \
51cb6e0c
BP
2295 case ENUM: { \
2296 static const struct ofputil_action action = { \
e23ae585
BP
2297 OFPUTIL_##ENUM, \
2298 sizeof(struct STRUCT), \
2299 sizeof(struct STRUCT) \
51cb6e0c
BP
2300 }; \
2301 return &action; \
2302 }
e23ae585 2303#include "ofp-util.def"
51cb6e0c
BP
2304
2305 case OFPAT_VENDOR:
2306 default:
2307 return &action_bad_type;
38f2e360
BP
2308 }
2309}
fa37b408 2310
51cb6e0c 2311static const struct ofputil_action *
38f2e360
BP
2312ofputil_decode_nxast_action(const union ofp_action *a)
2313{
51cb6e0c
BP
2314 const struct nx_action_header *nah = (const struct nx_action_header *) a;
2315 enum nx_action_subtype subtype = ntohs(nah->subtype);
2316
2317 switch (subtype) {
e23ae585
BP
2318#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2319 case ENUM: { \
2320 static const struct ofputil_action action = { \
2321 OFPUTIL_##ENUM, \
2322 sizeof(struct STRUCT), \
2323 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
2324 }; \
2325 return &action; \
38f2e360 2326 }
e23ae585 2327#include "ofp-util.def"
51cb6e0c
BP
2328
2329 case NXAST_SNAT__OBSOLETE:
2330 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
2331 default:
2332 return &action_bad_type;
fa37b408
BP
2333 }
2334}
2335
38f2e360
BP
2336/* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or
2337 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OpenFlow error
2338 * code (as returned by ofp_mkerr()).
2339 *
2340 * The caller must have already verified that 'a''s length is correct (that is,
2341 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
2342 * longer than the amount of space allocated to 'a').
2343 *
2344 * This function verifies that 'a''s length is correct for the type of action
2345 * that it represents. */
fa37b408 2346int
38f2e360 2347ofputil_decode_action(const union ofp_action *a)
fa37b408 2348{
51cb6e0c
BP
2349 const struct ofputil_action *action;
2350 uint16_t len = ntohs(a->header.len);
2351
38f2e360 2352 if (a->type != htons(OFPAT_VENDOR)) {
51cb6e0c 2353 action = ofputil_decode_ofpat_action(a);
38f2e360 2354 } else {
51cb6e0c
BP
2355 switch (ntohl(a->vendor.vendor)) {
2356 case NX_VENDOR_ID:
2357 if (len < sizeof(struct nx_action_header)) {
2358 return -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2359 }
2360 action = ofputil_decode_nxast_action(a);
2361 break;
2362 default:
2363 action = &action_bad_vendor;
2364 break;
2365 }
38f2e360 2366 }
51cb6e0c
BP
2367
2368 return (len >= action->min_len && len <= action->max_len
2369 ? action->code
2370 : -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN));
38f2e360 2371}
fa37b408 2372
38f2e360
BP
2373/* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_*
2374 * constant. The caller must have already validated that 'a' is a valid action
2375 * understood by Open vSwitch (e.g. by a previous successful call to
2376 * ofputil_decode_action()). */
2377enum ofputil_action_code
2378ofputil_decode_action_unsafe(const union ofp_action *a)
2379{
51cb6e0c
BP
2380 const struct ofputil_action *action;
2381
38f2e360 2382 if (a->type != htons(OFPAT_VENDOR)) {
51cb6e0c 2383 action = ofputil_decode_ofpat_action(a);
38f2e360 2384 } else {
51cb6e0c 2385 action = ofputil_decode_nxast_action(a);
fa37b408 2386 }
51cb6e0c
BP
2387
2388 return action->code;
fa37b408
BP
2389}
2390
e23ae585
BP
2391/* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
2392 * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if
2393 * 'name' is not the name of any action.
2394 *
2395 * ofp-util.def lists the mapping from names to action. */
2396int
2397ofputil_action_code_from_name(const char *name)
2398{
2399 static const char *names[OFPUTIL_N_ACTIONS] = {
2400#define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME,
2401#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
2402#include "ofp-util.def"
2403 };
2404
2405 const char **p;
2406
2407 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
2408 if (*p && !strcasecmp(name, *p)) {
2409 return p - names;
2410 }
2411 }
2412 return -1;
2413}
2414
93996add
BP
2415/* Appends an action of the type specified by 'code' to 'buf' and returns the
2416 * action. Initializes the parts of 'action' that identify it as having type
2417 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
2418 * have variable length, the length used and cleared is that of struct
2419 * <STRUCT>. */
2420void *
2421ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
2422{
2423 switch (code) {
2424#define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2425 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2426#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2427 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2428#include "ofp-util.def"
2429 }
2430 NOT_REACHED();
2431}
2432
2433#define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2434 void \
2435 ofputil_init_##ENUM(struct STRUCT *s) \
2436 { \
2437 memset(s, 0, sizeof *s); \
2438 s->type = htons(ENUM); \
2439 s->len = htons(sizeof *s); \
2440 } \
2441 \
2442 struct STRUCT * \
2443 ofputil_put_##ENUM(struct ofpbuf *buf) \
2444 { \
2445 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2446 ofputil_init_##ENUM(s); \
2447 return s; \
2448 }
2449#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2450 void \
2451 ofputil_init_##ENUM(struct STRUCT *s) \
2452 { \
2453 memset(s, 0, sizeof *s); \
2454 s->type = htons(OFPAT_VENDOR); \
2455 s->len = htons(sizeof *s); \
2456 s->vendor = htonl(NX_VENDOR_ID); \
2457 s->subtype = htons(ENUM); \
2458 } \
2459 \
2460 struct STRUCT * \
2461 ofputil_put_##ENUM(struct ofpbuf *buf) \
2462 { \
2463 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2464 ofputil_init_##ENUM(s); \
2465 return s; \
2466 }
2467#include "ofp-util.def"
2468
dbba996b 2469/* Returns true if 'action' outputs to 'port', false otherwise. */
c1c9c9c4 2470bool
dbba996b 2471action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
c1c9c9c4
BP
2472{
2473 switch (ntohs(action->type)) {
2474 case OFPAT_OUTPUT:
2475 return action->output.port == port;
2476 case OFPAT_ENQUEUE:
2477 return ((const struct ofp_action_enqueue *) action)->port == port;
2478 default:
2479 return false;
2480 }
2481}
2482
b459a924
BP
2483/* "Normalizes" the wildcards in 'rule'. That means:
2484 *
2485 * 1. If the type of level N is known, then only the valid fields for that
2486 * level may be specified. For example, ARP does not have a TOS field,
2487 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2488 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2489 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2490 * IPv4 flow.
2491 *
2492 * 2. If the type of level N is not known (or not understood by Open
2493 * vSwitch), then no fields at all for that level may be specified. For
2494 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2495 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2496 * SCTP flow.
2497 *
2498 * 'flow_format' specifies the format of the flow as received or as intended to
2499 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2500 * detailed matching. */
2501void
2502ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2503{
2504 enum {
2505 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2506 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2507 MAY_NW_PROTO = 1 << 2, /* nw_proto */
7257b535 2508 MAY_TOS_FRAG = 1 << 3, /* tos_frag */
b459a924
BP
2509 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2510 MAY_ARP_THA = 1 << 5, /* arp_tha */
2511 MAY_IPV6_ADDR = 1 << 6, /* ipv6_src, ipv6_dst */
2512 MAY_ND_TARGET = 1 << 7 /* nd_target */
2513 } may_match;
2514
2515 struct flow_wildcards wc;
2516
2517 /* Figure out what fields may be matched. */
2518 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
7257b535 2519 may_match = MAY_NW_PROTO | MAY_TOS_FRAG | MAY_NW_ADDR;
b459a924
BP
2520 if (rule->flow.nw_proto == IPPROTO_TCP ||
2521 rule->flow.nw_proto == IPPROTO_UDP ||
2522 rule->flow.nw_proto == IPPROTO_ICMP) {
2523 may_match |= MAY_TP_ADDR;
2524 }
2525 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2526 && flow_format == NXFF_NXM) {
7257b535 2527 may_match = MAY_NW_PROTO | MAY_TOS_FRAG | MAY_IPV6_ADDR;
b459a924
BP
2528 if (rule->flow.nw_proto == IPPROTO_TCP ||
2529 rule->flow.nw_proto == IPPROTO_UDP) {
2530 may_match |= MAY_TP_ADDR;
2531 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2532 may_match |= MAY_TP_ADDR;
2533 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2534 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2535 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2536 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2537 }
2538 }
2539 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2540 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2541 if (flow_format == NXFF_NXM) {
2542 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
fa37b408 2543 }
1c0b7503 2544 } else {
b459a924
BP
2545 may_match = 0;
2546 }
2547
2548 /* Clear the fields that may not be matched. */
2549 wc = rule->wc;
2550 if (!(may_match & MAY_NW_ADDR)) {
2551 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2552 }
2553 if (!(may_match & MAY_TP_ADDR)) {
2554 wc.wildcards |= FWW_TP_SRC | FWW_TP_DST;
2555 }
2556 if (!(may_match & MAY_NW_PROTO)) {
2557 wc.wildcards |= FWW_NW_PROTO;
2558 }
7257b535
BP
2559 if (!(may_match & MAY_TOS_FRAG)) {
2560 wc.tos_frag_mask = 0;
b459a924
BP
2561 }
2562 if (!(may_match & MAY_ARP_SHA)) {
2563 wc.wildcards |= FWW_ARP_SHA;
2564 }
2565 if (!(may_match & MAY_ARP_THA)) {
2566 wc.wildcards |= FWW_ARP_THA;
2567 }
2568 if (!(may_match & MAY_IPV6_ADDR)) {
2569 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2570 }
2571 if (!(may_match & MAY_ND_TARGET)) {
2572 wc.wildcards |= FWW_ND_TARGET;
2573 }
2574
2575 /* Log any changes. */
2576 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2577 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2578 char *pre = log ? cls_rule_to_string(rule) : NULL;
2579
2580 rule->wc = wc;
2581 cls_rule_zero_wildcarded_fields(rule);
2582
2583 if (log) {
2584 char *post = cls_rule_to_string(rule);
2585 VLOG_INFO("normalization changed ofp_match, details:");
2586 VLOG_INFO(" pre: %s", pre);
2587 VLOG_INFO("post: %s", post);
2588 free(pre);
2589 free(post);
2590 }
fa37b408 2591 }
3f09c339 2592}
26c112c2
BP
2593
2594static uint32_t
2595vendor_code_to_id(uint8_t code)
2596{
2597 switch (code) {
2598#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
3a75cda9
BP
2599 OFPUTIL_VENDORS
2600#undef OFPUTIL_VENDOR
26c112c2
BP
2601 default:
2602 return UINT32_MAX;
2603 }
2604}
2605
dc4762ed
BP
2606static int
2607vendor_id_to_code(uint32_t id)
2608{
2609 switch (id) {
2610#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2611 OFPUTIL_VENDORS
2612#undef OFPUTIL_VENDOR
2613 default:
2614 return -1;
2615 }
2616}
2617
26c112c2
BP
2618/* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2619 * information taken from 'error', whose encoding must be as described in the
2620 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2621 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2622 * of 'oh'.
2623 *
2624 * Returns NULL if 'error' is not an OpenFlow error code. */
2625struct ofpbuf *
dc4762ed 2626ofputil_encode_error_msg(int error, const struct ofp_header *oh)
26c112c2
BP
2627{
2628 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2629
2630 struct ofpbuf *buf;
2631 const void *data;
2632 size_t len;
2633 uint8_t vendor;
2634 uint16_t type;
2635 uint16_t code;
44381c1b 2636 ovs_be32 xid;
26c112c2
BP
2637
2638 if (!is_ofp_error(error)) {
2639 /* We format 'error' with strerror() here since it seems likely to be
2640 * a system errno value. */
2641 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2642 error, strerror(error));
2643 return NULL;
2644 }
2645
2646 if (oh) {
2647 xid = oh->xid;
2648 data = oh;
2649 len = ntohs(oh->length);
2650 if (len > 64) {
2651 len = 64;
2652 }
2653 } else {
2654 xid = 0;
2655 data = NULL;
2656 len = 0;
2657 }
2658
2659 vendor = get_ofp_err_vendor(error);
2660 type = get_ofp_err_type(error);
2661 code = get_ofp_err_code(error);
2662 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2663 struct ofp_error_msg *oem;
2664
2665 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2666 oem->type = htons(type);
2667 oem->code = htons(code);
2668 } else {
2669 struct ofp_error_msg *oem;
217f48c6 2670 struct nx_vendor_error *nve;
26c112c2
BP
2671 uint32_t vendor_id;
2672
2673 vendor_id = vendor_code_to_id(vendor);
2674 if (vendor_id == UINT32_MAX) {
2675 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2676 error, vendor);
2677 return NULL;
2678 }
2679
217f48c6 2680 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
26c112c2
BP
2681 OFPT_ERROR, xid, &buf);
2682 oem->type = htons(NXET_VENDOR);
2683 oem->code = htons(NXVC_VENDOR_ERROR);
2684
17764fb2 2685 nve = (struct nx_vendor_error *)oem->data;
217f48c6
BP
2686 nve->vendor = htonl(vendor_id);
2687 nve->type = htons(type);
2688 nve->code = htons(code);
26c112c2
BP
2689 }
2690
2691 if (len) {
59edb09c 2692 buf->size -= len;
26c112c2
BP
2693 ofpbuf_put(buf, data, len);
2694 }
2695
2696 return buf;
2697}
3052b0c5 2698
dc4762ed
BP
2699/* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2700 * Open vSwitch internal error code in the format described in the large
2701 * comment in ofp-util.h.
2702 *
2703 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2704 * to the payload starting from 'oh' and on failure it is set to 0. */
2705int
2706ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2707{
2708 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2709
2710 const struct ofp_error_msg *oem;
2711 uint16_t type, code;
2712 struct ofpbuf b;
2713 int vendor;
2714
2715 if (payload_ofs) {
2716 *payload_ofs = 0;
2717 }
2718 if (oh->type != OFPT_ERROR) {
2719 return EPROTO;
2720 }
2721
2722 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2723 oem = ofpbuf_try_pull(&b, sizeof *oem);
2724 if (!oem) {
2725 return EPROTO;
2726 }
2727
2728 type = ntohs(oem->type);
2729 code = ntohs(oem->code);
2730 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2731 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2732 if (!nve) {
2733 return EPROTO;
2734 }
2735
2736 vendor = vendor_id_to_code(ntohl(nve->vendor));
2737 if (vendor < 0) {
2738 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2739 ntohl(nve->vendor));
2740 return EPROTO;
2741 }
2742 type = ntohs(nve->type);
2743 code = ntohs(nve->code);
2744 } else {
2745 vendor = OFPUTIL_VENDOR_OPENFLOW;
2746 }
2747
2748 if (type >= 1024) {
2749 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2750 "supported maximum value 1023", type);
2751 return EPROTO;
2752 }
2753
2754 if (payload_ofs) {
2755 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2756 }
2757 return ofp_mkerr_vendor(vendor, type, code);
2758}
2759
2760void
2761ofputil_format_error(struct ds *s, int error)
2762{
2763 if (is_errno(error)) {
2764 ds_put_cstr(s, strerror(error));
2765 } else {
2766 uint16_t type = get_ofp_err_type(error);
2767 uint16_t code = get_ofp_err_code(error);
2768 const char *type_s = ofp_error_type_to_string(type);
2769 const char *code_s = ofp_error_code_to_string(type, code);
2770
2771 ds_put_format(s, "type ");
2772 if (type_s) {
2773 ds_put_cstr(s, type_s);
2774 } else {
2775 ds_put_format(s, "%"PRIu16, type);
2776 }
2777
2778 ds_put_cstr(s, ", code ");
2779 if (code_s) {
2780 ds_put_cstr(s, code_s);
2781 } else {
2782 ds_put_format(s, "%"PRIu16, code);
2783 }
2784 }
2785}
2786
2787char *
2788ofputil_error_to_string(int error)
2789{
2790 struct ds s = DS_EMPTY_INITIALIZER;
2791 ofputil_format_error(&s, error);
2792 return ds_steal_cstr(&s);
2793}
2794
3052b0c5
BP
2795/* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2796 * successful, otherwise an OpenFlow error.
2797 *
2798 * If successful, the first action is stored in '*actionsp' and the number of
2799 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2800 * are stored, respectively.
2801 *
2802 * This function does not check that the actions are valid (the caller should
2803 * do so, with validate_actions()). The caller is also responsible for making
2804 * sure that 'b->data' is initially aligned appropriately for "union
2805 * ofp_action". */
2806int
2807ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2808 union ofp_action **actionsp, size_t *n_actionsp)
2809{
69b6be19 2810 if (actions_len % OFP_ACTION_ALIGN != 0) {
f4350529
BP
2811 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2812 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
3052b0c5
BP
2813 goto error;
2814 }
2815
2816 *actionsp = ofpbuf_try_pull(b, actions_len);
2817 if (*actionsp == NULL) {
f4350529
BP
2818 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2819 "exceeds remaining message length (%zu)",
2820 actions_len, b->size);
3052b0c5
BP
2821 goto error;
2822 }
2823
69b6be19 2824 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
3052b0c5
BP
2825 return 0;
2826
2827error:
2828 *actionsp = NULL;
2829 *n_actionsp = 0;
2830 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2831}
18ddadc2
BP
2832
2833bool
2834ofputil_actions_equal(const union ofp_action *a, size_t n_a,
2835 const union ofp_action *b, size_t n_b)
2836{
2837 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
2838}
2839
2840union ofp_action *
2841ofputil_actions_clone(const union ofp_action *actions, size_t n)
2842{
2843 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
2844}
0ff22822
BP
2845
2846/* Parses a key or a key-value pair from '*stringp'.
2847 *
2848 * On success: Stores the key into '*keyp'. Stores the value, if present, into
2849 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
2850 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
2851 * are substrings of '*stringp' created by replacing some of its bytes by null
2852 * terminators. Returns true.
2853 *
2854 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
2855 * NULL and returns false. */
2856bool
2857ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
2858{
2859 char *pos, *key, *value;
2860 size_t key_len;
2861
2862 pos = *stringp;
2863 pos += strspn(pos, ", \t\r\n");
2864 if (*pos == '\0') {
2865 *keyp = *valuep = NULL;
2866 return false;
2867 }
2868
2869 key = pos;
2870 key_len = strcspn(pos, ":=(, \t\r\n");
2871 if (key[key_len] == ':' || key[key_len] == '=') {
2872 /* The value can be separated by a colon. */
2873 size_t value_len;
2874
2875 value = key + key_len + 1;
2876 value_len = strcspn(value, ", \t\r\n");
2877 pos = value + value_len + (value[value_len] != '\0');
2878 value[value_len] = '\0';
2879 } else if (key[key_len] == '(') {
2880 /* The value can be surrounded by balanced parentheses. The outermost
2881 * set of parentheses is removed. */
2882 int level = 1;
2883 size_t value_len;
2884
2885 value = key + key_len + 1;
2886 for (value_len = 0; level > 0; value_len++) {
2887 switch (value[value_len]) {
2888 case '\0':
2889 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
2890
2891 case '(':
2892 level++;
2893 break;
2894
2895 case ')':
2896 level--;
2897 break;
2898 }
2899 }
2900 value[value_len - 1] = '\0';
2901 pos = value + value_len;
2902 } else {
2903 /* There might be no value at all. */
2904 value = key + key_len; /* Will become the empty string below. */
2905 pos = key + key_len + (key[key_len] != '\0');
2906 }
2907 key[key_len] = '\0';
2908
2909 *stringp = pos;
2910 *keyp = key;
2911 *valuep = value;
2912 return true;
2913}