2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "multipath.h"
32 #include "meta-flow.h"
34 #include "ofp-errors.h"
39 #include "unaligned.h"
40 #include "type-props.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_util);
45 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
46 * in the peer and so there's not much point in showing a lot of them. */
47 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
49 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
50 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
53 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
54 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
55 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
56 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
59 ofputil_wcbits_to_netmask(int wcbits)
62 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
65 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
66 * that it wildcards, that is, the number of 0-bits in 'netmask'. 'netmask'
67 * must be a CIDR netmask (see ip_is_cidr()). */
69 ofputil_netmask_to_wcbits(ovs_be32 netmask)
71 return 32 - ip_count_cidr_bits(netmask);
74 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
76 #define WC_INVARIANT_LIST \
77 WC_INVARIANT_BIT(IN_PORT) \
78 WC_INVARIANT_BIT(DL_SRC) \
79 WC_INVARIANT_BIT(DL_DST) \
80 WC_INVARIANT_BIT(DL_TYPE) \
81 WC_INVARIANT_BIT(NW_PROTO)
83 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
84 * actually have the same names and values. */
85 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW_##NAME);
87 #undef WC_INVARIANT_BIT
89 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
91 static const flow_wildcards_t WC_INVARIANTS = 0
92 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
94 #undef WC_INVARIANT_BIT
97 /* Converts the wildcard in 'ofpfw' into a flow_wildcards in 'wc' for use in
98 * struct cls_rule. It is the caller's responsibility to handle the special
99 * case where the flow match's dl_vlan is set to OFP_VLAN_NONE. */
101 ofputil_wildcard_from_openflow(uint32_t ofpfw, struct flow_wildcards *wc)
103 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
105 /* Initialize most of rule->wc. */
106 flow_wildcards_init_catchall(wc);
107 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
109 /* Wildcard fields that aren't defined by ofp_match or tun_id. */
110 wc->wildcards |= (FWW_ARP_SHA | FWW_ARP_THA | FWW_NW_ECN | FWW_NW_TTL
111 | FWW_ND_TARGET | FWW_IPV6_LABEL);
113 if (ofpfw & OFPFW_NW_TOS) {
114 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
115 * the enum than we can use. */
116 wc->wildcards |= FWW_NW_DSCP;
119 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
120 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
122 if (!(ofpfw & OFPFW_TP_SRC)) {
123 wc->tp_src_mask = htons(UINT16_MAX);
125 if (!(ofpfw & OFPFW_TP_DST)) {
126 wc->tp_dst_mask = htons(UINT16_MAX);
129 if (ofpfw & OFPFW_DL_DST) {
130 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
131 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
132 * and FWW_ETH_MCAST. */
133 wc->wildcards |= FWW_ETH_MCAST;
137 if (!(ofpfw & OFPFW_DL_VLAN_PCP)) {
138 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
140 if (!(ofpfw & OFPFW_DL_VLAN)) {
141 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
145 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
148 ofputil_cls_rule_from_match(const struct ofp_match *match,
149 unsigned int priority, struct cls_rule *rule)
151 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW_ALL;
153 /* Initialize rule->priority, rule->wc. */
154 rule->priority = !ofpfw ? UINT16_MAX : priority;
155 ofputil_wildcard_from_openflow(ofpfw, &rule->wc);
157 /* Initialize most of rule->flow. */
158 rule->flow.nw_src = match->nw_src;
159 rule->flow.nw_dst = match->nw_dst;
160 rule->flow.in_port = ntohs(match->in_port);
161 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
162 rule->flow.tp_src = match->tp_src;
163 rule->flow.tp_dst = match->tp_dst;
164 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
165 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
166 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
167 rule->flow.nw_proto = match->nw_proto;
169 /* Translate VLANs. */
170 if (!(ofpfw & OFPFW_DL_VLAN) && match->dl_vlan == htons(OFP_VLAN_NONE)) {
171 /* Match only packets without 802.1Q header.
173 * When OFPFW_DL_VLAN_PCP is wildcarded, this is obviously correct.
175 * If OFPFW_DL_VLAN_PCP is matched, the flow match is contradictory,
176 * because we can't have a specific PCP without an 802.1Q header.
177 * However, older versions of OVS treated this as matching packets
178 * withut an 802.1Q header, so we do here too. */
179 rule->flow.vlan_tci = htons(0);
180 rule->wc.vlan_tci_mask = htons(0xffff);
182 ovs_be16 vid, pcp, tci;
184 vid = match->dl_vlan & htons(VLAN_VID_MASK);
185 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
186 tci = vid | pcp | htons(VLAN_CFI);
187 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
191 cls_rule_zero_wildcarded_fields(rule);
194 /* Convert 'rule' into the OpenFlow match structure 'match'. */
196 ofputil_cls_rule_to_match(const struct cls_rule *rule, struct ofp_match *match)
198 const struct flow_wildcards *wc = &rule->wc;
201 /* Figure out most OpenFlow wildcards. */
202 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
203 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
204 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
205 if (wc->wildcards & FWW_NW_DSCP) {
206 ofpfw |= OFPFW_NW_TOS;
208 if (!wc->tp_src_mask) {
209 ofpfw |= OFPFW_TP_SRC;
211 if (!wc->tp_dst_mask) {
212 ofpfw |= OFPFW_TP_DST;
215 /* Translate VLANs. */
216 match->dl_vlan = htons(0);
217 match->dl_vlan_pcp = 0;
218 if (rule->wc.vlan_tci_mask == htons(0)) {
219 ofpfw |= OFPFW_DL_VLAN | OFPFW_DL_VLAN_PCP;
220 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
221 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
222 match->dl_vlan = htons(OFP_VLAN_NONE);
224 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
225 ofpfw |= OFPFW_DL_VLAN;
227 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
230 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
231 ofpfw |= OFPFW_DL_VLAN_PCP;
233 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
237 /* Compose most of the match structure. */
238 match->wildcards = htonl(ofpfw);
239 match->in_port = htons(rule->flow.in_port);
240 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
241 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
242 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
243 match->nw_src = rule->flow.nw_src;
244 match->nw_dst = rule->flow.nw_dst;
245 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
246 match->nw_proto = rule->flow.nw_proto;
247 match->tp_src = rule->flow.tp_src;
248 match->tp_dst = rule->flow.tp_dst;
249 memset(match->pad1, '\0', sizeof match->pad1);
250 memset(match->pad2, '\0', sizeof match->pad2);
253 /* Given a 'dl_type' value in the format used in struct flow, returns the
254 * corresponding 'dl_type' value for use in an OpenFlow ofp_match structure. */
256 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
258 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
259 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
263 /* Given a 'dl_type' value in the format used in an OpenFlow ofp_match
264 * structure, returns the corresponding 'dl_type' value for use in struct
267 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
269 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
270 ? htons(FLOW_DL_TYPE_NONE)
274 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
278 static uint32_t next_xid = 1;
279 return htonl(next_xid++);
282 /* Basic parsing of OpenFlow messages. */
284 struct ofputil_msg_type {
285 enum ofputil_msg_code code; /* OFPUTIL_*. */
286 uint8_t ofp_version; /* An OpenFlow version or 0 for "any". */
287 uint32_t value; /* OFPT_*, OFPST_*, NXT_*, or NXST_*. */
288 const char *name; /* e.g. "OFPT_FLOW_REMOVED". */
289 unsigned int min_size; /* Minimum total message size in bytes. */
290 /* 0 if 'min_size' is the exact size that the message must be. Otherwise,
291 * the message may exceed 'min_size' by an even multiple of this value. */
292 unsigned int extra_multiple;
295 /* Represents a malformed OpenFlow message. */
296 static const struct ofputil_msg_type ofputil_invalid_type = {
297 OFPUTIL_MSG_INVALID, 0, 0, "OFPUTIL_MSG_INVALID", 0, 0
300 struct ofputil_msg_category {
301 const char *name; /* e.g. "OpenFlow message" */
302 const struct ofputil_msg_type *types;
304 enum ofperr missing_error; /* Error value for missing type. */
308 ofputil_check_length(const struct ofputil_msg_type *type, unsigned int size)
310 switch (type->extra_multiple) {
312 if (size != type->min_size) {
313 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
314 "length %u (expected length %u)",
315 type->name, size, type->min_size);
316 return OFPERR_OFPBRC_BAD_LEN;
321 if (size < type->min_size) {
322 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
323 "length %u (expected length at least %u bytes)",
324 type->name, size, type->min_size);
325 return OFPERR_OFPBRC_BAD_LEN;
330 if (size < type->min_size
331 || (size - type->min_size) % type->extra_multiple) {
332 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s with incorrect "
333 "length %u (must be exactly %u bytes or longer "
334 "by an integer multiple of %u bytes)",
336 type->min_size, type->extra_multiple);
337 return OFPERR_OFPBRC_BAD_LEN;
344 ofputil_lookup_openflow_message(const struct ofputil_msg_category *cat,
345 uint8_t version, uint32_t value,
346 const struct ofputil_msg_type **typep)
348 const struct ofputil_msg_type *type;
350 for (type = cat->types; type < &cat->types[cat->n_types]; type++) {
351 if (type->value == value
352 && (!type->ofp_version || version == type->ofp_version)) {
358 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s of unknown type %"PRIu32,
360 return cat->missing_error;
364 ofputil_decode_vendor(const struct ofp_header *oh, size_t length,
365 const struct ofputil_msg_type **typep)
367 static const struct ofputil_msg_type nxt_messages[] = {
368 { OFPUTIL_NXT_ROLE_REQUEST, OFP10_VERSION,
369 NXT_ROLE_REQUEST, "NXT_ROLE_REQUEST",
370 sizeof(struct nx_role_request), 0 },
372 { OFPUTIL_NXT_ROLE_REPLY, OFP10_VERSION,
373 NXT_ROLE_REPLY, "NXT_ROLE_REPLY",
374 sizeof(struct nx_role_request), 0 },
376 { OFPUTIL_NXT_SET_FLOW_FORMAT, OFP10_VERSION,
377 NXT_SET_FLOW_FORMAT, "NXT_SET_FLOW_FORMAT",
378 sizeof(struct nx_set_flow_format), 0 },
380 { OFPUTIL_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION,
381 NXT_SET_PACKET_IN_FORMAT, "NXT_SET_PACKET_IN_FORMAT",
382 sizeof(struct nx_set_packet_in_format), 0 },
384 { OFPUTIL_NXT_PACKET_IN, OFP10_VERSION,
385 NXT_PACKET_IN, "NXT_PACKET_IN",
386 sizeof(struct nx_packet_in), 1 },
388 { OFPUTIL_NXT_FLOW_MOD, OFP10_VERSION,
389 NXT_FLOW_MOD, "NXT_FLOW_MOD",
390 sizeof(struct nx_flow_mod), 8 },
392 { OFPUTIL_NXT_FLOW_REMOVED, OFP10_VERSION,
393 NXT_FLOW_REMOVED, "NXT_FLOW_REMOVED",
394 sizeof(struct nx_flow_removed), 8 },
396 { OFPUTIL_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION,
397 NXT_FLOW_MOD_TABLE_ID, "NXT_FLOW_MOD_TABLE_ID",
398 sizeof(struct nx_flow_mod_table_id), 0 },
400 { OFPUTIL_NXT_FLOW_AGE, OFP10_VERSION,
401 NXT_FLOW_AGE, "NXT_FLOW_AGE",
402 sizeof(struct nicira_header), 0 },
404 { OFPUTIL_NXT_SET_ASYNC_CONFIG, OFP10_VERSION,
405 NXT_SET_ASYNC_CONFIG, "NXT_SET_ASYNC_CONFIG",
406 sizeof(struct nx_async_config), 0 },
408 { OFPUTIL_NXT_SET_CONTROLLER_ID, OFP10_VERSION,
409 NXT_SET_CONTROLLER_ID, "NXT_SET_CONTROLLER_ID",
410 sizeof(struct nx_controller_id), 0 },
413 static const struct ofputil_msg_category nxt_category = {
414 "Nicira extension message",
415 nxt_messages, ARRAY_SIZE(nxt_messages),
416 OFPERR_OFPBRC_BAD_SUBTYPE
419 const struct ofp_vendor_header *ovh;
420 const struct nicira_header *nh;
422 if (length < sizeof(struct ofp_vendor_header)) {
423 if (length == ntohs(oh->length)) {
424 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor message");
426 return OFPERR_OFPBRC_BAD_LEN;
429 ovh = (const struct ofp_vendor_header *) oh;
430 if (ovh->vendor != htonl(NX_VENDOR_ID)) {
431 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor message for unknown "
432 "vendor %"PRIx32, ntohl(ovh->vendor));
433 return OFPERR_OFPBRC_BAD_VENDOR;
436 if (length < sizeof(struct nicira_header)) {
437 if (length == ntohs(oh->length)) {
438 VLOG_WARN_RL(&bad_ofmsg_rl, "received Nicira vendor message of "
439 "length %u (expected at least %zu)",
440 ntohs(ovh->header.length),
441 sizeof(struct nicira_header));
443 return OFPERR_OFPBRC_BAD_LEN;
446 nh = (const struct nicira_header *) oh;
447 return ofputil_lookup_openflow_message(&nxt_category, oh->version,
448 ntohl(nh->subtype), typep);
452 check_nxstats_msg(const struct ofp_header *oh, size_t length)
454 const struct ofp_stats_msg *osm = (const struct ofp_stats_msg *) oh;
457 if (length < sizeof(struct ofp_vendor_stats_msg)) {
458 if (length == ntohs(oh->length)) {
459 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated vendor stats message");
461 return OFPERR_OFPBRC_BAD_LEN;
464 memcpy(&vendor, osm + 1, sizeof vendor);
465 if (vendor != htonl(NX_VENDOR_ID)) {
466 VLOG_WARN_RL(&bad_ofmsg_rl, "received vendor stats message for "
467 "unknown vendor %"PRIx32, ntohl(vendor));
468 return OFPERR_OFPBRC_BAD_VENDOR;
471 if (length < sizeof(struct nicira_stats_msg)) {
472 if (length == ntohs(osm->header.length)) {
473 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated Nicira stats message");
475 return OFPERR_OFPBRC_BAD_LEN;
482 ofputil_decode_nxst_request(const struct ofp_header *oh, size_t length,
483 const struct ofputil_msg_type **typep)
485 static const struct ofputil_msg_type nxst_requests[] = {
486 { OFPUTIL_NXST_FLOW_REQUEST, OFP10_VERSION,
487 NXST_FLOW, "NXST_FLOW request",
488 sizeof(struct nx_flow_stats_request), 8 },
490 { OFPUTIL_NXST_AGGREGATE_REQUEST, OFP10_VERSION,
491 NXST_AGGREGATE, "NXST_AGGREGATE request",
492 sizeof(struct nx_aggregate_stats_request), 8 },
495 static const struct ofputil_msg_category nxst_request_category = {
496 "Nicira extension statistics request",
497 nxst_requests, ARRAY_SIZE(nxst_requests),
498 OFPERR_OFPBRC_BAD_SUBTYPE
501 const struct nicira_stats_msg *nsm;
504 error = check_nxstats_msg(oh, length);
509 nsm = (struct nicira_stats_msg *) oh;
510 return ofputil_lookup_openflow_message(&nxst_request_category, oh->version,
511 ntohl(nsm->subtype), typep);
515 ofputil_decode_nxst_reply(const struct ofp_header *oh, size_t length,
516 const struct ofputil_msg_type **typep)
518 static const struct ofputil_msg_type nxst_replies[] = {
519 { OFPUTIL_NXST_FLOW_REPLY, OFP10_VERSION,
520 NXST_FLOW, "NXST_FLOW reply",
521 sizeof(struct nicira_stats_msg), 8 },
523 { OFPUTIL_NXST_AGGREGATE_REPLY, OFP10_VERSION,
524 NXST_AGGREGATE, "NXST_AGGREGATE reply",
525 sizeof(struct nx_aggregate_stats_reply), 0 },
528 static const struct ofputil_msg_category nxst_reply_category = {
529 "Nicira extension statistics reply",
530 nxst_replies, ARRAY_SIZE(nxst_replies),
531 OFPERR_OFPBRC_BAD_SUBTYPE
534 const struct nicira_stats_msg *nsm;
537 error = check_nxstats_msg(oh, length);
542 nsm = (struct nicira_stats_msg *) oh;
543 return ofputil_lookup_openflow_message(&nxst_reply_category, oh->version,
544 ntohl(nsm->subtype), typep);
548 check_stats_msg(const struct ofp_header *oh, size_t length)
550 if (length < sizeof(struct ofp_stats_msg)) {
551 if (length == ntohs(oh->length)) {
552 VLOG_WARN_RL(&bad_ofmsg_rl, "truncated stats message");
554 return OFPERR_OFPBRC_BAD_LEN;
561 ofputil_decode_ofpst_request(const struct ofp_header *oh, size_t length,
562 const struct ofputil_msg_type **typep)
564 static const struct ofputil_msg_type ofpst_requests[] = {
565 { OFPUTIL_OFPST_DESC_REQUEST, OFP10_VERSION,
566 OFPST_DESC, "OFPST_DESC request",
567 sizeof(struct ofp_stats_msg), 0 },
569 { OFPUTIL_OFPST_FLOW_REQUEST, OFP10_VERSION,
570 OFPST_FLOW, "OFPST_FLOW request",
571 sizeof(struct ofp_flow_stats_request), 0 },
573 { OFPUTIL_OFPST_AGGREGATE_REQUEST, OFP10_VERSION,
574 OFPST_AGGREGATE, "OFPST_AGGREGATE request",
575 sizeof(struct ofp_flow_stats_request), 0 },
577 { OFPUTIL_OFPST_TABLE_REQUEST, OFP10_VERSION,
578 OFPST_TABLE, "OFPST_TABLE request",
579 sizeof(struct ofp_stats_msg), 0 },
581 { OFPUTIL_OFPST_PORT_REQUEST, OFP10_VERSION,
582 OFPST_PORT, "OFPST_PORT request",
583 sizeof(struct ofp_port_stats_request), 0 },
585 { OFPUTIL_OFPST_QUEUE_REQUEST, OFP10_VERSION,
586 OFPST_QUEUE, "OFPST_QUEUE request",
587 sizeof(struct ofp_queue_stats_request), 0 },
590 OFPST_VENDOR, "OFPST_VENDOR request",
591 sizeof(struct ofp_vendor_stats_msg), 1 },
594 static const struct ofputil_msg_category ofpst_request_category = {
595 "OpenFlow statistics",
596 ofpst_requests, ARRAY_SIZE(ofpst_requests),
597 OFPERR_OFPBRC_BAD_STAT
600 const struct ofp_stats_msg *request = (const struct ofp_stats_msg *) oh;
603 error = check_stats_msg(oh, length);
608 error = ofputil_lookup_openflow_message(&ofpst_request_category,
609 oh->version, ntohs(request->type),
611 if (!error && request->type == htons(OFPST_VENDOR)) {
612 error = ofputil_decode_nxst_request(oh, length, typep);
618 ofputil_decode_ofpst_reply(const struct ofp_header *oh, size_t length,
619 const struct ofputil_msg_type **typep)
621 static const struct ofputil_msg_type ofpst_replies[] = {
622 { OFPUTIL_OFPST_DESC_REPLY, OFP10_VERSION,
623 OFPST_DESC, "OFPST_DESC reply",
624 sizeof(struct ofp_desc_stats), 0 },
626 { OFPUTIL_OFPST_FLOW_REPLY, OFP10_VERSION,
627 OFPST_FLOW, "OFPST_FLOW reply",
628 sizeof(struct ofp_stats_msg), 1 },
630 { OFPUTIL_OFPST_AGGREGATE_REPLY, OFP10_VERSION,
631 OFPST_AGGREGATE, "OFPST_AGGREGATE reply",
632 sizeof(struct ofp_aggregate_stats_reply), 0 },
634 { OFPUTIL_OFPST_TABLE_REPLY, OFP10_VERSION,
635 OFPST_TABLE, "OFPST_TABLE reply",
636 sizeof(struct ofp_stats_msg), sizeof(struct ofp_table_stats) },
638 { OFPUTIL_OFPST_PORT_REPLY, OFP10_VERSION,
639 OFPST_PORT, "OFPST_PORT reply",
640 sizeof(struct ofp_stats_msg), sizeof(struct ofp_port_stats) },
642 { OFPUTIL_OFPST_QUEUE_REPLY, OFP10_VERSION,
643 OFPST_QUEUE, "OFPST_QUEUE reply",
644 sizeof(struct ofp_stats_msg), sizeof(struct ofp_queue_stats) },
647 OFPST_VENDOR, "OFPST_VENDOR reply",
648 sizeof(struct ofp_vendor_stats_msg), 1 },
651 static const struct ofputil_msg_category ofpst_reply_category = {
652 "OpenFlow statistics",
653 ofpst_replies, ARRAY_SIZE(ofpst_replies),
654 OFPERR_OFPBRC_BAD_STAT
657 const struct ofp_stats_msg *reply = (const struct ofp_stats_msg *) oh;
660 error = check_stats_msg(oh, length);
665 error = ofputil_lookup_openflow_message(&ofpst_reply_category, oh->version,
666 ntohs(reply->type), typep);
667 if (!error && reply->type == htons(OFPST_VENDOR)) {
668 error = ofputil_decode_nxst_reply(oh, length, typep);
674 ofputil_decode_msg_type__(const struct ofp_header *oh, size_t length,
675 const struct ofputil_msg_type **typep)
677 static const struct ofputil_msg_type ofpt_messages[] = {
678 { OFPUTIL_OFPT_HELLO, OFP10_VERSION,
679 OFPT_HELLO, "OFPT_HELLO",
680 sizeof(struct ofp_hello), 1 },
682 { OFPUTIL_OFPT_ERROR, 0,
683 OFPT_ERROR, "OFPT_ERROR",
684 sizeof(struct ofp_error_msg), 1 },
686 { OFPUTIL_OFPT_ECHO_REQUEST, OFP10_VERSION,
687 OFPT_ECHO_REQUEST, "OFPT_ECHO_REQUEST",
688 sizeof(struct ofp_header), 1 },
690 { OFPUTIL_OFPT_ECHO_REPLY, OFP10_VERSION,
691 OFPT_ECHO_REPLY, "OFPT_ECHO_REPLY",
692 sizeof(struct ofp_header), 1 },
694 { OFPUTIL_OFPT_FEATURES_REQUEST, OFP10_VERSION,
695 OFPT_FEATURES_REQUEST, "OFPT_FEATURES_REQUEST",
696 sizeof(struct ofp_header), 0 },
698 { OFPUTIL_OFPT_FEATURES_REPLY, OFP10_VERSION,
699 OFPT_FEATURES_REPLY, "OFPT_FEATURES_REPLY",
700 sizeof(struct ofp_switch_features), sizeof(struct ofp_phy_port) },
702 { OFPUTIL_OFPT_GET_CONFIG_REQUEST, OFP10_VERSION,
703 OFPT_GET_CONFIG_REQUEST, "OFPT_GET_CONFIG_REQUEST",
704 sizeof(struct ofp_header), 0 },
706 { OFPUTIL_OFPT_GET_CONFIG_REPLY, OFP10_VERSION,
707 OFPT_GET_CONFIG_REPLY, "OFPT_GET_CONFIG_REPLY",
708 sizeof(struct ofp_switch_config), 0 },
710 { OFPUTIL_OFPT_SET_CONFIG, OFP10_VERSION,
711 OFPT_SET_CONFIG, "OFPT_SET_CONFIG",
712 sizeof(struct ofp_switch_config), 0 },
714 { OFPUTIL_OFPT_PACKET_IN, OFP10_VERSION,
715 OFPT_PACKET_IN, "OFPT_PACKET_IN",
716 offsetof(struct ofp_packet_in, data), 1 },
718 { OFPUTIL_OFPT_FLOW_REMOVED, OFP10_VERSION,
719 OFPT_FLOW_REMOVED, "OFPT_FLOW_REMOVED",
720 sizeof(struct ofp_flow_removed), 0 },
722 { OFPUTIL_OFPT_PORT_STATUS, OFP10_VERSION,
723 OFPT_PORT_STATUS, "OFPT_PORT_STATUS",
724 sizeof(struct ofp_port_status), 0 },
726 { OFPUTIL_OFPT_PACKET_OUT, OFP10_VERSION,
727 OFPT10_PACKET_OUT, "OFPT_PACKET_OUT",
728 sizeof(struct ofp_packet_out), 1 },
730 { OFPUTIL_OFPT_FLOW_MOD, OFP10_VERSION,
731 OFPT10_FLOW_MOD, "OFPT_FLOW_MOD",
732 sizeof(struct ofp_flow_mod), 1 },
734 { OFPUTIL_OFPT_PORT_MOD, OFP10_VERSION,
735 OFPT10_PORT_MOD, "OFPT_PORT_MOD",
736 sizeof(struct ofp_port_mod), 0 },
739 OFPT10_STATS_REQUEST, "OFPT_STATS_REQUEST",
740 sizeof(struct ofp_stats_msg), 1 },
743 OFPT10_STATS_REPLY, "OFPT_STATS_REPLY",
744 sizeof(struct ofp_stats_msg), 1 },
746 { OFPUTIL_OFPT_BARRIER_REQUEST, OFP10_VERSION,
747 OFPT10_BARRIER_REQUEST, "OFPT_BARRIER_REQUEST",
748 sizeof(struct ofp_header), 0 },
750 { OFPUTIL_OFPT_BARRIER_REPLY, OFP10_VERSION,
751 OFPT10_BARRIER_REPLY, "OFPT_BARRIER_REPLY",
752 sizeof(struct ofp_header), 0 },
755 OFPT_VENDOR, "OFPT_VENDOR",
756 sizeof(struct ofp_vendor_header), 1 },
759 static const struct ofputil_msg_category ofpt_category = {
761 ofpt_messages, ARRAY_SIZE(ofpt_messages),
762 OFPERR_OFPBRC_BAD_TYPE
767 error = ofputil_lookup_openflow_message(&ofpt_category, oh->version,
770 switch ((oh->version << 8) | oh->type) {
771 case (OFP10_VERSION << 8) | OFPT_VENDOR:
772 case (OFP11_VERSION << 8) | OFPT_VENDOR:
773 error = ofputil_decode_vendor(oh, length, typep);
776 case (OFP10_VERSION << 8) | OFPT10_STATS_REQUEST:
777 case (OFP11_VERSION << 8) | OFPT11_STATS_REQUEST:
778 error = ofputil_decode_ofpst_request(oh, length, typep);
781 case (OFP10_VERSION << 8) | OFPT10_STATS_REPLY:
782 case (OFP11_VERSION << 8) | OFPT11_STATS_REPLY:
783 error = ofputil_decode_ofpst_reply(oh, length, typep);
792 /* Decodes the message type represented by 'oh'. Returns 0 if successful or an
793 * OpenFlow error code on failure. Either way, stores in '*typep' a type
794 * structure that can be inspected with the ofputil_msg_type_*() functions.
796 * oh->length must indicate the correct length of the message (and must be at
797 * least sizeof(struct ofp_header)).
799 * Success indicates that 'oh' is at least as long as the minimum-length
800 * message of its type. */
802 ofputil_decode_msg_type(const struct ofp_header *oh,
803 const struct ofputil_msg_type **typep)
805 size_t length = ntohs(oh->length);
808 error = ofputil_decode_msg_type__(oh, length, typep);
810 error = ofputil_check_length(*typep, length);
813 *typep = &ofputil_invalid_type;
818 /* Decodes the message type represented by 'oh', of which only the first
819 * 'length' bytes are available. Returns 0 if successful or an OpenFlow error
820 * code on failure. Either way, stores in '*typep' a type structure that can
821 * be inspected with the ofputil_msg_type_*() functions. */
823 ofputil_decode_msg_type_partial(const struct ofp_header *oh, size_t length,
824 const struct ofputil_msg_type **typep)
828 error = (length >= sizeof *oh
829 ? ofputil_decode_msg_type__(oh, length, typep)
830 : OFPERR_OFPBRC_BAD_LEN);
832 *typep = &ofputil_invalid_type;
837 /* Returns an OFPUTIL_* message type code for 'type'. */
838 enum ofputil_msg_code
839 ofputil_msg_type_code(const struct ofputil_msg_type *type)
846 struct proto_abbrev {
847 enum ofputil_protocol protocol;
851 /* Most users really don't care about some of the differences between
852 * protocols. These abbreviations help with that. */
853 static const struct proto_abbrev proto_abbrevs[] = {
854 { OFPUTIL_P_ANY, "any" },
855 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
856 { OFPUTIL_P_NXM_ANY, "NXM" },
858 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
860 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
864 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
866 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
867 * connection that has negotiated the given 'version'. 'version' should
868 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
869 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
870 * outside the valid range. */
871 enum ofputil_protocol
872 ofputil_protocol_from_ofp_version(int version)
875 case OFP10_VERSION: return OFPUTIL_P_OF10;
880 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
883 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
885 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
888 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
889 * extension turned on or off if 'enable' is true or false, respectively.
891 * This extension is only useful for protocols whose "standard" version does
892 * not allow specific tables to be modified. In particular, this is true of
893 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
894 * specifies a table ID and so there is no need for such an extension. When
895 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
896 * extension, this function just returns its 'protocol' argument unchanged
897 * regardless of the value of 'enable'. */
898 enum ofputil_protocol
899 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
903 case OFPUTIL_P_OF10_TID:
904 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
907 case OFPUTIL_P_NXM_TID:
908 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
915 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
916 * some extension to a standard protocol version, the return value is the
917 * standard version of that protocol without any extension. If 'protocol' is a
918 * standard protocol version, returns 'protocol' unchanged. */
919 enum ofputil_protocol
920 ofputil_protocol_to_base(enum ofputil_protocol protocol)
922 return ofputil_protocol_set_tid(protocol, false);
925 /* Returns 'new_base' with any extensions taken from 'cur'. */
926 enum ofputil_protocol
927 ofputil_protocol_set_base(enum ofputil_protocol cur,
928 enum ofputil_protocol new_base)
930 bool tid = (cur & OFPUTIL_P_TID) != 0;
934 case OFPUTIL_P_OF10_TID:
935 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
938 case OFPUTIL_P_NXM_TID:
939 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
946 /* Returns a string form of 'protocol', if a simple form exists (that is, if
947 * 'protocol' is either a single protocol or it is a combination of protocols
948 * that have a single abbreviation). Otherwise, returns NULL. */
950 ofputil_protocol_to_string(enum ofputil_protocol protocol)
952 const struct proto_abbrev *p;
954 /* Use a "switch" statement for single-bit names so that we get a compiler
955 * warning if we forget any. */
958 return "NXM-table_id";
960 case OFPUTIL_P_NXM_TID:
961 return "NXM+table_id";
964 return "OpenFlow10-table_id";
966 case OFPUTIL_P_OF10_TID:
967 return "OpenFlow10+table_id";
970 /* Check abbreviations. */
971 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
972 if (protocol == p->protocol) {
980 /* Returns a string that represents 'protocols'. The return value might be a
981 * comma-separated list if 'protocols' doesn't have a simple name. The return
982 * value is "none" if 'protocols' is 0.
984 * The caller must free the returned string (with free()). */
986 ofputil_protocols_to_string(enum ofputil_protocol protocols)
990 assert(!(protocols & ~OFPUTIL_P_ANY));
991 if (protocols == 0) {
992 return xstrdup("none");
997 const struct proto_abbrev *p;
1001 ds_put_char(&s, ',');
1004 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1005 if ((protocols & p->protocol) == p->protocol) {
1006 ds_put_cstr(&s, p->name);
1007 protocols &= ~p->protocol;
1012 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1013 enum ofputil_protocol bit = 1u << i;
1015 if (protocols & bit) {
1016 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1025 return ds_steal_cstr(&s);
1028 static enum ofputil_protocol
1029 ofputil_protocol_from_string__(const char *s, size_t n)
1031 const struct proto_abbrev *p;
1034 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1035 enum ofputil_protocol bit = 1u << i;
1036 const char *name = ofputil_protocol_to_string(bit);
1038 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1043 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1044 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1052 /* Returns the nonempty set of protocols represented by 's', which can be a
1053 * single protocol name or abbreviation or a comma-separated list of them.
1055 * Aborts the program with an error message if 's' is invalid. */
1056 enum ofputil_protocol
1057 ofputil_protocols_from_string(const char *s)
1059 const char *orig_s = s;
1060 enum ofputil_protocol protocols;
1064 enum ofputil_protocol p;
1067 n = strcspn(s, ",");
1073 p = ofputil_protocol_from_string__(s, n);
1075 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1083 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1089 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1091 switch (packet_in_format) {
1092 case NXPIF_OPENFLOW10:
1101 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1103 switch (packet_in_format) {
1104 case NXPIF_OPENFLOW10:
1105 return "openflow10";
1114 ofputil_packet_in_format_from_string(const char *s)
1116 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1117 : !strcmp(s, "nxm") ? NXPIF_NXM
1122 regs_fully_wildcarded(const struct flow_wildcards *wc)
1126 for (i = 0; i < FLOW_N_REGS; i++) {
1127 if (wc->reg_masks[i] != 0) {
1134 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
1135 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1136 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1137 * use OpenFlow 1.0 protocol for backward compatibility. */
1138 enum ofputil_protocol
1139 ofputil_usable_protocols(const struct cls_rule *rule)
1141 const struct flow_wildcards *wc = &rule->wc;
1143 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
1145 /* Only NXM supports separately wildcards the Ethernet multicast bit. */
1146 if (!(wc->wildcards & FWW_DL_DST) != !(wc->wildcards & FWW_ETH_MCAST)) {
1147 return OFPUTIL_P_NXM_ANY;
1150 /* Only NXM supports matching ARP hardware addresses. */
1151 if (!(wc->wildcards & FWW_ARP_SHA) || !(wc->wildcards & FWW_ARP_THA)) {
1152 return OFPUTIL_P_NXM_ANY;
1155 /* Only NXM supports matching IPv6 traffic. */
1156 if (!(wc->wildcards & FWW_DL_TYPE)
1157 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
1158 return OFPUTIL_P_NXM_ANY;
1161 /* Only NXM supports matching registers. */
1162 if (!regs_fully_wildcarded(wc)) {
1163 return OFPUTIL_P_NXM_ANY;
1166 /* Only NXM supports matching tun_id. */
1167 if (wc->tun_id_mask != htonll(0)) {
1168 return OFPUTIL_P_NXM_ANY;
1171 /* Only NXM supports matching fragments. */
1172 if (wc->nw_frag_mask) {
1173 return OFPUTIL_P_NXM_ANY;
1176 /* Only NXM supports matching IPv6 flow label. */
1177 if (!(wc->wildcards & FWW_IPV6_LABEL)) {
1178 return OFPUTIL_P_NXM_ANY;
1181 /* Only NXM supports matching IP ECN bits. */
1182 if (!(wc->wildcards & FWW_NW_ECN)) {
1183 return OFPUTIL_P_NXM_ANY;
1186 /* Only NXM supports matching IP TTL/hop limit. */
1187 if (!(wc->wildcards & FWW_NW_TTL)) {
1188 return OFPUTIL_P_NXM_ANY;
1191 /* Only NXM supports bitwise matching on transport port. */
1192 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
1193 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
1194 return OFPUTIL_P_NXM_ANY;
1197 /* Other formats can express this rule. */
1198 return OFPUTIL_P_ANY;
1201 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1202 * protocol is 'current', at least partly transitions the protocol to 'want'.
1203 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1204 * connection if the switch processes the returned message correctly. (If
1205 * '*next != want' then the caller will have to iterate.)
1207 * If 'current == want', returns NULL and stores 'current' in '*next'. */
1209 ofputil_encode_set_protocol(enum ofputil_protocol current,
1210 enum ofputil_protocol want,
1211 enum ofputil_protocol *next)
1213 enum ofputil_protocol cur_base, want_base;
1214 bool cur_tid, want_tid;
1216 cur_base = ofputil_protocol_to_base(current);
1217 want_base = ofputil_protocol_to_base(want);
1218 if (cur_base != want_base) {
1219 *next = ofputil_protocol_set_base(current, want_base);
1221 switch (want_base) {
1223 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1225 case OFPUTIL_P_OF10:
1226 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1228 case OFPUTIL_P_OF10_TID:
1229 case OFPUTIL_P_NXM_TID:
1234 cur_tid = (current & OFPUTIL_P_TID) != 0;
1235 want_tid = (want & OFPUTIL_P_TID) != 0;
1236 if (cur_tid != want_tid) {
1237 *next = ofputil_protocol_set_tid(current, want_tid);
1238 return ofputil_make_flow_mod_table_id(want_tid);
1241 assert(current == want);
1247 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1248 * format to 'nxff'. */
1250 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1252 struct nx_set_flow_format *sff;
1255 assert(ofputil_nx_flow_format_is_valid(nxff));
1257 sff = make_nxmsg(sizeof *sff, NXT_SET_FLOW_FORMAT, &msg);
1258 sff->format = htonl(nxff);
1263 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1265 enum ofputil_protocol
1266 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1268 switch (flow_format) {
1269 case NXFF_OPENFLOW10:
1270 return OFPUTIL_P_OF10;
1273 return OFPUTIL_P_NXM;
1280 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1282 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1284 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1287 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1290 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1292 switch (flow_format) {
1293 case NXFF_OPENFLOW10:
1294 return "openflow10";
1303 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1305 struct nx_set_packet_in_format *spif;
1308 spif = make_nxmsg(sizeof *spif, NXT_SET_PACKET_IN_FORMAT, &msg);
1309 spif->format = htonl(packet_in_format);
1314 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1315 * extension on or off (according to 'flow_mod_table_id'). */
1317 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1319 struct nx_flow_mod_table_id *nfmti;
1322 nfmti = make_nxmsg(sizeof *nfmti, NXT_FLOW_MOD_TABLE_ID, &msg);
1323 nfmti->set = flow_mod_table_id;
1327 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1328 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1331 * Does not validate the flow_mod actions. */
1333 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1334 const struct ofp_header *oh,
1335 enum ofputil_protocol protocol)
1337 const struct ofputil_msg_type *type;
1341 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1343 ofputil_decode_msg_type(oh, &type);
1344 if (ofputil_msg_type_code(type) == OFPUTIL_OFPT_FLOW_MOD) {
1345 /* Standard OpenFlow flow_mod. */
1346 const struct ofp_flow_mod *ofm;
1350 /* Dissect the message. */
1351 ofm = ofpbuf_pull(&b, sizeof *ofm);
1352 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1357 /* Set priority based on original wildcards. Normally we'd allow
1358 * ofputil_cls_rule_from_match() to do this for us, but
1359 * ofputil_normalize_rule() can put wildcards where the original flow
1360 * didn't have them. */
1361 priority = ntohs(ofm->priority);
1362 if (!(ofm->match.wildcards & htonl(OFPFW_ALL))) {
1363 priority = UINT16_MAX;
1366 /* Translate the rule. */
1367 ofputil_cls_rule_from_match(&ofm->match, priority, &fm->cr);
1368 ofputil_normalize_rule(&fm->cr);
1370 /* Translate the message. */
1371 fm->cookie = ofm->cookie;
1372 fm->cookie_mask = htonll(UINT64_MAX);
1373 command = ntohs(ofm->command);
1374 fm->idle_timeout = ntohs(ofm->idle_timeout);
1375 fm->hard_timeout = ntohs(ofm->hard_timeout);
1376 fm->buffer_id = ntohl(ofm->buffer_id);
1377 fm->out_port = ntohs(ofm->out_port);
1378 fm->flags = ntohs(ofm->flags);
1379 } else if (ofputil_msg_type_code(type) == OFPUTIL_NXT_FLOW_MOD) {
1380 /* Nicira extended flow_mod. */
1381 const struct nx_flow_mod *nfm;
1384 /* Dissect the message. */
1385 nfm = ofpbuf_pull(&b, sizeof *nfm);
1386 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1387 &fm->cr, &fm->cookie, &fm->cookie_mask);
1391 error = ofputil_pull_actions(&b, b.size, &fm->actions, &fm->n_actions);
1396 /* Translate the message. */
1397 command = ntohs(nfm->command);
1398 if (command == OFPFC_ADD) {
1399 if (fm->cookie_mask) {
1400 /* The "NXM_NX_COOKIE*" matches are not valid for flow
1401 * additions. Additions must use the "cookie" field of
1402 * the "nx_flow_mod" structure. */
1403 return OFPERR_NXBRC_NXM_INVALID;
1405 fm->cookie = nfm->cookie;
1406 fm->cookie_mask = htonll(UINT64_MAX);
1409 fm->idle_timeout = ntohs(nfm->idle_timeout);
1410 fm->hard_timeout = ntohs(nfm->hard_timeout);
1411 fm->buffer_id = ntohl(nfm->buffer_id);
1412 fm->out_port = ntohs(nfm->out_port);
1413 fm->flags = ntohs(nfm->flags);
1418 if (protocol & OFPUTIL_P_TID) {
1419 fm->command = command & 0xff;
1420 fm->table_id = command >> 8;
1422 fm->command = command;
1423 fm->table_id = 0xff;
1429 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1430 * 'protocol' and returns the message.
1432 * 'flow_mod_table_id' should be true if the NXT_FLOW_MOD_TABLE_ID extension is
1433 * enabled, false otherwise. */
1435 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1436 enum ofputil_protocol protocol)
1438 size_t actions_len = fm->n_actions * sizeof *fm->actions;
1439 struct ofp_flow_mod *ofm;
1440 struct nx_flow_mod *nfm;
1445 command = (protocol & OFPUTIL_P_TID
1446 ? (fm->command & 0xff) | (fm->table_id << 8)
1450 case OFPUTIL_P_OF10:
1451 case OFPUTIL_P_OF10_TID:
1452 msg = ofpbuf_new(sizeof *ofm + actions_len);
1453 ofm = put_openflow(sizeof *ofm, OFPT10_FLOW_MOD, msg);
1454 ofputil_cls_rule_to_match(&fm->cr, &ofm->match);
1455 ofm->cookie = fm->cookie;
1456 ofm->command = htons(command);
1457 ofm->idle_timeout = htons(fm->idle_timeout);
1458 ofm->hard_timeout = htons(fm->hard_timeout);
1459 ofm->priority = htons(fm->cr.priority);
1460 ofm->buffer_id = htonl(fm->buffer_id);
1461 ofm->out_port = htons(fm->out_port);
1462 ofm->flags = htons(fm->flags);
1466 case OFPUTIL_P_NXM_TID:
1467 msg = ofpbuf_new(sizeof *nfm + NXM_TYPICAL_LEN + actions_len);
1468 put_nxmsg(sizeof *nfm, NXT_FLOW_MOD, msg);
1470 nfm->command = htons(command);
1471 if (command == OFPFC_ADD) {
1472 nfm->cookie = fm->cookie;
1473 match_len = nx_put_match(msg, &fm->cr, 0, 0);
1476 match_len = nx_put_match(msg, &fm->cr,
1477 fm->cookie, fm->cookie_mask);
1479 nfm->idle_timeout = htons(fm->idle_timeout);
1480 nfm->hard_timeout = htons(fm->hard_timeout);
1481 nfm->priority = htons(fm->cr.priority);
1482 nfm->buffer_id = htonl(fm->buffer_id);
1483 nfm->out_port = htons(fm->out_port);
1484 nfm->flags = htons(fm->flags);
1485 nfm->match_len = htons(match_len);
1492 ofpbuf_put(msg, fm->actions, actions_len);
1493 update_openflow_length(msg);
1497 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1498 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1499 * 0-bit for each protocol that is inadequate.
1501 * (The return value will have at least one 1-bit.) */
1502 enum ofputil_protocol
1503 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1506 enum ofputil_protocol usable_protocols;
1509 usable_protocols = OFPUTIL_P_ANY;
1510 for (i = 0; i < n_fms; i++) {
1511 const struct ofputil_flow_mod *fm = &fms[i];
1513 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1514 if (fm->table_id != 0xff) {
1515 usable_protocols &= OFPUTIL_P_TID;
1517 if (fm->command != OFPFC_ADD && fm->cookie_mask != htonll(0)) {
1518 usable_protocols &= OFPUTIL_P_NXM_ANY;
1521 assert(usable_protocols);
1523 return usable_protocols;
1527 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1528 const struct ofp_header *oh,
1531 const struct ofp_flow_stats_request *ofsr =
1532 (const struct ofp_flow_stats_request *) oh;
1534 fsr->aggregate = aggregate;
1535 ofputil_cls_rule_from_match(&ofsr->match, 0, &fsr->match);
1536 fsr->out_port = ntohs(ofsr->out_port);
1537 fsr->table_id = ofsr->table_id;
1538 fsr->cookie = fsr->cookie_mask = htonll(0);
1544 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1545 const struct ofp_header *oh,
1548 const struct nx_flow_stats_request *nfsr;
1552 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1554 nfsr = ofpbuf_pull(&b, sizeof *nfsr);
1555 error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &fsr->match,
1556 &fsr->cookie, &fsr->cookie_mask);
1561 return OFPERR_OFPBRC_BAD_LEN;
1564 fsr->aggregate = aggregate;
1565 fsr->out_port = ntohs(nfsr->out_port);
1566 fsr->table_id = nfsr->table_id;
1571 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1572 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1573 * successful, otherwise an OpenFlow error code. */
1575 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1576 const struct ofp_header *oh)
1578 const struct ofputil_msg_type *type;
1582 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1584 ofputil_decode_msg_type(oh, &type);
1585 code = ofputil_msg_type_code(type);
1587 case OFPUTIL_OFPST_FLOW_REQUEST:
1588 return ofputil_decode_ofpst_flow_request(fsr, oh, false);
1590 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1591 return ofputil_decode_ofpst_flow_request(fsr, oh, true);
1593 case OFPUTIL_NXST_FLOW_REQUEST:
1594 return ofputil_decode_nxst_flow_request(fsr, oh, false);
1596 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1597 return ofputil_decode_nxst_flow_request(fsr, oh, true);
1600 /* Hey, the caller lied. */
1605 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1606 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1607 * 'protocol', and returns the message. */
1609 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1610 enum ofputil_protocol protocol)
1615 case OFPUTIL_P_OF10:
1616 case OFPUTIL_P_OF10_TID: {
1617 struct ofp_flow_stats_request *ofsr;
1620 type = fsr->aggregate ? OFPST_AGGREGATE : OFPST_FLOW;
1621 ofsr = ofputil_make_stats_request(sizeof *ofsr, type, 0, &msg);
1622 ofputil_cls_rule_to_match(&fsr->match, &ofsr->match);
1623 ofsr->table_id = fsr->table_id;
1624 ofsr->out_port = htons(fsr->out_port);
1629 case OFPUTIL_P_NXM_TID: {
1630 struct nx_flow_stats_request *nfsr;
1634 subtype = fsr->aggregate ? NXST_AGGREGATE : NXST_FLOW;
1635 ofputil_make_stats_request(sizeof *nfsr, OFPST_VENDOR, subtype, &msg);
1636 match_len = nx_put_match(msg, &fsr->match,
1637 fsr->cookie, fsr->cookie_mask);
1640 nfsr->out_port = htons(fsr->out_port);
1641 nfsr->match_len = htons(match_len);
1642 nfsr->table_id = fsr->table_id;
1653 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1654 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1656 * (The return value will have at least one 1-bit.) */
1657 enum ofputil_protocol
1658 ofputil_flow_stats_request_usable_protocols(
1659 const struct ofputil_flow_stats_request *fsr)
1661 enum ofputil_protocol usable_protocols;
1663 usable_protocols = ofputil_usable_protocols(&fsr->match);
1664 if (fsr->cookie_mask != htonll(0)) {
1665 usable_protocols &= OFPUTIL_P_NXM_ANY;
1667 return usable_protocols;
1670 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1671 * ofputil_flow_stats in 'fs'.
1673 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1674 * OpenFlow message. Calling this function multiple times for a single 'msg'
1675 * iterates through the replies. The caller must initially leave 'msg''s layer
1676 * pointers null and not modify them between calls.
1678 * Most switches don't send the values needed to populate fs->idle_age and
1679 * fs->hard_age, so those members will usually be set to 0. If the switch from
1680 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1681 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1682 * 'idle_age' and 'hard_age' members in 'fs'.
1684 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1685 * otherwise a positive errno value. */
1687 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1689 bool flow_age_extension)
1691 const struct ofputil_msg_type *type;
1694 ofputil_decode_msg_type(msg->l2 ? msg->l2 : msg->data, &type);
1695 code = ofputil_msg_type_code(type);
1697 msg->l2 = msg->data;
1698 if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1699 ofpbuf_pull(msg, sizeof(struct ofp_stats_msg));
1700 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1701 ofpbuf_pull(msg, sizeof(struct nicira_stats_msg));
1709 } else if (code == OFPUTIL_OFPST_FLOW_REPLY) {
1710 const struct ofp_flow_stats *ofs;
1713 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1715 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1716 "bytes at end", msg->size);
1720 length = ntohs(ofs->length);
1721 if (length < sizeof *ofs) {
1722 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1723 "length %zu", length);
1727 if (ofputil_pull_actions(msg, length - sizeof *ofs,
1728 &fs->actions, &fs->n_actions)) {
1732 fs->cookie = get_32aligned_be64(&ofs->cookie);
1733 ofputil_cls_rule_from_match(&ofs->match, ntohs(ofs->priority),
1735 fs->table_id = ofs->table_id;
1736 fs->duration_sec = ntohl(ofs->duration_sec);
1737 fs->duration_nsec = ntohl(ofs->duration_nsec);
1738 fs->idle_timeout = ntohs(ofs->idle_timeout);
1739 fs->hard_timeout = ntohs(ofs->hard_timeout);
1742 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1743 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1744 } else if (code == OFPUTIL_NXST_FLOW_REPLY) {
1745 const struct nx_flow_stats *nfs;
1746 size_t match_len, length;
1748 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1750 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1751 "bytes at end", msg->size);
1755 length = ntohs(nfs->length);
1756 match_len = ntohs(nfs->match_len);
1757 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1758 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1759 "claims invalid length %zu", match_len, length);
1762 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1767 if (ofputil_pull_actions(msg,
1768 length - sizeof *nfs - ROUND_UP(match_len, 8),
1769 &fs->actions, &fs->n_actions)) {
1773 fs->cookie = nfs->cookie;
1774 fs->table_id = nfs->table_id;
1775 fs->duration_sec = ntohl(nfs->duration_sec);
1776 fs->duration_nsec = ntohl(nfs->duration_nsec);
1777 fs->idle_timeout = ntohs(nfs->idle_timeout);
1778 fs->hard_timeout = ntohs(nfs->hard_timeout);
1781 if (flow_age_extension) {
1782 if (nfs->idle_age) {
1783 fs->idle_age = ntohs(nfs->idle_age) - 1;
1785 if (nfs->hard_age) {
1786 fs->hard_age = ntohs(nfs->hard_age) - 1;
1789 fs->packet_count = ntohll(nfs->packet_count);
1790 fs->byte_count = ntohll(nfs->byte_count);
1798 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1800 * We use this in situations where OVS internally uses UINT64_MAX to mean
1801 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1803 unknown_to_zero(uint64_t count)
1805 return count != UINT64_MAX ? count : 0;
1808 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1809 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1810 * have been initialized with ofputil_start_stats_reply(). */
1812 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1813 struct list *replies)
1815 size_t act_len = fs->n_actions * sizeof *fs->actions;
1816 const struct ofp_stats_msg *osm;
1818 osm = ofpbuf_from_list(list_back(replies))->data;
1819 if (osm->type == htons(OFPST_FLOW)) {
1820 size_t len = offsetof(struct ofp_flow_stats, actions) + act_len;
1821 struct ofp_flow_stats *ofs;
1823 ofs = ofputil_append_stats_reply(len, replies);
1824 ofs->length = htons(len);
1825 ofs->table_id = fs->table_id;
1827 ofputil_cls_rule_to_match(&fs->rule, &ofs->match);
1828 ofs->duration_sec = htonl(fs->duration_sec);
1829 ofs->duration_nsec = htonl(fs->duration_nsec);
1830 ofs->priority = htons(fs->rule.priority);
1831 ofs->idle_timeout = htons(fs->idle_timeout);
1832 ofs->hard_timeout = htons(fs->hard_timeout);
1833 memset(ofs->pad2, 0, sizeof ofs->pad2);
1834 put_32aligned_be64(&ofs->cookie, fs->cookie);
1835 put_32aligned_be64(&ofs->packet_count,
1836 htonll(unknown_to_zero(fs->packet_count)));
1837 put_32aligned_be64(&ofs->byte_count,
1838 htonll(unknown_to_zero(fs->byte_count)));
1839 memcpy(ofs->actions, fs->actions, act_len);
1840 } else if (osm->type == htons(OFPST_VENDOR)) {
1841 struct nx_flow_stats *nfs;
1845 msg = ofputil_reserve_stats_reply(
1846 sizeof *nfs + NXM_MAX_LEN + act_len, replies);
1847 start_len = msg->size;
1849 nfs = ofpbuf_put_uninit(msg, sizeof *nfs);
1850 nfs->table_id = fs->table_id;
1852 nfs->duration_sec = htonl(fs->duration_sec);
1853 nfs->duration_nsec = htonl(fs->duration_nsec);
1854 nfs->priority = htons(fs->rule.priority);
1855 nfs->idle_timeout = htons(fs->idle_timeout);
1856 nfs->hard_timeout = htons(fs->hard_timeout);
1857 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1858 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1860 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1861 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1863 nfs->match_len = htons(nx_put_match(msg, &fs->rule, 0, 0));
1864 nfs->cookie = fs->cookie;
1865 nfs->packet_count = htonll(fs->packet_count);
1866 nfs->byte_count = htonll(fs->byte_count);
1867 ofpbuf_put(msg, fs->actions, act_len);
1868 nfs->length = htons(msg->size - start_len);
1874 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1875 * NXST_AGGREGATE reply according to 'protocol', and returns the message. */
1877 ofputil_encode_aggregate_stats_reply(
1878 const struct ofputil_aggregate_stats *stats,
1879 const struct ofp_stats_msg *request)
1883 if (request->type == htons(OFPST_AGGREGATE)) {
1884 struct ofp_aggregate_stats_reply *asr;
1886 asr = ofputil_make_stats_reply(sizeof *asr, request, &msg);
1887 put_32aligned_be64(&asr->packet_count,
1888 htonll(unknown_to_zero(stats->packet_count)));
1889 put_32aligned_be64(&asr->byte_count,
1890 htonll(unknown_to_zero(stats->byte_count)));
1891 asr->flow_count = htonl(stats->flow_count);
1892 } else if (request->type == htons(OFPST_VENDOR)) {
1893 struct nx_aggregate_stats_reply *nasr;
1895 nasr = ofputil_make_stats_reply(sizeof *nasr, request, &msg);
1896 assert(nasr->nsm.subtype == htonl(NXST_AGGREGATE));
1897 nasr->packet_count = htonll(stats->packet_count);
1898 nasr->byte_count = htonll(stats->byte_count);
1899 nasr->flow_count = htonl(stats->flow_count);
1907 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1908 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1909 * an OpenFlow error code. */
1911 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1912 const struct ofp_header *oh)
1914 const struct ofputil_msg_type *type;
1915 enum ofputil_msg_code code;
1917 ofputil_decode_msg_type(oh, &type);
1918 code = ofputil_msg_type_code(type);
1919 if (code == OFPUTIL_OFPT_FLOW_REMOVED) {
1920 const struct ofp_flow_removed *ofr;
1922 ofr = (const struct ofp_flow_removed *) oh;
1923 ofputil_cls_rule_from_match(&ofr->match, ntohs(ofr->priority),
1925 fr->cookie = ofr->cookie;
1926 fr->reason = ofr->reason;
1927 fr->duration_sec = ntohl(ofr->duration_sec);
1928 fr->duration_nsec = ntohl(ofr->duration_nsec);
1929 fr->idle_timeout = ntohs(ofr->idle_timeout);
1930 fr->packet_count = ntohll(ofr->packet_count);
1931 fr->byte_count = ntohll(ofr->byte_count);
1932 } else if (code == OFPUTIL_NXT_FLOW_REMOVED) {
1933 struct nx_flow_removed *nfr;
1937 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1939 nfr = ofpbuf_pull(&b, sizeof *nfr);
1940 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1941 &fr->rule, NULL, NULL);
1946 return OFPERR_OFPBRC_BAD_LEN;
1949 fr->cookie = nfr->cookie;
1950 fr->reason = nfr->reason;
1951 fr->duration_sec = ntohl(nfr->duration_sec);
1952 fr->duration_nsec = ntohl(nfr->duration_nsec);
1953 fr->idle_timeout = ntohs(nfr->idle_timeout);
1954 fr->packet_count = ntohll(nfr->packet_count);
1955 fr->byte_count = ntohll(nfr->byte_count);
1963 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1964 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1967 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1968 enum ofputil_protocol protocol)
1973 case OFPUTIL_P_OF10:
1974 case OFPUTIL_P_OF10_TID: {
1975 struct ofp_flow_removed *ofr;
1977 ofr = make_openflow_xid(sizeof *ofr, OFPT_FLOW_REMOVED, htonl(0),
1979 ofputil_cls_rule_to_match(&fr->rule, &ofr->match);
1980 ofr->cookie = fr->cookie;
1981 ofr->priority = htons(fr->rule.priority);
1982 ofr->reason = fr->reason;
1983 ofr->duration_sec = htonl(fr->duration_sec);
1984 ofr->duration_nsec = htonl(fr->duration_nsec);
1985 ofr->idle_timeout = htons(fr->idle_timeout);
1986 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1987 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1992 case OFPUTIL_P_NXM_TID: {
1993 struct nx_flow_removed *nfr;
1996 make_nxmsg_xid(sizeof *nfr, NXT_FLOW_REMOVED, htonl(0), &msg);
1997 match_len = nx_put_match(msg, &fr->rule, 0, 0);
2000 nfr->cookie = fr->cookie;
2001 nfr->priority = htons(fr->rule.priority);
2002 nfr->reason = fr->reason;
2003 nfr->duration_sec = htonl(fr->duration_sec);
2004 nfr->duration_nsec = htonl(fr->duration_nsec);
2005 nfr->idle_timeout = htons(fr->idle_timeout);
2006 nfr->match_len = htons(match_len);
2007 nfr->packet_count = htonll(fr->packet_count);
2008 nfr->byte_count = htonll(fr->byte_count);
2020 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2021 const struct ofp_header *oh)
2023 const struct ofputil_msg_type *type;
2024 enum ofputil_msg_code code;
2026 ofputil_decode_msg_type(oh, &type);
2027 code = ofputil_msg_type_code(type);
2028 memset(pin, 0, sizeof *pin);
2030 if (code == OFPUTIL_OFPT_PACKET_IN) {
2031 const struct ofp_packet_in *opi = (const struct ofp_packet_in *) oh;
2033 pin->packet = opi->data;
2034 pin->packet_len = ntohs(opi->header.length)
2035 - offsetof(struct ofp_packet_in, data);
2037 pin->fmd.in_port = ntohs(opi->in_port);
2038 pin->reason = opi->reason;
2039 pin->buffer_id = ntohl(opi->buffer_id);
2040 pin->total_len = ntohs(opi->total_len);
2041 } else if (code == OFPUTIL_NXT_PACKET_IN) {
2042 const struct nx_packet_in *npi;
2043 struct cls_rule rule;
2047 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2049 npi = ofpbuf_pull(&b, sizeof *npi);
2050 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
2056 if (!ofpbuf_try_pull(&b, 2)) {
2057 return OFPERR_OFPBRC_BAD_LEN;
2060 pin->packet = b.data;
2061 pin->packet_len = b.size;
2062 pin->reason = npi->reason;
2063 pin->table_id = npi->table_id;
2064 pin->cookie = npi->cookie;
2066 pin->fmd.in_port = rule.flow.in_port;
2068 pin->fmd.tun_id = rule.flow.tun_id;
2069 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
2071 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
2072 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
2073 sizeof pin->fmd.reg_masks);
2075 pin->buffer_id = ntohl(npi->buffer_id);
2076 pin->total_len = ntohs(npi->total_len);
2084 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2085 * in the format specified by 'packet_in_format'. */
2087 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2088 enum nx_packet_in_format packet_in_format)
2090 size_t send_len = MIN(pin->send_len, pin->packet_len);
2091 struct ofpbuf *packet;
2093 /* Add OFPT_PACKET_IN. */
2094 if (packet_in_format == NXPIF_OPENFLOW10) {
2095 size_t header_len = offsetof(struct ofp_packet_in, data);
2096 struct ofp_packet_in *opi;
2098 packet = ofpbuf_new(send_len + header_len);
2099 opi = ofpbuf_put_zeros(packet, header_len);
2100 opi->header.version = OFP10_VERSION;
2101 opi->header.type = OFPT_PACKET_IN;
2102 opi->total_len = htons(pin->total_len);
2103 opi->in_port = htons(pin->fmd.in_port);
2104 opi->reason = pin->reason;
2105 opi->buffer_id = htonl(pin->buffer_id);
2107 ofpbuf_put(packet, pin->packet, send_len);
2108 } else if (packet_in_format == NXPIF_NXM) {
2109 struct nx_packet_in *npi;
2110 struct cls_rule rule;
2114 /* Estimate of required PACKET_IN length includes the NPI header, space
2115 * for the match (2 times sizeof the metadata seems like enough), 2
2116 * bytes for padding, and the packet length. */
2117 packet = ofpbuf_new(sizeof *npi + sizeof(struct flow_metadata) * 2
2120 cls_rule_init_catchall(&rule, 0);
2121 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
2122 pin->fmd.tun_id_mask);
2124 for (i = 0; i < FLOW_N_REGS; i++) {
2125 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
2126 pin->fmd.reg_masks[i]);
2129 cls_rule_set_in_port(&rule, pin->fmd.in_port);
2131 ofpbuf_put_zeros(packet, sizeof *npi);
2132 match_len = nx_put_match(packet, &rule, 0, 0);
2133 ofpbuf_put_zeros(packet, 2);
2134 ofpbuf_put(packet, pin->packet, send_len);
2137 npi->nxh.header.version = OFP10_VERSION;
2138 npi->nxh.header.type = OFPT_VENDOR;
2139 npi->nxh.vendor = htonl(NX_VENDOR_ID);
2140 npi->nxh.subtype = htonl(NXT_PACKET_IN);
2142 npi->buffer_id = htonl(pin->buffer_id);
2143 npi->total_len = htons(pin->total_len);
2144 npi->reason = pin->reason;
2145 npi->table_id = pin->table_id;
2146 npi->cookie = pin->cookie;
2147 npi->match_len = htons(match_len);
2151 update_openflow_length(packet);
2157 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2159 static char s[INT_STRLEN(int) + 1];
2166 case OFPR_INVALID_TTL:
2167 return "invalid_ttl";
2169 case OFPR_N_REASONS:
2171 sprintf(s, "%d", (int) reason);
2177 ofputil_packet_in_reason_from_string(const char *s,
2178 enum ofp_packet_in_reason *reason)
2182 for (i = 0; i < OFPR_N_REASONS; i++) {
2183 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2192 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2193 const struct ofp_packet_out *opo)
2198 po->buffer_id = ntohl(opo->buffer_id);
2199 po->in_port = ntohs(opo->in_port);
2200 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2201 && po->in_port != OFPP_NONE) {
2202 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2204 return OFPERR_NXBRC_BAD_IN_PORT;
2207 ofpbuf_use_const(&b, opo, ntohs(opo->header.length));
2208 ofpbuf_pull(&b, sizeof *opo);
2210 error = ofputil_pull_actions(&b, ntohs(opo->actions_len),
2211 &po->actions, &po->n_actions);
2216 if (po->buffer_id == UINT32_MAX) {
2217 po->packet = b.data;
2218 po->packet_len = b.size;
2228 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
2230 struct ofp_packet_out *opo;
2235 actions_len = po->n_actions * sizeof *po->actions;
2236 size = sizeof *opo + actions_len;
2237 if (po->buffer_id == UINT32_MAX) {
2238 size += po->packet_len;
2241 msg = ofpbuf_new(size);
2242 opo = put_openflow(sizeof *opo, OFPT10_PACKET_OUT, msg);
2243 opo->buffer_id = htonl(po->buffer_id);
2244 opo->in_port = htons(po->in_port);
2245 opo->actions_len = htons(actions_len);
2246 ofpbuf_put(msg, po->actions, actions_len);
2247 if (po->buffer_id == UINT32_MAX) {
2248 ofpbuf_put(msg, po->packet, po->packet_len);
2250 update_openflow_length(msg);
2255 /* Returns a string representing the message type of 'type'. The string is the
2256 * enumeration constant for the type, e.g. "OFPT_HELLO". For statistics
2257 * messages, the constant is followed by "request" or "reply",
2258 * e.g. "OFPST_AGGREGATE reply". */
2260 ofputil_msg_type_name(const struct ofputil_msg_type *type)
2265 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2266 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2267 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
2270 * The caller is responsible for freeing '*bufferp' when it is no longer
2273 * The OpenFlow header length is initially set to 'openflow_len'; if the
2274 * message is later extended, the length should be updated with
2275 * update_openflow_length() before sending.
2277 * Returns the header. */
2279 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
2281 *bufferp = ofpbuf_new(openflow_len);
2282 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
2285 /* Similar to make_openflow() but creates a Nicira vendor extension message
2286 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2288 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
2290 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
2293 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
2294 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
2295 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
2298 * The caller is responsible for freeing '*bufferp' when it is no longer
2301 * The OpenFlow header length is initially set to 'openflow_len'; if the
2302 * message is later extended, the length should be updated with
2303 * update_openflow_length() before sending.
2305 * Returns the header. */
2307 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
2308 struct ofpbuf **bufferp)
2310 *bufferp = ofpbuf_new(openflow_len);
2311 return put_openflow_xid(openflow_len, type, xid, *bufferp);
2314 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
2315 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2317 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2318 struct ofpbuf **bufferp)
2320 *bufferp = ofpbuf_new(openflow_len);
2321 return put_nxmsg_xid(openflow_len, subtype, xid, *bufferp);
2324 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2325 * with the given 'type' and an arbitrary transaction id. Allocated bytes
2326 * beyond the header, if any, are zeroed.
2328 * The OpenFlow header length is initially set to 'openflow_len'; if the
2329 * message is later extended, the length should be updated with
2330 * update_openflow_length() before sending.
2332 * Returns the header. */
2334 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
2336 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
2339 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
2340 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
2341 * the header, if any, are zeroed.
2343 * The OpenFlow header length is initially set to 'openflow_len'; if the
2344 * message is later extended, the length should be updated with
2345 * update_openflow_length() before sending.
2347 * Returns the header. */
2349 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
2350 struct ofpbuf *buffer)
2352 struct ofp_header *oh;
2354 assert(openflow_len >= sizeof *oh);
2355 assert(openflow_len <= UINT16_MAX);
2357 oh = ofpbuf_put_uninit(buffer, openflow_len);
2358 oh->version = OFP10_VERSION;
2360 oh->length = htons(openflow_len);
2362 memset(oh + 1, 0, openflow_len - sizeof *oh);
2366 /* Similar to put_openflow() but append a Nicira vendor extension message with
2367 * the specific 'subtype'. 'subtype' should be in host byte order. */
2369 put_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf *buffer)
2371 return put_nxmsg_xid(openflow_len, subtype, alloc_xid(), buffer);
2374 /* Similar to put_openflow_xid() but append a Nicira vendor extension message
2375 * with the specific 'subtype'. 'subtype' should be in host byte order. */
2377 put_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
2378 struct ofpbuf *buffer)
2380 struct nicira_header *nxh;
2382 nxh = put_openflow_xid(openflow_len, OFPT_VENDOR, xid, buffer);
2383 nxh->vendor = htonl(NX_VENDOR_ID);
2384 nxh->subtype = htonl(subtype);
2388 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
2389 * 'buffer->size'. */
2391 update_openflow_length(struct ofpbuf *buffer)
2393 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
2394 oh->length = htons(buffer->size);
2398 put_stats__(ovs_be32 xid, uint8_t ofp_type,
2399 ovs_be16 ofpst_type, ovs_be32 nxst_subtype,
2402 if (ofpst_type == htons(OFPST_VENDOR)) {
2403 struct nicira_stats_msg *nsm;
2405 nsm = put_openflow_xid(sizeof *nsm, ofp_type, xid, msg);
2406 nsm->vsm.osm.type = ofpst_type;
2407 nsm->vsm.vendor = htonl(NX_VENDOR_ID);
2408 nsm->subtype = nxst_subtype;
2410 struct ofp_stats_msg *osm;
2412 osm = put_openflow_xid(sizeof *osm, ofp_type, xid, msg);
2413 osm->type = ofpst_type;
2417 /* Creates a statistics request message with total length 'openflow_len'
2418 * (including all headers) and the given 'ofpst_type', and stores the buffer
2419 * containing the new message in '*bufferp'. If 'ofpst_type' is OFPST_VENDOR
2420 * then 'nxst_subtype' is used as the Nicira vendor extension statistics
2421 * subtype (otherwise 'nxst_subtype' is ignored).
2423 * Initializes bytes following the headers to all-bits-zero.
2425 * Returns the first byte of the new message. */
2427 ofputil_make_stats_request(size_t openflow_len, uint16_t ofpst_type,
2428 uint32_t nxst_subtype, struct ofpbuf **bufferp)
2432 msg = *bufferp = ofpbuf_new(openflow_len);
2433 put_stats__(alloc_xid(), OFPT10_STATS_REQUEST,
2434 htons(ofpst_type), htonl(nxst_subtype), msg);
2435 ofpbuf_padto(msg, openflow_len);
2441 put_stats_reply__(const struct ofp_stats_msg *request, struct ofpbuf *msg)
2443 assert(request->header.type == OFPT10_STATS_REQUEST ||
2444 request->header.type == OFPT10_STATS_REPLY);
2445 put_stats__(request->header.xid, OFPT10_STATS_REPLY, request->type,
2446 (request->type != htons(OFPST_VENDOR)
2448 : ((const struct nicira_stats_msg *) request)->subtype),
2452 /* Creates a statistics reply message with total length 'openflow_len'
2453 * (including all headers) and the same type (either a standard OpenFlow
2454 * statistics type or a Nicira extension type and subtype) as 'request', and
2455 * stores the buffer containing the new message in '*bufferp'.
2457 * Initializes bytes following the headers to all-bits-zero.
2459 * Returns the first byte of the new message. */
2461 ofputil_make_stats_reply(size_t openflow_len,
2462 const struct ofp_stats_msg *request,
2463 struct ofpbuf **bufferp)
2467 msg = *bufferp = ofpbuf_new(openflow_len);
2468 put_stats_reply__(request, msg);
2469 ofpbuf_padto(msg, openflow_len);
2474 /* Initializes 'replies' as a list of ofpbufs that will contain a series of
2475 * replies to 'request', which should be an OpenFlow or Nicira extension
2476 * statistics request. Initially 'replies' will have a single reply message
2477 * that has only a header. The functions ofputil_reserve_stats_reply() and
2478 * ofputil_append_stats_reply() may be used to add to the reply. */
2480 ofputil_start_stats_reply(const struct ofp_stats_msg *request,
2481 struct list *replies)
2485 msg = ofpbuf_new(1024);
2486 put_stats_reply__(request, msg);
2489 list_push_back(replies, &msg->list_node);
2492 /* Prepares to append up to 'len' bytes to the series of statistics replies in
2493 * 'replies', which should have been initialized with
2494 * ofputil_start_stats_reply(). Returns an ofpbuf with at least 'len' bytes of
2495 * tailroom. (The 'len' bytes have not actually be allocated; the caller must
2496 * do so with e.g. ofpbuf_put_uninit().) */
2498 ofputil_reserve_stats_reply(size_t len, struct list *replies)
2500 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
2501 struct ofp_stats_msg *osm = msg->data;
2503 if (msg->size + len <= UINT16_MAX) {
2504 ofpbuf_prealloc_tailroom(msg, len);
2506 osm->flags |= htons(OFPSF_REPLY_MORE);
2508 msg = ofpbuf_new(MAX(1024, sizeof(struct nicira_stats_msg) + len));
2509 put_stats_reply__(osm, msg);
2510 list_push_back(replies, &msg->list_node);
2515 /* Appends 'len' bytes to the series of statistics replies in 'replies', and
2516 * returns the first byte. */
2518 ofputil_append_stats_reply(size_t len, struct list *replies)
2520 return ofpbuf_put_uninit(ofputil_reserve_stats_reply(len, replies), len);
2523 /* Returns the first byte past the ofp_stats_msg header in 'oh'. */
2525 ofputil_stats_body(const struct ofp_header *oh)
2527 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
2528 return (const struct ofp_stats_msg *) oh + 1;
2531 /* Returns the number of bytes past the ofp_stats_msg header in 'oh'. */
2533 ofputil_stats_body_len(const struct ofp_header *oh)
2535 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
2536 return ntohs(oh->length) - sizeof(struct ofp_stats_msg);
2539 /* Returns the first byte past the nicira_stats_msg header in 'oh'. */
2541 ofputil_nxstats_body(const struct ofp_header *oh)
2543 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
2544 return ((const struct nicira_stats_msg *) oh) + 1;
2547 /* Returns the number of bytes past the nicira_stats_msg header in 'oh'. */
2549 ofputil_nxstats_body_len(const struct ofp_header *oh)
2551 assert(oh->type == OFPT10_STATS_REQUEST || oh->type == OFPT10_STATS_REPLY);
2552 return ntohs(oh->length) - sizeof(struct nicira_stats_msg);
2556 make_flow_mod(uint16_t command, const struct cls_rule *rule,
2559 struct ofp_flow_mod *ofm;
2560 size_t size = sizeof *ofm + actions_len;
2561 struct ofpbuf *out = ofpbuf_new(size);
2562 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
2563 ofm->header.version = OFP10_VERSION;
2564 ofm->header.type = OFPT10_FLOW_MOD;
2565 ofm->header.length = htons(size);
2567 ofm->priority = htons(MIN(rule->priority, UINT16_MAX));
2568 ofputil_cls_rule_to_match(rule, &ofm->match);
2569 ofm->command = htons(command);
2574 make_add_flow(const struct cls_rule *rule, uint32_t buffer_id,
2575 uint16_t idle_timeout, size_t actions_len)
2577 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, rule, actions_len);
2578 struct ofp_flow_mod *ofm = out->data;
2579 ofm->idle_timeout = htons(idle_timeout);
2580 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
2581 ofm->buffer_id = htonl(buffer_id);
2586 make_del_flow(const struct cls_rule *rule)
2588 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, rule, 0);
2589 struct ofp_flow_mod *ofm = out->data;
2590 ofm->out_port = htons(OFPP_NONE);
2595 make_add_simple_flow(const struct cls_rule *rule,
2596 uint32_t buffer_id, uint16_t out_port,
2597 uint16_t idle_timeout)
2599 if (out_port != OFPP_NONE) {
2600 struct ofp_action_output *oao;
2601 struct ofpbuf *buffer;
2603 buffer = make_add_flow(rule, buffer_id, idle_timeout, sizeof *oao);
2604 ofputil_put_OFPAT_OUTPUT(buffer)->port = htons(out_port);
2607 return make_add_flow(rule, buffer_id, idle_timeout, 0);
2612 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
2613 const struct ofpbuf *payload, int max_send_len)
2615 struct ofp_packet_in *opi;
2619 send_len = MIN(max_send_len, payload->size);
2620 buf = ofpbuf_new(sizeof *opi + send_len);
2621 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
2622 OFPT_PACKET_IN, 0, buf);
2623 opi->buffer_id = htonl(buffer_id);
2624 opi->total_len = htons(payload->size);
2625 opi->in_port = htons(in_port);
2626 opi->reason = reason;
2627 ofpbuf_put(buf, payload->data, send_len);
2628 update_openflow_length(buf);
2633 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
2635 make_echo_request(void)
2637 struct ofp_header *rq;
2638 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
2639 rq = ofpbuf_put_uninit(out, sizeof *rq);
2640 rq->version = OFP10_VERSION;
2641 rq->type = OFPT_ECHO_REQUEST;
2642 rq->length = htons(sizeof *rq);
2647 /* Creates and returns an OFPT_ECHO_REPLY message matching the
2648 * OFPT_ECHO_REQUEST message in 'rq'. */
2650 make_echo_reply(const struct ofp_header *rq)
2652 size_t size = ntohs(rq->length);
2653 struct ofpbuf *out = ofpbuf_new(size);
2654 struct ofp_header *reply = ofpbuf_put(out, rq, size);
2655 reply->type = OFPT_ECHO_REPLY;
2660 ofputil_encode_barrier_request(void)
2664 make_openflow(sizeof(struct ofp_header), OFPT10_BARRIER_REQUEST, &msg);
2669 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
2671 switch (flags & OFPC_FRAG_MASK) {
2672 case OFPC_FRAG_NORMAL: return "normal";
2673 case OFPC_FRAG_DROP: return "drop";
2674 case OFPC_FRAG_REASM: return "reassemble";
2675 case OFPC_FRAG_NX_MATCH: return "nx-match";
2682 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
2684 if (!strcasecmp(s, "normal")) {
2685 *flags = OFPC_FRAG_NORMAL;
2686 } else if (!strcasecmp(s, "drop")) {
2687 *flags = OFPC_FRAG_DROP;
2688 } else if (!strcasecmp(s, "reassemble")) {
2689 *flags = OFPC_FRAG_REASM;
2690 } else if (!strcasecmp(s, "nx-match")) {
2691 *flags = OFPC_FRAG_NX_MATCH;
2698 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
2699 * port number and stores the latter in '*ofp10_port', for the purpose of
2700 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
2701 * otherwise an OFPERR_* number.
2703 * See the definition of OFP11_MAX for an explanation of the mapping. */
2705 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
2707 uint32_t ofp11_port_h = ntohl(ofp11_port);
2709 if (ofp11_port_h < OFPP_MAX) {
2710 *ofp10_port = ofp11_port_h;
2712 } else if (ofp11_port_h >= OFPP11_MAX) {
2713 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
2716 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
2717 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
2718 ofp11_port_h, OFPP_MAX - 1,
2719 (uint32_t) OFPP11_MAX, UINT32_MAX);
2720 return OFPERR_OFPBAC_BAD_OUT_PORT;
2724 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
2725 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
2727 * See the definition of OFP11_MAX for an explanation of the mapping. */
2729 ofputil_port_to_ofp11(uint16_t ofp10_port)
2731 return htonl(ofp10_port < OFPP_MAX
2733 : ofp10_port + OFPP11_OFFSET);
2736 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
2737 * that the switch will never have more than 'max_ports' ports. Returns 0 if
2738 * 'port' is valid, otherwise an OpenFlow return code. */
2740 ofputil_check_output_port(uint16_t port, int max_ports)
2748 case OFPP_CONTROLLER:
2753 if (port < max_ports) {
2756 return OFPERR_OFPBAC_BAD_OUT_PORT;
2760 #define OFPUTIL_NAMED_PORTS \
2761 OFPUTIL_NAMED_PORT(IN_PORT) \
2762 OFPUTIL_NAMED_PORT(TABLE) \
2763 OFPUTIL_NAMED_PORT(NORMAL) \
2764 OFPUTIL_NAMED_PORT(FLOOD) \
2765 OFPUTIL_NAMED_PORT(ALL) \
2766 OFPUTIL_NAMED_PORT(CONTROLLER) \
2767 OFPUTIL_NAMED_PORT(LOCAL) \
2768 OFPUTIL_NAMED_PORT(NONE)
2770 /* Checks whether 's' is the string representation of an OpenFlow port number,
2771 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
2772 * number in '*port' and returns true. Otherwise, returns false. */
2774 ofputil_port_from_string(const char *name, uint16_t *port)
2780 static const struct pair pairs[] = {
2781 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
2783 #undef OFPUTIL_NAMED_PORT
2785 static const int n_pairs = ARRAY_SIZE(pairs);
2788 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
2793 for (i = 0; i < n_pairs; i++) {
2794 if (!strcasecmp(name, pairs[i].name)) {
2795 *port = pairs[i].value;
2802 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
2803 * Most ports' string representation is just the port number, but for special
2804 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
2806 ofputil_format_port(uint16_t port, struct ds *s)
2811 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
2813 #undef OFPUTIL_NAMED_PORT
2816 ds_put_format(s, "%"PRIu16, port);
2819 ds_put_cstr(s, name);
2823 check_resubmit_table(const struct nx_action_resubmit *nar)
2825 if (nar->pad[0] || nar->pad[1] || nar->pad[2]) {
2826 return OFPERR_OFPBAC_BAD_ARGUMENT;
2832 check_output_reg(const struct nx_action_output_reg *naor,
2833 const struct flow *flow)
2835 struct mf_subfield src;
2838 for (i = 0; i < sizeof naor->zero; i++) {
2839 if (naor->zero[i]) {
2840 return OFPERR_OFPBAC_BAD_ARGUMENT;
2844 nxm_decode(&src, naor->src, naor->ofs_nbits);
2845 return mf_check_src(&src, flow);
2849 validate_actions(const union ofp_action *actions, size_t n_actions,
2850 const struct flow *flow, int max_ports)
2852 const union ofp_action *a;
2855 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
2860 code = ofputil_decode_action(a);
2863 VLOG_WARN_RL(&bad_ofmsg_rl,
2864 "action decoding error at offset %td (%s)",
2865 (a - actions) * sizeof *a, ofperr_get_name(error));
2871 switch ((enum ofputil_action_code) code) {
2872 case OFPUTIL_OFPAT_OUTPUT:
2873 error = ofputil_check_output_port(ntohs(a->output.port),
2877 case OFPUTIL_OFPAT_SET_VLAN_VID:
2878 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
2879 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2883 case OFPUTIL_OFPAT_SET_VLAN_PCP:
2884 if (a->vlan_pcp.vlan_pcp & ~7) {
2885 error = OFPERR_OFPBAC_BAD_ARGUMENT;
2889 case OFPUTIL_OFPAT_ENQUEUE:
2890 port = ntohs(((const struct ofp_action_enqueue *) a)->port);
2891 if (port >= max_ports && port != OFPP_IN_PORT
2892 && port != OFPP_LOCAL) {
2893 error = OFPERR_OFPBAC_BAD_OUT_PORT;
2897 case OFPUTIL_NXAST_REG_MOVE:
2898 error = nxm_check_reg_move((const struct nx_action_reg_move *) a,
2902 case OFPUTIL_NXAST_REG_LOAD:
2903 error = nxm_check_reg_load((const struct nx_action_reg_load *) a,
2907 case OFPUTIL_NXAST_MULTIPATH:
2908 error = multipath_check((const struct nx_action_multipath *) a,
2912 case OFPUTIL_NXAST_AUTOPATH:
2913 error = autopath_check((const struct nx_action_autopath *) a,
2917 case OFPUTIL_NXAST_BUNDLE:
2918 case OFPUTIL_NXAST_BUNDLE_LOAD:
2919 error = bundle_check((const struct nx_action_bundle *) a,
2923 case OFPUTIL_NXAST_OUTPUT_REG:
2924 error = check_output_reg((const struct nx_action_output_reg *) a,
2928 case OFPUTIL_NXAST_RESUBMIT_TABLE:
2929 error = check_resubmit_table(
2930 (const struct nx_action_resubmit *) a);
2933 case OFPUTIL_NXAST_LEARN:
2934 error = learn_check((const struct nx_action_learn *) a, flow);
2937 case OFPUTIL_NXAST_CONTROLLER:
2938 if (((const struct nx_action_controller *) a)->zero) {
2939 error = OFPERR_NXBAC_MUST_BE_ZERO;
2943 case OFPUTIL_OFPAT_STRIP_VLAN:
2944 case OFPUTIL_OFPAT_SET_NW_SRC:
2945 case OFPUTIL_OFPAT_SET_NW_DST:
2946 case OFPUTIL_OFPAT_SET_NW_TOS:
2947 case OFPUTIL_OFPAT_SET_TP_SRC:
2948 case OFPUTIL_OFPAT_SET_TP_DST:
2949 case OFPUTIL_OFPAT_SET_DL_SRC:
2950 case OFPUTIL_OFPAT_SET_DL_DST:
2951 case OFPUTIL_NXAST_RESUBMIT:
2952 case OFPUTIL_NXAST_SET_TUNNEL:
2953 case OFPUTIL_NXAST_SET_QUEUE:
2954 case OFPUTIL_NXAST_POP_QUEUE:
2955 case OFPUTIL_NXAST_NOTE:
2956 case OFPUTIL_NXAST_SET_TUNNEL64:
2957 case OFPUTIL_NXAST_EXIT:
2958 case OFPUTIL_NXAST_DEC_TTL:
2959 case OFPUTIL_NXAST_FIN_TIMEOUT:
2964 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action at offset %td (%s)",
2965 (a - actions) * sizeof *a, ofperr_get_name(error));
2970 VLOG_WARN_RL(&bad_ofmsg_rl, "bad action format at offset %zu",
2971 (n_actions - left) * sizeof *a);
2972 return OFPERR_OFPBAC_BAD_LEN;
2977 struct ofputil_action {
2979 unsigned int min_len;
2980 unsigned int max_len;
2983 static const struct ofputil_action action_bad_type
2984 = { -OFPERR_OFPBAC_BAD_TYPE, 0, UINT_MAX };
2985 static const struct ofputil_action action_bad_len
2986 = { -OFPERR_OFPBAC_BAD_LEN, 0, UINT_MAX };
2987 static const struct ofputil_action action_bad_vendor
2988 = { -OFPERR_OFPBAC_BAD_VENDOR, 0, UINT_MAX };
2990 static const struct ofputil_action *
2991 ofputil_decode_ofpat_action(const union ofp_action *a)
2993 enum ofp_action_type type = ntohs(a->type);
2996 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
2998 static const struct ofputil_action action = { \
3000 sizeof(struct STRUCT), \
3001 sizeof(struct STRUCT) \
3005 #include "ofp-util.def"
3009 return &action_bad_type;
3013 static const struct ofputil_action *
3014 ofputil_decode_nxast_action(const union ofp_action *a)
3016 const struct nx_action_header *nah = (const struct nx_action_header *) a;
3017 enum nx_action_subtype subtype = ntohs(nah->subtype);
3020 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3022 static const struct ofputil_action action = { \
3024 sizeof(struct STRUCT), \
3025 EXTENSIBLE ? UINT_MAX : sizeof(struct STRUCT) \
3029 #include "ofp-util.def"
3031 case NXAST_SNAT__OBSOLETE:
3032 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
3034 return &action_bad_type;
3038 /* Parses 'a' to determine its type. Returns a nonnegative OFPUTIL_OFPAT_* or
3039 * OFPUTIL_NXAST_* constant if successful, otherwise a negative OFPERR_* error
3042 * The caller must have already verified that 'a''s length is correct (that is,
3043 * a->header.len is nonzero and a multiple of sizeof(union ofp_action) and no
3044 * longer than the amount of space allocated to 'a').
3046 * This function verifies that 'a''s length is correct for the type of action
3047 * that it represents. */
3049 ofputil_decode_action(const union ofp_action *a)
3051 const struct ofputil_action *action;
3052 uint16_t len = ntohs(a->header.len);
3054 if (a->type != htons(OFPAT_VENDOR)) {
3055 action = ofputil_decode_ofpat_action(a);
3057 switch (ntohl(a->vendor.vendor)) {
3059 if (len < sizeof(struct nx_action_header)) {
3060 return -OFPERR_OFPBAC_BAD_LEN;
3062 action = ofputil_decode_nxast_action(a);
3065 action = &action_bad_vendor;
3070 return (len >= action->min_len && len <= action->max_len
3072 : -OFPERR_OFPBAC_BAD_LEN);
3075 /* Parses 'a' and returns its type as an OFPUTIL_OFPAT_* or OFPUTIL_NXAST_*
3076 * constant. The caller must have already validated that 'a' is a valid action
3077 * understood by Open vSwitch (e.g. by a previous successful call to
3078 * ofputil_decode_action()). */
3079 enum ofputil_action_code
3080 ofputil_decode_action_unsafe(const union ofp_action *a)
3082 const struct ofputil_action *action;
3084 if (a->type != htons(OFPAT_VENDOR)) {
3085 action = ofputil_decode_ofpat_action(a);
3087 action = ofputil_decode_nxast_action(a);
3090 return action->code;
3093 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3094 * 'name' is "output" then the return value is OFPUTIL_OFPAT_OUTPUT), or -1 if
3095 * 'name' is not the name of any action.
3097 * ofp-util.def lists the mapping from names to action. */
3099 ofputil_action_code_from_name(const char *name)
3101 static const char *names[OFPUTIL_N_ACTIONS] = {
3102 #define OFPAT_ACTION(ENUM, STRUCT, NAME) NAME,
3103 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3104 #include "ofp-util.def"
3109 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3110 if (*p && !strcasecmp(name, *p)) {
3117 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3118 * action. Initializes the parts of 'action' that identify it as having type
3119 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3120 * have variable length, the length used and cleared is that of struct
3123 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3126 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
3127 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3128 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3129 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3130 #include "ofp-util.def"
3135 #define OFPAT_ACTION(ENUM, STRUCT, NAME) \
3137 ofputil_init_##ENUM(struct STRUCT *s) \
3139 memset(s, 0, sizeof *s); \
3140 s->type = htons(ENUM); \
3141 s->len = htons(sizeof *s); \
3145 ofputil_put_##ENUM(struct ofpbuf *buf) \
3147 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3148 ofputil_init_##ENUM(s); \
3151 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3153 ofputil_init_##ENUM(struct STRUCT *s) \
3155 memset(s, 0, sizeof *s); \
3156 s->type = htons(OFPAT_VENDOR); \
3157 s->len = htons(sizeof *s); \
3158 s->vendor = htonl(NX_VENDOR_ID); \
3159 s->subtype = htons(ENUM); \
3163 ofputil_put_##ENUM(struct ofpbuf *buf) \
3165 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3166 ofputil_init_##ENUM(s); \
3169 #include "ofp-util.def"
3171 /* Returns true if 'action' outputs to 'port', false otherwise. */
3173 action_outputs_to_port(const union ofp_action *action, ovs_be16 port)
3175 switch (ntohs(action->type)) {
3177 return action->output.port == port;
3179 return ((const struct ofp_action_enqueue *) action)->port == port;
3185 /* "Normalizes" the wildcards in 'rule'. That means:
3187 * 1. If the type of level N is known, then only the valid fields for that
3188 * level may be specified. For example, ARP does not have a TOS field,
3189 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3190 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3191 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3194 * 2. If the type of level N is not known (or not understood by Open
3195 * vSwitch), then no fields at all for that level may be specified. For
3196 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3197 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3201 ofputil_normalize_rule(struct cls_rule *rule)
3204 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3205 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3206 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3207 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3208 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3209 MAY_ARP_THA = 1 << 5, /* arp_tha */
3210 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3211 MAY_ND_TARGET = 1 << 7 /* nd_target */
3214 struct flow_wildcards wc;
3216 /* Figure out what fields may be matched. */
3217 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
3218 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3219 if (rule->flow.nw_proto == IPPROTO_TCP ||
3220 rule->flow.nw_proto == IPPROTO_UDP ||
3221 rule->flow.nw_proto == IPPROTO_ICMP) {
3222 may_match |= MAY_TP_ADDR;
3224 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3225 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3226 if (rule->flow.nw_proto == IPPROTO_TCP ||
3227 rule->flow.nw_proto == IPPROTO_UDP) {
3228 may_match |= MAY_TP_ADDR;
3229 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3230 may_match |= MAY_TP_ADDR;
3231 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3232 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3233 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3234 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3237 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
3238 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3243 /* Clear the fields that may not be matched. */
3245 if (!(may_match & MAY_NW_ADDR)) {
3246 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3248 if (!(may_match & MAY_TP_ADDR)) {
3249 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
3251 if (!(may_match & MAY_NW_PROTO)) {
3252 wc.wildcards |= FWW_NW_PROTO;
3254 if (!(may_match & MAY_IPVx)) {
3255 wc.wildcards |= FWW_NW_DSCP;
3256 wc.wildcards |= FWW_NW_ECN;
3257 wc.wildcards |= FWW_NW_TTL;
3259 if (!(may_match & MAY_ARP_SHA)) {
3260 wc.wildcards |= FWW_ARP_SHA;
3262 if (!(may_match & MAY_ARP_THA)) {
3263 wc.wildcards |= FWW_ARP_THA;
3265 if (!(may_match & MAY_IPV6)) {
3266 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
3267 wc.wildcards |= FWW_IPV6_LABEL;
3269 if (!(may_match & MAY_ND_TARGET)) {
3270 wc.wildcards |= FWW_ND_TARGET;
3273 /* Log any changes. */
3274 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3275 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
3276 char *pre = log ? cls_rule_to_string(rule) : NULL;
3279 cls_rule_zero_wildcarded_fields(rule);
3282 char *post = cls_rule_to_string(rule);
3283 VLOG_INFO("normalization changed ofp_match, details:");
3284 VLOG_INFO(" pre: %s", pre);
3285 VLOG_INFO("post: %s", post);
3292 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
3293 * successful, otherwise an OpenFlow error.
3295 * If successful, the first action is stored in '*actionsp' and the number of
3296 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
3297 * are stored, respectively.
3299 * This function does not check that the actions are valid (the caller should
3300 * do so, with validate_actions()). The caller is also responsible for making
3301 * sure that 'b->data' is initially aligned appropriately for "union
3304 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
3305 union ofp_action **actionsp, size_t *n_actionsp)
3307 if (actions_len % OFP_ACTION_ALIGN != 0) {
3308 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3309 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
3313 *actionsp = ofpbuf_try_pull(b, actions_len);
3314 if (*actionsp == NULL) {
3315 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
3316 "exceeds remaining message length (%zu)",
3317 actions_len, b->size);
3321 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
3327 return OFPERR_OFPBRC_BAD_LEN;
3331 ofputil_actions_equal(const union ofp_action *a, size_t n_a,
3332 const union ofp_action *b, size_t n_b)
3334 return n_a == n_b && (!n_a || !memcmp(a, b, n_a * sizeof *a));
3338 ofputil_actions_clone(const union ofp_action *actions, size_t n)
3340 return n ? xmemdup(actions, n * sizeof *actions) : NULL;
3343 /* Parses a key or a key-value pair from '*stringp'.
3345 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3346 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3347 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3348 * are substrings of '*stringp' created by replacing some of its bytes by null
3349 * terminators. Returns true.
3351 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3352 * NULL and returns false. */
3354 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3356 char *pos, *key, *value;
3360 pos += strspn(pos, ", \t\r\n");
3362 *keyp = *valuep = NULL;
3367 key_len = strcspn(pos, ":=(, \t\r\n");
3368 if (key[key_len] == ':' || key[key_len] == '=') {
3369 /* The value can be separated by a colon. */
3372 value = key + key_len + 1;
3373 value_len = strcspn(value, ", \t\r\n");
3374 pos = value + value_len + (value[value_len] != '\0');
3375 value[value_len] = '\0';
3376 } else if (key[key_len] == '(') {
3377 /* The value can be surrounded by balanced parentheses. The outermost
3378 * set of parentheses is removed. */
3382 value = key + key_len + 1;
3383 for (value_len = 0; level > 0; value_len++) {
3384 switch (value[value_len]) {
3386 ovs_fatal(0, "unbalanced parentheses in argument to %s", key);
3397 value[value_len - 1] = '\0';
3398 pos = value + value_len;
3400 /* There might be no value at all. */
3401 value = key + key_len; /* Will become the empty string below. */
3402 pos = key + key_len + (key[key_len] != '\0');
3404 key[key_len] = '\0';