2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "meta-flow.h"
32 #include "multipath.h"
35 #include "ofp-errors.h"
40 #include "unaligned.h"
41 #include "type-props.h"
44 VLOG_DEFINE_THIS_MODULE(ofp_util);
46 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
47 * in the peer and so there's not much point in showing a lot of them. */
48 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
50 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
51 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
54 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
55 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
56 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
57 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
60 ofputil_wcbits_to_netmask(int wcbits)
63 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
66 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
67 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
68 * must be a CIDR netmask (see ip_is_cidr()). */
70 ofputil_netmask_to_wcbits(ovs_be32 netmask)
72 return 32 - ip_count_cidr_bits(netmask);
75 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
77 #define WC_INVARIANT_LIST \
78 WC_INVARIANT_BIT(IN_PORT) \
79 WC_INVARIANT_BIT(DL_TYPE) \
80 WC_INVARIANT_BIT(NW_PROTO)
82 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
83 * actually have the same names and values. */
84 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
86 #undef WC_INVARIANT_BIT
88 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
90 static const flow_wildcards_t WC_INVARIANTS = 0
91 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
93 #undef WC_INVARIANT_BIT
96 /* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
97 * struct cls_rule. It is the caller's responsibility to handle the special
98 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
100 ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
102 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11);
104 /* Initialize most of rule->wc. */
105 flow_wildcards_init_catchall(wc);
106 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
108 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
109 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL
112 if (ofpfw & OFPFW_NW_TOS) {
113 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
114 * the enum than we can use. */
115 wc->wildcards |= FWW_NW_DSCP;
118 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
119 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
121 if (!(ofpfw & OFPFW_TP_SRC)) {
122 wc->tp_src_mask = htons(UINT16_MAX);
124 if (!(ofpfw & OFPFW_TP_DST)) {
125 wc->tp_dst_mask = htons(UINT16_MAX);
128 if (!(ofpfw & OFPFW_DL_SRC)) {
129 memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN);
131 if (!(ofpfw & OFPFW_DL_DST)) {
132 memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN);
136 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
137 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
139 if (!(ofpfw & OFPFW_DL_VLAN)) {
140 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
144 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
147 ofputil_cls_rule_from_match(const struct ofp_match *match,
148 unsigned int priority, struct cls_rule *rule)
150 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
152 /* Initialize rule->priority, rule->wc. */
153 rule->priority = !ofpfw ? UINT16_MAX : priority;
154 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
156 /* Initialize most of rule->flow. */
157 rule->flow.nw_src = match->nw_src;
158 rule->flow.nw_dst = match->nw_dst;
159 rule->flow.in_port = ntohs(match->in_port);
160 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
161 rule->flow.tp_src = match->tp_src;
162 rule->flow.tp_dst = match->tp_dst;
163 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
164 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
165 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
166 rule->flow.nw_proto = match->nw_proto;
168 /* Translate VLANs. */
169 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
170 /* Match only packets without 802.1Q header.
172 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
174 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
175 * because we can't have a specific PCP without an 802.1Q header.
176 * However, older versions of OVS treated this as matching packets
177 * withut an 802.1Q header, so we do here too. */
178 rule->flow.vlan_tci = htons(0);
179 rule->wc.vlan_tci_mask = htons(0xffff);
181 ovs_be16 vid, pcp, tci;
183 vid = match->dl_vlan & htons(VLAN_VID_MASK);
184 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
185 tci = vid | pcp | htons(VLAN_CFI);
186 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
190 cls_rule_zero_wildcarded_fields(rule);
193 /* Convert 'rule' into the OpenFlow match structure 'match'. */
195 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
197 const struct flow_wildcards *wc = &rule->wc;
200 /* Figure out most OpenFlow wildcards. */
201 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
202 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
203 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
204 if (wc->wildcards & FWW_NW_DSCP) {
205 ofpfw |= OFPFW_NW_TOS;
207 if (!wc->tp_src_mask) {
208 ofpfw |= OFPFW_TP_SRC;
210 if (!wc->tp_dst_mask) {
211 ofpfw |= OFPFW_TP_DST;
213 if (eth_addr_is_zero(wc->dl_src_mask)) {
214 ofpfw |= OFPFW_DL_SRC;
216 if (eth_addr_is_zero(wc->dl_dst_mask)) {
217 ofpfw |= OFPFW_DL_DST;
220 /* Translate VLANs. */
221 match->dl_vlan = htons(0);
222 match->dl_vlan_pcp = 0;
223 if (rule->wc.vlan_tci_mask == htons(0)) {
224 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
225 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
226 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
227 match->dl_vlan = htons(OFP_VLAN_NONE);
229 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
230 ofpfw |= OFPFW_DL_VLAN;
232 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
235 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
236 ofpfw |= OFPFW_DL_VLAN_PCP;
238 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
242 /* Compose most of the match structure. */
243 match->wildcards = htonl(ofpfw);
244 match->in_port = htons(rule->flow.in_port);
245 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
246 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
247 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
248 match->nw_src = rule->flow.nw_src;
249 match->nw_dst = rule->flow.nw_dst;
250 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
251 match->nw_proto = rule->flow.nw_proto;
252 match->tp_src = rule->flow.tp_src;
253 match->tp_dst = rule->flow.tp_dst;
254 memset(match->pad1, '\0', sizeof match->pad1);
255 memset(match->pad2, '\0', sizeof match->pad2);
258 /* Given a 'dl_type' value in the format used in struct flow, returns the
259 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
261 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
263 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
264 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
268 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
269 * structure, returns the corresponding 'dl_type' value for use in struct
272 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
274 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
275 ? htons(FLOW_DL_TYPE_NONE)
279 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
283 static uint32_t next_xid = 1;
284 return htonl(next_xid++);
287 /* Basic parsing of OpenFlow messages. */
289 struct ofputil_msg_type {
290 enum ofputil_msg_code code; /* OFPUTIL_*. */
291 uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */
292 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
293 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
294 unsigned int min_size; /* Minimum total message size in bytes. */
295 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
296 * the message may exceed 'min_size' by an even multiple of this value. */
297 unsigned int extra_multiple;
300 /* Represents a malformed OpenFlow message. */
301 static const struct ofputil_msg_type ofputil_invalid_type = {
302 OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0
305 struct ofputil_msg_category {
306 const char *name; /* e.g. "OpenFlow message" */
307 const struct ofputil_msg_type *types;
309 enum ofperr missing_error; /* Error value for missing type. */
313 ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
315 switch (type->extra_multiple) {
317 if (size != type->min_size) {
318 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
319 "length %u (expected length %u)",
320 type->name, size, type->min_size);
321 return OFPERR_OFPBRC_BAD_LEN;
326 if (size < type->min_size) {
327 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
328 "length %u (expected length at least %u bytes)",
329 type->name, size, type->min_size);
330 return OFPERR_OFPBRC_BAD_LEN;
335 if (size < type->min_size
336 || (size - type->min_size) % type->extra_multiple) {
337 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
338 "length %u (must be exactly %u bytes or longer "
339 "by an integer multiple of %u bytes)",
341 type->min_size, type->extra_multiple);
342 return OFPERR_OFPBRC_BAD_LEN;
349 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
350 uint8_t version, uint32_t value,
351 const struct ofputil_msg_type **typep)
353 const struct ofputil_msg_type *type;
355 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
356 if (type->value == value
357 && (!type->ofp_version || version == type->ofp_version)) {
363 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
365 return cat->missing_error;
369 ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
370 const struct ofputil_msg_type **typep)
372 static const struct ofputil_msg_type nxt_messages[] = {
373 { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION,
374 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
375 sizeof(struct nx_role_request), 0 },
377 { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION,
378 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
379 sizeof(struct nx_role_request), 0 },
381 { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION,
382 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
383 sizeof(struct nx_set_flow_format), 0 },
385 { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION,
386 NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT",
387 sizeof(struct nx_set_packet_in_format), 0 },
389 { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION,
390 NXT_PACKET_IN, "NXT_PACKET_IN",
391 sizeof(struct nx_packet_in), 1 },
393 { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION,
394 NXT_FLOW_MOD, "NXT_FLOW_MOD",
395 sizeof(struct nx_flow_mod), 8 },
397 { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION,
398 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
399 sizeof(struct nx_flow_removed), 8 },
401 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION,
402 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
403 sizeof(struct nx_flow_mod_table_id), 0 },
405 { OFPUTIL_NXT_FLOW_AGE, OFP10_VERSION,
406 NXT_FLOW_AGE, "NXT_FLOW_AGE",
407 sizeof(struct nicira_header), 0 },
409 { OFPUTIL_NXT_SET_ASYNC_CONFIG, OFP10_VERSION,
410 NXT_SET_ASYNC_CONFIG, "NXT_SET_ASYNC_CONFIG",
411 sizeof(struct nx_async_config), 0 },
413 { OFPUTIL_NXT_SET_CONTROLLER_ID, OFP10_VERSION,
414 NXT_SET_CONTROLLER_ID, "NXT_SET_CONTROLLER_ID",
415 sizeof(struct nx_controller_id), 0 },
418 static const struct ofputil_msg_category nxt_category = {
419 "Nicira extension message",
420 nxt_messages, ARRAY_SIZE(nxt_messages),
421 OFPERR_OFPBRC_BAD_SUBTYPE
424 const struct ofp_vendor_header *ovh;
425 const struct nicira_header *nh;
427 if (length < sizeof(struct ofp_vendor_header)) {
428 if (length == ntohs(oh->length)) {
429 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
431 return OFPERR_OFPBRC_BAD_LEN;
434 ovh = (const struct ofp_vendor_header *) oh;
435 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
436 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
437 "vendor %"PRIx32, ntohl(ovh->vendor));
438 return OFPERR_OFPBRC_BAD_VENDOR;
441 if (length < sizeof(struct nicira_header)) {
442 if (length == ntohs(oh->length)) {
443 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
444 "length %u (expected at least %zu)",
445 ntohs(ovh->header.length),
446 sizeof(struct nicira_header));
448 return OFPERR_OFPBRC_BAD_LEN;
451 nh = (const struct nicira_header *) oh;
452 return ofputil_lookup_openflow_message(&nxt_category, oh->version,
453 ntohl(nh->subtype), typep);
457 check_nxstats_msg(const struct ofp_header *oh, size_t length)
459 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
462 if (length < sizeof(struct ofp_vendor_stats_msg)) {
463 if (length == ntohs(oh->length)) {
464 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
466 return OFPERR_OFPBRC_BAD_LEN;
469 memcpy(&vendor, osm + 1, sizeof vendor);
470 if (vendor != htonl(NX_VENDOR_ID)) {
471 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
472 "unknown vendor %"PRIx32, ntohl(vendor));
473 return OFPERR_OFPBRC_BAD_VENDOR;
476 if (length < sizeof(struct nicira_stats_msg)) {
477 if (length == ntohs(osm->header.length)) {
478 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
480 return OFPERR_OFPBRC_BAD_LEN;
487 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
488 const struct ofputil_msg_type **typep)
490 static const struct ofputil_msg_type nxst_requests[] = {
491 { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION,
492 NXST_FLOW, "NXST_FLOW request",
493 sizeof(struct nx_flow_stats_request), 8 },
495 { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION,
496 NXST_AGGREGATE, "NXST_AGGREGATE request",
497 sizeof(struct nx_aggregate_stats_request), 8 },
500 static const struct ofputil_msg_category nxst_request_category = {
501 "Nicira extension statistics request",
502 nxst_requests, ARRAY_SIZE(nxst_requests),
503 OFPERR_OFPBRC_BAD_SUBTYPE
506 const struct nicira_stats_msg *nsm;
509 error = check_nxstats_msg(oh, length);
514 nsm = (struct nicira_stats_msg *) oh;
515 return ofputil_lookup_openflow_message(&nxst_request_category, oh->version,
516 ntohl(nsm->subtype), typep);
520 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
521 const struct ofputil_msg_type **typep)
523 static const struct ofputil_msg_type nxst_replies[] = {
524 { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION,
525 NXST_FLOW, "NXST_FLOW reply",
526 sizeof(struct nicira_stats_msg), 8 },
528 { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION,
529 NXST_AGGREGATE, "NXST_AGGREGATE reply",
530 sizeof(struct nx_aggregate_stats_reply), 0 },
533 static const struct ofputil_msg_category nxst_reply_category = {
534 "Nicira extension statistics reply",
535 nxst_replies, ARRAY_SIZE(nxst_replies),
536 OFPERR_OFPBRC_BAD_SUBTYPE
539 const struct nicira_stats_msg *nsm;
542 error = check_nxstats_msg(oh, length);
547 nsm = (struct nicira_stats_msg *) oh;
548 return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version,
549 ntohl(nsm->subtype), typep);
553 check_stats_msg(const struct ofp_header *oh, size_t length)
555 if (length < sizeof(struct ofp_stats_msg)) {
556 if (length == ntohs(oh->length)) {
557 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
559 return OFPERR_OFPBRC_BAD_LEN;
566 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
567 const struct ofputil_msg_type **typep)
569 static const struct ofputil_msg_type ofpst_requests[] = {
570 { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION,
571 OFPST_DESC, "OFPST_DESC request",
572 sizeof(struct ofp_stats_msg), 0 },
574 { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION,
575 OFPST_FLOW, "OFPST_FLOW request",
576 sizeof(struct ofp_flow_stats_request), 0 },
578 { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION,
579 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
580 sizeof(struct ofp_flow_stats_request), 0 },
582 { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION,
583 OFPST_TABLE, "OFPST_TABLE request",
584 sizeof(struct ofp_stats_msg), 0 },
586 { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION,
587 OFPST_PORT, "OFPST_PORT request",
588 sizeof(struct ofp_port_stats_request), 0 },
590 { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION,
591 OFPST_QUEUE, "OFPST_QUEUE request",
592 sizeof(struct ofp_queue_stats_request), 0 },
594 { OFPUTIL_OFPST_PORT_DESC_REQUEST, OFP10_VERSION,
595 OFPST_PORT_DESC, "OFPST_PORT_DESC request",
596 sizeof(struct ofp_stats_msg), 0 },
599 OFPST_VENDOR, "OFPST_VENDOR request",
600 sizeof(struct ofp_vendor_stats_msg), 1 },
603 static const struct ofputil_msg_category ofpst_request_category = {
604 "OpenFlow statistics",
605 ofpst_requests, ARRAY_SIZE(ofpst_requests),
606 OFPERR_OFPBRC_BAD_STAT
609 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
612 error = check_stats_msg(oh, length);
617 error = ofputil_lookup_openflow_message(&ofpst_request_category,
618 oh->version, ntohs(request->type),
620 if (!error && request->type == htons(OFPST_VENDOR)) {
621 error = ofputil_decode_nxst_request(oh, length, typep);
627 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
628 const struct ofputil_msg_type **typep)
630 static const struct ofputil_msg_type ofpst_replies[] = {
631 { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION,
632 OFPST_DESC, "OFPST_DESC reply",
633 sizeof(struct ofp_desc_stats), 0 },
635 { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION,
636 OFPST_FLOW, "OFPST_FLOW reply",
637 sizeof(struct ofp_stats_msg), 1 },
639 { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION,
640 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
641 sizeof(struct ofp_aggregate_stats_reply), 0 },
643 { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION,
644 OFPST_TABLE, "OFPST_TABLE reply",
645 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
647 { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION,
648 OFPST_PORT, "OFPST_PORT reply",
649 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
651 { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION,
652 OFPST_QUEUE, "OFPST_QUEUE reply",
653 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
655 { OFPUTIL_OFPST_PORT_DESC_REPLY, OFP10_VERSION,
656 OFPST_PORT_DESC, "OFPST_PORT_DESC reply",
657 sizeof(struct ofp_stats_msg), sizeof(struct ofp10_phy_port) },
660 OFPST_VENDOR, "OFPST_VENDOR reply",
661 sizeof(struct ofp_vendor_stats_msg), 1 },
664 static const struct ofputil_msg_category ofpst_reply_category = {
665 "OpenFlow statistics",
666 ofpst_replies, ARRAY_SIZE(ofpst_replies),
667 OFPERR_OFPBRC_BAD_STAT
670 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
673 error = check_stats_msg(oh, length);
678 error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version,
679 ntohs(reply->type), typep);
680 if (!error && reply->type == htons(OFPST_VENDOR)) {
681 error = ofputil_decode_nxst_reply(oh, length, typep);
687 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
688 const struct ofputil_msg_type **typep)
690 static const struct ofputil_msg_type ofpt_messages[] = {
691 { OFPUTIL_OFPT_HELLO, OFP10_VERSION,
692 OFPT_HELLO, "OFPT_HELLO",
693 sizeof(struct ofp_hello), 1 },
695 { OFPUTIL_OFPT_ERROR, 0,
696 OFPT_ERROR, "OFPT_ERROR",
697 sizeof(struct ofp_error_msg), 1 },
699 { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION,
700 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
701 sizeof(struct ofp_header), 1 },
703 { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION,
704 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
705 sizeof(struct ofp_header), 1 },
707 { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION,
708 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
709 sizeof(struct ofp_header), 0 },
711 { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION,
712 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
713 sizeof(struct ofp_switch_features), sizeof(struct ofp10_phy_port) },
714 { OFPUTIL_OFPT_FEATURES_REPLY, OFP11_VERSION,
715 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
716 sizeof(struct ofp_switch_features), sizeof(struct ofp11_port) },
718 { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION,
719 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
720 sizeof(struct ofp_header), 0 },
722 { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION,
723 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
724 sizeof(struct ofp_switch_config), 0 },
726 { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION,
727 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
728 sizeof(struct ofp_switch_config), 0 },
730 { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION,
731 OFPT_PACKET_IN, "OFPT_PACKET_IN",
732 offsetof(struct ofp_packet_in, data), 1 },
734 { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION,
735 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
736 sizeof(struct ofp_flow_removed), 0 },
738 { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION,
739 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
740 sizeof(struct ofp_port_status) + sizeof(struct ofp10_phy_port), 0 },
741 { OFPUTIL_OFPT_PORT_STATUS, OFP11_VERSION,
742 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
743 sizeof(struct ofp_port_status) + sizeof(struct ofp11_port), 0 },
745 { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION,
746 OFPT10_PACKET_OUT, "OFPT_PACKET_OUT",
747 sizeof(struct ofp_packet_out), 1 },
749 { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION,
750 OFPT10_FLOW_MOD, "OFPT_FLOW_MOD",
751 sizeof(struct ofp_flow_mod), 1 },
753 { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION,
754 OFPT10_PORT_MOD, "OFPT_PORT_MOD",
755 sizeof(struct ofp10_port_mod), 0 },
756 { OFPUTIL_OFPT_PORT_MOD, OFP11_VERSION,
757 OFPT11_PORT_MOD, "OFPT_PORT_MOD",
758 sizeof(struct ofp11_port_mod), 0 },
761 OFPT10_STATS_REQUEST, "OFPT_STATS_REQUEST",
762 sizeof(struct ofp_stats_msg), 1 },
765 OFPT10_STATS_REPLY, "OFPT_STATS_REPLY",
766 sizeof(struct ofp_stats_msg), 1 },
768 { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION,
769 OFPT10_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
770 sizeof(struct ofp_header), 0 },
772 { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION,
773 OFPT10_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
774 sizeof(struct ofp_header), 0 },
777 OFPT_VENDOR, "OFPT_VENDOR",
778 sizeof(struct ofp_vendor_header), 1 },
781 static const struct ofputil_msg_category ofpt_category = {
783 ofpt_messages, ARRAY_SIZE(ofpt_messages),
784 OFPERR_OFPBRC_BAD_TYPE
789 error = ofputil_lookup_openflow_message(&ofpt_category, oh->version,
792 switch ((oh->version << 8) | oh->type) {
793 case (OFP10_VERSION << 8) | OFPT_VENDOR:
794 case (OFP11_VERSION << 8) | OFPT_VENDOR:
795 error = ofputil_decode_vendor(oh, length, typep);
798 case (OFP10_VERSION << 8) | OFPT10_STATS_REQUEST:
799 case (OFP11_VERSION << 8) | OFPT11_STATS_REQUEST:
800 error = ofputil_decode_ofpst_request(oh, length, typep);
803 case (OFP10_VERSION << 8) | OFPT10_STATS_REPLY:
804 case (OFP11_VERSION << 8) | OFPT11_STATS_REPLY:
805 error = ofputil_decode_ofpst_reply(oh, length, typep);
814 /* Decodes the message type represented by 'oh'. Returns 0 if successful or an
815 * OpenFlow error code on failure. Either way, stores in '*typep' a type
816 * structure that can be inspected with the ofputil_msg_type_*() functions.
818 * oh->length must indicate the correct length of the message (and must be at
819 * least sizeof(struct ofp_header)).
821 * Success indicates that 'oh' is at least as long as the minimum-length
822 * message of its type. */
824 ofputil_decode_msg_type(const struct ofp_header *oh,
825 const struct ofputil_msg_type **typep)
827 size_t length = ntohs(oh->length);
830 error = ofputil_decode_msg_type__(oh, length, typep);
832 error = ofputil_check_length(*typep, length);
835 *typep = &ofputil_invalid_type;
840 /* Decodes the message type represented by 'oh', of which only the first
841 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
842 * code on failure. Either way, stores in '*typep' a type structure that can
843 * be inspected with the ofputil_msg_type_*() functions. */
845 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
846 const struct ofputil_msg_type **typep)
850 error = (length >= sizeof *oh
851 ? ofputil_decode_msg_type__(oh, length, typep)
852 : OFPERR_OFPBRC_BAD_LEN);
854 *typep = &ofputil_invalid_type;
859 /* Returns an OFPUTIL_* message type code for 'type'. */
860 enum ofputil_msg_code
861 ofputil_msg_type_code(const struct ofputil_msg_type *type)
868 struct proto_abbrev {
869 enum ofputil_protocol protocol;
873 /* Most users really don't care about some of the differences between
874 * protocols. These abbreviations help with that. */
875 static const struct proto_abbrev proto_abbrevs[] = {
876 { OFPUTIL_P_ANY, "any" },
877 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
878 { OFPUTIL_P_NXM_ANY, "NXM" },
880 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
882 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
886 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
888 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
889 * connection that has negotiated the given 'version'. 'version' should
890 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
891 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
892 * outside the valid range. */
893 enum ofputil_protocol
894 ofputil_protocol_from_ofp_version(int version)
897 case OFP10_VERSION: return OFPUTIL_P_OF10;
902 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION or
903 * OFP11_VERSION) that corresponds to 'protocol'. */
905 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
909 case OFPUTIL_P_OF10_TID:
911 case OFPUTIL_P_NXM_TID:
912 return OFP10_VERSION;
918 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
921 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
923 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
926 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
927 * extension turned on or off if 'enable' is true or false, respectively.
929 * This extension is only useful for protocols whose "standard" version does
930 * not allow specific tables to be modified. In particular, this is true of
931 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
932 * specifies a table ID and so there is no need for such an extension. When
933 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
934 * extension, this function just returns its 'protocol' argument unchanged
935 * regardless of the value of 'enable'. */
936 enum ofputil_protocol
937 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
941 case OFPUTIL_P_OF10_TID:
942 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
945 case OFPUTIL_P_NXM_TID:
946 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
953 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
954 * some extension to a standard protocol version, the return value is the
955 * standard version of that protocol without any extension. If 'protocol' is a
956 * standard protocol version, returns 'protocol' unchanged. */
957 enum ofputil_protocol
958 ofputil_protocol_to_base(enum ofputil_protocol protocol)
960 return ofputil_protocol_set_tid(protocol, false);
963 /* Returns 'new_base' with any extensions taken from 'cur'. */
964 enum ofputil_protocol
965 ofputil_protocol_set_base(enum ofputil_protocol cur,
966 enum ofputil_protocol new_base)
968 bool tid = (cur & OFPUTIL_P_TID) != 0;
972 case OFPUTIL_P_OF10_TID:
973 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
976 case OFPUTIL_P_NXM_TID:
977 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
984 /* Returns a string form of 'protocol', if a simple form exists (that is, if
985 * 'protocol' is either a single protocol or it is a combination of protocols
986 * that have a single abbreviation). Otherwise, returns NULL. */
988 ofputil_protocol_to_string(enum ofputil_protocol protocol)
990 const struct proto_abbrev *p;
992 /* Use a "switch" statement for single-bit names so that we get a compiler
993 * warning if we forget any. */
996 return "NXM-table_id";
998 case OFPUTIL_P_NXM_TID:
999 return "NXM+table_id";
1001 case OFPUTIL_P_OF10:
1002 return "OpenFlow10-table_id";
1004 case OFPUTIL_P_OF10_TID:
1005 return "OpenFlow10+table_id";
1008 /* Check abbreviations. */
1009 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1010 if (protocol == p->protocol) {
1018 /* Returns a string that represents 'protocols'. The return value might be a
1019 * comma-separated list if 'protocols' doesn't have a simple name. The return
1020 * value is "none" if 'protocols' is 0.
1022 * The caller must free the returned string (with free()). */
1024 ofputil_protocols_to_string(enum ofputil_protocol protocols)
1028 assert(!(protocols & ~OFPUTIL_P_ANY));
1029 if (protocols == 0) {
1030 return xstrdup("none");
1035 const struct proto_abbrev *p;
1039 ds_put_char(&s, ',');
1042 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1043 if ((protocols & p->protocol) == p->protocol) {
1044 ds_put_cstr(&s, p->name);
1045 protocols &= ~p->protocol;
1050 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1051 enum ofputil_protocol bit = 1u << i;
1053 if (protocols & bit) {
1054 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1063 return ds_steal_cstr(&s);
1066 static enum ofputil_protocol
1067 ofputil_protocol_from_string__(const char *s, size_t n)
1069 const struct proto_abbrev *p;
1072 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1073 enum ofputil_protocol bit = 1u << i;
1074 const char *name = ofputil_protocol_to_string(bit);
1076 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1081 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1082 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1090 /* Returns the nonempty set of protocols represented by 's', which can be a
1091 * single protocol name or abbreviation or a comma-separated list of them.
1093 * Aborts the program with an error message if 's' is invalid. */
1094 enum ofputil_protocol
1095 ofputil_protocols_from_string(const char *s)
1097 const char *orig_s = s;
1098 enum ofputil_protocol protocols;
1102 enum ofputil_protocol p;
1105 n = strcspn(s, ",");
1111 p = ofputil_protocol_from_string__(s, n);
1113 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1121 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1127 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1129 switch (packet_in_format) {
1130 case NXPIF_OPENFLOW10:
1139 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1141 switch (packet_in_format) {
1142 case NXPIF_OPENFLOW10:
1143 return "openflow10";
1152 ofputil_packet_in_format_from_string(const char *s)
1154 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1155 : !strcmp(s, "nxm") ? NXPIF_NXM
1160 regs_fully_wildcarded(const struct flow_wildcards *wc)
1164 for (i = 0; i < FLOW_N_REGS; i++) {
1165 if (wc->reg_masks[i] != 0) {
1172 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
1173 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1174 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1175 * use OpenFlow 1.0 protocol for backward compatibility. */
1176 enum ofputil_protocol
1177 ofputil_usable_protocols(const struct cls_rule *rule)
1179 const struct flow_wildcards *wc = &rule->wc;
1181 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11);
1183 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
1184 if (!eth_mask_is_exact(wc->dl_src_mask)
1185 && !eth_addr_is_zero(wc->dl_src_mask)) {
1186 return OFPUTIL_P_NXM_ANY;
1188 if (!eth_mask_is_exact(wc->dl_dst_mask)
1189 && !eth_addr_is_zero(wc->dl_dst_mask)) {
1190 return OFPUTIL_P_NXM_ANY;
1193 /* Only NXM supports matching ARP hardware addresses. */
1194 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
1195 return OFPUTIL_P_NXM_ANY;
1198 /* Only NXM supports matching IPv6 traffic. */
1199 if (!(wc->wildcards & FWW_DL_TYPE)
1200 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
1201 return OFPUTIL_P_NXM_ANY;
1204 /* Only NXM supports matching registers. */
1205 if (!regs_fully_wildcarded(wc)) {
1206 return OFPUTIL_P_NXM_ANY;
1209 /* Only NXM supports matching tun_id. */
1210 if (wc->tun_id_mask != htonll(0)) {
1211 return OFPUTIL_P_NXM_ANY;
1214 /* Only NXM supports matching fragments. */
1215 if (wc->nw_frag_mask) {
1216 return OFPUTIL_P_NXM_ANY;
1219 /* Only NXM supports matching IPv6 flow label. */
1220 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
1221 return OFPUTIL_P_NXM_ANY;
1224 /* Only NXM supports matching IP ECN bits. */
1225 if (!(wc->wildcards & FWW_NW_ECN)) {
1226 return OFPUTIL_P_NXM_ANY;
1229 /* Only NXM supports matching IP TTL/hop limit. */
1230 if (!(wc->wildcards & FWW_NW_TTL)) {
1231 return OFPUTIL_P_NXM_ANY;
1234 /* Only NXM supports bitwise matching on transport port. */
1235 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
1236 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
1237 return OFPUTIL_P_NXM_ANY;
1240 /* Other formats can express this rule. */
1241 return OFPUTIL_P_ANY;
1244 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1245 * protocol is 'current', at least partly transitions the protocol to 'want'.
1246 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1247 * connection if the switch processes the returned message correctly. (If
1248 * '*next != want' then the caller will have to iterate.)
1250 * If 'current == want', returns NULL and stores 'current' in '*next'. */
1252 ofputil_encode_set_protocol(enum ofputil_protocol current,
1253 enum ofputil_protocol want,
1254 enum ofputil_protocol *next)
1256 enum ofputil_protocol cur_base, want_base;
1257 bool cur_tid, want_tid;
1259 cur_base = ofputil_protocol_to_base(current);
1260 want_base = ofputil_protocol_to_base(want);
1261 if (cur_base != want_base) {
1262 *next = ofputil_protocol_set_base(current, want_base);
1264 switch (want_base) {
1266 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1268 case OFPUTIL_P_OF10:
1269 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1271 case OFPUTIL_P_OF10_TID:
1272 case OFPUTIL_P_NXM_TID:
1277 cur_tid = (current & OFPUTIL_P_TID) != 0;
1278 want_tid = (want & OFPUTIL_P_TID) != 0;
1279 if (cur_tid != want_tid) {
1280 *next = ofputil_protocol_set_tid(current, want_tid);
1281 return ofputil_make_flow_mod_table_id(want_tid);
1284 assert(current == want);
1290 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1291 * format to 'nxff'. */
1293 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1295 struct nx_set_flow_format *sff;
1298 assert(ofputil_nx_flow_format_is_valid(nxff));
1300 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
1301 sff->format = htonl(nxff);
1306 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1308 enum ofputil_protocol
1309 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1311 switch (flow_format) {
1312 case NXFF_OPENFLOW10:
1313 return OFPUTIL_P_OF10;
1316 return OFPUTIL_P_NXM;
1323 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1325 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1327 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1330 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1333 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1335 switch (flow_format) {
1336 case NXFF_OPENFLOW10:
1337 return "openflow10";
1346 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1348 struct nx_set_packet_in_format *spif;
1351 spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg);
1352 spif->format = htonl(packet_in_format);
1357 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1358 * extension on or off (according to 'flow_mod_table_id'). */
1360 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1362 struct nx_flow_mod_table_id *nfmti;
1365 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
1366 nfmti->set = flow_mod_table_id;
1370 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1371 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1374 * Does not validate the flow_mod actions. */
1376 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1377 const struct ofp_header *oh,
1378 enum ofputil_protocol protocol)
1380 const struct ofputil_msg_type *type;
1384 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1386 ofputil_decode_msg_type(oh, &type);
1387 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
1388 /* Standard OpenFlow flow_mod. */
1389 const struct ofp_flow_mod *ofm;
1393 /* Dissect the message. */
1394 ofm = ofpbuf_pull(&b, sizeof *ofm);
1395 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1400 /* Set priority based on original wildcards. Normally we'd allow
1401 * ofputil_cls_rule_from_match() to do this for us, but
1402 * ofputil_normalize_rule() can put wildcards where the original flow
1403 * didn't have them. */
1404 priority = ntohs(ofm->priority);
1405 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
1406 priority = UINT16_MAX;
1409 /* Translate the rule. */
1410 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
1411 ofputil_normalize_rule(&fm->cr);
1413 /* Translate the message. */
1414 command = ntohs(ofm->command);
1415 fm->cookie = htonll(0);
1416 fm->cookie_mask = htonll(0);
1417 fm->new_cookie = ofm->cookie;
1418 fm->idle_timeout = ntohs(ofm->idle_timeout);
1419 fm->hard_timeout = ntohs(ofm->hard_timeout);
1420 fm->buffer_id = ntohl(ofm->buffer_id);
1421 fm->out_port = ntohs(ofm->out_port);
1422 fm->flags = ntohs(ofm->flags);
1423 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1424 /* Nicira extended flow_mod. */
1425 const struct nx_flow_mod *nfm;
1428 /* Dissect the message. */
1429 nfm = ofpbuf_pull(&b, sizeof *nfm);
1430 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1431 &fm->cr, &fm->cookie, &fm->cookie_mask);
1435 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1440 /* Translate the message. */
1441 command = ntohs(nfm->command);
1442 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1443 /* Flow additions may only set a new cookie, not match an
1444 * existing cookie. */
1445 return OFPERR_NXBRC_NXM_INVALID;
1447 fm->new_cookie = nfm->cookie;
1448 fm->idle_timeout = ntohs(nfm->idle_timeout);
1449 fm->hard_timeout = ntohs(nfm->hard_timeout);
1450 fm->buffer_id = ntohl(nfm->buffer_id);
1451 fm->out_port = ntohs(nfm->out_port);
1452 fm->flags = ntohs(nfm->flags);
1457 if (protocol & OFPUTIL_P_TID) {
1458 fm->command = command & 0xff;
1459 fm->table_id = command >> 8;
1461 fm->command = command;
1462 fm->table_id = 0xff;
1468 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1469 * 'protocol' and returns the message. */
1471 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1472 enum ofputil_protocol protocol)
1474 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1475 struct ofp_flow_mod *ofm;
1476 struct nx_flow_mod *nfm;
1481 command = (protocol & OFPUTIL_P_TID
1482 ? (fm->command & 0xff) | (fm->table_id << 8)
1486 case OFPUTIL_P_OF10:
1487 case OFPUTIL_P_OF10_TID:
1488 msg = ofpbuf_new(sizeof *ofm + actions_len);
1489 ofm = put_openflow(sizeof *ofm, OFPT10_FLOW_MOD, msg);
1490 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1491 ofm->cookie = fm->new_cookie;
1492 ofm->command = htons(command);
1493 ofm->idle_timeout = htons(fm->idle_timeout);
1494 ofm->hard_timeout = htons(fm->hard_timeout);
1495 ofm->priority = htons(fm->cr.priority);
1496 ofm->buffer_id = htonl(fm->buffer_id);
1497 ofm->out_port = htons(fm->out_port);
1498 ofm->flags = htons(fm->flags);
1502 case OFPUTIL_P_NXM_TID:
1503 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1504 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1506 nfm->command = htons(command);
1507 nfm->cookie = fm->new_cookie;
1508 match_len = nx_put_match(msg, false, &fm->cr,
1509 fm->cookie, fm->cookie_mask);
1510 nfm->idle_timeout = htons(fm->idle_timeout);
1511 nfm->hard_timeout = htons(fm->hard_timeout);
1512 nfm->priority = htons(fm->cr.priority);
1513 nfm->buffer_id = htonl(fm->buffer_id);
1514 nfm->out_port = htons(fm->out_port);
1515 nfm->flags = htons(fm->flags);
1516 nfm->match_len = htons(match_len);
1523 ofpbuf_put(msg, fm->actions, actions_len);
1524 update_openflow_length(msg);
1528 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1529 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1530 * 0-bit for each protocol that is inadequate.
1532 * (The return value will have at least one 1-bit.) */
1533 enum ofputil_protocol
1534 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1537 enum ofputil_protocol usable_protocols;
1540 usable_protocols = OFPUTIL_P_ANY;
1541 for (i = 0; i < n_fms; i++) {
1542 const struct ofputil_flow_mod *fm = &fms[i];
1544 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1545 if (fm->table_id != 0xff) {
1546 usable_protocols &= OFPUTIL_P_TID;
1549 /* Matching of the cookie is only supported through NXM. */
1550 if (fm->cookie_mask != htonll(0)) {
1551 usable_protocols &= OFPUTIL_P_NXM_ANY;
1554 assert(usable_protocols);
1556 return usable_protocols;
1560 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1561 const struct ofp_header *oh,
1564 const struct ofp_flow_stats_request *ofsr =
1565 (const struct ofp_flow_stats_request *) oh;
1567 fsr->aggregate = aggregate;
1568 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1569 fsr->out_port = ntohs(ofsr->out_port);
1570 fsr->table_id = ofsr->table_id;
1571 fsr->cookie = fsr->cookie_mask = htonll(0);
1577 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1578 const struct ofp_header *oh,
1581 const struct nx_flow_stats_request *nfsr;
1585 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1587 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1588 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match,
1589 &fsr->cookie, &fsr->cookie_mask);
1594 return OFPERR_OFPBRC_BAD_LEN;
1597 fsr->aggregate = aggregate;
1598 fsr->out_port = ntohs(nfsr->out_port);
1599 fsr->table_id = nfsr->table_id;
1604 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1605 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1606 * successful, otherwise an OpenFlow error code. */
1608 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1609 const struct ofp_header *oh)
1611 const struct ofputil_msg_type *type;
1615 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1617 ofputil_decode_msg_type(oh, &type);
1618 code = ofputil_msg_type_code(type);
1620 case OFPUTIL_OFPST_FLOW_REQUEST:
1621 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1623 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1624 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1626 case OFPUTIL_NXST_FLOW_REQUEST:
1627 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1629 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1630 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1633 /* Hey, the caller lied. */
1638 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1639 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1640 * 'protocol', and returns the message. */
1642 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1643 enum ofputil_protocol protocol)
1648 case OFPUTIL_P_OF10:
1649 case OFPUTIL_P_OF10_TID: {
1650 struct ofp_flow_stats_request *ofsr;
1653 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1654 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1655 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1656 ofsr->table_id = fsr->table_id;
1657 ofsr->out_port = htons(fsr->out_port);
1662 case OFPUTIL_P_NXM_TID: {
1663 struct nx_flow_stats_request *nfsr;
1667 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1668 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1669 match_len = nx_put_match(msg, false, &fsr->match,
1670 fsr->cookie, fsr->cookie_mask);
1673 nfsr->out_port = htons(fsr->out_port);
1674 nfsr->match_len = htons(match_len);
1675 nfsr->table_id = fsr->table_id;
1686 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1687 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1689 * (The return value will have at least one 1-bit.) */
1690 enum ofputil_protocol
1691 ofputil_flow_stats_request_usable_protocols(
1692 const struct ofputil_flow_stats_request *fsr)
1694 enum ofputil_protocol usable_protocols;
1696 usable_protocols = ofputil_usable_protocols(&fsr->match);
1697 if (fsr->cookie_mask != htonll(0)) {
1698 usable_protocols &= OFPUTIL_P_NXM_ANY;
1700 return usable_protocols;
1703 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1704 * ofputil_flow_stats in 'fs'.
1706 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1707 * OpenFlow message. Calling this function multiple times for a single 'msg'
1708 * iterates through the replies. The caller must initially leave 'msg''s layer
1709 * pointers null and not modify them between calls.
1711 * Most switches don't send the values needed to populate fs->idle_age and
1712 * fs->hard_age, so those members will usually be set to 0. If the switch from
1713 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1714 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1715 * 'idle_age' and 'hard_age' members in 'fs'.
1717 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1718 * otherwise a positive errno value. */
1720 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1722 bool flow_age_extension)
1724 const struct ofputil_msg_type *type;
1727 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1728 code = ofputil_msg_type_code(type);
1730 msg->l2 = msg->data;
1731 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1732 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1733 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1734 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1742 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1743 const struct ofp_flow_stats *ofs;
1746 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1748 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1749 "bytes at end", msg->size);
1753 length = ntohs(ofs->length);
1754 if (length < sizeof *ofs) {
1755 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1756 "length %zu", length);
1760 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1761 &fs->actions, &fs->n_actions)) {
1765 fs->cookie = get_32aligned_be64(&ofs->cookie);
1766 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1768 fs->table_id = ofs->table_id;
1769 fs->duration_sec = ntohl(ofs->duration_sec);
1770 fs->duration_nsec = ntohl(ofs->duration_nsec);
1771 fs->idle_timeout = ntohs(ofs->idle_timeout);
1772 fs->hard_timeout = ntohs(ofs->hard_timeout);
1775 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1776 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1777 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1778 const struct nx_flow_stats *nfs;
1779 size_t match_len, length;
1781 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1783 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1784 "bytes at end", msg->size);
1788 length = ntohs(nfs->length);
1789 match_len = ntohs(nfs->match_len);
1790 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1791 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1792 "claims invalid length %zu", match_len, length);
1795 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1800 if (ofputil_pull_actions(msg,
1801 length - sizeof *nfs - ROUND_UP(match_len, 8),
1802 &fs->actions, &fs->n_actions)) {
1806 fs->cookie = nfs->cookie;
1807 fs->table_id = nfs->table_id;
1808 fs->duration_sec = ntohl(nfs->duration_sec);
1809 fs->duration_nsec = ntohl(nfs->duration_nsec);
1810 fs->idle_timeout = ntohs(nfs->idle_timeout);
1811 fs->hard_timeout = ntohs(nfs->hard_timeout);
1814 if (flow_age_extension) {
1815 if (nfs->idle_age) {
1816 fs->idle_age = ntohs(nfs->idle_age) - 1;
1818 if (nfs->hard_age) {
1819 fs->hard_age = ntohs(nfs->hard_age) - 1;
1822 fs->packet_count = ntohll(nfs->packet_count);
1823 fs->byte_count = ntohll(nfs->byte_count);
1831 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1833 * We use this in situations where OVS internally uses UINT64_MAX to mean
1834 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1836 unknown_to_zero(uint64_t count)
1838 return count != UINT64_MAX ? count : 0;
1841 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1842 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1843 * have been initialized with ofputil_start_stats_reply(). */
1845 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1846 struct list *replies)
1848 size_t act_len = fs->n_actions * sizeof *fs->actions;
1849 const struct ofp_stats_msg *osm;
1851 osm = ofpbuf_from_list(list_back(replies))->data;
1852 if (osm->type == htons(OFPST_FLOW)) {
1853 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1854 struct ofp_flow_stats *ofs;
1856 ofs = ofputil_append_stats_reply(len, replies);
1857 ofs->length = htons(len);
1858 ofs->table_id = fs->table_id;
1860 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1861 ofs->duration_sec = htonl(fs->duration_sec);
1862 ofs->duration_nsec = htonl(fs->duration_nsec);
1863 ofs->priority = htons(fs->rule.priority);
1864 ofs->idle_timeout = htons(fs->idle_timeout);
1865 ofs->hard_timeout = htons(fs->hard_timeout);
1866 memset(ofs->pad2, 0, sizeof ofs->pad2);
1867 put_32aligned_be64(&ofs->cookie, fs->cookie);
1868 put_32aligned_be64(&ofs->packet_count,
1869 htonll(unknown_to_zero(fs->packet_count)));
1870 put_32aligned_be64(&ofs->byte_count,
1871 htonll(unknown_to_zero(fs->byte_count)));
1872 memcpy(ofs->actions, fs->actions, act_len);
1873 } else if (osm->type == htons(OFPST_VENDOR)) {
1874 struct nx_flow_stats *nfs;
1878 msg = ofputil_reserve_stats_reply(
1879 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1880 start_len = msg->size;
1882 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1883 nfs->table_id = fs->table_id;
1885 nfs->duration_sec = htonl(fs->duration_sec);
1886 nfs->duration_nsec = htonl(fs->duration_nsec);
1887 nfs->priority = htons(fs->rule.priority);
1888 nfs->idle_timeout = htons(fs->idle_timeout);
1889 nfs->hard_timeout = htons(fs->hard_timeout);
1890 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1891 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1893 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1894 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1896 nfs->match_len = htons(nx_put_match(msg, false, &fs->rule, 0, 0));
1897 nfs->cookie = fs->cookie;
1898 nfs->packet_count = htonll(fs->packet_count);
1899 nfs->byte_count = htonll(fs->byte_count);
1900 ofpbuf_put(msg, fs->actions, act_len);
1901 nfs->length = htons(msg->size - start_len);
1907 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1908 * NXST_AGGREGATE reply according to 'protocol', and returns the message. */
1910 ofputil_encode_aggregate_stats_reply(
1911 const struct ofputil_aggregate_stats *stats,
1912 const struct ofp_stats_msg *request)
1916 if (request->type == htons(OFPST_AGGREGATE)) {
1917 struct ofp_aggregate_stats_reply *asr;
1919 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
1920 put_32aligned_be64(&asr->packet_count,
1921 htonll(unknown_to_zero(stats->packet_count)));
1922 put_32aligned_be64(&asr->byte_count,
1923 htonll(unknown_to_zero(stats->byte_count)));
1924 asr->flow_count = htonl(stats->flow_count);
1925 } else if (request->type == htons(OFPST_VENDOR)) {
1926 struct nx_aggregate_stats_reply *nasr;
1928 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1929 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1930 nasr->packet_count = htonll(stats->packet_count);
1931 nasr->byte_count = htonll(stats->byte_count);
1932 nasr->flow_count = htonl(stats->flow_count);
1940 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1941 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1942 * an OpenFlow error code. */
1944 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1945 const struct ofp_header *oh)
1947 const struct ofputil_msg_type *type;
1948 enum ofputil_msg_code code;
1950 ofputil_decode_msg_type(oh, &type);
1951 code = ofputil_msg_type_code(type);
1952 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1953 const struct ofp_flow_removed *ofr;
1955 ofr = (const struct ofp_flow_removed *) oh;
1956 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1958 fr->cookie = ofr->cookie;
1959 fr->reason = ofr->reason;
1960 fr->duration_sec = ntohl(ofr->duration_sec);
1961 fr->duration_nsec = ntohl(ofr->duration_nsec);
1962 fr->idle_timeout = ntohs(ofr->idle_timeout);
1963 fr->packet_count = ntohll(ofr->packet_count);
1964 fr->byte_count = ntohll(ofr->byte_count);
1965 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1966 struct nx_flow_removed *nfr;
1970 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1972 nfr = ofpbuf_pull(&b, sizeof *nfr);
1973 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1974 &fr->rule, NULL, NULL);
1979 return OFPERR_OFPBRC_BAD_LEN;
1982 fr->cookie = nfr->cookie;
1983 fr->reason = nfr->reason;
1984 fr->duration_sec = ntohl(nfr->duration_sec);
1985 fr->duration_nsec = ntohl(nfr->duration_nsec);
1986 fr->idle_timeout = ntohs(nfr->idle_timeout);
1987 fr->packet_count = ntohll(nfr->packet_count);
1988 fr->byte_count = ntohll(nfr->byte_count);
1996 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1997 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
2000 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
2001 enum ofputil_protocol protocol)
2006 case OFPUTIL_P_OF10:
2007 case OFPUTIL_P_OF10_TID: {
2008 struct ofp_flow_removed *ofr;
2010 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
2012 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
2013 ofr->cookie = fr->cookie;
2014 ofr->priority = htons(fr->rule.priority);
2015 ofr->reason = fr->reason;
2016 ofr->duration_sec = htonl(fr->duration_sec);
2017 ofr->duration_nsec = htonl(fr->duration_nsec);
2018 ofr->idle_timeout = htons(fr->idle_timeout);
2019 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2020 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2025 case OFPUTIL_P_NXM_TID: {
2026 struct nx_flow_removed *nfr;
2029 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
2030 match_len = nx_put_match(msg, false, &fr->rule, 0, 0);
2033 nfr->cookie = fr->cookie;
2034 nfr->priority = htons(fr->rule.priority);
2035 nfr->reason = fr->reason;
2036 nfr->duration_sec = htonl(fr->duration_sec);
2037 nfr->duration_nsec = htonl(fr->duration_nsec);
2038 nfr->idle_timeout = htons(fr->idle_timeout);
2039 nfr->match_len = htons(match_len);
2040 nfr->packet_count = htonll(fr->packet_count);
2041 nfr->byte_count = htonll(fr->byte_count);
2053 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2054 const struct ofp_header *oh)
2056 const struct ofputil_msg_type *type;
2057 enum ofputil_msg_code code;
2059 ofputil_decode_msg_type(oh, &type);
2060 code = ofputil_msg_type_code(type);
2061 memset(pin, 0, sizeof *pin);
2063 if (code == OFPUTIL_OFPT_PACKET_IN) {
2064 const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh;
2066 pin->packet = opi->data;
2067 pin->packet_len = ntohs(opi->header.length)
2068 - offsetof(struct ofp_packet_in, data);
2070 pin->fmd.in_port = ntohs(opi->in_port);
2071 pin->reason = opi->reason;
2072 pin->buffer_id = ntohl(opi->buffer_id);
2073 pin->total_len = ntohs(opi->total_len);
2074 } else if (code == OFPUTIL_NXT_PACKET_IN) {
2075 const struct nx_packet_in *npi;
2076 struct cls_rule rule;
2080 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2082 npi = ofpbuf_pull(&b, sizeof *npi);
2083 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
2089 if (!ofpbuf_try_pull(&b, 2)) {
2090 return OFPERR_OFPBRC_BAD_LEN;
2093 pin->packet = b.data;
2094 pin->packet_len = b.size;
2095 pin->reason = npi->reason;
2096 pin->table_id = npi->table_id;
2097 pin->cookie = npi->cookie;
2099 pin->fmd.in_port = rule.flow.in_port;
2101 pin->fmd.tun_id = rule.flow.tun_id;
2102 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
2104 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
2105 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
2106 sizeof pin->fmd.reg_masks);
2108 pin->buffer_id = ntohl(npi->buffer_id);
2109 pin->total_len = ntohs(npi->total_len);
2117 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2118 * in the format specified by 'packet_in_format'. */
2120 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2121 enum nx_packet_in_format packet_in_format)
2123 size_t send_len = MIN(pin->send_len, pin->packet_len);
2124 struct ofpbuf *packet;
2126 /* Add OFPT_PACKET_IN. */
2127 if (packet_in_format == NXPIF_OPENFLOW10) {
2128 size_t header_len = offsetof(struct ofp_packet_in, data);
2129 struct ofp_packet_in *opi;
2131 packet = ofpbuf_new(send_len + header_len);
2132 opi = ofpbuf_put_zeros(packet, header_len);
2133 opi->header.version = OFP10_VERSION;
2134 opi->header.type = OFPT_PACKET_IN;
2135 opi->total_len = htons(pin->total_len);
2136 opi->in_port = htons(pin->fmd.in_port);
2137 opi->reason = pin->reason;
2138 opi->buffer_id = htonl(pin->buffer_id);
2140 ofpbuf_put(packet, pin->packet, send_len);
2141 } else if (packet_in_format == NXPIF_NXM) {
2142 struct nx_packet_in *npi;
2143 struct cls_rule rule;
2147 /* Estimate of required PACKET_IN length includes the NPI header, space
2148 * for the match (2 times sizeof the metadata seems like enough), 2
2149 * bytes for padding, and the packet length. */
2150 packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2
2153 cls_rule_init_catchall(&rule, 0);
2154 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
2155 pin->fmd.tun_id_mask);
2157 for (i = 0; i < FLOW_N_REGS; i++) {
2158 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
2159 pin->fmd.reg_masks[i]);
2162 cls_rule_set_in_port(&rule, pin->fmd.in_port);
2164 ofpbuf_put_zeros(packet, sizeof *npi);
2165 match_len = nx_put_match(packet, false, &rule, 0, 0);
2166 ofpbuf_put_zeros(packet, 2);
2167 ofpbuf_put(packet, pin->packet, send_len);
2170 npi->nxh.header.version = OFP10_VERSION;
2171 npi->nxh.header.type = OFPT_VENDOR;
2172 npi->nxh.vendor = htonl(NX_VENDOR_ID);
2173 npi->nxh.subtype = htonl(NXT_PACKET_IN);
2175 npi->buffer_id = htonl(pin->buffer_id);
2176 npi->total_len = htons(pin->total_len);
2177 npi->reason = pin->reason;
2178 npi->table_id = pin->table_id;
2179 npi->cookie = pin->cookie;
2180 npi->match_len = htons(match_len);
2184 update_openflow_length(packet);
2190 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2192 static char s[INT_STRLEN(int) + 1];
2199 case OFPR_INVALID_TTL:
2200 return "invalid_ttl";
2202 case OFPR_N_REASONS:
2204 sprintf(s, "%d", (int) reason);
2210 ofputil_packet_in_reason_from_string(const char *s,
2211 enum ofp_packet_in_reason *reason)
2215 for (i = 0; i < OFPR_N_REASONS; i++) {
2216 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2225 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2226 const struct ofp_packet_out *opo)
2231 po->buffer_id = ntohl(opo->buffer_id);
2232 po->in_port = ntohs(opo->in_port);
2233 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2234 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2235 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2237 return OFPERR_NXBRC_BAD_IN_PORT;
2240 ofpbuf_use_const(&b, opo, ntohs(opo->header.length));
2241 ofpbuf_pull(&b, sizeof *opo);
2243 error = ofputil_pull_actions(&b, ntohs(opo->actions_len),
2244 &po->actions, &po->n_actions);
2249 if (po->buffer_id == UINT32_MAX) {
2250 po->packet = b.data;
2251 po->packet_len = b.size;
2260 /* ofputil_phy_port */
2262 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2263 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2264 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2265 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2266 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2267 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2268 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2269 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2271 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2272 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2273 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2274 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2275 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2276 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2278 static enum netdev_features
2279 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2281 uint32_t ofp10 = ntohl(ofp10_);
2282 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2286 netdev_port_features_to_ofp10(enum netdev_features features)
2288 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2291 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2292 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2293 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2294 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2295 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2296 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2297 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2298 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2299 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2300 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2301 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2302 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2303 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2304 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2305 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2306 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2308 static enum netdev_features
2309 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2311 return ntohl(ofp11) & 0xffff;
2315 netdev_port_features_to_ofp11(enum netdev_features features)
2317 return htonl(features & 0xffff);
2321 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2322 const struct ofp10_phy_port *opp)
2324 memset(pp, 0, sizeof *pp);
2326 pp->port_no = ntohs(opp->port_no);
2327 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2328 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2330 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2331 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2333 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2334 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2335 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2336 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2338 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2339 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2345 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2346 const struct ofp11_port *op)
2350 memset(pp, 0, sizeof *pp);
2352 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2356 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2357 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2359 pp->config = ntohl(op->config) & OFPPC11_ALL;
2360 pp->state = ntohl(op->state) & OFPPC11_ALL;
2362 pp->curr = netdev_port_features_from_ofp11(op->curr);
2363 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2364 pp->supported = netdev_port_features_from_ofp11(op->supported);
2365 pp->peer = netdev_port_features_from_ofp11(op->peer);
2367 pp->curr_speed = ntohl(op->curr_speed);
2368 pp->max_speed = ntohl(op->max_speed);
2374 ofputil_get_phy_port_size(uint8_t ofp_version)
2376 return ofp_version == OFP10_VERSION ? sizeof(struct ofp10_phy_port)
2377 : sizeof(struct ofp11_port);
2381 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2382 struct ofp10_phy_port *opp)
2384 memset(opp, 0, sizeof *opp);
2386 opp->port_no = htons(pp->port_no);
2387 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2388 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2390 opp->config = htonl(pp->config & OFPPC10_ALL);
2391 opp->state = htonl(pp->state & OFPPS10_ALL);
2393 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2394 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2395 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2396 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2400 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2401 struct ofp11_port *op)
2403 memset(op, 0, sizeof *op);
2405 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2406 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2407 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2409 op->config = htonl(pp->config & OFPPC11_ALL);
2410 op->state = htonl(pp->state & OFPPS11_ALL);
2412 op->curr = netdev_port_features_to_ofp11(pp->curr);
2413 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2414 op->supported = netdev_port_features_to_ofp11(pp->supported);
2415 op->peer = netdev_port_features_to_ofp11(pp->peer);
2417 op->curr_speed = htonl(pp->curr_speed);
2418 op->max_speed = htonl(pp->max_speed);
2422 ofputil_put_phy_port(uint8_t ofp_version, const struct ofputil_phy_port *pp,
2425 if (ofp_version == OFP10_VERSION) {
2426 struct ofp10_phy_port *opp;
2427 if (b->size + sizeof *opp <= UINT16_MAX) {
2428 opp = ofpbuf_put_uninit(b, sizeof *opp);
2429 ofputil_encode_ofp10_phy_port(pp, opp);
2432 struct ofp11_port *op;
2433 if (b->size + sizeof *op <= UINT16_MAX) {
2434 op = ofpbuf_put_uninit(b, sizeof *op);
2435 ofputil_encode_ofp11_port(pp, op);
2441 ofputil_append_port_desc_stats_reply(uint8_t ofp_version,
2442 const struct ofputil_phy_port *pp,
2443 struct list *replies)
2445 if (ofp_version == OFP10_VERSION) {
2446 struct ofp10_phy_port *opp;
2448 opp = ofputil_append_stats_reply(sizeof *opp, replies);
2449 ofputil_encode_ofp10_phy_port(pp, opp);
2451 struct ofp11_port *op;
2453 op = ofputil_append_stats_reply(sizeof *op, replies);
2454 ofputil_encode_ofp11_port(pp, op);
2458 /* ofputil_switch_features */
2460 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2461 OFPC_IP_REASM | OFPC_QUEUE_STATS | OFPC_ARP_MATCH_IP)
2462 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2463 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2464 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2465 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2466 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2467 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2469 struct ofputil_action_bit_translation {
2470 enum ofputil_action_bitmap ofputil_bit;
2474 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2475 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2476 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2477 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2478 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2479 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2480 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2481 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2482 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2483 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2484 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2485 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2486 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2490 static const struct ofputil_action_bit_translation of11_action_bits[] = {
2491 { OFPUTIL_A_OUTPUT, OFPAT11_OUTPUT },
2492 { OFPUTIL_A_SET_VLAN_VID, OFPAT11_SET_VLAN_VID },
2493 { OFPUTIL_A_SET_VLAN_PCP, OFPAT11_SET_VLAN_PCP },
2494 { OFPUTIL_A_SET_DL_SRC, OFPAT11_SET_DL_SRC },
2495 { OFPUTIL_A_SET_DL_DST, OFPAT11_SET_DL_DST },
2496 { OFPUTIL_A_SET_NW_SRC, OFPAT11_SET_NW_SRC },
2497 { OFPUTIL_A_SET_NW_DST, OFPAT11_SET_NW_DST },
2498 { OFPUTIL_A_SET_NW_TOS, OFPAT11_SET_NW_TOS },
2499 { OFPUTIL_A_SET_NW_ECN, OFPAT11_SET_NW_ECN },
2500 { OFPUTIL_A_SET_TP_SRC, OFPAT11_SET_TP_SRC },
2501 { OFPUTIL_A_SET_TP_DST, OFPAT11_SET_TP_DST },
2502 { OFPUTIL_A_COPY_TTL_OUT, OFPAT11_COPY_TTL_OUT },
2503 { OFPUTIL_A_COPY_TTL_IN, OFPAT11_COPY_TTL_IN },
2504 { OFPUTIL_A_SET_MPLS_LABEL, OFPAT11_SET_MPLS_LABEL },
2505 { OFPUTIL_A_SET_MPLS_TC, OFPAT11_SET_MPLS_TC },
2506 { OFPUTIL_A_SET_MPLS_TTL, OFPAT11_SET_MPLS_TTL },
2507 { OFPUTIL_A_DEC_MPLS_TTL, OFPAT11_DEC_MPLS_TTL },
2508 { OFPUTIL_A_PUSH_VLAN, OFPAT11_PUSH_VLAN },
2509 { OFPUTIL_A_POP_VLAN, OFPAT11_POP_VLAN },
2510 { OFPUTIL_A_PUSH_MPLS, OFPAT11_PUSH_MPLS },
2511 { OFPUTIL_A_POP_MPLS, OFPAT11_POP_MPLS },
2512 { OFPUTIL_A_SET_QUEUE, OFPAT11_SET_QUEUE },
2513 { OFPUTIL_A_GROUP, OFPAT11_GROUP },
2514 { OFPUTIL_A_SET_NW_TTL, OFPAT11_SET_NW_TTL },
2515 { OFPUTIL_A_DEC_NW_TTL, OFPAT11_DEC_NW_TTL },
2519 static enum ofputil_action_bitmap
2520 decode_action_bits(ovs_be32 of_actions,
2521 const struct ofputil_action_bit_translation *x)
2523 enum ofputil_action_bitmap ofputil_actions;
2525 ofputil_actions = 0;
2526 for (; x->ofputil_bit; x++) {
2527 if (of_actions & htonl(1u << x->of_bit)) {
2528 ofputil_actions |= x->ofputil_bit;
2531 return ofputil_actions;
2534 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2535 * abstract representation in '*features'. Initializes '*b' to iterate over
2536 * the OpenFlow port structures following 'osf' with later calls to
2537 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2538 * OFPERR_* value. */
2540 ofputil_decode_switch_features(const struct ofp_switch_features *osf,
2541 struct ofputil_switch_features *features,
2544 ofpbuf_use_const(b, osf, ntohs(osf->header.length));
2545 ofpbuf_pull(b, sizeof *osf);
2547 features->datapath_id = ntohll(osf->datapath_id);
2548 features->n_buffers = ntohl(osf->n_buffers);
2549 features->n_tables = osf->n_tables;
2551 features->capabilities = ntohl(osf->capabilities) & OFPC_COMMON;
2553 if (b->size % ofputil_get_phy_port_size(osf->header.version)) {
2554 return OFPERR_OFPBRC_BAD_LEN;
2557 if (osf->header.version == OFP10_VERSION) {
2558 if (osf->capabilities & htonl(OFPC10_STP)) {
2559 features->capabilities |= OFPUTIL_C_STP;
2561 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2562 } else if (osf->header.version == OFP11_VERSION) {
2563 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2564 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2566 features->actions = decode_action_bits(osf->actions, of11_action_bits);
2568 return OFPERR_OFPBRC_BAD_VERSION;
2574 /* Returns true if the maximum number of ports are in 'osf'. */
2576 max_ports_in_features(const struct ofp_switch_features *osf)
2578 size_t pp_size = ofputil_get_phy_port_size(osf->header.version);
2579 return ntohs(osf->header.length) + pp_size > UINT16_MAX;
2582 /* Given a buffer 'b' that contains a Features Reply message, checks if
2583 * it contains the maximum number of ports that will fit. If so, it
2584 * returns true and removes the ports from the message. The caller
2585 * should then send an OFPST_PORT_DESC stats request to get the ports,
2586 * since the switch may have more ports than could be represented in the
2587 * Features Reply. Otherwise, returns false.
2590 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2592 struct ofp_switch_features *osf = b->data;
2594 if (max_ports_in_features(osf)) {
2595 /* Remove all the ports. */
2596 b->size = sizeof(*osf);
2597 update_openflow_length(b);
2606 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2607 const struct ofputil_action_bit_translation *x)
2609 uint32_t of_actions;
2612 for (; x->ofputil_bit; x++) {
2613 if (ofputil_actions & x->ofputil_bit) {
2614 of_actions |= 1 << x->of_bit;
2617 return htonl(of_actions);
2620 /* Returns a buffer owned by the caller that encodes 'features' in the format
2621 * required by 'protocol' with the given 'xid'. The caller should append port
2622 * information to the buffer with subsequent calls to
2623 * ofputil_put_switch_features_port(). */
2625 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2626 enum ofputil_protocol protocol, ovs_be32 xid)
2628 struct ofp_switch_features *osf;
2631 osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, xid, &b);
2632 osf->header.version = ofputil_protocol_to_ofp_version(protocol);
2633 osf->datapath_id = htonll(features->datapath_id);
2634 osf->n_buffers = htonl(features->n_buffers);
2635 osf->n_tables = features->n_tables;
2637 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2638 if (osf->header.version == OFP10_VERSION) {
2639 if (features->capabilities & OFPUTIL_C_STP) {
2640 osf->capabilities |= htonl(OFPC10_STP);
2642 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2644 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2645 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2647 osf->actions = encode_action_bits(features->actions, of11_action_bits);
2653 /* Encodes 'pp' into the format required by the switch_features message already
2654 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2655 * and appends the encoded version to 'b'. */
2657 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2660 const struct ofp_switch_features *osf = b->data;
2662 ofputil_put_phy_port(osf->header.version, pp, b);
2665 /* ofputil_port_status */
2667 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2668 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2670 ofputil_decode_port_status(const struct ofp_port_status *ops,
2671 struct ofputil_port_status *ps)
2676 if (ops->reason != OFPPR_ADD &&
2677 ops->reason != OFPPR_DELETE &&
2678 ops->reason != OFPPR_MODIFY) {
2679 return OFPERR_NXBRC_BAD_REASON;
2681 ps->reason = ops->reason;
2683 ofpbuf_use_const(&b, ops, ntohs(ops->header.length));
2684 ofpbuf_pull(&b, sizeof *ops);
2685 retval = ofputil_pull_phy_port(ops->header.version, &b, &ps->desc);
2686 assert(retval != EOF);
2690 /* Converts the abstract form of a "port status" message in '*ps' into an
2691 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2692 * a buffer owned by the caller. */
2694 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2695 enum ofputil_protocol protocol)
2697 struct ofp_port_status *ops;
2700 b = ofpbuf_new(sizeof *ops + sizeof(struct ofp11_port));
2701 ops = put_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, htonl(0), b);
2702 ops->header.version = ofputil_protocol_to_ofp_version(protocol);
2703 ops->reason = ps->reason;
2704 ofputil_put_phy_port(ops->header.version, &ps->desc, b);
2705 update_openflow_length(b);
2709 /* ofputil_port_mod */
2711 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2712 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2714 ofputil_decode_port_mod(const struct ofp_header *oh,
2715 struct ofputil_port_mod *pm)
2717 if (oh->version == OFP10_VERSION) {
2718 const struct ofp10_port_mod *opm = (const struct ofp10_port_mod *) oh;
2720 if (oh->length != htons(sizeof *opm)) {
2721 return OFPERR_OFPBRC_BAD_LEN;
2724 pm->port_no = ntohs(opm->port_no);
2725 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2726 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2727 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2728 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2729 } else if (oh->version == OFP11_VERSION) {
2730 const struct ofp11_port_mod *opm = (const struct ofp11_port_mod *) oh;
2733 if (oh->length != htons(sizeof *opm)) {
2734 return OFPERR_OFPBRC_BAD_LEN;
2737 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2742 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2743 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2744 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2745 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2747 return OFPERR_OFPBRC_BAD_VERSION;
2750 pm->config &= pm->mask;
2754 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2755 * message suitable for 'protocol', and returns that encoded form in a buffer
2756 * owned by the caller. */
2758 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2759 enum ofputil_protocol protocol)
2761 uint8_t ofp_version = ofputil_protocol_to_ofp_version(protocol);
2764 if (ofp_version == OFP10_VERSION) {
2765 struct ofp10_port_mod *opm;
2767 opm = make_openflow(sizeof *opm, OFPT10_PORT_MOD, &b);
2768 opm->port_no = htons(pm->port_no);
2769 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2770 opm->config = htonl(pm->config & OFPPC10_ALL);
2771 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2772 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2773 } else if (ofp_version == OFP11_VERSION) {
2774 struct ofp11_port_mod *opm;
2776 opm = make_openflow(sizeof *opm, OFPT11_PORT_MOD, &b);
2777 opm->port_no = htonl(pm->port_no);
2778 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2779 opm->config = htonl(pm->config & OFPPC11_ALL);
2780 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2781 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2790 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
2792 struct ofp_packet_out *opo;
2797 actions_len = po->n_actions * sizeof *po->actions;
2798 size = sizeof *opo + actions_len;
2799 if (po->buffer_id == UINT32_MAX) {
2800 size += po->packet_len;
2803 msg = ofpbuf_new(size);
2804 opo = put_openflow(sizeof *opo, OFPT10_PACKET_OUT, msg);
2805 opo->buffer_id = htonl(po->buffer_id);
2806 opo->in_port = htons(po->in_port);
2807 opo->actions_len = htons(actions_len);
2808 ofpbuf_put(msg, po->actions, actions_len);
2809 if (po->buffer_id == UINT32_MAX) {
2810 ofpbuf_put(msg, po->packet, po->packet_len);
2812 update_openflow_length(msg);
2817 /* Returns a string representing the message type of 'type'. The string is the
2818 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
2819 * messages, the constant is followed by "request" or "reply",
2820 * e.g. "OFPST_AGGREGATE reply". */
2822 ofputil_msg_type_name(const struct ofputil_msg_type *type)
2827 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2828 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2829 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
2832 * The caller is responsible for freeing '*bufferp' when it is no longer
2835 * The OpenFlow header length is initially set to 'openflow_len'; if the
2836 * message is later extended, the length should be updated with
2837 * update_openflow_length() before sending.
2839 * Returns the header. */
2841 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
2843 *bufferp = ofpbuf_new(openflow_len);
2844 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
2847 /* Similar to make_openflow() but creates a Nicira vendor extension message
2848 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2850 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
2852 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
2855 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2856 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2857 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
2860 * The caller is responsible for freeing '*bufferp' when it is no longer
2863 * The OpenFlow header length is initially set to 'openflow_len'; if the
2864 * message is later extended, the length should be updated with
2865 * update_openflow_length() before sending.
2867 * Returns the header. */
2869 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
2870 struct ofpbuf **bufferp)
2872 *bufferp = ofpbuf_new(openflow_len);
2873 return put_openflow_xid(openflow_len, type, xid, *bufferp);
2876 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
2877 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2879 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2880 struct ofpbuf **bufferp)
2882 *bufferp = ofpbuf_new(openflow_len);
2883 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
2886 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2887 * with the given 'type' and an arbitrary transaction id. Allocated bytes
2888 * beyond the header, if any, are zeroed.
2890 * The OpenFlow header length is initially set to 'openflow_len'; if the
2891 * message is later extended, the length should be updated with
2892 * update_openflow_length() before sending.
2894 * Returns the header. */
2896 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
2898 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
2901 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2902 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
2903 * the header, if any, are zeroed.
2905 * The OpenFlow header length is initially set to 'openflow_len'; if the
2906 * message is later extended, the length should be updated with
2907 * update_openflow_length() before sending.
2909 * Returns the header. */
2911 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
2912 struct ofpbuf *buffer)
2914 struct ofp_header *oh;
2916 assert(openflow_len >= sizeof *oh);
2917 assert(openflow_len <= UINT16_MAX);
2919 oh = ofpbuf_put_uninit(buffer, openflow_len);
2920 oh->version = OFP10_VERSION;
2922 oh->length = htons(openflow_len);
2924 memset(oh + 1, 0, openflow_len - sizeof *oh);
2928 /* Similar to put_openflow() but append a Nicira vendor extension message with
2929 * the specific 'subtype'. 'subtype' should be in host byte order. */
2931 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
2933 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
2936 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
2937 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2939 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2940 struct ofpbuf *buffer)
2942 struct nicira_header *nxh;
2944 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
2945 nxh->vendor = htonl(NX_VENDOR_ID);
2946 nxh->subtype = htonl(subtype);
2950 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
2951 * 'buffer->size'. */
2953 update_openflow_length(struct ofpbuf *buffer)
2955 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
2956 oh->length = htons(buffer->size);
2960 put_stats__(ovs_be32 xid, uint8_t ofp_type,
2961 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
2964 if (ofpst_type == htons(OFPST_VENDOR)) {
2965 struct nicira_stats_msg *nsm;
2967 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
2968 nsm->vsm.osm.type = ofpst_type;
2969 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
2970 nsm->subtype = nxst_subtype;
2972 struct ofp_stats_msg *osm;
2974 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
2975 osm->type = ofpst_type;
2979 /* Creates a statistics request message with total length 'openflow_len'
2980 * (including all headers) and the given 'ofpst_type', and stores the buffer
2981 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
2982 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
2983 * subtype (otherwise 'nxst_subtype' is ignored).
2985 * Initializes bytes following the headers to all-bits-zero.
2987 * Returns the first byte of the new message. */
2989 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
2990 uint32_t nxst_subtype, struct ofpbuf **bufferp)
2994 msg = *bufferp = ofpbuf_new(openflow_len);
2995 put_stats__(alloc_xid(), OFPT10_STATS_REQUEST,
2996 htons(ofpst_type), htonl(nxst_subtype), msg);
2997 ofpbuf_padto(msg, openflow_len);
3003 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
3005 assert(request->header.type == OFPT10_STATS_REQUEST ||
3006 request->header.type == OFPT10_STATS_REPLY);
3007 put_stats__(request->header.xid, OFPT10_STATS_REPLY, request->type,
3008 (request->type != htons(OFPST_VENDOR)
3010 : ((const struct nicira_stats_msg *) request)->subtype),
3014 /* Creates a statistics reply message with total length 'openflow_len'
3015 * (including all headers) and the same type (either a standard OpenFlow
3016 * statistics type or a Nicira extension type and subtype) as 'request', and
3017 * stores the buffer containing the new message in '*bufferp'.
3019 * Initializes bytes following the headers to all-bits-zero.
3021 * Returns the first byte of the new message. */
3023 ofputil_make_stats_reply(size_t openflow_len,
3024 const struct ofp_stats_msg *request,
3025 struct ofpbuf **bufferp)
3029 msg = *bufferp = ofpbuf_new(openflow_len);
3030 put_stats_reply__(request, msg);
3031 ofpbuf_padto(msg, openflow_len);
3036 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
3037 * replies to 'request', which should be an OpenFlow or Nicira extension
3038 * statistics request. Initially 'replies' will have a single reply message
3039 * that has only a header. The functions ofputil_reserve_stats_reply() and
3040 * ofputil_append_stats_reply() may be used to add to the reply. */
3042 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
3043 struct list *replies)
3047 msg = ofpbuf_new(1024);
3048 put_stats_reply__(request, msg);
3051 list_push_back(replies, &msg->list_node);
3054 /* Prepares to append up to 'len' bytes to the series of statistics replies in
3055 * 'replies', which should have been initialized with
3056 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
3057 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
3058 * do so with e.g. ofpbuf_put_uninit().) */
3060 ofputil_reserve_stats_reply(size_t len, struct list *replies)
3062 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
3063 struct ofp_stats_msg *osm = msg->data;
3065 if (msg->size + len <= UINT16_MAX) {
3066 ofpbuf_prealloc_tailroom(msg, len);
3068 osm->flags |= htons(OFPSF_REPLY_MORE);
3070 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
3071 put_stats_reply__(osm, msg);
3072 list_push_back(replies, &msg->list_node);
3077 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
3078 * returns the first byte. */
3080 ofputil_append_stats_reply(size_t len, struct list *replies)
3082 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
3085 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
3087 ofputil_stats_body(const struct ofp_header *oh)
3089 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
3090 return (const struct ofp_stats_msg *) oh + 1;
3093 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
3095 ofputil_stats_body_len(const struct ofp_header *oh)
3097 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
3098 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
3101 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
3103 ofputil_nxstats_body(const struct ofp_header *oh)
3105 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
3106 return ((const struct nicira_stats_msg *) oh) + 1;
3109 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
3111 ofputil_nxstats_body_len(const struct ofp_header *oh)
3113 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
3114 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
3118 make_flow_mod(uint16_t command, const struct cls_rule *rule,
3121 struct ofp_flow_mod *ofm;
3122 size_t size = sizeof *ofm + actions_len;
3123 struct ofpbuf *out = ofpbuf_new(size);
3124 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
3125 ofm->header.version = OFP10_VERSION;
3126 ofm->header.type = OFPT10_FLOW_MOD;
3127 ofm->header.length = htons(size);
3129 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
3130 ofputil_cls_rule_to_match(rule, &ofm->match);
3131 ofm->command = htons(command);
3136 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
3137 uint16_t idle_timeout, size_t actions_len)
3139 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
3140 struct ofp_flow_mod *ofm = out->data;
3141 ofm->idle_timeout = htons(idle_timeout);
3142 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
3143 ofm->buffer_id = htonl(buffer_id);
3148 make_del_flow(const struct cls_rule *rule)
3150 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
3151 struct ofp_flow_mod *ofm = out->data;
3152 ofm->out_port = htons(OFPP_NONE);
3157 make_add_simple_flow(const struct cls_rule *rule,
3158 uint32_t buffer_id, uint16_t out_port,
3159 uint16_t idle_timeout)
3161 if (out_port != OFPP_NONE) {
3162 struct ofp_action_output *oao;
3163 struct ofpbuf *buffer;
3165 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
3166 ofputil_put_OFPAT10_OUTPUT(buffer)->port = htons(out_port);
3169 return make_add_flow(rule, buffer_id, idle_timeout, 0);
3174 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
3175 const struct ofpbuf *payload, int max_send_len)
3177 struct ofp_packet_in *opi;
3181 send_len = MIN(max_send_len, payload->size);
3182 buf = ofpbuf_new(sizeof *opi + send_len);
3183 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
3184 OFPT_PACKET_IN, 0, buf);
3185 opi->buffer_id = htonl(buffer_id);
3186 opi->total_len = htons(payload->size);
3187 opi->in_port = htons(in_port);
3188 opi->reason = reason;
3189 ofpbuf_put(buf, payload->data, send_len);
3190 update_openflow_length(buf);
3195 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3197 make_echo_request(void)
3199 struct ofp_header *rq;
3200 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
3201 rq = ofpbuf_put_uninit(out, sizeof *rq);
3202 rq->version = OFP10_VERSION;
3203 rq->type = OFPT_ECHO_REQUEST;
3204 rq->length = htons(sizeof *rq);
3209 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3210 * OFPT_ECHO_REQUEST message in 'rq'. */
3212 make_echo_reply(const struct ofp_header *rq)
3214 size_t size = ntohs(rq->length);
3215 struct ofpbuf *out = ofpbuf_new(size);
3216 struct ofp_header *reply = ofpbuf_put(out, rq, size);
3217 reply->type = OFPT_ECHO_REPLY;
3222 ofputil_encode_barrier_request(void)
3226 make_openflow(sizeof(struct ofp_header), OFPT10_BARRIER_REQUEST, &msg);
3231 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3233 switch (flags & OFPC_FRAG_MASK) {
3234 case OFPC_FRAG_NORMAL: return "normal";
3235 case OFPC_FRAG_DROP: return "drop";
3236 case OFPC_FRAG_REASM: return "reassemble";
3237 case OFPC_FRAG_NX_MATCH: return "nx-match";
3244 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3246 if (!strcasecmp(s, "normal")) {
3247 *flags = OFPC_FRAG_NORMAL;
3248 } else if (!strcasecmp(s, "drop")) {
3249 *flags = OFPC_FRAG_DROP;
3250 } else if (!strcasecmp(s, "reassemble")) {
3251 *flags = OFPC_FRAG_REASM;
3252 } else if (!strcasecmp(s, "nx-match")) {
3253 *flags = OFPC_FRAG_NX_MATCH;
3260 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3261 * port number and stores the latter in '*ofp10_port', for the purpose of
3262 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3263 * otherwise an OFPERR_* number.
3265 * See the definition of OFP11_MAX for an explanation of the mapping. */
3267 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3269 uint32_t ofp11_port_h = ntohl(ofp11_port);
3271 if (ofp11_port_h < OFPP_MAX) {
3272 *ofp10_port = ofp11_port_h;
3274 } else if (ofp11_port_h >= OFPP11_MAX) {
3275 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3278 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3279 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3280 ofp11_port_h, OFPP_MAX - 1,
3281 (uint32_t) OFPP11_MAX, UINT32_MAX);
3282 return OFPERR_OFPBAC_BAD_OUT_PORT;
3286 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3287 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3289 * See the definition of OFP11_MAX for an explanation of the mapping. */
3291 ofputil_port_to_ofp11(uint16_t ofp10_port)
3293 return htonl(ofp10_port < OFPP_MAX
3295 : ofp10_port + OFPP11_OFFSET);
3298 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3299 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3300 * 'port' is valid, otherwise an OpenFlow return code. */
3302 ofputil_check_output_port(uint16_t port, int max_ports)
3310 case OFPP_CONTROLLER:
3316 if (port < max_ports) {
3319 return OFPERR_OFPBAC_BAD_OUT_PORT;
3323 #define OFPUTIL_NAMED_PORTS \
3324 OFPUTIL_NAMED_PORT(IN_PORT) \
3325 OFPUTIL_NAMED_PORT(TABLE) \
3326 OFPUTIL_NAMED_PORT(NORMAL) \
3327 OFPUTIL_NAMED_PORT(FLOOD) \
3328 OFPUTIL_NAMED_PORT(ALL) \
3329 OFPUTIL_NAMED_PORT(CONTROLLER) \
3330 OFPUTIL_NAMED_PORT(LOCAL) \
3331 OFPUTIL_NAMED_PORT(NONE)
3333 /* Checks whether 's' is the string representation of an OpenFlow port number,
3334 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
3335 * number in '*port' and returns true. Otherwise, returns false. */
3337 ofputil_port_from_string(const char *name, uint16_t *port)
3343 static const struct pair pairs[] = {
3344 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3346 #undef OFPUTIL_NAMED_PORT
3348 static const int n_pairs = ARRAY_SIZE(pairs);
3351 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
3356 for (i = 0; i < n_pairs; i++) {
3357 if (!strcasecmp(name, pairs[i].name)) {
3358 *port = pairs[i].value;
3365 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3366 * Most ports' string representation is just the port number, but for special
3367 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3369 ofputil_format_port(uint16_t port, struct ds *s)
3374 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3376 #undef OFPUTIL_NAMED_PORT
3379 ds_put_format(s, "%"PRIu16, port);
3382 ds_put_cstr(s, name);
3385 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3386 * 'ofp_version', tries to pull the first element from the array. If
3387 * successful, initializes '*pp' with an abstract representation of the
3388 * port and returns 0. If no ports remain to be decoded, returns EOF.
3389 * On an error, returns a positive OFPERR_* value. */
3391 ofputil_pull_phy_port(uint8_t ofp_version, struct ofpbuf *b,
3392 struct ofputil_phy_port *pp)
3394 if (ofp_version == OFP10_VERSION) {
3395 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3396 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3398 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3399 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3403 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3404 * 'ofp_version', returns the number of elements. */
3405 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3407 return b->size / ofputil_get_phy_port_size(ofp_version);
3411 check_resubmit_table(const struct nx_action_resubmit *nar)
3413 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
3414 return OFPERR_OFPBAC_BAD_ARGUMENT;
3420 check_output_reg(const struct nx_action_output_reg *naor,
3421 const struct flow *flow)
3423 struct mf_subfield src;
3426 for (i = 0; i < sizeof naor->zero; i++) {
3427 if (naor->zero[i]) {
3428 return OFPERR_OFPBAC_BAD_ARGUMENT;
3432 nxm_decode(&src, naor->src, naor->ofs_nbits);
3433 return mf_check_src(&src, flow);
3437 validate_actions(const union ofp_action *actions, size_t n_actions,
3438 const struct flow *flow, int max_ports)
3440 const union ofp_action *a;
3443 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
3448 code = ofputil_decode_action(a);
3451 VLOG_WARN_RL(&bad_ofmsg_rl,
3452 "action decoding error at offset %td (%s)",
3453 (a - actions) * sizeof *a, ofperr_get_name(error));
3459 switch ((enum ofputil_action_code) code) {
3460 case OFPUTIL_OFPAT10_OUTPUT:
3461 error = ofputil_check_output_port(ntohs(a->output.port),
3465 case OFPUTIL_OFPAT10_SET_VLAN_VID:
3466 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
3467 error = OFPERR_OFPBAC_BAD_ARGUMENT;
3471 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
3472 if (a->vlan_pcp.vlan_pcp & ~7) {
3473 error = OFPERR_OFPBAC_BAD_ARGUMENT;
3477 case OFPUTIL_OFPAT10_ENQUEUE:
3478 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
3479 if (port >= max_ports && port != OFPP_IN_PORT
3480 && port != OFPP_LOCAL) {
3481 error = OFPERR_OFPBAC_BAD_OUT_PORT;
3485 case OFPUTIL_NXAST_REG_MOVE:
3486 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
3490 case OFPUTIL_NXAST_REG_LOAD:
3491 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
3495 case OFPUTIL_NXAST_MULTIPATH:
3496 error = multipath_check((const struct nx_action_multipath *) a,
3500 case OFPUTIL_NXAST_AUTOPATH:
3501 error = autopath_check((const struct nx_action_autopath *) a,
3505 case OFPUTIL_NXAST_BUNDLE:
3506 case OFPUTIL_NXAST_BUNDLE_LOAD:
3507 error = bundle_check((const struct nx_action_bundle *) a,
3511 case OFPUTIL_NXAST_OUTPUT_REG:
3512 error = check_output_reg((const struct nx_action_output_reg *) a,
3516 case OFPUTIL_NXAST_RESUBMIT_TABLE:
3517 error = check_resubmit_table(
3518 (const struct nx_action_resubmit *) a);
3521 case OFPUTIL_NXAST_LEARN:
3522 error = learn_check((const struct nx_action_learn *) a, flow);
3525 case OFPUTIL_NXAST_CONTROLLER:
3526 if (((const struct nx_action_controller *) a)->zero) {
3527 error = OFPERR_NXBAC_MUST_BE_ZERO;
3531 case OFPUTIL_OFPAT10_STRIP_VLAN:
3532 case OFPUTIL_OFPAT10_SET_NW_SRC:
3533 case OFPUTIL_OFPAT10_SET_NW_DST:
3534 case OFPUTIL_OFPAT10_SET_NW_TOS:
3535 case OFPUTIL_OFPAT10_SET_TP_SRC:
3536 case OFPUTIL_OFPAT10_SET_TP_DST:
3537 case OFPUTIL_OFPAT10_SET_DL_SRC:
3538 case OFPUTIL_OFPAT10_SET_DL_DST:
3539 case OFPUTIL_NXAST_RESUBMIT:
3540 case OFPUTIL_NXAST_SET_TUNNEL:
3541 case OFPUTIL_NXAST_SET_QUEUE:
3542 case OFPUTIL_NXAST_POP_QUEUE:
3543 case OFPUTIL_NXAST_NOTE:
3544 case OFPUTIL_NXAST_SET_TUNNEL64:
3545 case OFPUTIL_NXAST_EXIT:
3546 case OFPUTIL_NXAST_DEC_TTL:
3547 case OFPUTIL_NXAST_FIN_TIMEOUT:
3552 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
3553 (a - actions) * sizeof *a, ofperr_get_name(error));
3558 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
3559 (n_actions - left) * sizeof *a);
3560 return OFPERR_OFPBAC_BAD_LEN;
3565 struct ofputil_action {
3567 unsigned int min_len;
3568 unsigned int max_len;
3571 static const struct ofputil_action action_bad_type
3572 = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX };
3573 static const struct ofputil_action action_bad_len
3574 = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX };
3575 static const struct ofputil_action action_bad_vendor
3576 = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX };
3578 static const struct ofputil_action *
3579 ofputil_decode_ofpat_action(const union ofp_action *a)
3581 enum ofp10_action_type type = ntohs(a->type);
3584 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3586 static const struct ofputil_action action = { \
3588 sizeof(struct STRUCT), \
3589 sizeof(struct STRUCT) \
3593 #include "ofp-util.def"
3595 case OFPAT10_VENDOR:
3597 return &action_bad_type;
3601 static const struct ofputil_action *
3602 ofputil_decode_nxast_action(const union ofp_action *a)
3604 const struct nx_action_header *nah = (const struct nx_action_header *) a;
3605 enum nx_action_subtype subtype = ntohs(nah->subtype);
3608 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3610 static const struct ofputil_action action = { \
3612 sizeof(struct STRUCT), \
3613 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
3617 #include "ofp-util.def"
3619 case NXAST_SNAT__OBSOLETE:
3620 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
3622 return &action_bad_type;
3626 /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT10_* or
3627 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error
3630 * The caller must have already verified that 'a''s length is correct (that is,
3631 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
3632 * longer than the amount of space allocated to 'a').
3634 * This function verifies that 'a''s length is correct for the type of action
3635 * that it represents. */
3637 ofputil_decode_action(const union ofp_action *a)
3639 const struct ofputil_action *action;
3640 uint16_t len = ntohs(a->header.len);
3642 if (a->type != htons(OFPAT10_VENDOR)) {
3643 action = ofputil_decode_ofpat_action(a);
3645 switch (ntohl(a->vendor.vendor)) {
3647 if (len < sizeof(struct nx_action_header)) {
3648 return -OFPERR_OFPBAC_BAD_LEN;
3650 action = ofputil_decode_nxast_action(a);
3653 action = &action_bad_vendor;
3658 return (len >= action->min_len && len <= action->max_len
3660 : -OFPERR_OFPBAC_BAD_LEN);
3663 /* Parses 'a' and returns its type as an OFPUTIL_OFPAT10_* or OFPUTIL_NXAST_*
3664 * constant. The caller must have already validated that 'a' is a valid action
3665 * understood by Open vSwitch (e.g. by a previous successful call to
3666 * ofputil_decode_action()). */
3667 enum ofputil_action_code
3668 ofputil_decode_action_unsafe(const union ofp_action *a)
3670 const struct ofputil_action *action;
3672 if (a->type != htons(OFPAT10_VENDOR)) {
3673 action = ofputil_decode_ofpat_action(a);
3675 action = ofputil_decode_nxast_action(a);
3678 return action->code;
3681 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3682 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3683 * 'name' is not the name of any action.
3685 * ofp-util.def lists the mapping from names to action. */
3687 ofputil_action_code_from_name(const char *name)
3689 static const char *names[OFPUTIL_N_ACTIONS] = {
3690 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3691 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3692 #include "ofp-util.def"
3697 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3698 if (*p && !strcasecmp(name, *p)) {
3705 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3706 * action. Initializes the parts of 'action' that identify it as having type
3707 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3708 * have variable length, the length used and cleared is that of struct
3711 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3714 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3715 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3716 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3717 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3718 #include "ofp-util.def"
3723 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3725 ofputil_init_##ENUM(struct STRUCT *s) \
3727 memset(s, 0, sizeof *s); \
3728 s->type = htons(ENUM); \
3729 s->len = htons(sizeof *s); \
3733 ofputil_put_##ENUM(struct ofpbuf *buf) \
3735 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3736 ofputil_init_##ENUM(s); \
3739 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3741 ofputil_init_##ENUM(struct STRUCT *s) \
3743 memset(s, 0, sizeof *s); \
3744 s->type = htons(OFPAT10_VENDOR); \
3745 s->len = htons(sizeof *s); \
3746 s->vendor = htonl(NX_VENDOR_ID); \
3747 s->subtype = htons(ENUM); \
3751 ofputil_put_##ENUM(struct ofpbuf *buf) \
3753 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3754 ofputil_init_##ENUM(s); \
3757 #include "ofp-util.def"
3759 /* Returns true if 'action' outputs to 'port', false otherwise. */
3761 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
3763 switch (ofputil_decode_action(action)) {
3764 case OFPUTIL_OFPAT10_OUTPUT:
3765 return action->output.port == port;
3766 case OFPUTIL_OFPAT10_ENQUEUE:
3767 return ((const struct ofp_action_enqueue *) action)->port == port;
3768 case OFPUTIL_NXAST_CONTROLLER:
3769 return port == htons(OFPP_CONTROLLER);
3775 /* "Normalizes" the wildcards in 'rule'. That means:
3777 * 1. If the type of level N is known, then only the valid fields for that
3778 * level may be specified. For example, ARP does not have a TOS field,
3779 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3780 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3781 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3784 * 2. If the type of level N is not known (or not understood by Open
3785 * vSwitch), then no fields at all for that level may be specified. For
3786 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3787 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3791 ofputil_normalize_rule(struct cls_rule *rule)
3794 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3795 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3796 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3797 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3798 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3799 MAY_ARP_THA = 1 << 5, /* arp_tha */
3800 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3801 MAY_ND_TARGET = 1 << 7 /* nd_target */
3804 struct flow_wildcards wc;
3806 /* Figure out what fields may be matched. */
3807 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
3808 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3809 if (rule->flow.nw_proto == IPPROTO_TCP ||
3810 rule->flow.nw_proto == IPPROTO_UDP ||
3811 rule->flow.nw_proto == IPPROTO_ICMP) {
3812 may_match |= MAY_TP_ADDR;
3814 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3815 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3816 if (rule->flow.nw_proto == IPPROTO_TCP ||
3817 rule->flow.nw_proto == IPPROTO_UDP) {
3818 may_match |= MAY_TP_ADDR;
3819 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3820 may_match |= MAY_TP_ADDR;
3821 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3822 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3823 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3824 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3827 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
3828 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3833 /* Clear the fields that may not be matched. */
3835 if (!(may_match & MAY_NW_ADDR)) {
3836 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3838 if (!(may_match & MAY_TP_ADDR)) {
3839 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
3841 if (!(may_match & MAY_NW_PROTO)) {
3842 wc.wildcards |= FWW_NW_PROTO;
3844 if (!(may_match & MAY_IPVx)) {
3845 wc.wildcards |= FWW_NW_DSCP;
3846 wc.wildcards |= FWW_NW_ECN;
3847 wc.wildcards |= FWW_NW_TTL;
3849 if (!(may_match & MAY_ARP_SHA)) {
3850 wc.wildcards |= FWW_ARP_SHA;
3852 if (!(may_match & MAY_ARP_THA)) {
3853 wc.wildcards |= FWW_ARP_THA;
3855 if (!(may_match & MAY_IPV6)) {
3856 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
3857 wc.wildcards |= FWW_IPV6_LABEL;
3859 if (!(may_match & MAY_ND_TARGET)) {
3860 wc.nd_target_mask = in6addr_any;
3863 /* Log any changes. */
3864 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3865 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
3866 char *pre = log ? cls_rule_to_string(rule) : NULL;
3869 cls_rule_zero_wildcarded_fields(rule);
3872 char *post = cls_rule_to_string(rule);
3873 VLOG_INFO("normalization changed ofp_match, details:");
3874 VLOG_INFO(" pre: %s", pre);
3875 VLOG_INFO("post: %s", post);
3882 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
3883 * successful, otherwise an OpenFlow error.
3885 * If successful, the first action is stored in '*actionsp' and the number of
3886 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
3887 * are stored, respectively.
3889 * This function does not check that the actions are valid (the caller should
3890 * do so, with validate_actions()). The caller is also responsible for making
3891 * sure that 'b->data' is initially aligned appropriately for "union
3894 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
3895 union ofp_action **actionsp, size_t *n_actionsp)
3897 if (actions_len % OFP_ACTION_ALIGN != 0) {
3898 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3899 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
3903 *actionsp = ofpbuf_try_pull(b, actions_len);
3904 if (*actionsp == NULL) {
3905 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3906 "exceeds remaining message length (%zu)",
3907 actions_len, b->size);
3911 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
3917 return OFPERR_OFPBRC_BAD_LEN;
3921 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
3922 const union ofp_action *b, size_t n_b)
3924 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
3928 ofputil_actions_clone(const union ofp_action *actions, size_t n)
3930 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
3933 /* Parses a key or a key-value pair from '*stringp'.
3935 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3936 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3937 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3938 * are substrings of '*stringp' created by replacing some of its bytes by null
3939 * terminators. Returns true.
3941 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3942 * NULL and returns false. */
3944 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3946 char *pos, *key, *value;
3950 pos += strspn(pos, ", \t\r\n");
3952 *keyp = *valuep = NULL;
3957 key_len = strcspn(pos, ":=(, \t\r\n");
3958 if (key[key_len] == ':' || key[key_len] == '=') {
3959 /* The value can be separated by a colon. */
3962 value = key + key_len + 1;
3963 value_len = strcspn(value, ", \t\r\n");
3964 pos = value + value_len + (value[value_len] != '\0');
3965 value[value_len] = '\0';
3966 } else if (key[key_len] == '(') {
3967 /* The value can be surrounded by balanced parentheses. The outermost
3968 * set of parentheses is removed. */
3972 value = key + key_len + 1;
3973 for (value_len = 0; level > 0; value_len++) {
3974 switch (value[value_len]) {
3988 value[value_len - 1] = '\0';
3989 pos = value + value_len;
3991 /* There might be no value at all. */
3992 value = key + key_len; /* Will become the empty string below. */
3993 pos = key + key_len + (key[key_len] != '\0');
3995 key[key_len] = '\0';