2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofp-print.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "multipath.h"
33 #include "ofp-errors.h"
38 #include "unaligned.h"
39 #include "type-props.h"
42 VLOG_DEFINE_THIS_MODULE(ofp_util);
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. */
46 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
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
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
58 ofputil_wcbits_to_netmask(int wcbits)
61 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
64 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
65 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
66 * must be a CIDR netmask (see ip_is_cidr()). */
68 ofputil_netmask_to_wcbits(ovs_be32 netmask)
70 return 32 - ip_count_cidr_bits(netmask);
73 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
75 #define WC_INVARIANT_LIST \
76 WC_INVARIANT_BIT(IN_PORT) \
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)
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);
88 #undef WC_INVARIANT_BIT
90 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
92 static const flow_wildcards_t WC_INVARIANTS = 0
93 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
95 #undef WC_INVARIANT_BIT
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. */
102 ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
104 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
106 /* Initialize most of rule->wc. */
107 flow_wildcards_init_catchall(wc);
108 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
110 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
111 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL
112 | FWW_ND_TARGET | FWW_IPV6_LABEL);
114 if (ofpfw & OFPFW_NW_TOS) {
115 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
116 * the enum than we can use. */
117 wc->wildcards |= FWW_NW_DSCP;
120 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
121 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
123 if (ofpfw & OFPFW_DL_DST) {
124 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
125 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
126 * and FWW_ETH_MCAST. */
127 wc->wildcards |= FWW_ETH_MCAST;
131 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
132 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
134 if (!(ofpfw & OFPFW_DL_VLAN)) {
135 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
139 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
142 ofputil_cls_rule_from_match(const struct ofp_match *match,
143 unsigned int priority, struct cls_rule *rule)
145 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
147 /* Initialize rule->priority, rule->wc. */
148 rule->priority = !ofpfw ? UINT16_MAX : priority;
149 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
151 /* Initialize most of rule->flow. */
152 rule->flow.nw_src = match->nw_src;
153 rule->flow.nw_dst = match->nw_dst;
154 rule->flow.in_port = ntohs(match->in_port);
155 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
156 rule->flow.tp_src = match->tp_src;
157 rule->flow.tp_dst = match->tp_dst;
158 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
159 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
160 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
161 rule->flow.nw_proto = match->nw_proto;
163 /* Translate VLANs. */
164 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
165 /* Match only packets without 802.1Q header.
167 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
169 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
170 * because we can't have a specific PCP without an 802.1Q header.
171 * However, older versions of OVS treated this as matching packets
172 * withut an 802.1Q header, so we do here too. */
173 rule->flow.vlan_tci = htons(0);
174 rule->wc.vlan_tci_mask = htons(0xffff);
176 ovs_be16 vid, pcp, tci;
178 vid = match->dl_vlan & htons(VLAN_VID_MASK);
179 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
180 tci = vid | pcp | htons(VLAN_CFI);
181 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
185 cls_rule_zero_wildcarded_fields(rule);
188 /* Convert 'rule' into the OpenFlow match structure 'match'. */
190 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
192 const struct flow_wildcards *wc = &rule->wc;
195 /* Figure out most OpenFlow wildcards. */
196 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
197 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
198 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
199 if (wc->wildcards & FWW_NW_DSCP) {
200 ofpfw |= OFPFW_NW_TOS;
203 /* Translate VLANs. */
204 match->dl_vlan = htons(0);
205 match->dl_vlan_pcp = 0;
206 if (rule->wc.vlan_tci_mask == htons(0)) {
207 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
208 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
209 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
210 match->dl_vlan = htons(OFP_VLAN_NONE);
212 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
213 ofpfw |= OFPFW_DL_VLAN;
215 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
218 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
219 ofpfw |= OFPFW_DL_VLAN_PCP;
221 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
225 /* Compose most of the match structure. */
226 match->wildcards = htonl(ofpfw);
227 match->in_port = htons(rule->flow.in_port);
228 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
229 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
230 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
231 match->nw_src = rule->flow.nw_src;
232 match->nw_dst = rule->flow.nw_dst;
233 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
234 match->nw_proto = rule->flow.nw_proto;
235 match->tp_src = rule->flow.tp_src;
236 match->tp_dst = rule->flow.tp_dst;
237 memset(match->pad1, '\0', sizeof match->pad1);
238 memset(match->pad2, '\0', sizeof match->pad2);
241 /* Given a 'dl_type' value in the format used in struct flow, returns the
242 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
244 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
246 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
247 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
251 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
252 * structure, returns the corresponding 'dl_type' value for use in struct
255 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
257 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
258 ? htons(FLOW_DL_TYPE_NONE)
262 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
266 static uint32_t next_xid = 1;
267 return htonl(next_xid++);
270 /* Basic parsing of OpenFlow messages. */
272 struct ofputil_msg_type {
273 enum ofputil_msg_code code; /* OFPUTIL_*. */
274 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
275 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
276 unsigned int min_size; /* Minimum total message size in bytes. */
277 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
278 * the message may exceed 'min_size' by an even multiple of this value. */
279 unsigned int extra_multiple;
282 /* Represents a malformed OpenFlow message. */
283 static const struct ofputil_msg_type ofputil_invalid_type = {
284 OFPUTIL_MSG_INVALID, 0, "OFPUTIL_MSG_INVALID", 0, 0
287 struct ofputil_msg_category {
288 const char *name; /* e.g. "OpenFlow message" */
289 const struct ofputil_msg_type *types;
291 int missing_error; /* ofp_mkerr() value for missing type. */
295 ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
297 switch (type->extra_multiple) {
299 if (size != type->min_size) {
300 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
301 "length %u (expected length %u)",
302 type->name, size, type->min_size);
303 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
308 if (size < type->min_size) {
309 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
310 "length %u (expected length at least %u bytes)",
311 type->name, size, type->min_size);
312 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
317 if (size < type->min_size
318 || (size - type->min_size) % type->extra_multiple) {
319 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
320 "length %u (must be exactly %u bytes or longer "
321 "by an integer multiple of %u bytes)",
323 type->min_size, type->extra_multiple);
324 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
331 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
333 const struct ofputil_msg_type **typep)
335 const struct ofputil_msg_type *type;
337 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
338 if (type->value == value) {
344 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
346 return cat->missing_error;
350 ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
351 const struct ofputil_msg_type **typep)
353 static const struct ofputil_msg_type nxt_messages[] = {
354 { OFPUTIL_NXT_ROLE_REQUEST,
355 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
356 sizeof(struct nx_role_request), 0 },
358 { OFPUTIL_NXT_ROLE_REPLY,
359 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
360 sizeof(struct nx_role_request), 0 },
362 { OFPUTIL_NXT_SET_FLOW_FORMAT,
363 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
364 sizeof(struct nxt_set_flow_format), 0 },
366 { OFPUTIL_NXT_FLOW_MOD,
367 NXT_FLOW_MOD, "NXT_FLOW_MOD",
368 sizeof(struct nx_flow_mod), 8 },
370 { OFPUTIL_NXT_FLOW_REMOVED,
371 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
372 sizeof(struct nx_flow_removed), 8 },
374 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID,
375 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
376 sizeof(struct nxt_flow_mod_table_id), 0 },
379 static const struct ofputil_msg_category nxt_category = {
380 "Nicira extension message",
381 nxt_messages, ARRAY_SIZE(nxt_messages),
382 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
385 const struct ofp_vendor_header *ovh;
386 const struct nicira_header *nh;
388 if (length < sizeof(struct ofp_vendor_header)) {
389 if (length == ntohs(oh->length)) {
390 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
392 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
395 ovh = (const struct ofp_vendor_header *) oh;
396 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
397 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
398 "vendor %"PRIx32, ntohl(ovh->vendor));
399 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
402 if (length < sizeof(struct nicira_header)) {
403 if (length == ntohs(oh->length)) {
404 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
405 "length %u (expected at least %zu)",
406 ntohs(ovh->header.length),
407 sizeof(struct nicira_header));
409 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
412 nh = (const struct nicira_header *) oh;
413 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
418 check_nxstats_msg(const struct ofp_header *oh, size_t length)
420 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
423 if (length < sizeof(struct ofp_vendor_stats_msg)) {
424 if (length == ntohs(oh->length)) {
425 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
427 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
430 memcpy(&vendor, osm + 1, sizeof vendor);
431 if (vendor != htonl(NX_VENDOR_ID)) {
432 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
433 "unknown vendor %"PRIx32, ntohl(vendor));
434 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
437 if (length < sizeof(struct nicira_stats_msg)) {
438 if (length == ntohs(osm->header.length)) {
439 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
441 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
448 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
449 const struct ofputil_msg_type **typep)
451 static const struct ofputil_msg_type nxst_requests[] = {
452 { OFPUTIL_NXST_FLOW_REQUEST,
453 NXST_FLOW, "NXST_FLOW request",
454 sizeof(struct nx_flow_stats_request), 8 },
456 { OFPUTIL_NXST_AGGREGATE_REQUEST,
457 NXST_AGGREGATE, "NXST_AGGREGATE request",
458 sizeof(struct nx_aggregate_stats_request), 8 },
461 static const struct ofputil_msg_category nxst_request_category = {
462 "Nicira extension statistics request",
463 nxst_requests, ARRAY_SIZE(nxst_requests),
464 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
467 const struct nicira_stats_msg *nsm;
470 error = check_nxstats_msg(oh, length);
475 nsm = (struct nicira_stats_msg *) oh;
476 return ofputil_lookup_openflow_message(&nxst_request_category,
477 ntohl(nsm->subtype), typep);
481 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
482 const struct ofputil_msg_type **typep)
484 static const struct ofputil_msg_type nxst_replies[] = {
485 { OFPUTIL_NXST_FLOW_REPLY,
486 NXST_FLOW, "NXST_FLOW reply",
487 sizeof(struct nicira_stats_msg), 8 },
489 { OFPUTIL_NXST_AGGREGATE_REPLY,
490 NXST_AGGREGATE, "NXST_AGGREGATE reply",
491 sizeof(struct nx_aggregate_stats_reply), 0 },
494 static const struct ofputil_msg_category nxst_reply_category = {
495 "Nicira extension statistics reply",
496 nxst_replies, ARRAY_SIZE(nxst_replies),
497 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
500 const struct nicira_stats_msg *nsm;
503 error = check_nxstats_msg(oh, length);
508 nsm = (struct nicira_stats_msg *) oh;
509 return ofputil_lookup_openflow_message(&nxst_reply_category,
510 ntohl(nsm->subtype), typep);
514 check_stats_msg(const struct ofp_header *oh, size_t length)
516 if (length < sizeof(struct ofp_stats_msg)) {
517 if (length == ntohs(oh->length)) {
518 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
520 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
527 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
528 const struct ofputil_msg_type **typep)
530 static const struct ofputil_msg_type ofpst_requests[] = {
531 { OFPUTIL_OFPST_DESC_REQUEST,
532 OFPST_DESC, "OFPST_DESC request",
533 sizeof(struct ofp_stats_msg), 0 },
535 { OFPUTIL_OFPST_FLOW_REQUEST,
536 OFPST_FLOW, "OFPST_FLOW request",
537 sizeof(struct ofp_flow_stats_request), 0 },
539 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
540 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
541 sizeof(struct ofp_flow_stats_request), 0 },
543 { OFPUTIL_OFPST_TABLE_REQUEST,
544 OFPST_TABLE, "OFPST_TABLE request",
545 sizeof(struct ofp_stats_msg), 0 },
547 { OFPUTIL_OFPST_PORT_REQUEST,
548 OFPST_PORT, "OFPST_PORT request",
549 sizeof(struct ofp_port_stats_request), 0 },
551 { OFPUTIL_OFPST_QUEUE_REQUEST,
552 OFPST_QUEUE, "OFPST_QUEUE request",
553 sizeof(struct ofp_queue_stats_request), 0 },
556 OFPST_VENDOR, "OFPST_VENDOR request",
557 sizeof(struct ofp_vendor_stats_msg), 1 },
560 static const struct ofputil_msg_category ofpst_request_category = {
561 "OpenFlow statistics",
562 ofpst_requests, ARRAY_SIZE(ofpst_requests),
563 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
566 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
569 error = check_stats_msg(oh, length);
574 error = ofputil_lookup_openflow_message(&ofpst_request_category,
575 ntohs(request->type), typep);
576 if (!error && request->type == htons(OFPST_VENDOR)) {
577 error = ofputil_decode_nxst_request(oh, length, typep);
583 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
584 const struct ofputil_msg_type **typep)
586 static const struct ofputil_msg_type ofpst_replies[] = {
587 { OFPUTIL_OFPST_DESC_REPLY,
588 OFPST_DESC, "OFPST_DESC reply",
589 sizeof(struct ofp_desc_stats), 0 },
591 { OFPUTIL_OFPST_FLOW_REPLY,
592 OFPST_FLOW, "OFPST_FLOW reply",
593 sizeof(struct ofp_stats_msg), 1 },
595 { OFPUTIL_OFPST_AGGREGATE_REPLY,
596 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
597 sizeof(struct ofp_aggregate_stats_reply), 0 },
599 { OFPUTIL_OFPST_TABLE_REPLY,
600 OFPST_TABLE, "OFPST_TABLE reply",
601 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
603 { OFPUTIL_OFPST_PORT_REPLY,
604 OFPST_PORT, "OFPST_PORT reply",
605 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
607 { OFPUTIL_OFPST_QUEUE_REPLY,
608 OFPST_QUEUE, "OFPST_QUEUE reply",
609 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
612 OFPST_VENDOR, "OFPST_VENDOR reply",
613 sizeof(struct ofp_vendor_stats_msg), 1 },
616 static const struct ofputil_msg_category ofpst_reply_category = {
617 "OpenFlow statistics",
618 ofpst_replies, ARRAY_SIZE(ofpst_replies),
619 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT)
622 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
625 error = check_stats_msg(oh, length);
630 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
631 ntohs(reply->type), typep);
632 if (!error && reply->type == htons(OFPST_VENDOR)) {
633 error = ofputil_decode_nxst_reply(oh, length, typep);
639 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
640 const struct ofputil_msg_type **typep)
642 static const struct ofputil_msg_type ofpt_messages[] = {
643 { OFPUTIL_OFPT_HELLO,
644 OFPT_HELLO, "OFPT_HELLO",
645 sizeof(struct ofp_hello), 1 },
647 { OFPUTIL_OFPT_ERROR,
648 OFPT_ERROR, "OFPT_ERROR",
649 sizeof(struct ofp_error_msg), 1 },
651 { OFPUTIL_OFPT_ECHO_REQUEST,
652 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
653 sizeof(struct ofp_header), 1 },
655 { OFPUTIL_OFPT_ECHO_REPLY,
656 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
657 sizeof(struct ofp_header), 1 },
659 { OFPUTIL_OFPT_FEATURES_REQUEST,
660 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
661 sizeof(struct ofp_header), 0 },
663 { OFPUTIL_OFPT_FEATURES_REPLY,
664 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
665 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
667 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
668 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
669 sizeof(struct ofp_header), 0 },
671 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
672 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
673 sizeof(struct ofp_switch_config), 0 },
675 { OFPUTIL_OFPT_SET_CONFIG,
676 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
677 sizeof(struct ofp_switch_config), 0 },
679 { OFPUTIL_OFPT_PACKET_IN,
680 OFPT_PACKET_IN, "OFPT_PACKET_IN",
681 offsetof(struct ofp_packet_in, data), 1 },
683 { OFPUTIL_OFPT_FLOW_REMOVED,
684 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
685 sizeof(struct ofp_flow_removed), 0 },
687 { OFPUTIL_OFPT_PORT_STATUS,
688 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
689 sizeof(struct ofp_port_status), 0 },
691 { OFPUTIL_OFPT_PACKET_OUT,
692 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
693 sizeof(struct ofp_packet_out), 1 },
695 { OFPUTIL_OFPT_FLOW_MOD,
696 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
697 sizeof(struct ofp_flow_mod), 1 },
699 { OFPUTIL_OFPT_PORT_MOD,
700 OFPT_PORT_MOD, "OFPT_PORT_MOD",
701 sizeof(struct ofp_port_mod), 0 },
704 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
705 sizeof(struct ofp_stats_msg), 1 },
708 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
709 sizeof(struct ofp_stats_msg), 1 },
711 { OFPUTIL_OFPT_BARRIER_REQUEST,
712 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
713 sizeof(struct ofp_header), 0 },
715 { OFPUTIL_OFPT_BARRIER_REPLY,
716 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
717 sizeof(struct ofp_header), 0 },
720 OFPT_VENDOR, "OFPT_VENDOR",
721 sizeof(struct ofp_vendor_header), 1 },
724 static const struct ofputil_msg_category ofpt_category = {
726 ofpt_messages, ARRAY_SIZE(ofpt_messages),
727 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
732 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type, typep);
736 error = ofputil_decode_vendor(oh, length, typep);
739 case OFPT_STATS_REQUEST:
740 error = ofputil_decode_ofpst_request(oh, length, typep);
743 case OFPT_STATS_REPLY:
744 error = ofputil_decode_ofpst_reply(oh, length, typep);
753 /* Decodes the message type represented by 'oh'. Returns 0 if successful or
754 * an OpenFlow error code constructed with ofp_mkerr() on failure. Either
755 * way, stores in '*typep' a type structure that can be inspected with the
756 * ofputil_msg_type_*() functions.
758 * oh->length must indicate the correct length of the message (and must be at
759 * least sizeof(struct ofp_header)).
761 * Success indicates that 'oh' is at least as long as the minimum-length
762 * message of its type. */
764 ofputil_decode_msg_type(const struct ofp_header *oh,
765 const struct ofputil_msg_type **typep)
767 size_t length = ntohs(oh->length);
770 error = ofputil_decode_msg_type__(oh, length, typep);
772 error = ofputil_check_length(*typep, length);
775 *typep = &ofputil_invalid_type;
780 /* Decodes the message type represented by 'oh', of which only the first
781 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
782 * code constructed with ofp_mkerr() on failure. Either way, stores in
783 * '*typep' a type structure that can be inspected with the
784 * ofputil_msg_type_*() functions. */
786 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
787 const struct ofputil_msg_type **typep)
791 error = (length >= sizeof *oh
792 ? ofputil_decode_msg_type__(oh, length, typep)
793 : ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN));
795 *typep = &ofputil_invalid_type;
800 /* Returns an OFPUTIL_* message type code for 'type'. */
801 enum ofputil_msg_code
802 ofputil_msg_type_code(const struct ofputil_msg_type *type)
810 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
812 switch (flow_format) {
813 case NXFF_OPENFLOW10:
822 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
824 switch (flow_format) {
825 case NXFF_OPENFLOW10:
835 ofputil_flow_format_from_string(const char *s)
837 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
838 : !strcmp(s, "nxm") ? NXFF_NXM
843 regs_fully_wildcarded(const struct flow_wildcards *wc)
847 for (i = 0; i < FLOW_N_REGS; i++) {
848 if (wc->reg_masks[i] != 0) {
855 /* Returns the minimum nx_flow_format to use for sending 'rule' to a switch
856 * (e.g. to add or remove a flow). Only NXM can handle tunnel IDs, registers,
857 * or fixing the Ethernet multicast bit. Otherwise, it's better to use
858 * NXFF_OPENFLOW10 for backward compatibility. */
860 ofputil_min_flow_format(const struct cls_rule *rule)
862 const struct flow_wildcards *wc = &rule->wc;
864 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
866 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
867 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
871 /* Only NXM supports matching ARP hardware addresses. */
872 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
876 /* Only NXM supports matching IPv6 traffic. */
877 if (!(wc->wildcards & FWW_DL_TYPE)
878 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
882 /* Only NXM supports matching registers. */
883 if (!regs_fully_wildcarded(wc)) {
887 /* Only NXM supports matching tun_id. */
888 if (wc->tun_id_mask != htonll(0)) {
892 /* Only NXM supports matching fragments. */
893 if (wc->nw_frag_mask) {
897 /* Only NXM supports matching IPv6 flow label. */
898 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
902 /* Only NXM supports matching IP ECN bits. */
903 if (!(wc->wildcards & FWW_NW_ECN)) {
907 /* Only NXM supports matching IP TTL/hop limit. */
908 if (!(wc->wildcards & FWW_NW_TTL)) {
912 /* Other formats can express this rule. */
913 return NXFF_OPENFLOW10;
916 /* Returns an OpenFlow message that can be used to set the flow format to
919 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
921 struct nxt_set_flow_format *sff;
924 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
925 sff->format = htonl(flow_format);
930 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
931 * extension on or off (according to 'flow_mod_table_id'). */
933 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
935 struct nxt_flow_mod_table_id *nfmti;
938 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
939 nfmti->set = flow_mod_table_id;
943 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
944 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
947 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
948 * enabled, false otherwise.
950 * Does not validate the flow_mod actions. */
952 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
953 const struct ofp_header *oh, bool flow_mod_table_id)
955 const struct ofputil_msg_type *type;
959 ofpbuf_use_const(&b, oh, ntohs(oh->length));
961 ofputil_decode_msg_type(oh, &type);
962 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
963 /* Standard OpenFlow flow_mod. */
964 const struct ofp_flow_mod *ofm;
968 /* Dissect the message. */
969 ofm = ofpbuf_pull(&b, sizeof *ofm);
970 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
975 /* Set priority based on original wildcards. Normally we'd allow
976 * ofputil_cls_rule_from_match() to do this for us, but
977 * ofputil_normalize_rule() can put wildcards where the original flow
978 * didn't have them. */
979 priority = ntohs(ofm->priority);
980 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
981 priority = UINT16_MAX;
984 /* Translate the rule. */
985 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
986 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
988 /* Translate the message. */
989 fm->cookie = ofm->cookie;
990 command = ntohs(ofm->command);
991 fm->idle_timeout = ntohs(ofm->idle_timeout);
992 fm->hard_timeout = ntohs(ofm->hard_timeout);
993 fm->buffer_id = ntohl(ofm->buffer_id);
994 fm->out_port = ntohs(ofm->out_port);
995 fm->flags = ntohs(ofm->flags);
996 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
997 /* Nicira extended flow_mod. */
998 const struct nx_flow_mod *nfm;
1001 /* Dissect the message. */
1002 nfm = ofpbuf_pull(&b, sizeof *nfm);
1003 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1008 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1013 /* Translate the message. */
1014 fm->cookie = nfm->cookie;
1015 command = ntohs(nfm->command);
1016 fm->idle_timeout = ntohs(nfm->idle_timeout);
1017 fm->hard_timeout = ntohs(nfm->hard_timeout);
1018 fm->buffer_id = ntohl(nfm->buffer_id);
1019 fm->out_port = ntohs(nfm->out_port);
1020 fm->flags = ntohs(nfm->flags);
1025 if (flow_mod_table_id) {
1026 fm->command = command & 0xff;
1027 fm->table_id = command >> 8;
1029 fm->command = command;
1030 fm->table_id = 0xff;
1036 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1037 * 'flow_format' and returns the message.
1039 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1040 * enabled, false otherwise. */
1042 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1043 enum nx_flow_format flow_format,
1044 bool flow_mod_table_id)
1046 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1050 command = (flow_mod_table_id
1051 ? (fm->command & 0xff) | (fm->table_id << 8)
1054 if (flow_format == NXFF_OPENFLOW10) {
1055 struct ofp_flow_mod *ofm;
1057 msg = ofpbuf_new(sizeof *ofm + actions_len);
1058 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1059 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1060 ofm->cookie = fm->cookie;
1061 ofm->command = htons(command);
1062 ofm->idle_timeout = htons(fm->idle_timeout);
1063 ofm->hard_timeout = htons(fm->hard_timeout);
1064 ofm->priority = htons(fm->cr.priority);
1065 ofm->buffer_id = htonl(fm->buffer_id);
1066 ofm->out_port = htons(fm->out_port);
1067 ofm->flags = htons(fm->flags);
1068 } else if (flow_format == NXFF_NXM) {
1069 struct nx_flow_mod *nfm;
1072 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1073 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1074 match_len = nx_put_match(msg, &fm->cr);
1077 nfm->cookie = fm->cookie;
1078 nfm->command = htons(command);
1079 nfm->idle_timeout = htons(fm->idle_timeout);
1080 nfm->hard_timeout = htons(fm->hard_timeout);
1081 nfm->priority = htons(fm->cr.priority);
1082 nfm->buffer_id = htonl(fm->buffer_id);
1083 nfm->out_port = htons(fm->out_port);
1084 nfm->flags = htons(fm->flags);
1085 nfm->match_len = htons(match_len);
1090 ofpbuf_put(msg, fm->actions, actions_len);
1091 update_openflow_length(msg);
1096 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1097 const struct ofp_header *oh,
1100 const struct ofp_flow_stats_request *ofsr =
1101 (const struct ofp_flow_stats_request *) oh;
1103 fsr->aggregate = aggregate;
1104 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1105 fsr->out_port = ntohs(ofsr->out_port);
1106 fsr->table_id = ofsr->table_id;
1112 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1113 const struct ofp_header *oh,
1116 const struct nx_flow_stats_request *nfsr;
1120 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1122 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1123 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1128 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1131 fsr->aggregate = aggregate;
1132 fsr->out_port = ntohs(nfsr->out_port);
1133 fsr->table_id = nfsr->table_id;
1138 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1139 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1140 * successful, otherwise an OpenFlow error code. */
1142 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1143 const struct ofp_header *oh)
1145 const struct ofputil_msg_type *type;
1149 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1151 ofputil_decode_msg_type(oh, &type);
1152 code = ofputil_msg_type_code(type);
1154 case OFPUTIL_OFPST_FLOW_REQUEST:
1155 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1157 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1158 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1160 case OFPUTIL_NXST_FLOW_REQUEST:
1161 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1163 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1164 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1167 /* Hey, the caller lied. */
1172 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1173 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1174 * 'flow_format', and returns the message. */
1176 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1177 enum nx_flow_format flow_format)
1181 if (flow_format == NXFF_OPENFLOW10) {
1182 struct ofp_flow_stats_request *ofsr;
1185 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1186 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1187 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1188 ofsr->table_id = fsr->table_id;
1189 ofsr->out_port = htons(fsr->out_port);
1190 } else if (flow_format == NXFF_NXM) {
1191 struct nx_flow_stats_request *nfsr;
1195 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1196 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1197 match_len = nx_put_match(msg, &fsr->match);
1200 nfsr->out_port = htons(fsr->out_port);
1201 nfsr->match_len = htons(match_len);
1202 nfsr->table_id = fsr->table_id;
1210 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1211 * ofputil_flow_stats in 'fs'.
1213 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1214 * OpenFlow message. Calling this function multiple times for a single 'msg'
1215 * iterates through the replies. The caller must initially leave 'msg''s layer
1216 * pointers null and not modify them between calls.
1218 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1219 * otherwise a positive errno value. */
1221 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1224 const struct ofputil_msg_type *type;
1227 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1228 code = ofputil_msg_type_code(type);
1230 msg->l2 = msg->data;
1231 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1232 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1233 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1234 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1242 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1243 const struct ofp_flow_stats *ofs;
1246 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1248 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1249 "bytes at end", msg->size);
1253 length = ntohs(ofs->length);
1254 if (length < sizeof *ofs) {
1255 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1256 "length %zu", length);
1260 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1261 &fs->actions, &fs->n_actions)) {
1265 fs->cookie = get_32aligned_be64(&ofs->cookie);
1266 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1268 fs->table_id = ofs->table_id;
1269 fs->duration_sec = ntohl(ofs->duration_sec);
1270 fs->duration_nsec = ntohl(ofs->duration_nsec);
1271 fs->idle_timeout = ntohs(ofs->idle_timeout);
1272 fs->hard_timeout = ntohs(ofs->hard_timeout);
1273 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1274 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1275 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1276 const struct nx_flow_stats *nfs;
1277 size_t match_len, length;
1279 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1281 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1282 "bytes at end", msg->size);
1286 length = ntohs(nfs->length);
1287 match_len = ntohs(nfs->match_len);
1288 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1289 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1290 "claims invalid length %zu", match_len, length);
1293 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1297 if (ofputil_pull_actions(msg,
1298 length - sizeof *nfs - ROUND_UP(match_len, 8),
1299 &fs->actions, &fs->n_actions)) {
1303 fs->cookie = nfs->cookie;
1304 fs->table_id = nfs->table_id;
1305 fs->duration_sec = ntohl(nfs->duration_sec);
1306 fs->duration_nsec = ntohl(nfs->duration_nsec);
1307 fs->idle_timeout = ntohs(nfs->idle_timeout);
1308 fs->hard_timeout = ntohs(nfs->hard_timeout);
1309 fs->packet_count = ntohll(nfs->packet_count);
1310 fs->byte_count = ntohll(nfs->byte_count);
1318 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1320 * We use this in situations where OVS internally uses UINT64_MAX to mean
1321 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1323 unknown_to_zero(uint64_t count)
1325 return count != UINT64_MAX ? count : 0;
1328 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1329 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1330 * have been initialized with ofputil_start_stats_reply(). */
1332 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1333 struct list *replies)
1335 size_t act_len = fs->n_actions * sizeof *fs->actions;
1336 const struct ofp_stats_msg *osm;
1338 osm = ofpbuf_from_list(list_back(replies))->data;
1339 if (osm->type == htons(OFPST_FLOW)) {
1340 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1341 struct ofp_flow_stats *ofs;
1343 ofs = ofputil_append_stats_reply(len, replies);
1344 ofs->length = htons(len);
1345 ofs->table_id = fs->table_id;
1347 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1348 ofs->duration_sec = htonl(fs->duration_sec);
1349 ofs->duration_nsec = htonl(fs->duration_nsec);
1350 ofs->priority = htons(fs->rule.priority);
1351 ofs->idle_timeout = htons(fs->idle_timeout);
1352 ofs->hard_timeout = htons(fs->hard_timeout);
1353 memset(ofs->pad2, 0, sizeof ofs->pad2);
1354 put_32aligned_be64(&ofs->cookie, fs->cookie);
1355 put_32aligned_be64(&ofs->packet_count,
1356 htonll(unknown_to_zero(fs->packet_count)));
1357 put_32aligned_be64(&ofs->byte_count,
1358 htonll(unknown_to_zero(fs->byte_count)));
1359 memcpy(ofs->actions, fs->actions, act_len);
1360 } else if (osm->type == htons(OFPST_VENDOR)) {
1361 struct nx_flow_stats *nfs;
1365 msg = ofputil_reserve_stats_reply(
1366 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1367 start_len = msg->size;
1369 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1370 nfs->table_id = fs->table_id;
1372 nfs->duration_sec = htonl(fs->duration_sec);
1373 nfs->duration_nsec = htonl(fs->duration_nsec);
1374 nfs->priority = htons(fs->rule.priority);
1375 nfs->idle_timeout = htons(fs->idle_timeout);
1376 nfs->hard_timeout = htons(fs->hard_timeout);
1377 nfs->match_len = htons(nx_put_match(msg, &fs->rule));
1378 memset(nfs->pad2, 0, sizeof nfs->pad2);
1379 nfs->cookie = fs->cookie;
1380 nfs->packet_count = htonll(fs->packet_count);
1381 nfs->byte_count = htonll(fs->byte_count);
1382 ofpbuf_put(msg, fs->actions, act_len);
1383 nfs->length = htons(msg->size - start_len);
1389 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1390 * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */
1392 ofputil_encode_aggregate_stats_reply(
1393 const struct ofputil_aggregate_stats *stats,
1394 const struct ofp_stats_msg *request)
1398 if (request->type == htons(OFPST_AGGREGATE)) {
1399 struct ofp_aggregate_stats_reply *asr;
1401 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
1402 put_32aligned_be64(&asr->packet_count,
1403 htonll(unknown_to_zero(stats->packet_count)));
1404 put_32aligned_be64(&asr->byte_count,
1405 htonll(unknown_to_zero(stats->byte_count)));
1406 asr->flow_count = htonl(stats->flow_count);
1407 } else if (request->type == htons(OFPST_VENDOR)) {
1408 struct nx_aggregate_stats_reply *nasr;
1410 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1411 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1412 nasr->packet_count = htonll(stats->packet_count);
1413 nasr->byte_count = htonll(stats->byte_count);
1414 nasr->flow_count = htonl(stats->flow_count);
1422 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1423 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1424 * an OpenFlow error code. */
1426 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1427 const struct ofp_header *oh)
1429 const struct ofputil_msg_type *type;
1430 enum ofputil_msg_code code;
1432 ofputil_decode_msg_type(oh, &type);
1433 code = ofputil_msg_type_code(type);
1434 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1435 const struct ofp_flow_removed *ofr;
1437 ofr = (const struct ofp_flow_removed *) oh;
1438 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1440 fr->cookie = ofr->cookie;
1441 fr->reason = ofr->reason;
1442 fr->duration_sec = ntohl(ofr->duration_sec);
1443 fr->duration_nsec = ntohl(ofr->duration_nsec);
1444 fr->idle_timeout = ntohs(ofr->idle_timeout);
1445 fr->packet_count = ntohll(ofr->packet_count);
1446 fr->byte_count = ntohll(ofr->byte_count);
1447 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1448 struct nx_flow_removed *nfr;
1452 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1454 nfr = ofpbuf_pull(&b, sizeof *nfr);
1455 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1461 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1464 fr->cookie = nfr->cookie;
1465 fr->reason = nfr->reason;
1466 fr->duration_sec = ntohl(nfr->duration_sec);
1467 fr->duration_nsec = ntohl(nfr->duration_nsec);
1468 fr->idle_timeout = ntohs(nfr->idle_timeout);
1469 fr->packet_count = ntohll(nfr->packet_count);
1470 fr->byte_count = ntohll(nfr->byte_count);
1478 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1479 * NXT_FLOW_REMOVED message 'oh' according to 'flow_format', and returns the
1482 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1483 enum nx_flow_format flow_format)
1487 if (flow_format == NXFF_OPENFLOW10) {
1488 struct ofp_flow_removed *ofr;
1490 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1492 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1493 ofr->cookie = fr->cookie;
1494 ofr->priority = htons(fr->rule.priority);
1495 ofr->reason = fr->reason;
1496 ofr->duration_sec = htonl(fr->duration_sec);
1497 ofr->duration_nsec = htonl(fr->duration_nsec);
1498 ofr->idle_timeout = htons(fr->idle_timeout);
1499 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1500 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1501 } else if (flow_format == NXFF_NXM) {
1502 struct nx_flow_removed *nfr;
1505 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1506 match_len = nx_put_match(msg, &fr->rule);
1509 nfr->cookie = fr->cookie;
1510 nfr->priority = htons(fr->rule.priority);
1511 nfr->reason = fr->reason;
1512 nfr->duration_sec = htonl(fr->duration_sec);
1513 nfr->duration_nsec = htonl(fr->duration_nsec);
1514 nfr->idle_timeout = htons(fr->idle_timeout);
1515 nfr->match_len = htons(match_len);
1516 nfr->packet_count = htonll(fr->packet_count);
1517 nfr->byte_count = htonll(fr->byte_count);
1525 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1526 * and returns the message.
1528 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
1531 * If 'rw_packet' is nonnull, then it must contain the same data as
1532 * pin->packet. 'rw_packet' is allowed to be the same ofpbuf as pin->packet.
1533 * It is modified in-place into an OFPT_PACKET_IN message according to 'pin',
1534 * and then ofputil_encode_packet_in() returns 'rw_packet'. If 'rw_packet' has
1535 * enough headroom to insert a "struct ofp_packet_in", this is more efficient
1536 * than ofputil_encode_packet_in() because it does not copy the packet
1539 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1540 struct ofpbuf *rw_packet)
1542 int total_len = pin->packet->size;
1543 struct ofp_packet_in opi;
1546 if (pin->send_len < rw_packet->size) {
1547 rw_packet->size = pin->send_len;
1550 rw_packet = ofpbuf_clone_data_with_headroom(
1551 pin->packet->data, MIN(pin->send_len, pin->packet->size),
1552 offsetof(struct ofp_packet_in, data));
1555 /* Add OFPT_PACKET_IN. */
1556 memset(&opi, 0, sizeof opi);
1557 opi.header.version = OFP_VERSION;
1558 opi.header.type = OFPT_PACKET_IN;
1559 opi.total_len = htons(total_len);
1560 opi.in_port = htons(pin->in_port);
1561 opi.reason = pin->reason;
1562 opi.buffer_id = htonl(pin->buffer_id);
1563 ofpbuf_push(rw_packet, &opi, offsetof(struct ofp_packet_in, data));
1564 update_openflow_length(rw_packet);
1569 /* Returns a string representing the message type of 'type'. The string is the
1570 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
1571 * messages, the constant is followed by "request" or "reply",
1572 * e.g. "OFPST_AGGREGATE reply". */
1574 ofputil_msg_type_name(const struct ofputil_msg_type *type)
1579 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1580 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1581 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
1584 * The caller is responsible for freeing '*bufferp' when it is no longer
1587 * The OpenFlow header length is initially set to 'openflow_len'; if the
1588 * message is later extended, the length should be updated with
1589 * update_openflow_length() before sending.
1591 * Returns the header. */
1593 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1595 *bufferp = ofpbuf_new(openflow_len);
1596 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1599 /* Similar to make_openflow() but creates a Nicira vendor extension message
1600 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1602 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1604 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
1607 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
1608 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
1609 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
1612 * The caller is responsible for freeing '*bufferp' when it is no longer
1615 * The OpenFlow header length is initially set to 'openflow_len'; if the
1616 * message is later extended, the length should be updated with
1617 * update_openflow_length() before sending.
1619 * Returns the header. */
1621 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1622 struct ofpbuf **bufferp)
1624 *bufferp = ofpbuf_new(openflow_len);
1625 return put_openflow_xid(openflow_len, type, xid, *bufferp);
1628 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
1629 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1631 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1632 struct ofpbuf **bufferp)
1634 *bufferp = ofpbuf_new(openflow_len);
1635 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
1638 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1639 * with the given 'type' and an arbitrary transaction id. Allocated bytes
1640 * beyond the header, if any, are zeroed.
1642 * The OpenFlow header length is initially set to 'openflow_len'; if the
1643 * message is later extended, the length should be updated with
1644 * update_openflow_length() before sending.
1646 * Returns the header. */
1648 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1650 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
1653 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
1654 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
1655 * the header, if any, are zeroed.
1657 * The OpenFlow header length is initially set to 'openflow_len'; if the
1658 * message is later extended, the length should be updated with
1659 * update_openflow_length() before sending.
1661 * Returns the header. */
1663 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1664 struct ofpbuf *buffer)
1666 struct ofp_header *oh;
1668 assert(openflow_len >= sizeof *oh);
1669 assert(openflow_len <= UINT16_MAX);
1671 oh = ofpbuf_put_uninit(buffer, openflow_len);
1672 oh->version = OFP_VERSION;
1674 oh->length = htons(openflow_len);
1676 memset(oh + 1, 0, openflow_len - sizeof *oh);
1680 /* Similar to put_openflow() but append a Nicira vendor extension message with
1681 * the specific 'subtype'. 'subtype' should be in host byte order. */
1683 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1685 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
1688 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
1689 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1691 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1692 struct ofpbuf *buffer)
1694 struct nicira_header *nxh;
1696 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1697 nxh->vendor = htonl(NX_VENDOR_ID);
1698 nxh->subtype = htonl(subtype);
1702 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1703 * 'buffer->size'. */
1705 update_openflow_length(struct ofpbuf *buffer)
1707 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1708 oh->length = htons(buffer->size);
1712 put_stats__(ovs_be32 xid, uint8_t ofp_type,
1713 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
1716 if (ofpst_type == htons(OFPST_VENDOR)) {
1717 struct nicira_stats_msg *nsm;
1719 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
1720 nsm->vsm.osm.type = ofpst_type;
1721 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
1722 nsm->subtype = nxst_subtype;
1724 struct ofp_stats_msg *osm;
1726 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
1727 osm->type = ofpst_type;
1731 /* Creates a statistics request message with total length 'openflow_len'
1732 * (including all headers) and the given 'ofpst_type', and stores the buffer
1733 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
1734 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
1735 * subtype (otherwise 'nxst_subtype' is ignored).
1737 * Initializes bytes following the headers to all-bits-zero.
1739 * Returns the first byte of the new message. */
1741 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
1742 uint32_t nxst_subtype, struct ofpbuf **bufferp)
1746 msg = *bufferp = ofpbuf_new(openflow_len);
1747 put_stats__(alloc_xid(), OFPT_STATS_REQUEST,
1748 htons(ofpst_type), htonl(nxst_subtype), msg);
1749 ofpbuf_padto(msg, openflow_len);
1755 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
1757 assert(request->header.type == OFPT_STATS_REQUEST ||
1758 request->header.type == OFPT_STATS_REPLY);
1759 put_stats__(request->header.xid, OFPT_STATS_REPLY, request->type,
1760 (request->type != htons(OFPST_VENDOR)
1762 : ((const struct nicira_stats_msg *) request)->subtype),
1766 /* Creates a statistics reply message with total length 'openflow_len'
1767 * (including all headers) and the same type (either a standard OpenFlow
1768 * statistics type or a Nicira extension type and subtype) as 'request', and
1769 * stores the buffer containing the new message in '*bufferp'.
1771 * Initializes bytes following the headers to all-bits-zero.
1773 * Returns the first byte of the new message. */
1775 ofputil_make_stats_reply(size_t openflow_len,
1776 const struct ofp_stats_msg *request,
1777 struct ofpbuf **bufferp)
1781 msg = *bufferp = ofpbuf_new(openflow_len);
1782 put_stats_reply__(request, msg);
1783 ofpbuf_padto(msg, openflow_len);
1788 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
1789 * replies to 'request', which should be an OpenFlow or Nicira extension
1790 * statistics request. Initially 'replies' will have a single reply message
1791 * that has only a header. The functions ofputil_reserve_stats_reply() and
1792 * ofputil_append_stats_reply() may be used to add to the reply. */
1794 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
1795 struct list *replies)
1799 msg = ofpbuf_new(1024);
1800 put_stats_reply__(request, msg);
1803 list_push_back(replies, &msg->list_node);
1806 /* Prepares to append up to 'len' bytes to the series of statistics replies in
1807 * 'replies', which should have been initialized with
1808 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
1809 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
1810 * do so with e.g. ofpbuf_put_uninit().) */
1812 ofputil_reserve_stats_reply(size_t len, struct list *replies)
1814 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1815 struct ofp_stats_msg *osm = msg->data;
1817 if (msg->size + len <= UINT16_MAX) {
1818 ofpbuf_prealloc_tailroom(msg, len);
1820 osm->flags |= htons(OFPSF_REPLY_MORE);
1822 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
1823 put_stats_reply__(osm, msg);
1824 list_push_back(replies, &msg->list_node);
1829 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
1830 * returns the first byte. */
1832 ofputil_append_stats_reply(size_t len, struct list *replies)
1834 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
1837 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
1839 ofputil_stats_body(const struct ofp_header *oh)
1841 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1842 return (const struct ofp_stats_msg *) oh + 1;
1845 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
1847 ofputil_stats_body_len(const struct ofp_header *oh)
1849 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1850 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
1853 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
1855 ofputil_nxstats_body(const struct ofp_header *oh)
1857 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1858 return ((const struct nicira_stats_msg *) oh) + 1;
1861 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
1863 ofputil_nxstats_body_len(const struct ofp_header *oh)
1865 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1866 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1870 make_flow_mod(uint16_t command, const struct cls_rule *rule,
1873 struct ofp_flow_mod *ofm;
1874 size_t size = sizeof *ofm + actions_len;
1875 struct ofpbuf *out = ofpbuf_new(size);
1876 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
1877 ofm->header.version = OFP_VERSION;
1878 ofm->header.type = OFPT_FLOW_MOD;
1879 ofm->header.length = htons(size);
1881 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1882 ofputil_cls_rule_to_match(rule, &ofm->match);
1883 ofm->command = htons(command);
1888 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1889 uint16_t idle_timeout, size_t actions_len)
1891 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
1892 struct ofp_flow_mod *ofm = out->data;
1893 ofm->idle_timeout = htons(idle_timeout);
1894 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1895 ofm->buffer_id = htonl(buffer_id);
1900 make_del_flow(const struct cls_rule *rule)
1902 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1903 struct ofp_flow_mod *ofm = out->data;
1904 ofm->out_port = htons(OFPP_NONE);
1909 make_add_simple_flow(const struct cls_rule *rule,
1910 uint32_t buffer_id, uint16_t out_port,
1911 uint16_t idle_timeout)
1913 if (out_port != OFPP_NONE) {
1914 struct ofp_action_output *oao;
1915 struct ofpbuf *buffer;
1917 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1918 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
1921 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1926 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1927 const struct ofpbuf *payload, int max_send_len)
1929 struct ofp_packet_in *opi;
1933 send_len = MIN(max_send_len, payload->size);
1934 buf = ofpbuf_new(sizeof *opi + send_len);
1935 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1936 OFPT_PACKET_IN, 0, buf);
1937 opi->buffer_id = htonl(buffer_id);
1938 opi->total_len = htons(payload->size);
1939 opi->in_port = htons(in_port);
1940 opi->reason = reason;
1941 ofpbuf_put(buf, payload->data, send_len);
1942 update_openflow_length(buf);
1948 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1950 const struct ofp_action_header *actions, size_t n_actions)
1952 size_t actions_len = n_actions * sizeof *actions;
1953 struct ofp_packet_out *opo;
1954 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1955 struct ofpbuf *out = ofpbuf_new(size);
1957 opo = ofpbuf_put_uninit(out, sizeof *opo);
1958 opo->header.version = OFP_VERSION;
1959 opo->header.type = OFPT_PACKET_OUT;
1960 opo->header.length = htons(size);
1961 opo->header.xid = htonl(0);
1962 opo->buffer_id = htonl(buffer_id);
1963 opo->in_port = htons(in_port);
1964 opo->actions_len = htons(actions_len);
1965 ofpbuf_put(out, actions, actions_len);
1967 ofpbuf_put(out, packet->data, packet->size);
1973 make_unbuffered_packet_out(const struct ofpbuf *packet,
1974 uint16_t in_port, uint16_t out_port)
1976 struct ofp_action_output action;
1977 action.type = htons(OFPAT_OUTPUT);
1978 action.len = htons(sizeof action);
1979 action.port = htons(out_port);
1980 return make_packet_out(packet, UINT32_MAX, in_port,
1981 (struct ofp_action_header *) &action, 1);
1985 make_buffered_packet_out(uint32_t buffer_id,
1986 uint16_t in_port, uint16_t out_port)
1988 if (out_port != OFPP_NONE) {
1989 struct ofp_action_output action;
1990 action.type = htons(OFPAT_OUTPUT);
1991 action.len = htons(sizeof action);
1992 action.port = htons(out_port);
1993 return make_packet_out(NULL, buffer_id, in_port,
1994 (struct ofp_action_header *) &action, 1);
1996 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
2000 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
2002 make_echo_request(void)
2004 struct ofp_header *rq;
2005 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
2006 rq = ofpbuf_put_uninit(out, sizeof *rq);
2007 rq->version = OFP_VERSION;
2008 rq->type = OFPT_ECHO_REQUEST;
2009 rq->length = htons(sizeof *rq);
2014 /* Creates and returns an OFPT_ECHO_REPLY message matching the
2015 * OFPT_ECHO_REQUEST message in 'rq'. */
2017 make_echo_reply(const struct ofp_header *rq)
2019 size_t size = ntohs(rq->length);
2020 struct ofpbuf *out = ofpbuf_new(size);
2021 struct ofp_header *reply = ofpbuf_put(out, rq, size);
2022 reply->type = OFPT_ECHO_REPLY;
2027 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
2029 switch (flags & OFPC_FRAG_MASK) {
2030 case OFPC_FRAG_NORMAL: return "normal";
2031 case OFPC_FRAG_DROP: return "drop";
2032 case OFPC_FRAG_REASM: return "reassemble";
2033 case OFPC_FRAG_NX_MATCH: return "nx-match";
2040 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
2042 if (!strcasecmp(s, "normal")) {
2043 *flags = OFPC_FRAG_NORMAL;
2044 } else if (!strcasecmp(s, "drop")) {
2045 *flags = OFPC_FRAG_DROP;
2046 } else if (!strcasecmp(s, "reassemble")) {
2047 *flags = OFPC_FRAG_REASM;
2048 } else if (!strcasecmp(s, "nx-match")) {
2049 *flags = OFPC_FRAG_NX_MATCH;
2056 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
2057 * that the switch will never have more than 'max_ports' ports. Returns 0 if
2058 * 'port' is valid, otherwise an ofp_mkerr() return code. */
2060 ofputil_check_output_port(uint16_t port, int max_ports)
2068 case OFPP_CONTROLLER:
2073 if (port < max_ports) {
2076 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
2080 #define OFPUTIL_NAMED_PORTS \
2081 OFPUTIL_NAMED_PORT(IN_PORT) \
2082 OFPUTIL_NAMED_PORT(TABLE) \
2083 OFPUTIL_NAMED_PORT(NORMAL) \
2084 OFPUTIL_NAMED_PORT(FLOOD) \
2085 OFPUTIL_NAMED_PORT(ALL) \
2086 OFPUTIL_NAMED_PORT(CONTROLLER) \
2087 OFPUTIL_NAMED_PORT(LOCAL) \
2088 OFPUTIL_NAMED_PORT(NONE)
2090 /* Checks whether 's' is the string representation of an OpenFlow port number,
2091 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
2092 * number in '*port' and returns true. Otherwise, returns false. */
2094 ofputil_port_from_string(const char *name, uint16_t *port)
2100 static const struct pair pairs[] = {
2101 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2103 #undef OFPUTIL_NAMED_PORT
2105 static const int n_pairs = ARRAY_SIZE(pairs);
2108 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2113 for (i = 0; i < n_pairs; i++) {
2114 if (!strcasecmp(name, pairs[i].name)) {
2115 *port = pairs[i].value;
2122 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
2123 * Most ports' string representation is just the port number, but for special
2124 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
2126 ofputil_format_port(uint16_t port, struct ds *s)
2131 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2133 #undef OFPUTIL_NAMED_PORT
2136 ds_put_format(s, "%"PRIu16, port);
2139 ds_put_cstr(s, name);
2143 check_resubmit_table(const struct nx_action_resubmit *nar)
2145 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2146 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2152 check_output_reg(const struct nx_action_output_reg *naor,
2153 const struct flow *flow)
2157 for (i = 0; i < sizeof naor->zero; i++) {
2158 if (naor->zero[i]) {
2159 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2163 return nxm_src_check(naor->src, nxm_decode_ofs(naor->ofs_nbits),
2164 nxm_decode_n_bits(naor->ofs_nbits), flow);
2168 validate_actions(const union ofp_action *actions, size_t n_actions,
2169 const struct flow *flow, int max_ports)
2171 const union ofp_action *a;
2174 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2179 code = ofputil_decode_action(a);
2184 msg = ofputil_error_to_string(error);
2185 VLOG_WARN_RL(&bad_ofmsg_rl,
2186 "action decoding error at offset %td (%s)",
2187 (a - actions) * sizeof *a, msg);
2194 switch ((enum ofputil_action_code) code) {
2195 case OFPUTIL_OFPAT_OUTPUT:
2196 error = ofputil_check_output_port(ntohs(a->output.port),
2200 case OFPUTIL_OFPAT_SET_VLAN_VID:
2201 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2202 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2206 case OFPUTIL_OFPAT_SET_VLAN_PCP:
2207 if (a->vlan_pcp.vlan_pcp & ~7) {
2208 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2212 case OFPUTIL_OFPAT_ENQUEUE:
2213 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
2214 if (port >= max_ports && port != OFPP_IN_PORT
2215 && port != OFPP_LOCAL) {
2216 error = ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
2220 case OFPUTIL_NXAST_REG_MOVE:
2221 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2225 case OFPUTIL_NXAST_REG_LOAD:
2226 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2230 case OFPUTIL_NXAST_MULTIPATH:
2231 error = multipath_check((const struct nx_action_multipath *) a,
2235 case OFPUTIL_NXAST_AUTOPATH:
2236 error = autopath_check((const struct nx_action_autopath *) a,
2240 case OFPUTIL_NXAST_BUNDLE:
2241 case OFPUTIL_NXAST_BUNDLE_LOAD:
2242 error = bundle_check((const struct nx_action_bundle *) a,
2246 case OFPUTIL_NXAST_OUTPUT_REG:
2247 error = check_output_reg((const struct nx_action_output_reg *) a,
2251 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2252 error = check_resubmit_table(
2253 (const struct nx_action_resubmit *) a);
2256 case OFPUTIL_NXAST_LEARN:
2257 error = learn_check((const struct nx_action_learn *) a, flow);
2260 case OFPUTIL_OFPAT_STRIP_VLAN:
2261 case OFPUTIL_OFPAT_SET_NW_SRC:
2262 case OFPUTIL_OFPAT_SET_NW_DST:
2263 case OFPUTIL_OFPAT_SET_NW_TOS:
2264 case OFPUTIL_OFPAT_SET_TP_SRC:
2265 case OFPUTIL_OFPAT_SET_TP_DST:
2266 case OFPUTIL_OFPAT_SET_DL_SRC:
2267 case OFPUTIL_OFPAT_SET_DL_DST:
2268 case OFPUTIL_NXAST_RESUBMIT:
2269 case OFPUTIL_NXAST_SET_TUNNEL:
2270 case OFPUTIL_NXAST_SET_QUEUE:
2271 case OFPUTIL_NXAST_POP_QUEUE:
2272 case OFPUTIL_NXAST_NOTE:
2273 case OFPUTIL_NXAST_SET_TUNNEL64:
2274 case OFPUTIL_NXAST_EXIT:
2279 char *msg = ofputil_error_to_string(error);
2280 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
2281 (a - actions) * sizeof *a, msg);
2287 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
2288 (n_actions - left) * sizeof *a);
2289 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2294 struct ofputil_action {
2296 unsigned int min_len;
2297 unsigned int max_len;
2300 static const struct ofputil_action action_bad_type
2301 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE), 0, UINT_MAX };
2302 static const struct ofputil_action action_bad_len
2303 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_LEN), 0, UINT_MAX };
2304 static const struct ofputil_action action_bad_vendor
2305 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR), 0, UINT_MAX };
2307 static const struct ofputil_action *
2308 ofputil_decode_ofpat_action(const union ofp_action *a)
2310 enum ofp_action_type type = ntohs(a->type);
2313 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2315 static const struct ofputil_action action = { \
2317 sizeof(struct STRUCT), \
2318 sizeof(struct STRUCT) \
2322 #include "ofp-util.def"
2326 return &action_bad_type;
2330 static const struct ofputil_action *
2331 ofputil_decode_nxast_action(const union ofp_action *a)
2333 const struct nx_action_header *nah = (const struct nx_action_header *) a;
2334 enum nx_action_subtype subtype = ntohs(nah->subtype);
2337 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2339 static const struct ofputil_action action = { \
2341 sizeof(struct STRUCT), \
2342 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
2346 #include "ofp-util.def"
2348 case NXAST_SNAT__OBSOLETE:
2349 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
2351 return &action_bad_type;
2355 /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or
2356 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OpenFlow error
2357 * code (as returned by ofp_mkerr()).
2359 * The caller must have already verified that 'a''s length is correct (that is,
2360 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
2361 * longer than the amount of space allocated to 'a').
2363 * This function verifies that 'a''s length is correct for the type of action
2364 * that it represents. */
2366 ofputil_decode_action(const union ofp_action *a)
2368 const struct ofputil_action *action;
2369 uint16_t len = ntohs(a->header.len);
2371 if (a->type != htons(OFPAT_VENDOR)) {
2372 action = ofputil_decode_ofpat_action(a);
2374 switch (ntohl(a->vendor.vendor)) {
2376 if (len < sizeof(struct nx_action_header)) {
2377 return -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2379 action = ofputil_decode_nxast_action(a);
2382 action = &action_bad_vendor;
2387 return (len >= action->min_len && len <= action->max_len
2389 : -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN));
2392 /* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_*
2393 * constant. The caller must have already validated that 'a' is a valid action
2394 * understood by Open vSwitch (e.g. by a previous successful call to
2395 * ofputil_decode_action()). */
2396 enum ofputil_action_code
2397 ofputil_decode_action_unsafe(const union ofp_action *a)
2399 const struct ofputil_action *action;
2401 if (a->type != htons(OFPAT_VENDOR)) {
2402 action = ofputil_decode_ofpat_action(a);
2404 action = ofputil_decode_nxast_action(a);
2407 return action->code;
2410 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
2411 * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if
2412 * 'name' is not the name of any action.
2414 * ofp-util.def lists the mapping from names to action. */
2416 ofputil_action_code_from_name(const char *name)
2418 static const char *names[OFPUTIL_N_ACTIONS] = {
2419 #define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME,
2420 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
2421 #include "ofp-util.def"
2426 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
2427 if (*p && !strcasecmp(name, *p)) {
2434 /* Appends an action of the type specified by 'code' to 'buf' and returns the
2435 * action. Initializes the parts of 'action' that identify it as having type
2436 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
2437 * have variable length, the length used and cleared is that of struct
2440 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
2443 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2444 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2445 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2446 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
2447 #include "ofp-util.def"
2452 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2454 ofputil_init_##ENUM(struct STRUCT *s) \
2456 memset(s, 0, sizeof *s); \
2457 s->type = htons(ENUM); \
2458 s->len = htons(sizeof *s); \
2462 ofputil_put_##ENUM(struct ofpbuf *buf) \
2464 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2465 ofputil_init_##ENUM(s); \
2468 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2470 ofputil_init_##ENUM(struct STRUCT *s) \
2472 memset(s, 0, sizeof *s); \
2473 s->type = htons(OFPAT_VENDOR); \
2474 s->len = htons(sizeof *s); \
2475 s->vendor = htonl(NX_VENDOR_ID); \
2476 s->subtype = htons(ENUM); \
2480 ofputil_put_##ENUM(struct ofpbuf *buf) \
2482 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2483 ofputil_init_##ENUM(s); \
2486 #include "ofp-util.def"
2488 /* Returns true if 'action' outputs to 'port', false otherwise. */
2490 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2492 switch (ntohs(action->type)) {
2494 return action->output.port == port;
2496 return ((const struct ofp_action_enqueue *) action)->port == port;
2502 /* "Normalizes" the wildcards in 'rule'. That means:
2504 * 1. If the type of level N is known, then only the valid fields for that
2505 * level may be specified. For example, ARP does not have a TOS field,
2506 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
2507 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
2508 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
2511 * 2. If the type of level N is not known (or not understood by Open
2512 * vSwitch), then no fields at all for that level may be specified. For
2513 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
2514 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
2517 * 'flow_format' specifies the format of the flow as received or as intended to
2518 * be sent. This is important for IPv6 and ARP, for which NXM supports more
2519 * detailed matching. */
2521 ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
2524 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
2525 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
2526 MAY_NW_PROTO = 1 << 2, /* nw_proto */
2527 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
2528 MAY_ARP_SHA = 1 << 4, /* arp_sha */
2529 MAY_ARP_THA = 1 << 5, /* arp_tha */
2530 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
2531 MAY_ND_TARGET = 1 << 7 /* nd_target */
2534 struct flow_wildcards wc;
2536 /* Figure out what fields may be matched. */
2537 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2538 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
2539 if (rule->flow.nw_proto == IPPROTO_TCP ||
2540 rule->flow.nw_proto == IPPROTO_UDP ||
2541 rule->flow.nw_proto == IPPROTO_ICMP) {
2542 may_match |= MAY_TP_ADDR;
2544 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2545 && flow_format == NXFF_NXM) {
2546 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
2547 if (rule->flow.nw_proto == IPPROTO_TCP ||
2548 rule->flow.nw_proto == IPPROTO_UDP) {
2549 may_match |= MAY_TP_ADDR;
2550 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
2551 may_match |= MAY_TP_ADDR;
2552 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
2553 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
2554 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
2555 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
2558 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
2559 may_match = MAY_NW_PROTO | MAY_NW_ADDR;
2560 if (flow_format == NXFF_NXM) {
2561 may_match |= MAY_ARP_SHA | MAY_ARP_THA;
2567 /* Clear the fields that may not be matched. */
2569 if (!(may_match & MAY_NW_ADDR)) {
2570 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2572 if (!(may_match & MAY_TP_ADDR)) {
2573 wc.wildcards |= FWW_TP_SRC | FWW_TP_DST;
2575 if (!(may_match & MAY_NW_PROTO)) {
2576 wc.wildcards |= FWW_NW_PROTO;
2578 if (!(may_match & MAY_IPVx)) {
2579 wc.wildcards |= FWW_NW_DSCP;
2580 wc.wildcards |= FWW_NW_ECN;
2581 wc.wildcards |= FWW_NW_TTL;
2583 if (!(may_match & MAY_ARP_SHA)) {
2584 wc.wildcards |= FWW_ARP_SHA;
2586 if (!(may_match & MAY_ARP_THA)) {
2587 wc.wildcards |= FWW_ARP_THA;
2589 if (!(may_match & MAY_IPV6)) {
2590 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2591 wc.wildcards |= FWW_IPV6_LABEL;
2593 if (!(may_match & MAY_ND_TARGET)) {
2594 wc.wildcards |= FWW_ND_TARGET;
2597 /* Log any changes. */
2598 if (!flow_wildcards_equal(&wc, &rule->wc)) {
2599 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
2600 char *pre = log ? cls_rule_to_string(rule) : NULL;
2603 cls_rule_zero_wildcarded_fields(rule);
2606 char *post = cls_rule_to_string(rule);
2607 VLOG_INFO("normalization changed ofp_match, details:");
2608 VLOG_INFO(" pre: %s", pre);
2609 VLOG_INFO("post: %s", post);
2617 vendor_code_to_id(uint8_t code)
2620 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2622 #undef OFPUTIL_VENDOR
2629 vendor_id_to_code(uint32_t id)
2632 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2634 #undef OFPUTIL_VENDOR
2640 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
2641 * information taken from 'error', whose encoding must be as described in the
2642 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
2643 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
2646 * Returns NULL if 'error' is not an OpenFlow error code. */
2648 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2650 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2660 if (!is_ofp_error(error)) {
2661 /* We format 'error' with strerror() here since it seems likely to be
2662 * a system errno value. */
2663 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
2664 error, strerror(error));
2671 len = ntohs(oh->length);
2681 vendor = get_ofp_err_vendor(error);
2682 type = get_ofp_err_type(error);
2683 code = get_ofp_err_code(error);
2684 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
2685 struct ofp_error_msg *oem;
2687 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2688 oem->type = htons(type);
2689 oem->code = htons(code);
2691 struct ofp_error_msg *oem;
2692 struct nx_vendor_error *nve;
2695 vendor_id = vendor_code_to_id(vendor);
2696 if (vendor_id == UINT32_MAX) {
2697 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
2702 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2703 OFPT_ERROR, xid, &buf);
2704 oem->type = htons(NXET_VENDOR);
2705 oem->code = htons(NXVC_VENDOR_ERROR);
2707 nve = (struct nx_vendor_error *)oem->data;
2708 nve->vendor = htonl(vendor_id);
2709 nve->type = htons(type);
2710 nve->code = htons(code);
2715 ofpbuf_put(buf, data, len);
2721 /* Decodes 'oh', which should be an OpenFlow OFPT_ERROR message, and returns an
2722 * Open vSwitch internal error code in the format described in the large
2723 * comment in ofp-util.h.
2725 * If 'payload_ofs' is nonnull, on success '*payload_ofs' is set to the offset
2726 * to the payload starting from 'oh' and on failure it is set to 0. */
2728 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2730 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2732 const struct ofp_error_msg *oem;
2733 uint16_t type, code;
2740 if (oh->type != OFPT_ERROR) {
2744 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2745 oem = ofpbuf_try_pull(&b, sizeof *oem);
2750 type = ntohs(oem->type);
2751 code = ntohs(oem->code);
2752 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
2753 const struct nx_vendor_error *nve = ofpbuf_try_pull(&b, sizeof *nve);
2758 vendor = vendor_id_to_code(ntohl(nve->vendor));
2760 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2761 ntohl(nve->vendor));
2764 type = ntohs(nve->type);
2765 code = ntohs(nve->code);
2767 vendor = OFPUTIL_VENDOR_OPENFLOW;
2771 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2772 "supported maximum value 1023", type);
2777 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2779 return ofp_mkerr_vendor(vendor, type, code);
2783 ofputil_format_error(struct ds *s, int error)
2785 if (is_errno(error)) {
2786 ds_put_cstr(s, strerror(error));
2788 uint16_t type = get_ofp_err_type(error);
2789 uint16_t code = get_ofp_err_code(error);
2790 const char *type_s = ofp_error_type_to_string(type);
2791 const char *code_s = ofp_error_code_to_string(type, code);
2793 ds_put_format(s, "type ");
2795 ds_put_cstr(s, type_s);
2797 ds_put_format(s, "%"PRIu16, type);
2800 ds_put_cstr(s, ", code ");
2802 ds_put_cstr(s, code_s);
2804 ds_put_format(s, "%"PRIu16, code);
2810 ofputil_error_to_string(int error)
2812 struct ds s = DS_EMPTY_INITIALIZER;
2813 ofputil_format_error(&s, error);
2814 return ds_steal_cstr(&s);
2817 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2818 * successful, otherwise an OpenFlow error.
2820 * If successful, the first action is stored in '*actionsp' and the number of
2821 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
2822 * are stored, respectively.
2824 * This function does not check that the actions are valid (the caller should
2825 * do so, with validate_actions()). The caller is also responsible for making
2826 * sure that 'b->data' is initially aligned appropriately for "union
2829 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2830 union ofp_action **actionsp, size_t *n_actionsp)
2832 if (actions_len % OFP_ACTION_ALIGN != 0) {
2833 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2834 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2838 *actionsp = ofpbuf_try_pull(b, actions_len);
2839 if (*actionsp == NULL) {
2840 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2841 "exceeds remaining message length (%zu)",
2842 actions_len, b->size);
2846 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2852 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2856 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
2857 const union ofp_action *b, size_t n_b)
2859 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
2863 ofputil_actions_clone(const union ofp_action *actions, size_t n)
2865 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
2868 /* Parses a key or a key-value pair from '*stringp'.
2870 * On success: Stores the key into '*keyp'. Stores the value, if present, into
2871 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
2872 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
2873 * are substrings of '*stringp' created by replacing some of its bytes by null
2874 * terminators. Returns true.
2876 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
2877 * NULL and returns false. */
2879 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
2881 char *pos, *key, *value;
2885 pos += strspn(pos, ", \t\r\n");
2887 *keyp = *valuep = NULL;
2892 key_len = strcspn(pos, ":=(, \t\r\n");
2893 if (key[key_len] == ':' || key[key_len] == '=') {
2894 /* The value can be separated by a colon. */
2897 value = key + key_len + 1;
2898 value_len = strcspn(value, ", \t\r\n");
2899 pos = value + value_len + (value[value_len] != '\0');
2900 value[value_len] = '\0';
2901 } else if (key[key_len] == '(') {
2902 /* The value can be surrounded by balanced parentheses. The outermost
2903 * set of parentheses is removed. */
2907 value = key + key_len + 1;
2908 for (value_len = 0; level > 0; value_len++) {
2909 switch (value[value_len]) {
2911 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
2922 value[value_len - 1] = '\0';
2923 pos = value + value_len;
2925 /* There might be no value at all. */
2926 value = key + key_len; /* Will become the empty string below. */
2927 pos = key + key_len + (key[key_len] != '\0');
2929 key[key_len] = '\0';