2 * Copyright (c) 2008, 2009, 2010 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 "byte-order.h"
22 #include "classifier.h"
30 VLOG_DEFINE_THIS_MODULE(ofp_util);
32 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
33 * in the peer and so there's not much point in showing a lot of them. */
34 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
36 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
37 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
40 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
41 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
42 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
43 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
46 ofputil_wcbits_to_netmask(int wcbits)
49 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
52 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
53 * that it wildcards. 'netmask' must be a CIDR netmask (see ip_is_cidr()). */
55 ofputil_netmask_to_wcbits(ovs_be32 netmask)
57 assert(ip_is_cidr(netmask));
59 return netmask == htonl(0) ? 32 : __builtin_ctz(ntohl(netmask));
63 for (wcbits = 32; netmask; wcbits--) {
64 netmask &= netmask - 1;
71 /* A list of the FWW_* and OFPFW_ bits that have the same value, meaning, and
73 #define WC_INVARIANT_LIST \
74 WC_INVARIANT_BIT(IN_PORT) \
75 WC_INVARIANT_BIT(DL_VLAN) \
76 WC_INVARIANT_BIT(DL_SRC) \
77 WC_INVARIANT_BIT(DL_DST) \
78 WC_INVARIANT_BIT(DL_TYPE) \
79 WC_INVARIANT_BIT(NW_PROTO) \
80 WC_INVARIANT_BIT(TP_SRC) \
81 WC_INVARIANT_BIT(TP_DST)
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
93 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
95 #undef WC_INVARIANT_BIT
98 /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
101 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
102 * the latter case only, 'flow''s tun_id field will be taken from the high bits
103 * of 'cookie', if 'match''s wildcards do not indicate that tun_id is
106 ofputil_cls_rule_from_match(const struct ofp_match *match,
107 unsigned int priority, int flow_format,
108 uint64_t cookie, struct cls_rule *rule)
110 struct flow_wildcards *wc = &rule->wc;
113 /* Initialize rule->priority. */
114 ofpfw = ntohl(match->wildcards);
115 ofpfw &= flow_format == NXFF_TUN_ID_FROM_COOKIE ? OVSFW_ALL : OFPFW_ALL;
116 rule->priority = !ofpfw ? UINT16_MAX : priority;
118 /* Initialize most of rule->wc. */
119 wc->wildcards = ofpfw & WC_INVARIANTS;
120 if (ofpfw & OFPFW_DL_VLAN_PCP) {
121 wc->wildcards |= FWW_DL_VLAN_PCP;
123 if (ofpfw & OFPFW_NW_TOS) {
124 wc->wildcards |= FWW_NW_TOS;
126 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
127 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_SRC_SHIFT);
128 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW_NW_DST_SHIFT);
130 if (!(ofpfw & NXFW_TUN_ID)) {
131 rule->flow.tun_id = htonl(ntohll(cookie) >> 32);
133 wc->wildcards |= FWW_TUN_ID;
134 rule->flow.tun_id = 0;
137 if (ofpfw & OFPFW_DL_DST) {
138 /* OpenFlow 1.0 OFPFW_DL_DST covers the whole Ethernet destination, but
139 * Open vSwitch breaks the Ethernet destination into bits as FWW_DL_DST
140 * and FWW_ETH_MCAST. */
141 wc->wildcards |= FWW_ETH_MCAST;
144 /* Initialize rule->flow. */
145 rule->flow.nw_src = match->nw_src;
146 rule->flow.nw_dst = match->nw_dst;
147 rule->flow.in_port = (match->in_port == htons(OFPP_LOCAL) ? ODPP_LOCAL
148 : ntohs(match->in_port));
149 rule->flow.dl_vlan = match->dl_vlan;
150 rule->flow.dl_vlan_pcp = match->dl_vlan_pcp;
151 rule->flow.dl_type = match->dl_type;
152 rule->flow.tp_src = match->tp_src;
153 rule->flow.tp_dst = match->tp_dst;
154 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
155 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
156 rule->flow.nw_tos = match->nw_tos;
157 rule->flow.nw_proto = match->nw_proto;
160 cls_rule_zero_wildcarded_fields(rule);
163 /* Extract 'flow' with 'wildcards' into the OpenFlow match structure
166 * 'flow_format' must either NXFF_OPENFLOW10 or NXFF_TUN_ID_FROM_COOKIE. In
167 * the latter case only, 'match''s NXFW_TUN_ID bit will be filled in; otherwise
168 * it is always set to 0. */
170 ofputil_cls_rule_to_match(const struct cls_rule *rule, int flow_format,
171 struct ofp_match *match)
173 const struct flow_wildcards *wc = &rule->wc;
176 /* Figure out OpenFlow wildcards. */
177 ofpfw = wc->wildcards & WC_INVARIANTS;
178 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_src_mask) << OFPFW_NW_SRC_SHIFT;
179 ofpfw |= ofputil_netmask_to_wcbits(wc->nw_dst_mask) << OFPFW_NW_DST_SHIFT;
180 if (wc->wildcards & FWW_DL_VLAN_PCP) {
181 ofpfw |= OFPFW_DL_VLAN_PCP;
183 if (wc->wildcards & FWW_NW_TOS) {
184 ofpfw |= OFPFW_NW_TOS;
186 if (flow_format == NXFF_TUN_ID_FROM_COOKIE && wc->wildcards & FWW_TUN_ID) {
187 ofpfw |= NXFW_TUN_ID;
190 /* Compose match structure. */
191 match->wildcards = htonl(ofpfw);
192 match->in_port = htons(rule->flow.in_port == ODPP_LOCAL ? OFPP_LOCAL
193 : rule->flow.in_port);
194 match->dl_vlan = rule->flow.dl_vlan;
195 match->dl_vlan_pcp = rule->flow.dl_vlan_pcp;
196 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
197 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
198 match->dl_type = rule->flow.dl_type;
199 match->nw_src = rule->flow.nw_src;
200 match->nw_dst = rule->flow.nw_dst;
201 match->nw_tos = rule->flow.nw_tos;
202 match->nw_proto = rule->flow.nw_proto;
203 match->tp_src = rule->flow.tp_src;
204 match->tp_dst = rule->flow.tp_dst;
205 memset(match->pad1, '\0', sizeof match->pad1);
206 memset(match->pad2, '\0', sizeof match->pad2);
209 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
213 static uint32_t next_xid = 1;
214 return htonl(next_xid++);
217 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
218 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
219 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
222 * The caller is responsible for freeing '*bufferp' when it is no longer
225 * The OpenFlow header length is initially set to 'openflow_len'; if the
226 * message is later extended, the length should be updated with
227 * update_openflow_length() before sending.
229 * Returns the header. */
231 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
233 *bufferp = ofpbuf_new(openflow_len);
234 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
237 /* Similar to make_openflow() but creates a Nicira vendor extension message
238 * with the specific 'subtype'. 'subtype' should be in host byte order. */
240 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
242 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
245 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
246 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
247 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
250 * The caller is responsible for freeing '*bufferp' when it is no longer
253 * The OpenFlow header length is initially set to 'openflow_len'; if the
254 * message is later extended, the length should be updated with
255 * update_openflow_length() before sending.
257 * Returns the header. */
259 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
260 struct ofpbuf **bufferp)
262 *bufferp = ofpbuf_new(openflow_len);
263 return put_openflow_xid(openflow_len, type, xid, *bufferp);
266 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
267 * with the specific 'subtype'. 'subtype' should be in host byte order. */
269 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
270 struct ofpbuf **bufferp)
272 struct nicira_header *nxh = make_openflow_xid(openflow_len, OFPT_VENDOR,
274 nxh->vendor = htonl(NX_VENDOR_ID);
275 nxh->subtype = htonl(subtype);
279 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
280 * with the given 'type' and an arbitrary transaction id. Allocated bytes
281 * beyond the header, if any, are zeroed.
283 * The OpenFlow header length is initially set to 'openflow_len'; if the
284 * message is later extended, the length should be updated with
285 * update_openflow_length() before sending.
287 * Returns the header. */
289 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
291 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
294 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
295 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
296 * the header, if any, are zeroed.
298 * The OpenFlow header length is initially set to 'openflow_len'; if the
299 * message is later extended, the length should be updated with
300 * update_openflow_length() before sending.
302 * Returns the header. */
304 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
305 struct ofpbuf *buffer)
307 struct ofp_header *oh;
309 assert(openflow_len >= sizeof *oh);
310 assert(openflow_len <= UINT16_MAX);
312 oh = ofpbuf_put_uninit(buffer, openflow_len);
313 oh->version = OFP_VERSION;
315 oh->length = htons(openflow_len);
317 memset(oh + 1, 0, openflow_len - sizeof *oh);
321 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
324 update_openflow_length(struct ofpbuf *buffer)
326 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
327 oh->length = htons(buffer->size);
331 make_flow_mod(uint16_t command, const struct flow *flow, size_t actions_len)
333 struct ofp_flow_mod *ofm;
334 size_t size = sizeof *ofm + actions_len;
335 struct ofpbuf *out = ofpbuf_new(size);
336 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
337 ofm->header.version = OFP_VERSION;
338 ofm->header.type = OFPT_FLOW_MOD;
339 ofm->header.length = htons(size);
341 ofm->match.wildcards = htonl(0);
342 ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
344 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
345 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
346 ofm->match.dl_vlan = flow->dl_vlan;
347 ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp;
348 ofm->match.dl_type = flow->dl_type;
349 ofm->match.nw_src = flow->nw_src;
350 ofm->match.nw_dst = flow->nw_dst;
351 ofm->match.nw_proto = flow->nw_proto;
352 ofm->match.nw_tos = flow->nw_tos;
353 ofm->match.tp_src = flow->tp_src;
354 ofm->match.tp_dst = flow->tp_dst;
355 ofm->command = htons(command);
360 make_add_flow(const struct flow *flow, uint32_t buffer_id,
361 uint16_t idle_timeout, size_t actions_len)
363 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
364 struct ofp_flow_mod *ofm = out->data;
365 ofm->idle_timeout = htons(idle_timeout);
366 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
367 ofm->buffer_id = htonl(buffer_id);
372 make_del_flow(const struct flow *flow)
374 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
375 struct ofp_flow_mod *ofm = out->data;
376 ofm->out_port = htons(OFPP_NONE);
381 make_add_simple_flow(const struct flow *flow,
382 uint32_t buffer_id, uint16_t out_port,
383 uint16_t idle_timeout)
385 if (out_port != OFPP_NONE) {
386 struct ofp_action_output *oao;
387 struct ofpbuf *buffer;
389 buffer = make_add_flow(flow, buffer_id, idle_timeout, sizeof *oao);
390 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
391 oao->type = htons(OFPAT_OUTPUT);
392 oao->len = htons(sizeof *oao);
393 oao->port = htons(out_port);
396 return make_add_flow(flow, buffer_id, idle_timeout, 0);
401 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
402 const struct ofpbuf *payload, int max_send_len)
404 struct ofp_packet_in *opi;
408 send_len = MIN(max_send_len, payload->size);
409 buf = ofpbuf_new(sizeof *opi + send_len);
410 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
411 OFPT_PACKET_IN, 0, buf);
412 opi->buffer_id = htonl(buffer_id);
413 opi->total_len = htons(payload->size);
414 opi->in_port = htons(in_port);
415 opi->reason = reason;
416 ofpbuf_put(buf, payload->data, send_len);
417 update_openflow_length(buf);
423 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
425 const struct ofp_action_header *actions, size_t n_actions)
427 size_t actions_len = n_actions * sizeof *actions;
428 struct ofp_packet_out *opo;
429 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
430 struct ofpbuf *out = ofpbuf_new(size);
432 opo = ofpbuf_put_uninit(out, sizeof *opo);
433 opo->header.version = OFP_VERSION;
434 opo->header.type = OFPT_PACKET_OUT;
435 opo->header.length = htons(size);
436 opo->header.xid = htonl(0);
437 opo->buffer_id = htonl(buffer_id);
438 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
439 opo->actions_len = htons(actions_len);
440 ofpbuf_put(out, actions, actions_len);
442 ofpbuf_put(out, packet->data, packet->size);
448 make_unbuffered_packet_out(const struct ofpbuf *packet,
449 uint16_t in_port, uint16_t out_port)
451 struct ofp_action_output action;
452 action.type = htons(OFPAT_OUTPUT);
453 action.len = htons(sizeof action);
454 action.port = htons(out_port);
455 return make_packet_out(packet, UINT32_MAX, in_port,
456 (struct ofp_action_header *) &action, 1);
460 make_buffered_packet_out(uint32_t buffer_id,
461 uint16_t in_port, uint16_t out_port)
463 if (out_port != OFPP_NONE) {
464 struct ofp_action_output action;
465 action.type = htons(OFPAT_OUTPUT);
466 action.len = htons(sizeof action);
467 action.port = htons(out_port);
468 return make_packet_out(NULL, buffer_id, in_port,
469 (struct ofp_action_header *) &action, 1);
471 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
475 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
477 make_echo_request(void)
479 struct ofp_header *rq;
480 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
481 rq = ofpbuf_put_uninit(out, sizeof *rq);
482 rq->version = OFP_VERSION;
483 rq->type = OFPT_ECHO_REQUEST;
484 rq->length = htons(sizeof *rq);
489 /* Creates and returns an OFPT_ECHO_REPLY message matching the
490 * OFPT_ECHO_REQUEST message in 'rq'. */
492 make_echo_reply(const struct ofp_header *rq)
494 size_t size = ntohs(rq->length);
495 struct ofpbuf *out = ofpbuf_new(size);
496 struct ofp_header *reply = ofpbuf_put(out, rq, size);
497 reply->type = OFPT_ECHO_REPLY;
502 check_message_type(uint8_t got_type, uint8_t want_type)
504 if (got_type != want_type) {
505 char *want_type_name = ofp_message_type_to_string(want_type);
506 char *got_type_name = ofp_message_type_to_string(got_type);
507 VLOG_WARN_RL(&bad_ofmsg_rl,
508 "received bad message type %s (expected %s)",
509 got_type_name, want_type_name);
510 free(want_type_name);
512 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
517 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
518 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
519 * with ofp_mkerr()). */
521 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
526 error = check_message_type(msg->type, type);
531 got_size = ntohs(msg->length);
532 if (got_size != size) {
533 char *type_name = ofp_message_type_to_string(type);
534 VLOG_WARN_RL(&bad_ofmsg_rl,
535 "received %s message of length %zu (expected %zu)",
536 type_name, got_size, size);
538 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
544 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
545 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
546 * the checks pass, otherwise an OpenFlow error code (produced with
549 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
550 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
553 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
554 size_t min_size, size_t array_elt_size,
555 size_t *n_array_elts)
560 assert(array_elt_size);
562 error = check_message_type(msg->type, type);
567 got_size = ntohs(msg->length);
568 if (got_size < min_size) {
569 char *type_name = ofp_message_type_to_string(type);
570 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
571 "(expected at least %zu)",
572 type_name, got_size, min_size);
574 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
576 if ((got_size - min_size) % array_elt_size) {
577 char *type_name = ofp_message_type_to_string(type);
578 VLOG_WARN_RL(&bad_ofmsg_rl,
579 "received %s message of bad length %zu: the "
580 "excess over %zu (%zu) is not evenly divisible by %zu "
581 "(remainder is %zu)",
582 type_name, got_size, min_size, got_size - min_size,
583 array_elt_size, (got_size - min_size) % array_elt_size);
585 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
588 *n_array_elts = (got_size - min_size) / array_elt_size;
593 const struct ofp_flow_stats *
594 flow_stats_first(struct flow_stats_iterator *iter,
595 const struct ofp_stats_reply *osr)
597 iter->pos = osr->body;
598 iter->end = osr->body + (ntohs(osr->header.length)
599 - offsetof(struct ofp_stats_reply, body));
600 return flow_stats_next(iter);
603 const struct ofp_flow_stats *
604 flow_stats_next(struct flow_stats_iterator *iter)
606 ptrdiff_t bytes_left = iter->end - iter->pos;
607 const struct ofp_flow_stats *fs;
610 if (bytes_left < sizeof *fs) {
611 if (bytes_left != 0) {
612 VLOG_WARN_RL(&bad_ofmsg_rl,
613 "%td leftover bytes in flow stats reply", bytes_left);
618 fs = (const void *) iter->pos;
619 length = ntohs(fs->length);
620 if (length < sizeof *fs) {
621 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
622 "min %zu", length, sizeof *fs);
624 } else if (length > bytes_left) {
625 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
626 "bytes left", length, bytes_left);
628 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
629 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
630 "left over in final action", length,
631 (length - sizeof *fs) % sizeof fs->actions[0]);
639 check_action_exact_len(const union ofp_action *a, unsigned int len,
640 unsigned int required_len)
642 if (len != required_len) {
643 VLOG_DBG_RL(&bad_ofmsg_rl,
644 "action %u has invalid length %"PRIu16" (must be %u)\n",
645 a->type, ntohs(a->header.len), required_len);
646 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
651 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
652 * that the switch will never have more than 'max_ports' ports. Returns 0 if
653 * 'port' is valid, otherwise an ofp_mkerr() return code. */
655 check_output_port(uint16_t port, int max_ports)
663 case OFPP_CONTROLLER:
668 if (port < max_ports) {
671 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
672 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
676 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
677 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
678 * otherwise an ofp_mkerr() return code. */
680 check_enqueue_action(const union ofp_action *a, unsigned int len,
683 const struct ofp_action_enqueue *oae;
687 error = check_action_exact_len(a, len, 16);
692 oae = (const struct ofp_action_enqueue *) a;
693 port = ntohs(oae->port);
694 if (port < max_ports || port == OFPP_IN_PORT) {
697 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
698 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
702 check_nicira_action(const union ofp_action *a, unsigned int len,
703 const struct flow *flow)
705 const struct nx_action_header *nah;
709 VLOG_DBG_RL(&bad_ofmsg_rl,
710 "Nicira vendor action only %u bytes", len);
711 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
713 nah = (const struct nx_action_header *) a;
715 switch (ntohs(nah->subtype)) {
717 case NXAST_SET_TUNNEL:
718 case NXAST_DROP_SPOOFED_ARP:
719 case NXAST_SET_QUEUE:
720 case NXAST_POP_QUEUE:
721 return check_action_exact_len(a, len, 16);
724 error = check_action_exact_len(a, len,
725 sizeof(struct nx_action_reg_move));
729 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
732 error = check_action_exact_len(a, len,
733 sizeof(struct nx_action_reg_load));
737 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
743 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
748 check_action(const union ofp_action *a, unsigned int len,
749 const struct flow *flow, int max_ports)
753 switch (ntohs(a->type)) {
755 error = check_action_exact_len(a, len, 8);
759 return check_output_port(ntohs(a->output.port), max_ports);
761 case OFPAT_SET_VLAN_VID:
762 error = check_action_exact_len(a, len, 8);
766 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
767 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
771 case OFPAT_SET_VLAN_PCP:
772 error = check_action_exact_len(a, len, 8);
776 if (a->vlan_vid.vlan_vid & ~7) {
777 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
781 case OFPAT_STRIP_VLAN:
782 case OFPAT_SET_NW_SRC:
783 case OFPAT_SET_NW_DST:
784 case OFPAT_SET_NW_TOS:
785 case OFPAT_SET_TP_SRC:
786 case OFPAT_SET_TP_DST:
787 return check_action_exact_len(a, len, 8);
789 case OFPAT_SET_DL_SRC:
790 case OFPAT_SET_DL_DST:
791 return check_action_exact_len(a, len, 16);
794 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
795 ? check_nicira_action(a, len, flow)
796 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
799 return check_enqueue_action(a, len, max_ports);
802 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
804 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
809 validate_actions(const union ofp_action *actions, size_t n_actions,
810 const struct flow *flow, int max_ports)
814 for (i = 0; i < n_actions; ) {
815 const union ofp_action *a = &actions[i];
816 unsigned int len = ntohs(a->header.len);
817 unsigned int n_slots = len / OFP_ACTION_ALIGN;
818 unsigned int slots_left = &actions[n_actions] - a;
821 if (n_slots > slots_left) {
822 VLOG_DBG_RL(&bad_ofmsg_rl,
823 "action requires %u slots but only %u remain",
824 n_slots, slots_left);
825 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
827 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
828 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
829 } else if (len % OFP_ACTION_ALIGN) {
830 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
831 "of %d", len, OFP_ACTION_ALIGN);
832 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
835 error = check_action(a, len, flow, max_ports);
844 /* Returns true if 'action' outputs to 'port' (which must be in network byte
845 * order), false otherwise. */
847 action_outputs_to_port(const union ofp_action *action, uint16_t port)
849 switch (ntohs(action->type)) {
851 return action->output.port == port;
853 return ((const struct ofp_action_enqueue *) action)->port == port;
859 /* The set of actions must either come from a trusted source or have been
860 * previously validated with validate_actions(). */
861 const union ofp_action *
862 actions_first(struct actions_iterator *iter,
863 const union ofp_action *oa, size_t n_actions)
866 iter->end = oa + n_actions;
867 return actions_next(iter);
870 const union ofp_action *
871 actions_next(struct actions_iterator *iter)
873 if (iter->pos != iter->end) {
874 const union ofp_action *a = iter->pos;
875 unsigned int len = ntohs(a->header.len);
876 iter->pos += len / OFP_ACTION_ALIGN;
884 normalize_match(struct ofp_match *m)
886 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
888 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
891 wc = ntohl(m->wildcards) & OVSFW_ALL;
892 if (wc & OFPFW_DL_TYPE) {
895 /* Can't sensibly match on network or transport headers if the
896 * data link type is unknown. */
897 wc |= OFPFW_NW | OFPFW_TP;
898 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
899 m->tp_src = m->tp_dst = 0;
900 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
901 if (wc & OFPFW_NW_PROTO) {
904 /* Can't sensibly match on transport headers if the network
905 * protocol is unknown. */
907 m->tp_src = m->tp_dst = 0;
908 } else if (m->nw_proto == IPPROTO_TCP ||
909 m->nw_proto == IPPROTO_UDP ||
910 m->nw_proto == IPPROTO_ICMP) {
911 if (wc & OFPFW_TP_SRC) {
914 if (wc & OFPFW_TP_DST) {
918 /* Transport layer fields will always be extracted as zeros, so we
919 * can do an exact-match on those values. */
921 m->tp_src = m->tp_dst = 0;
923 if (wc & OFPFW_NW_SRC_MASK) {
924 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
926 if (wc & OFPFW_NW_DST_MASK) {
927 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
929 if (wc & OFPFW_NW_TOS) {
932 m->nw_tos &= IP_DSCP_MASK;
934 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
935 if (wc & OFPFW_NW_PROTO) {
938 if (wc & OFPFW_NW_SRC_MASK) {
939 m->nw_src &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_SRC_SHIFT);
941 if (wc & OFPFW_NW_DST_MASK) {
942 m->nw_dst &= ofputil_wcbits_to_netmask(wc >> OFPFW_NW_DST_SHIFT);
944 m->tp_src = m->tp_dst = m->nw_tos = 0;
946 /* Network and transport layer fields will always be extracted as
947 * zeros, so we can do an exact-match on those values. */
948 wc &= ~(OFPFW_NW | OFPFW_TP);
949 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
950 m->tp_src = m->tp_dst = 0;
952 if (wc & OFPFW_DL_SRC) {
953 memset(m->dl_src, 0, sizeof m->dl_src);
955 if (wc & OFPFW_DL_DST) {
956 memset(m->dl_dst, 0, sizeof m->dl_dst);
958 m->wildcards = htonl(wc);
961 /* Returns a string that describes 'match' in a very literal way, without
962 * interpreting its contents except in a very basic fashion. The returned
963 * string is intended to be fixed-length, so that it is easy to see differences
964 * between two such strings if one is put above another. This is useful for
965 * describing changes made by normalize_match().
967 * The caller must free the returned string (with free()). */
969 ofp_match_to_literal_string(const struct ofp_match *match)
971 return xasprintf("wildcards=%#10"PRIx32" "
972 " in_port=%5"PRId16" "
973 " dl_src="ETH_ADDR_FMT" "
974 " dl_dst="ETH_ADDR_FMT" "
975 " dl_vlan=%5"PRId16" "
976 " dl_vlan_pcp=%3"PRId8" "
977 " dl_type=%#6"PRIx16" "
978 " nw_tos=%#4"PRIx8" "
979 " nw_proto=%#4"PRIx16" "
980 " nw_src=%#10"PRIx32" "
981 " nw_dst=%#10"PRIx32" "
982 " tp_src=%5"PRId16" "
984 ntohl(match->wildcards),
985 ntohs(match->in_port),
986 ETH_ADDR_ARGS(match->dl_src),
987 ETH_ADDR_ARGS(match->dl_dst),
988 ntohs(match->dl_vlan),
990 ntohs(match->dl_type),
993 ntohl(match->nw_src),
994 ntohl(match->nw_dst),
995 ntohs(match->tp_src),
996 ntohs(match->tp_dst));
1000 vendor_code_to_id(uint8_t code)
1003 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
1005 #undef OFPUTIL_VENDOR
1011 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
1012 * information taken from 'error', whose encoding must be as described in the
1013 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
1014 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
1017 * Returns NULL if 'error' is not an OpenFlow error code. */
1019 make_ofp_error_msg(int error, const struct ofp_header *oh)
1021 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1031 if (!is_ofp_error(error)) {
1032 /* We format 'error' with strerror() here since it seems likely to be
1033 * a system errno value. */
1034 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
1035 error, strerror(error));
1042 len = ntohs(oh->length);
1052 vendor = get_ofp_err_vendor(error);
1053 type = get_ofp_err_type(error);
1054 code = get_ofp_err_code(error);
1055 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
1056 struct ofp_error_msg *oem;
1058 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
1059 oem->type = htons(type);
1060 oem->code = htons(code);
1062 struct ofp_error_msg *oem;
1063 struct nx_vendor_error *nve;
1066 vendor_id = vendor_code_to_id(vendor);
1067 if (vendor_id == UINT32_MAX) {
1068 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
1073 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
1074 OFPT_ERROR, xid, &buf);
1075 oem->type = htons(NXET_VENDOR);
1076 oem->code = htons(NXVC_VENDOR_ERROR);
1078 nve = ofpbuf_put_uninit(buf, sizeof *nve);
1079 nve->vendor = htonl(vendor_id);
1080 nve->type = htons(type);
1081 nve->code = htons(code);
1085 ofpbuf_put(buf, data, len);
1091 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
1092 * successful, otherwise an OpenFlow error.
1094 * If successful, the first action is stored in '*actionsp' and the number of
1095 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
1096 * are stored, respectively.
1098 * This function does not check that the actions are valid (the caller should
1099 * do so, with validate_actions()). The caller is also responsible for making
1100 * sure that 'b->data' is initially aligned appropriately for "union
1103 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
1104 union ofp_action **actionsp, size_t *n_actionsp)
1106 if (actions_len % OFP_ACTION_ALIGN != 0) {
1107 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
1108 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
1112 *actionsp = ofpbuf_try_pull(b, actions_len);
1113 if (*actionsp == NULL) {
1114 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
1115 "exceeds remaining message length (%zu)",
1116 actions_len, b->size);
1120 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
1126 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);