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 == 3);
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_ND_TARGET);
113 if (!(ofpfw & OFPFW_NW_TOS)) {
114 wc->tos_frag_mask |= IP_DSCP_MASK;
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);
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;
128 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
129 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
131 if (!(ofpfw & OFPFW_DL_VLAN)) {
132 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
136 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
139 ofputil_cls_rule_from_match(const struct ofp_match *match,
140 unsigned int priority, struct cls_rule *rule)
142 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
144 /* Initialize rule->priority, rule->wc. */
145 rule->priority = !ofpfw ? UINT16_MAX : priority;
146 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
148 /* Initialize most of rule->flow. */
149 rule->flow.nw_src = match->nw_src;
150 rule->flow.nw_dst = match->nw_dst;
151 rule->flow.in_port = ntohs(match->in_port);
152 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
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);
157 rule->flow.tos_frag = match->nw_tos & IP_DSCP_MASK;
158 rule->flow.nw_proto = match->nw_proto;
160 /* Translate VLANs. */
161 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
162 /* Match only packets without 802.1Q header.
164 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
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. */
170 rule->flow.vlan_tci = htons(0);
171 rule->wc.vlan_tci_mask = htons(0xffff);
173 ovs_be16 vid, pcp, tci;
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);
178 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
182 cls_rule_zero_wildcarded_fields(rule);
185 /* Convert 'rule' into the OpenFlow match structure 'match'. */
187 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
189 const struct flow_wildcards *wc = &rule->wc;
192 /* Figure out most OpenFlow wildcards. */
193 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
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;
196 if (!(wc->tos_frag_mask & IP_DSCP_MASK)) {
197 ofpfw |= OFPFW_NW_TOS;
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);
209 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
210 ofpfw |= OFPFW_DL_VLAN;
212 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
215 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
216 ofpfw |= OFPFW_DL_VLAN_PCP;
218 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
222 /* Compose most of the match structure. */
223 match->wildcards = htonl(ofpfw);
224 match->in_port = htons(rule->flow.in_port);
225 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
226 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
227 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
228 match->nw_src = rule->flow.nw_src;
229 match->nw_dst = rule->flow.nw_dst;
230 match->nw_tos = rule->flow.tos_frag & IP_DSCP_MASK;
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);
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. */
241 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
243 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
244 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
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
252 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
254 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
255 ? htons(FLOW_DL_TYPE_NONE)
259 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
263 static uint32_t next_xid = 1;
264 return htonl(next_xid++);
267 /* Basic parsing of OpenFlow messages. */
269 struct 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;
279 /* Represents a malformed OpenFlow message. */
280 static const struct ofputil_msg_type ofputil_invalid_type = {
281 OFPUTIL_MSG_INVALID, 0, "OFPUTIL_MSG_INVALID", 0, 0
284 struct ofputil_msg_category {
285 const char *name; /* e.g. "OpenFlow message" */
286 const struct ofputil_msg_type *types;
288 int missing_error; /* ofp_mkerr() value for missing type. */
292 ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
294 switch (type->extra_multiple) {
296 if (size != type->min_size) {
297 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
298 "length %u (expected length %u)",
299 type->name, size, type->min_size);
300 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
305 if (size < type->min_size) {
306 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
307 "length %u (expected length at least %u bytes)",
308 type->name, size, type->min_size);
309 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
314 if (size < type->min_size
315 || (size - type->min_size) % type->extra_multiple) {
316 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
317 "length %u (must be exactly %u bytes or longer "
318 "by an integer multiple of %u bytes)",
320 type->min_size, type->extra_multiple);
321 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
328 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
330 const struct ofputil_msg_type **typep)
332 const struct ofputil_msg_type *type;
334 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
335 if (type->value == value) {
341 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
343 return cat->missing_error;
347 ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
348 const struct ofputil_msg_type **typep)
350 static const struct ofputil_msg_type nxt_messages[] = {
351 { OFPUTIL_NXT_ROLE_REQUEST,
352 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
353 sizeof(struct nx_role_request), 0 },
355 { OFPUTIL_NXT_ROLE_REPLY,
356 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
357 sizeof(struct nx_role_request), 0 },
359 { OFPUTIL_NXT_SET_FLOW_FORMAT,
360 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
361 sizeof(struct nxt_set_flow_format), 0 },
363 { OFPUTIL_NXT_FLOW_MOD,
364 NXT_FLOW_MOD, "NXT_FLOW_MOD",
365 sizeof(struct nx_flow_mod), 8 },
367 { OFPUTIL_NXT_FLOW_REMOVED,
368 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
369 sizeof(struct nx_flow_removed), 8 },
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 },
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)
382 const struct ofp_vendor_header *ovh;
383 const struct nicira_header *nh;
385 if (length < sizeof(struct ofp_vendor_header)) {
386 if (length == ntohs(oh->length)) {
387 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
389 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
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);
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));
406 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
409 nh = (const struct nicira_header *) oh;
410 return ofputil_lookup_openflow_message(&nxt_category, ntohl(nh->subtype),
415 check_nxstats_msg(const struct ofp_header *oh, size_t length)
417 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
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");
424 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
427 memcpy(&vendor, osm + 1, sizeof vendor);
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);
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");
438 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
445 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
446 const struct ofputil_msg_type **typep)
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 },
453 { OFPUTIL_NXST_AGGREGATE_REQUEST,
454 NXST_AGGREGATE, "NXST_AGGREGATE request",
455 sizeof(struct nx_aggregate_stats_request), 8 },
458 static const struct ofputil_msg_category nxst_request_category = {
459 "Nicira extension statistics request",
460 nxst_requests, ARRAY_SIZE(nxst_requests),
461 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
464 const struct nicira_stats_msg *nsm;
467 error = check_nxstats_msg(oh, length);
472 nsm = (struct nicira_stats_msg *) oh;
473 return ofputil_lookup_openflow_message(&nxst_request_category,
474 ntohl(nsm->subtype), typep);
478 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
479 const struct ofputil_msg_type **typep)
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 },
486 { OFPUTIL_NXST_AGGREGATE_REPLY,
487 NXST_AGGREGATE, "NXST_AGGREGATE reply",
488 sizeof(struct nx_aggregate_stats_reply), 0 },
491 static const struct ofputil_msg_category nxst_reply_category = {
492 "Nicira extension statistics reply",
493 nxst_replies, ARRAY_SIZE(nxst_replies),
494 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE)
497 const struct nicira_stats_msg *nsm;
500 error = check_nxstats_msg(oh, length);
505 nsm = (struct nicira_stats_msg *) oh;
506 return ofputil_lookup_openflow_message(&nxst_reply_category,
507 ntohl(nsm->subtype), typep);
511 check_stats_msg(const struct ofp_header *oh, size_t length)
513 if (length < sizeof(struct ofp_stats_msg)) {
514 if (length == ntohs(oh->length)) {
515 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
517 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
524 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
525 const struct ofputil_msg_type **typep)
527 static const struct ofputil_msg_type ofpst_requests[] = {
528 { OFPUTIL_OFPST_DESC_REQUEST,
529 OFPST_DESC, "OFPST_DESC request",
530 sizeof(struct ofp_stats_msg), 0 },
532 { OFPUTIL_OFPST_FLOW_REQUEST,
533 OFPST_FLOW, "OFPST_FLOW request",
534 sizeof(struct ofp_flow_stats_request), 0 },
536 { OFPUTIL_OFPST_AGGREGATE_REQUEST,
537 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
538 sizeof(struct ofp_flow_stats_request), 0 },
540 { OFPUTIL_OFPST_TABLE_REQUEST,
541 OFPST_TABLE, "OFPST_TABLE request",
542 sizeof(struct ofp_stats_msg), 0 },
544 { OFPUTIL_OFPST_PORT_REQUEST,
545 OFPST_PORT, "OFPST_PORT request",
546 sizeof(struct ofp_port_stats_request), 0 },
548 { OFPUTIL_OFPST_QUEUE_REQUEST,
549 OFPST_QUEUE, "OFPST_QUEUE request",
550 sizeof(struct ofp_queue_stats_request), 0 },
553 OFPST_VENDOR, "OFPST_VENDOR request",
554 sizeof(struct ofp_vendor_stats_msg), 1 },
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)
563 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
566 error = check_stats_msg(oh, length);
571 error = ofputil_lookup_openflow_message(&ofpst_request_category,
572 ntohs(request->type), typep);
573 if (!error && request->type == htons(OFPST_VENDOR)) {
574 error = ofputil_decode_nxst_request(oh, length, typep);
580 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
581 const struct ofputil_msg_type **typep)
583 static const struct ofputil_msg_type ofpst_replies[] = {
584 { OFPUTIL_OFPST_DESC_REPLY,
585 OFPST_DESC, "OFPST_DESC reply",
586 sizeof(struct ofp_desc_stats), 0 },
588 { OFPUTIL_OFPST_FLOW_REPLY,
589 OFPST_FLOW, "OFPST_FLOW reply",
590 sizeof(struct ofp_stats_msg), 1 },
592 { OFPUTIL_OFPST_AGGREGATE_REPLY,
593 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
594 sizeof(struct ofp_aggregate_stats_reply), 0 },
596 { OFPUTIL_OFPST_TABLE_REPLY,
597 OFPST_TABLE, "OFPST_TABLE reply",
598 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
600 { OFPUTIL_OFPST_PORT_REPLY,
601 OFPST_PORT, "OFPST_PORT reply",
602 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
604 { OFPUTIL_OFPST_QUEUE_REPLY,
605 OFPST_QUEUE, "OFPST_QUEUE reply",
606 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
609 OFPST_VENDOR, "OFPST_VENDOR reply",
610 sizeof(struct ofp_vendor_stats_msg), 1 },
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)
619 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
622 error = check_stats_msg(oh, length);
627 error = ofputil_lookup_openflow_message(&ofpst_reply_category,
628 ntohs(reply->type), typep);
629 if (!error && reply->type == htons(OFPST_VENDOR)) {
630 error = ofputil_decode_nxst_reply(oh, length, typep);
636 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
637 const struct ofputil_msg_type **typep)
639 static const struct ofputil_msg_type ofpt_messages[] = {
640 { OFPUTIL_OFPT_HELLO,
641 OFPT_HELLO, "OFPT_HELLO",
642 sizeof(struct ofp_hello), 1 },
644 { OFPUTIL_OFPT_ERROR,
645 OFPT_ERROR, "OFPT_ERROR",
646 sizeof(struct ofp_error_msg), 1 },
648 { OFPUTIL_OFPT_ECHO_REQUEST,
649 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
650 sizeof(struct ofp_header), 1 },
652 { OFPUTIL_OFPT_ECHO_REPLY,
653 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
654 sizeof(struct ofp_header), 1 },
656 { OFPUTIL_OFPT_FEATURES_REQUEST,
657 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
658 sizeof(struct ofp_header), 0 },
660 { OFPUTIL_OFPT_FEATURES_REPLY,
661 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
662 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
664 { OFPUTIL_OFPT_GET_CONFIG_REQUEST,
665 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
666 sizeof(struct ofp_header), 0 },
668 { OFPUTIL_OFPT_GET_CONFIG_REPLY,
669 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
670 sizeof(struct ofp_switch_config), 0 },
672 { OFPUTIL_OFPT_SET_CONFIG,
673 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
674 sizeof(struct ofp_switch_config), 0 },
676 { OFPUTIL_OFPT_PACKET_IN,
677 OFPT_PACKET_IN, "OFPT_PACKET_IN",
678 offsetof(struct ofp_packet_in, data), 1 },
680 { OFPUTIL_OFPT_FLOW_REMOVED,
681 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
682 sizeof(struct ofp_flow_removed), 0 },
684 { OFPUTIL_OFPT_PORT_STATUS,
685 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
686 sizeof(struct ofp_port_status), 0 },
688 { OFPUTIL_OFPT_PACKET_OUT,
689 OFPT_PACKET_OUT, "OFPT_PACKET_OUT",
690 sizeof(struct ofp_packet_out), 1 },
692 { OFPUTIL_OFPT_FLOW_MOD,
693 OFPT_FLOW_MOD, "OFPT_FLOW_MOD",
694 sizeof(struct ofp_flow_mod), 1 },
696 { OFPUTIL_OFPT_PORT_MOD,
697 OFPT_PORT_MOD, "OFPT_PORT_MOD",
698 sizeof(struct ofp_port_mod), 0 },
701 OFPT_STATS_REQUEST, "OFPT_STATS_REQUEST",
702 sizeof(struct ofp_stats_msg), 1 },
705 OFPT_STATS_REPLY, "OFPT_STATS_REPLY",
706 sizeof(struct ofp_stats_msg), 1 },
708 { OFPUTIL_OFPT_BARRIER_REQUEST,
709 OFPT_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
710 sizeof(struct ofp_header), 0 },
712 { OFPUTIL_OFPT_BARRIER_REPLY,
713 OFPT_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
714 sizeof(struct ofp_header), 0 },
717 OFPT_VENDOR, "OFPT_VENDOR",
718 sizeof(struct ofp_vendor_header), 1 },
721 static const struct ofputil_msg_category ofpt_category = {
723 ofpt_messages, ARRAY_SIZE(ofpt_messages),
724 OFP_MKERR(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE)
729 error = ofputil_lookup_openflow_message(&ofpt_category, oh->type, typep);
733 error = ofputil_decode_vendor(oh, length, typep);
736 case OFPT_STATS_REQUEST:
737 error = ofputil_decode_ofpst_request(oh, length, typep);
740 case OFPT_STATS_REPLY:
741 error = ofputil_decode_ofpst_reply(oh, length, typep);
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.
755 * oh->length must indicate the correct length of the message (and must be at
756 * least sizeof(struct ofp_header)).
758 * Success indicates that 'oh' is at least as long as the minimum-length
759 * message of its type. */
761 ofputil_decode_msg_type(const struct ofp_header *oh,
762 const struct ofputil_msg_type **typep)
764 size_t length = ntohs(oh->length);
767 error = ofputil_decode_msg_type__(oh, length, typep);
769 error = ofputil_check_length(*typep, length);
772 *typep = &ofputil_invalid_type;
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. */
783 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
784 const struct ofputil_msg_type **typep)
788 error = (length >= sizeof *oh
789 ? ofputil_decode_msg_type__(oh, length, typep)
790 : ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN));
792 *typep = &ofputil_invalid_type;
797 /* Returns an OFPUTIL_* message type code for 'type'. */
798 enum ofputil_msg_code
799 ofputil_msg_type_code(const struct ofputil_msg_type *type)
807 ofputil_flow_format_is_valid(enum nx_flow_format flow_format)
809 switch (flow_format) {
810 case NXFF_OPENFLOW10:
819 ofputil_flow_format_to_string(enum nx_flow_format flow_format)
821 switch (flow_format) {
822 case NXFF_OPENFLOW10:
832 ofputil_flow_format_from_string(const char *s)
834 return (!strcmp(s, "openflow10") ? NXFF_OPENFLOW10
835 : !strcmp(s, "nxm") ? NXFF_NXM
840 regs_fully_wildcarded(const struct flow_wildcards *wc)
844 for (i = 0; i < FLOW_N_REGS; i++) {
845 if (wc->reg_masks[i] != 0) {
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. */
857 ofputil_min_flow_format(const struct cls_rule *rule)
859 const struct flow_wildcards *wc = &rule->wc;
861 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 3);
863 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
864 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
868 /* Only NXM supports matching ARP hardware addresses. */
869 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
873 /* Only NXM supports matching IPv6 traffic. */
874 if (!(wc->wildcards & FWW_DL_TYPE)
875 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
879 /* Only NXM supports matching registers. */
880 if (!regs_fully_wildcarded(wc)) {
884 /* Only NXM supports matching tun_id. */
885 if (wc->tun_id_mask != htonll(0)) {
889 /* Only NXM supports matching fragments. */
890 if (wc->tos_frag_mask & FLOW_FRAG_MASK) {
894 /* Other formats can express this rule. */
895 return NXFF_OPENFLOW10;
898 /* Returns an OpenFlow message that can be used to set the flow format to
901 ofputil_make_set_flow_format(enum nx_flow_format flow_format)
903 struct nxt_set_flow_format *sff;
906 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
907 sff->format = htonl(flow_format);
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'). */
915 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
917 struct nxt_flow_mod_table_id *nfmti;
920 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
921 nfmti->set = flow_mod_table_id;
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
929 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
930 * enabled, false otherwise.
932 * Does not validate the flow_mod actions. */
934 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
935 const struct ofp_header *oh, bool flow_mod_table_id)
937 const struct ofputil_msg_type *type;
941 ofpbuf_use_const(&b, oh, ntohs(oh->length));
943 ofputil_decode_msg_type(oh, &type);
944 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
945 /* Standard OpenFlow flow_mod. */
946 const struct ofp_flow_mod *ofm;
950 /* Dissect the message. */
951 ofm = ofpbuf_pull(&b, sizeof *ofm);
952 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
957 /* Set priority based on original wildcards. Normally we'd allow
958 * ofputil_cls_rule_from_match() to do this for us, but
959 * ofputil_normalize_rule() can put wildcards where the original flow
960 * didn't have them. */
961 priority = ntohs(ofm->priority);
962 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
963 priority = UINT16_MAX;
966 /* Translate the rule. */
967 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
968 ofputil_normalize_rule(&fm->cr, NXFF_OPENFLOW10);
970 /* Translate the message. */
971 fm->cookie = ofm->cookie;
972 command = ntohs(ofm->command);
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;
983 /* Dissect the message. */
984 nfm = ofpbuf_pull(&b, sizeof *nfm);
985 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
990 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
995 /* Translate the message. */
996 fm->cookie = nfm->cookie;
997 command = ntohs(nfm->command);
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);
1007 if (flow_mod_table_id) {
1008 fm->command = command & 0xff;
1009 fm->table_id = command >> 8;
1011 fm->command = command;
1012 fm->table_id = 0xff;
1018 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1019 * 'flow_format' and returns the message.
1021 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1022 * enabled, false otherwise. */
1024 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1025 enum nx_flow_format flow_format,
1026 bool flow_mod_table_id)
1028 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1032 command = (flow_mod_table_id
1033 ? (fm->command & 0xff) | (fm->table_id << 8)
1036 if (flow_format == NXFF_OPENFLOW10) {
1037 struct ofp_flow_mod *ofm;
1039 msg = ofpbuf_new(sizeof *ofm + actions_len);
1040 ofm = put_openflow(sizeof *ofm, OFPT_FLOW_MOD, msg);
1041 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1042 ofm->cookie = fm->cookie;
1043 ofm->command = htons(command);
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;
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);
1059 nfm->cookie = fm->cookie;
1060 nfm->command = htons(command);
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);
1072 ofpbuf_put(msg, fm->actions, actions_len);
1073 update_openflow_length(msg);
1078 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1079 const struct ofp_header *oh,
1082 const struct ofp_flow_stats_request *ofsr =
1083 (const struct ofp_flow_stats_request *) oh;
1085 fsr->aggregate = aggregate;
1086 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1087 fsr->out_port = ntohs(ofsr->out_port);
1088 fsr->table_id = ofsr->table_id;
1094 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1095 const struct ofp_header *oh,
1098 const struct nx_flow_stats_request *nfsr;
1102 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1104 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1105 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match);
1110 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1113 fsr->aggregate = aggregate;
1114 fsr->out_port = ntohs(nfsr->out_port);
1115 fsr->table_id = nfsr->table_id;
1120 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1121 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1122 * successful, otherwise an OpenFlow error code. */
1124 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1125 const struct ofp_header *oh)
1127 const struct ofputil_msg_type *type;
1131 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1133 ofputil_decode_msg_type(oh, &type);
1134 code = ofputil_msg_type_code(type);
1136 case OFPUTIL_OFPST_FLOW_REQUEST:
1137 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1139 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1140 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1142 case OFPUTIL_NXST_FLOW_REQUEST:
1143 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1145 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1146 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1149 /* Hey, the caller lied. */
1154 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1155 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1156 * 'flow_format', and returns the message. */
1158 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1159 enum nx_flow_format flow_format)
1163 if (flow_format == NXFF_OPENFLOW10) {
1164 struct ofp_flow_stats_request *ofsr;
1167 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1168 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1169 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
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;
1177 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1178 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1179 match_len = nx_put_match(msg, &fsr->match);
1182 nfsr->out_port = htons(fsr->out_port);
1183 nfsr->match_len = htons(match_len);
1184 nfsr->table_id = fsr->table_id;
1192 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1193 * ofputil_flow_stats in 'fs'.
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.
1200 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1201 * otherwise a positive errno value. */
1203 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1206 const struct ofputil_msg_type *type;
1209 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1210 code = ofputil_msg_type_code(type);
1212 msg->l2 = msg->data;
1213 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1214 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1215 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1216 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1224 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1225 const struct ofp_flow_stats *ofs;
1228 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1230 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1231 "bytes at end", msg->size);
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);
1242 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1243 &fs->actions, &fs->n_actions)) {
1247 fs->cookie = get_32aligned_be64(&ofs->cookie);
1248 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
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;
1261 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1263 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1264 "bytes at end", msg->size);
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);
1275 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule)) {
1279 if (ofputil_pull_actions(msg,
1280 length - sizeof *nfs - ROUND_UP(match_len, 8),
1281 &fs->actions, &fs->n_actions)) {
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);
1300 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
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. */
1305 unknown_to_zero(uint64_t count)
1307 return count != UINT64_MAX ? count : 0;
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(). */
1314 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1315 struct list *replies)
1317 size_t act_len = fs->n_actions * sizeof *fs->actions;
1318 const struct ofp_stats_msg *osm;
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;
1325 ofs = ofputil_append_stats_reply(len, replies);
1326 ofs->length = htons(len);
1327 ofs->table_id = fs->table_id;
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);
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)));
1341 memcpy(ofs->actions, fs->actions, act_len);
1342 } else if (osm->type == htons(OFPST_VENDOR)) {
1343 struct nx_flow_stats *nfs;
1347 msg = ofputil_reserve_stats_reply(
1348 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1349 start_len = msg->size;
1351 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1352 nfs->table_id = fs->table_id;
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);
1371 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1372 * NXST_AGGREGATE reply according to 'flow_format', and returns the message. */
1374 ofputil_encode_aggregate_stats_reply(
1375 const struct ofputil_aggregate_stats *stats,
1376 const struct ofp_stats_msg *request)
1380 if (request->type == htons(OFPST_AGGREGATE)) {
1381 struct ofp_aggregate_stats_reply *asr;
1383 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
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)));
1388 asr->flow_count = htonl(stats->flow_count);
1389 } else if (request->type == htons(OFPST_VENDOR)) {
1390 struct nx_aggregate_stats_reply *nasr;
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);
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. */
1408 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1409 const struct ofp_header *oh)
1411 const struct ofputil_msg_type *type;
1412 enum ofputil_msg_code code;
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;
1419 ofr = (const struct ofp_flow_removed *) oh;
1420 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
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;
1434 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1436 nfr = ofpbuf_pull(&b, sizeof *nfr);
1437 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1443 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
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);
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
1464 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1465 enum nx_flow_format flow_format)
1469 if (flow_format == NXFF_OPENFLOW10) {
1470 struct ofp_flow_removed *ofr;
1472 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1474 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1475 ofr->cookie = fr->cookie;
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);
1481 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1482 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1483 } else if (flow_format == NXFF_NXM) {
1484 struct nx_flow_removed *nfr;
1487 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1488 match_len = nx_put_match(msg, &fr->rule);
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);
1507 /* Converts abstract ofputil_packet_in 'pin' into an OFPT_PACKET_IN message
1508 * and returns the message.
1510 * If 'rw_packet' is NULL, the caller takes ownership of the newly allocated
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
1521 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1522 struct ofpbuf *rw_packet)
1524 int total_len = pin->packet->size;
1525 struct ofp_packet_in opi;
1528 if (pin->send_len < rw_packet->size) {
1529 rw_packet->size = pin->send_len;
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));
1537 /* Add OFPT_PACKET_IN. */
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));
1546 update_openflow_length(rw_packet);
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". */
1556 ofputil_msg_type_name(const struct ofputil_msg_type *type)
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
1566 * The caller is responsible for freeing '*bufferp' when it is no longer
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.
1573 * Returns the header. */
1575 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
1577 *bufferp = ofpbuf_new(openflow_len);
1578 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
1581 /* Similar to make_openflow() but creates a Nicira vendor extension message
1582 * with the specific 'subtype'. 'subtype' should be in host byte order. */
1584 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
1586 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
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
1594 * The caller is responsible for freeing '*bufferp' when it is no longer
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.
1601 * Returns the header. */
1603 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1604 struct ofpbuf **bufferp)
1606 *bufferp = ofpbuf_new(openflow_len);
1607 return put_openflow_xid(openflow_len, type, xid, *bufferp);
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. */
1613 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1614 struct ofpbuf **bufferp)
1616 *bufferp = ofpbuf_new(openflow_len);
1617 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
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.
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.
1628 * Returns the header. */
1630 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
1632 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
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.
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.
1643 * Returns the header. */
1645 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
1646 struct ofpbuf *buffer)
1648 struct ofp_header *oh;
1650 assert(openflow_len >= sizeof *oh);
1651 assert(openflow_len <= UINT16_MAX);
1653 oh = ofpbuf_put_uninit(buffer, openflow_len);
1654 oh->version = OFP_VERSION;
1656 oh->length = htons(openflow_len);
1658 memset(oh + 1, 0, openflow_len - sizeof *oh);
1662 /* Similar to put_openflow() but append a Nicira vendor extension message with
1663 * the specific 'subtype'. 'subtype' should be in host byte order. */
1665 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
1667 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
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. */
1673 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
1674 struct ofpbuf *buffer)
1676 struct nicira_header *nxh;
1678 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
1679 nxh->vendor = htonl(NX_VENDOR_ID);
1680 nxh->subtype = htonl(subtype);
1684 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
1685 * 'buffer->size'. */
1687 update_openflow_length(struct ofpbuf *buffer)
1689 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
1690 oh->length = htons(buffer->size);
1694 put_stats__(ovs_be32 xid, uint8_t ofp_type,
1695 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
1698 if (ofpst_type == htons(OFPST_VENDOR)) {
1699 struct nicira_stats_msg *nsm;
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;
1706 struct ofp_stats_msg *osm;
1708 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
1709 osm->type = ofpst_type;
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).
1719 * Initializes bytes following the headers to all-bits-zero.
1721 * Returns the first byte of the new message. */
1723 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
1724 uint32_t nxst_subtype, struct ofpbuf **bufferp)
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);
1737 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
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)
1744 : ((const struct nicira_stats_msg *) request)->subtype),
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'.
1753 * Initializes bytes following the headers to all-bits-zero.
1755 * Returns the first byte of the new message. */
1757 ofputil_make_stats_reply(size_t openflow_len,
1758 const struct ofp_stats_msg *request,
1759 struct ofpbuf **bufferp)
1763 msg = *bufferp = ofpbuf_new(openflow_len);
1764 put_stats_reply__(request, msg);
1765 ofpbuf_padto(msg, openflow_len);
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. */
1776 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
1777 struct list *replies)
1781 msg = ofpbuf_new(1024);
1782 put_stats_reply__(request, msg);
1785 list_push_back(replies, &msg->list_node);
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().) */
1794 ofputil_reserve_stats_reply(size_t len, struct list *replies)
1796 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1797 struct ofp_stats_msg *osm = msg->data;
1799 if (msg->size + len <= UINT16_MAX) {
1800 ofpbuf_prealloc_tailroom(msg, len);
1802 osm->flags |= htons(OFPSF_REPLY_MORE);
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);
1811 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
1812 * returns the first byte. */
1814 ofputil_append_stats_reply(size_t len, struct list *replies)
1816 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
1819 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
1821 ofputil_stats_body(const struct ofp_header *oh)
1823 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1824 return (const struct ofp_stats_msg *) oh + 1;
1827 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
1829 ofputil_stats_body_len(const struct ofp_header *oh)
1831 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1832 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
1835 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
1837 ofputil_nxstats_body(const struct ofp_header *oh)
1839 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1840 return ((const struct nicira_stats_msg *) oh) + 1;
1843 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
1845 ofputil_nxstats_body_len(const struct ofp_header *oh)
1847 assert(oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY);
1848 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
1852 make_flow_mod(uint16_t command, const struct cls_rule *rule,
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);
1863 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
1864 ofputil_cls_rule_to_match(rule, &ofm->match);
1865 ofm->command = htons(command);
1870 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
1871 uint16_t idle_timeout, size_t actions_len)
1873 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
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);
1882 make_del_flow(const struct cls_rule *rule)
1884 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
1885 struct ofp_flow_mod *ofm = out->data;
1886 ofm->out_port = htons(OFPP_NONE);
1891 make_add_simple_flow(const struct cls_rule *rule,
1892 uint32_t buffer_id, uint16_t out_port,
1893 uint16_t idle_timeout)
1895 if (out_port != OFPP_NONE) {
1896 struct ofp_action_output *oao;
1897 struct ofpbuf *buffer;
1899 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
1900 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
1903 return make_add_flow(rule, buffer_id, idle_timeout, 0);
1908 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1909 const struct ofpbuf *payload, int max_send_len)
1911 struct ofp_packet_in *opi;
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);
1930 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1932 const struct ofp_action_header *actions, size_t n_actions)
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);
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);
1945 opo->in_port = htons(in_port);
1946 opo->actions_len = htons(actions_len);
1947 ofpbuf_put(out, actions, actions_len);
1949 ofpbuf_put(out, packet->data, packet->size);
1955 make_unbuffered_packet_out(const struct ofpbuf *packet,
1956 uint16_t in_port, uint16_t out_port)
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);
1967 make_buffered_packet_out(uint32_t buffer_id,
1968 uint16_t in_port, uint16_t out_port)
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);
1978 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
1982 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1984 make_echo_request(void)
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);
1996 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1997 * OFPT_ECHO_REQUEST message in 'rq'. */
1999 make_echo_reply(const struct ofp_header *rq)
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;
2009 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
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";
2022 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
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;
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. */
2042 ofputil_check_output_port(uint16_t port, int max_ports)
2050 case OFPP_CONTROLLER:
2055 if (port < max_ports) {
2058 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
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)
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. */
2076 ofputil_port_from_string(const char *name, uint16_t *port)
2082 static const struct pair pairs[] = {
2083 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2085 #undef OFPUTIL_NAMED_PORT
2087 static const int n_pairs = ARRAY_SIZE(pairs);
2090 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2095 for (i = 0; i < n_pairs; i++) {
2096 if (!strcasecmp(name, pairs[i].name)) {
2097 *port = pairs[i].value;
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". */
2108 ofputil_format_port(uint16_t port, struct ds *s)
2113 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2115 #undef OFPUTIL_NAMED_PORT
2118 ds_put_format(s, "%"PRIu16, port);
2121 ds_put_cstr(s, name);
2125 check_resubmit_table(const struct nx_action_resubmit *nar)
2127 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2128 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2134 check_output_reg(const struct nx_action_output_reg *naor,
2135 const struct flow *flow)
2139 for (i = 0; i < sizeof naor->zero; i++) {
2140 if (naor->zero[i]) {
2141 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
2145 return nxm_src_check(naor->src, nxm_decode_ofs(naor->ofs_nbits),
2146 nxm_decode_n_bits(naor->ofs_nbits), flow);
2150 validate_actions(const union ofp_action *actions, size_t n_actions,
2151 const struct flow *flow, int max_ports)
2153 const union ofp_action *a;
2156 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2161 code = ofputil_decode_action(a);
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);
2176 switch ((enum ofputil_action_code) code) {
2177 case OFPUTIL_OFPAT_OUTPUT:
2178 error = ofputil_check_output_port(ntohs(a->output.port),
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);
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);
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);
2201 case OFPUTIL_NXAST_REG_MOVE:
2202 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2206 case OFPUTIL_NXAST_REG_LOAD:
2207 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2211 case OFPUTIL_NXAST_MULTIPATH:
2212 error = multipath_check((const struct nx_action_multipath *) a,
2216 case OFPUTIL_NXAST_AUTOPATH:
2217 error = autopath_check((const struct nx_action_autopath *) a,
2221 case OFPUTIL_NXAST_BUNDLE:
2222 case OFPUTIL_NXAST_BUNDLE_LOAD:
2223 error = bundle_check((const struct nx_action_bundle *) a,
2227 case OFPUTIL_NXAST_OUTPUT_REG:
2228 error = check_output_reg((const struct nx_action_output_reg *) a,
2232 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2233 error = check_resubmit_table(
2234 (const struct nx_action_resubmit *) a);
2237 case OFPUTIL_NXAST_LEARN:
2238 error = learn_check((const struct nx_action_learn *) a, flow);
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:
2255 case OFPUTIL_NXAST_EXIT:
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);
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);
2275 struct ofputil_action {
2277 unsigned int min_len;
2278 unsigned int max_len;
2281 static const struct ofputil_action action_bad_type
2282 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE), 0, UINT_MAX };
2283 static const struct ofputil_action action_bad_len
2284 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_LEN), 0, UINT_MAX };
2285 static const struct ofputil_action action_bad_vendor
2286 = { -OFP_MKERR(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR), 0, UINT_MAX };
2288 static const struct ofputil_action *
2289 ofputil_decode_ofpat_action(const union ofp_action *a)
2291 enum ofp_action_type type = ntohs(a->type);
2294 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2296 static const struct ofputil_action action = { \
2298 sizeof(struct STRUCT), \
2299 sizeof(struct STRUCT) \
2303 #include "ofp-util.def"
2307 return &action_bad_type;
2311 static const struct ofputil_action *
2312 ofputil_decode_nxast_action(const union ofp_action *a)
2314 const struct nx_action_header *nah = (const struct nx_action_header *) a;
2315 enum nx_action_subtype subtype = ntohs(nah->subtype);
2318 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2320 static const struct ofputil_action action = { \
2322 sizeof(struct STRUCT), \
2323 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
2327 #include "ofp-util.def"
2329 case NXAST_SNAT__OBSOLETE:
2330 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
2332 return &action_bad_type;
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()).
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').
2344 * This function verifies that 'a''s length is correct for the type of action
2345 * that it represents. */
2347 ofputil_decode_action(const union ofp_action *a)
2349 const struct ofputil_action *action;
2350 uint16_t len = ntohs(a->header.len);
2352 if (a->type != htons(OFPAT_VENDOR)) {
2353 action = ofputil_decode_ofpat_action(a);
2355 switch (ntohl(a->vendor.vendor)) {
2357 if (len < sizeof(struct nx_action_header)) {
2358 return -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
2360 action = ofputil_decode_nxast_action(a);
2363 action = &action_bad_vendor;
2368 return (len >= action->min_len && len <= action->max_len
2370 : -ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN));
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()). */
2377 enum ofputil_action_code
2378 ofputil_decode_action_unsafe(const union ofp_action *a)
2380 const struct ofputil_action *action;
2382 if (a->type != htons(OFPAT_VENDOR)) {
2383 action = ofputil_decode_ofpat_action(a);
2385 action = ofputil_decode_nxast_action(a);
2388 return action->code;
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.
2395 * ofp-util.def lists the mapping from names to action. */
2397 ofputil_action_code_from_name(const char *name)
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"
2407 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
2408 if (*p && !strcasecmp(name, *p)) {
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
2421 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
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"
2433 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2435 ofputil_init_##ENUM(struct STRUCT *s) \
2437 memset(s, 0, sizeof *s); \
2438 s->type = htons(ENUM); \
2439 s->len = htons(sizeof *s); \
2443 ofputil_put_##ENUM(struct ofpbuf *buf) \
2445 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2446 ofputil_init_##ENUM(s); \
2449 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2451 ofputil_init_##ENUM(struct STRUCT *s) \
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); \
2461 ofputil_put_##ENUM(struct ofpbuf *buf) \
2463 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
2464 ofputil_init_##ENUM(s); \
2467 #include "ofp-util.def"
2469 /* Returns true if 'action' outputs to 'port', false otherwise. */
2471 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
2473 switch (ntohs(action->type)) {
2475 return action->output.port == port;
2477 return ((const struct ofp_action_enqueue *) action)->port == port;
2483 /* "Normalizes" the wildcards in 'rule'. That means:
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
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
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. */
2502 ofputil_normalize_rule(struct cls_rule *rule, enum nx_flow_format flow_format)
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 */
2508 MAY_TOS_FRAG = 1 << 3, /* tos_frag */
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 */
2515 struct flow_wildcards wc;
2517 /* Figure out what fields may be matched. */
2518 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
2519 may_match = MAY_NW_PROTO | MAY_TOS_FRAG | MAY_NW_ADDR;
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;
2525 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)
2526 && flow_format == NXFF_NXM) {
2527 may_match = MAY_NW_PROTO | MAY_TOS_FRAG | MAY_IPV6_ADDR;
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;
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;
2548 /* Clear the fields that may not be matched. */
2550 if (!(may_match & MAY_NW_ADDR)) {
2551 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
2553 if (!(may_match & MAY_TP_ADDR)) {
2554 wc.wildcards |= FWW_TP_SRC | FWW_TP_DST;
2556 if (!(may_match & MAY_NW_PROTO)) {
2557 wc.wildcards |= FWW_NW_PROTO;
2559 if (!(may_match & MAY_TOS_FRAG)) {
2560 wc.tos_frag_mask = 0;
2562 if (!(may_match & MAY_ARP_SHA)) {
2563 wc.wildcards |= FWW_ARP_SHA;
2565 if (!(may_match & MAY_ARP_THA)) {
2566 wc.wildcards |= FWW_ARP_THA;
2568 if (!(may_match & MAY_IPV6_ADDR)) {
2569 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
2571 if (!(may_match & MAY_ND_TARGET)) {
2572 wc.wildcards |= FWW_ND_TARGET;
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;
2581 cls_rule_zero_wildcarded_fields(rule);
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);
2595 vendor_code_to_id(uint8_t code)
2598 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
2600 #undef OFPUTIL_VENDOR
2607 vendor_id_to_code(uint32_t id)
2610 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case VENDOR_ID: return NAME;
2612 #undef OFPUTIL_VENDOR
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
2624 * Returns NULL if 'error' is not an OpenFlow error code. */
2626 ofputil_encode_error_msg(int error, const struct ofp_header *oh)
2628 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
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));
2649 len = ntohs(oh->length);
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;
2665 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
2666 oem->type = htons(type);
2667 oem->code = htons(code);
2669 struct ofp_error_msg *oem;
2670 struct nx_vendor_error *nve;
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",
2680 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
2681 OFPT_ERROR, xid, &buf);
2682 oem->type = htons(NXET_VENDOR);
2683 oem->code = htons(NXVC_VENDOR_ERROR);
2685 nve = (struct nx_vendor_error *)oem->data;
2686 nve->vendor = htonl(vendor_id);
2687 nve->type = htons(type);
2688 nve->code = htons(code);
2693 ofpbuf_put(buf, data, len);
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.
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. */
2706 ofputil_decode_error_msg(const struct ofp_header *oh, size_t *payload_ofs)
2708 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2710 const struct ofp_error_msg *oem;
2711 uint16_t type, code;
2718 if (oh->type != OFPT_ERROR) {
2722 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2723 oem = ofpbuf_try_pull(&b, sizeof *oem);
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);
2736 vendor = vendor_id_to_code(ntohl(nve->vendor));
2738 VLOG_WARN_RL(&rl, "error contains unknown vendor ID %#"PRIx32,
2739 ntohl(nve->vendor));
2742 type = ntohs(nve->type);
2743 code = ntohs(nve->code);
2745 vendor = OFPUTIL_VENDOR_OPENFLOW;
2749 VLOG_WARN_RL(&rl, "error contains type %"PRIu16" greater than "
2750 "supported maximum value 1023", type);
2755 *payload_ofs = (uint8_t *) b.data - (uint8_t *) oh;
2757 return ofp_mkerr_vendor(vendor, type, code);
2761 ofputil_format_error(struct ds *s, int error)
2763 if (is_errno(error)) {
2764 ds_put_cstr(s, strerror(error));
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);
2771 ds_put_format(s, "type ");
2773 ds_put_cstr(s, type_s);
2775 ds_put_format(s, "%"PRIu16, type);
2778 ds_put_cstr(s, ", code ");
2780 ds_put_cstr(s, code_s);
2782 ds_put_format(s, "%"PRIu16, code);
2788 ofputil_error_to_string(int error)
2790 struct ds s = DS_EMPTY_INITIALIZER;
2791 ofputil_format_error(&s, error);
2792 return ds_steal_cstr(&s);
2795 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
2796 * successful, otherwise an OpenFlow error.
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.
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
2807 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
2808 union ofp_action **actionsp, size_t *n_actionsp)
2810 if (actions_len % OFP_ACTION_ALIGN != 0) {
2811 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2812 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
2816 *actionsp = ofpbuf_try_pull(b, actions_len);
2817 if (*actionsp == NULL) {
2818 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
2819 "exceeds remaining message length (%zu)",
2820 actions_len, b->size);
2824 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
2830 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
2834 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
2835 const union ofp_action *b, size_t n_b)
2837 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
2841 ofputil_actions_clone(const union ofp_action *actions, size_t n)
2843 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
2846 /* Parses a key or a key-value pair from '*stringp'.
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.
2854 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
2855 * NULL and returns false. */
2857 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
2859 char *pos, *key, *value;
2863 pos += strspn(pos, ", \t\r\n");
2865 *keyp = *valuep = NULL;
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. */
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. */
2885 value = key + key_len + 1;
2886 for (value_len = 0; level > 0; value_len++) {
2887 switch (value[value_len]) {
2889 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
2900 value[value_len - 1] = '\0';
2901 pos = value + value_len;
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');
2907 key[key_len] = '\0';