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"
29 VLOG_DEFINE_THIS_MODULE(ofp_util);
31 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
32 * in the peer and so there's not much point in showing a lot of them. */
33 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
35 /* Returns a transaction ID to use for an outgoing OpenFlow message. */
39 static uint32_t next_xid = 1;
40 return htonl(next_xid++);
43 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
44 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
45 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
48 * The caller is responsible for freeing '*bufferp' when it is no longer
51 * The OpenFlow header length is initially set to 'openflow_len'; if the
52 * message is later extended, the length should be updated with
53 * update_openflow_length() before sending.
55 * Returns the header. */
57 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
59 *bufferp = ofpbuf_new(openflow_len);
60 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
63 /* Similar to make_openflow() but creates a Nicira vendor extension message
64 * with the specific 'subtype'. 'subtype' should be in host byte order. */
66 make_nxmsg(size_t openflow_len, uint32_t subtype, struct ofpbuf **bufferp)
68 return make_nxmsg_xid(openflow_len, subtype, alloc_xid(), bufferp);
71 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
72 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
73 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
76 * The caller is responsible for freeing '*bufferp' when it is no longer
79 * The OpenFlow header length is initially set to 'openflow_len'; if the
80 * message is later extended, the length should be updated with
81 * update_openflow_length() before sending.
83 * Returns the header. */
85 make_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
86 struct ofpbuf **bufferp)
88 *bufferp = ofpbuf_new(openflow_len);
89 return put_openflow_xid(openflow_len, type, xid, *bufferp);
92 /* Similar to make_openflow_xid() but creates a Nicira vendor extension message
93 * with the specific 'subtype'. 'subtype' should be in host byte order. */
95 make_nxmsg_xid(size_t openflow_len, uint32_t subtype, ovs_be32 xid,
96 struct ofpbuf **bufferp)
98 struct nicira_header *nxh = make_openflow_xid(openflow_len, OFPT_VENDOR,
100 nxh->vendor = htonl(NX_VENDOR_ID);
101 nxh->subtype = htonl(subtype);
105 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
106 * with the given 'type' and an arbitrary transaction id. Allocated bytes
107 * beyond the header, if any, are zeroed.
109 * The OpenFlow header length is initially set to 'openflow_len'; if the
110 * message is later extended, the length should be updated with
111 * update_openflow_length() before sending.
113 * Returns the header. */
115 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
117 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
120 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
121 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
122 * the header, if any, are zeroed.
124 * The OpenFlow header length is initially set to 'openflow_len'; if the
125 * message is later extended, the length should be updated with
126 * update_openflow_length() before sending.
128 * Returns the header. */
130 put_openflow_xid(size_t openflow_len, uint8_t type, ovs_be32 xid,
131 struct ofpbuf *buffer)
133 struct ofp_header *oh;
135 assert(openflow_len >= sizeof *oh);
136 assert(openflow_len <= UINT16_MAX);
138 oh = ofpbuf_put_uninit(buffer, openflow_len);
139 oh->version = OFP_VERSION;
141 oh->length = htons(openflow_len);
143 memset(oh + 1, 0, openflow_len - sizeof *oh);
147 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
150 update_openflow_length(struct ofpbuf *buffer)
152 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
153 oh->length = htons(buffer->size);
157 make_flow_mod(uint16_t command, const struct flow *flow, size_t actions_len)
159 struct ofp_flow_mod *ofm;
160 size_t size = sizeof *ofm + actions_len;
161 struct ofpbuf *out = ofpbuf_new(size);
162 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
163 ofm->header.version = OFP_VERSION;
164 ofm->header.type = OFPT_FLOW_MOD;
165 ofm->header.length = htons(size);
167 ofm->match.wildcards = htonl(0);
168 ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
170 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
171 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
172 ofm->match.dl_vlan = flow->dl_vlan;
173 ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp;
174 ofm->match.dl_type = flow->dl_type;
175 ofm->match.nw_src = flow->nw_src;
176 ofm->match.nw_dst = flow->nw_dst;
177 ofm->match.nw_proto = flow->nw_proto;
178 ofm->match.nw_tos = flow->nw_tos;
179 ofm->match.tp_src = flow->tp_src;
180 ofm->match.tp_dst = flow->tp_dst;
181 ofm->command = htons(command);
186 make_add_flow(const struct flow *flow, uint32_t buffer_id,
187 uint16_t idle_timeout, size_t actions_len)
189 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
190 struct ofp_flow_mod *ofm = out->data;
191 ofm->idle_timeout = htons(idle_timeout);
192 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
193 ofm->buffer_id = htonl(buffer_id);
198 make_del_flow(const struct flow *flow)
200 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
201 struct ofp_flow_mod *ofm = out->data;
202 ofm->out_port = htons(OFPP_NONE);
207 make_add_simple_flow(const struct flow *flow,
208 uint32_t buffer_id, uint16_t out_port,
209 uint16_t idle_timeout)
211 if (out_port != OFPP_NONE) {
212 struct ofp_action_output *oao;
213 struct ofpbuf *buffer;
215 buffer = make_add_flow(flow, buffer_id, idle_timeout, sizeof *oao);
216 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
217 oao->type = htons(OFPAT_OUTPUT);
218 oao->len = htons(sizeof *oao);
219 oao->port = htons(out_port);
222 return make_add_flow(flow, buffer_id, idle_timeout, 0);
227 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
228 const struct ofpbuf *payload, int max_send_len)
230 struct ofp_packet_in *opi;
234 send_len = MIN(max_send_len, payload->size);
235 buf = ofpbuf_new(sizeof *opi + send_len);
236 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
237 OFPT_PACKET_IN, 0, buf);
238 opi->buffer_id = htonl(buffer_id);
239 opi->total_len = htons(payload->size);
240 opi->in_port = htons(in_port);
241 opi->reason = reason;
242 ofpbuf_put(buf, payload->data, send_len);
243 update_openflow_length(buf);
249 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
251 const struct ofp_action_header *actions, size_t n_actions)
253 size_t actions_len = n_actions * sizeof *actions;
254 struct ofp_packet_out *opo;
255 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
256 struct ofpbuf *out = ofpbuf_new(size);
258 opo = ofpbuf_put_uninit(out, sizeof *opo);
259 opo->header.version = OFP_VERSION;
260 opo->header.type = OFPT_PACKET_OUT;
261 opo->header.length = htons(size);
262 opo->header.xid = htonl(0);
263 opo->buffer_id = htonl(buffer_id);
264 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
265 opo->actions_len = htons(actions_len);
266 ofpbuf_put(out, actions, actions_len);
268 ofpbuf_put(out, packet->data, packet->size);
274 make_unbuffered_packet_out(const struct ofpbuf *packet,
275 uint16_t in_port, uint16_t out_port)
277 struct ofp_action_output action;
278 action.type = htons(OFPAT_OUTPUT);
279 action.len = htons(sizeof action);
280 action.port = htons(out_port);
281 return make_packet_out(packet, UINT32_MAX, in_port,
282 (struct ofp_action_header *) &action, 1);
286 make_buffered_packet_out(uint32_t buffer_id,
287 uint16_t in_port, uint16_t out_port)
289 if (out_port != OFPP_NONE) {
290 struct ofp_action_output action;
291 action.type = htons(OFPAT_OUTPUT);
292 action.len = htons(sizeof action);
293 action.port = htons(out_port);
294 return make_packet_out(NULL, buffer_id, in_port,
295 (struct ofp_action_header *) &action, 1);
297 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
301 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
303 make_echo_request(void)
305 struct ofp_header *rq;
306 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
307 rq = ofpbuf_put_uninit(out, sizeof *rq);
308 rq->version = OFP_VERSION;
309 rq->type = OFPT_ECHO_REQUEST;
310 rq->length = htons(sizeof *rq);
315 /* Creates and returns an OFPT_ECHO_REPLY message matching the
316 * OFPT_ECHO_REQUEST message in 'rq'. */
318 make_echo_reply(const struct ofp_header *rq)
320 size_t size = ntohs(rq->length);
321 struct ofpbuf *out = ofpbuf_new(size);
322 struct ofp_header *reply = ofpbuf_put(out, rq, size);
323 reply->type = OFPT_ECHO_REPLY;
328 check_message_type(uint8_t got_type, uint8_t want_type)
330 if (got_type != want_type) {
331 char *want_type_name = ofp_message_type_to_string(want_type);
332 char *got_type_name = ofp_message_type_to_string(got_type);
333 VLOG_WARN_RL(&bad_ofmsg_rl,
334 "received bad message type %s (expected %s)",
335 got_type_name, want_type_name);
336 free(want_type_name);
338 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
343 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
344 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
345 * with ofp_mkerr()). */
347 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
352 error = check_message_type(msg->type, type);
357 got_size = ntohs(msg->length);
358 if (got_size != size) {
359 char *type_name = ofp_message_type_to_string(type);
360 VLOG_WARN_RL(&bad_ofmsg_rl,
361 "received %s message of length %zu (expected %zu)",
362 type_name, got_size, size);
364 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
370 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
371 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
372 * the checks pass, otherwise an OpenFlow error code (produced with
375 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
376 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
379 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
380 size_t min_size, size_t array_elt_size,
381 size_t *n_array_elts)
386 assert(array_elt_size);
388 error = check_message_type(msg->type, type);
393 got_size = ntohs(msg->length);
394 if (got_size < min_size) {
395 char *type_name = ofp_message_type_to_string(type);
396 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
397 "(expected at least %zu)",
398 type_name, got_size, min_size);
400 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
402 if ((got_size - min_size) % array_elt_size) {
403 char *type_name = ofp_message_type_to_string(type);
404 VLOG_WARN_RL(&bad_ofmsg_rl,
405 "received %s message of bad length %zu: the "
406 "excess over %zu (%zu) is not evenly divisible by %zu "
407 "(remainder is %zu)",
408 type_name, got_size, min_size, got_size - min_size,
409 array_elt_size, (got_size - min_size) % array_elt_size);
411 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
414 *n_array_elts = (got_size - min_size) / array_elt_size;
419 const struct ofp_flow_stats *
420 flow_stats_first(struct flow_stats_iterator *iter,
421 const struct ofp_stats_reply *osr)
423 iter->pos = osr->body;
424 iter->end = osr->body + (ntohs(osr->header.length)
425 - offsetof(struct ofp_stats_reply, body));
426 return flow_stats_next(iter);
429 const struct ofp_flow_stats *
430 flow_stats_next(struct flow_stats_iterator *iter)
432 ptrdiff_t bytes_left = iter->end - iter->pos;
433 const struct ofp_flow_stats *fs;
436 if (bytes_left < sizeof *fs) {
437 if (bytes_left != 0) {
438 VLOG_WARN_RL(&bad_ofmsg_rl,
439 "%td leftover bytes in flow stats reply", bytes_left);
444 fs = (const void *) iter->pos;
445 length = ntohs(fs->length);
446 if (length < sizeof *fs) {
447 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
448 "min %zu", length, sizeof *fs);
450 } else if (length > bytes_left) {
451 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
452 "bytes left", length, bytes_left);
454 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
455 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
456 "left over in final action", length,
457 (length - sizeof *fs) % sizeof fs->actions[0]);
465 check_action_exact_len(const union ofp_action *a, unsigned int len,
466 unsigned int required_len)
468 if (len != required_len) {
469 VLOG_DBG_RL(&bad_ofmsg_rl,
470 "action %u has invalid length %"PRIu16" (must be %u)\n",
471 a->type, ntohs(a->header.len), required_len);
472 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
477 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
478 * that the switch will never have more than 'max_ports' ports. Returns 0 if
479 * 'port' is valid, otherwise an ofp_mkerr() return code. */
481 check_output_port(uint16_t port, int max_ports)
489 case OFPP_CONTROLLER:
494 if (port < max_ports) {
497 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
498 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
502 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
503 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
504 * otherwise an ofp_mkerr() return code. */
506 check_enqueue_action(const union ofp_action *a, unsigned int len,
509 const struct ofp_action_enqueue *oae;
513 error = check_action_exact_len(a, len, 16);
518 oae = (const struct ofp_action_enqueue *) a;
519 port = ntohs(oae->port);
520 if (port < max_ports || port == OFPP_IN_PORT) {
523 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
524 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
528 check_nicira_action(const union ofp_action *a, unsigned int len,
529 const struct flow *flow)
531 const struct nx_action_header *nah;
535 VLOG_DBG_RL(&bad_ofmsg_rl,
536 "Nicira vendor action only %u bytes", len);
537 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
539 nah = (const struct nx_action_header *) a;
541 switch (ntohs(nah->subtype)) {
543 case NXAST_SET_TUNNEL:
544 case NXAST_DROP_SPOOFED_ARP:
545 case NXAST_SET_QUEUE:
546 case NXAST_POP_QUEUE:
547 return check_action_exact_len(a, len, 16);
550 error = check_action_exact_len(a, len,
551 sizeof(struct nx_action_reg_move));
555 return nxm_check_reg_move((const struct nx_action_reg_move *) a, flow);
558 error = check_action_exact_len(a, len,
559 sizeof(struct nx_action_reg_load));
563 return nxm_check_reg_load((const struct nx_action_reg_load *) a, flow);
569 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
574 check_action(const union ofp_action *a, unsigned int len,
575 const struct flow *flow, int max_ports)
579 switch (ntohs(a->type)) {
581 error = check_action_exact_len(a, len, 8);
585 return check_output_port(ntohs(a->output.port), max_ports);
587 case OFPAT_SET_VLAN_VID:
588 error = check_action_exact_len(a, len, 8);
592 if (a->vlan_vid.vlan_vid & ~htons(0xfff)) {
593 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
597 case OFPAT_SET_VLAN_PCP:
598 error = check_action_exact_len(a, len, 8);
602 if (a->vlan_vid.vlan_vid & ~7) {
603 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT);
607 case OFPAT_STRIP_VLAN:
608 case OFPAT_SET_NW_SRC:
609 case OFPAT_SET_NW_DST:
610 case OFPAT_SET_NW_TOS:
611 case OFPAT_SET_TP_SRC:
612 case OFPAT_SET_TP_DST:
613 return check_action_exact_len(a, len, 8);
615 case OFPAT_SET_DL_SRC:
616 case OFPAT_SET_DL_DST:
617 return check_action_exact_len(a, len, 16);
620 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
621 ? check_nicira_action(a, len, flow)
622 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
625 return check_enqueue_action(a, len, max_ports);
628 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
630 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
635 validate_actions(const union ofp_action *actions, size_t n_actions,
636 const struct flow *flow, int max_ports)
640 for (i = 0; i < n_actions; ) {
641 const union ofp_action *a = &actions[i];
642 unsigned int len = ntohs(a->header.len);
643 unsigned int n_slots = len / OFP_ACTION_ALIGN;
644 unsigned int slots_left = &actions[n_actions] - a;
647 if (n_slots > slots_left) {
648 VLOG_DBG_RL(&bad_ofmsg_rl,
649 "action requires %u slots but only %u remain",
650 n_slots, slots_left);
651 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
653 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
654 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
655 } else if (len % OFP_ACTION_ALIGN) {
656 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
657 "of %d", len, OFP_ACTION_ALIGN);
658 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
661 error = check_action(a, len, flow, max_ports);
670 /* Returns true if 'action' outputs to 'port' (which must be in network byte
671 * order), false otherwise. */
673 action_outputs_to_port(const union ofp_action *action, uint16_t port)
675 switch (ntohs(action->type)) {
677 return action->output.port == port;
679 return ((const struct ofp_action_enqueue *) action)->port == port;
685 /* The set of actions must either come from a trusted source or have been
686 * previously validated with validate_actions(). */
687 const union ofp_action *
688 actions_first(struct actions_iterator *iter,
689 const union ofp_action *oa, size_t n_actions)
692 iter->end = oa + n_actions;
693 return actions_next(iter);
696 const union ofp_action *
697 actions_next(struct actions_iterator *iter)
699 if (iter->pos != iter->end) {
700 const union ofp_action *a = iter->pos;
701 unsigned int len = ntohs(a->header.len);
702 iter->pos += len / OFP_ACTION_ALIGN;
710 normalize_match(struct ofp_match *m)
712 enum { OFPFW_NW = (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO
714 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
717 wc = ntohl(m->wildcards) & OVSFW_ALL;
718 if (wc & OFPFW_DL_TYPE) {
721 /* Can't sensibly match on network or transport headers if the
722 * data link type is unknown. */
723 wc |= OFPFW_NW | OFPFW_TP;
724 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
725 m->tp_src = m->tp_dst = 0;
726 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
727 if (wc & OFPFW_NW_PROTO) {
730 /* Can't sensibly match on transport headers if the network
731 * protocol is unknown. */
733 m->tp_src = m->tp_dst = 0;
734 } else if (m->nw_proto == IPPROTO_TCP ||
735 m->nw_proto == IPPROTO_UDP ||
736 m->nw_proto == IPPROTO_ICMP) {
737 if (wc & OFPFW_TP_SRC) {
740 if (wc & OFPFW_TP_DST) {
744 /* Transport layer fields will always be extracted as zeros, so we
745 * can do an exact-match on those values. */
747 m->tp_src = m->tp_dst = 0;
749 if (wc & OFPFW_NW_SRC_MASK) {
750 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
752 if (wc & OFPFW_NW_DST_MASK) {
753 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
755 if (wc & OFPFW_NW_TOS) {
758 m->nw_tos &= IP_DSCP_MASK;
760 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
761 if (wc & OFPFW_NW_PROTO) {
764 if (wc & OFPFW_NW_SRC_MASK) {
765 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
767 if (wc & OFPFW_NW_DST_MASK) {
768 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
770 m->tp_src = m->tp_dst = m->nw_tos = 0;
772 /* Network and transport layer fields will always be extracted as
773 * zeros, so we can do an exact-match on those values. */
774 wc &= ~(OFPFW_NW | OFPFW_TP);
775 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
776 m->tp_src = m->tp_dst = 0;
778 if (wc & OFPFW_DL_SRC) {
779 memset(m->dl_src, 0, sizeof m->dl_src);
781 if (wc & OFPFW_DL_DST) {
782 memset(m->dl_dst, 0, sizeof m->dl_dst);
784 m->wildcards = htonl(wc);
787 /* Returns a string that describes 'match' in a very literal way, without
788 * interpreting its contents except in a very basic fashion. The returned
789 * string is intended to be fixed-length, so that it is easy to see differences
790 * between two such strings if one is put above another. This is useful for
791 * describing changes made by normalize_match().
793 * The caller must free the returned string (with free()). */
795 ofp_match_to_literal_string(const struct ofp_match *match)
797 return xasprintf("wildcards=%#10"PRIx32" "
798 " in_port=%5"PRId16" "
799 " dl_src="ETH_ADDR_FMT" "
800 " dl_dst="ETH_ADDR_FMT" "
801 " dl_vlan=%5"PRId16" "
802 " dl_vlan_pcp=%3"PRId8" "
803 " dl_type=%#6"PRIx16" "
804 " nw_tos=%#4"PRIx8" "
805 " nw_proto=%#4"PRIx16" "
806 " nw_src=%#10"PRIx32" "
807 " nw_dst=%#10"PRIx32" "
808 " tp_src=%5"PRId16" "
810 ntohl(match->wildcards),
811 ntohs(match->in_port),
812 ETH_ADDR_ARGS(match->dl_src),
813 ETH_ADDR_ARGS(match->dl_dst),
814 ntohs(match->dl_vlan),
816 ntohs(match->dl_type),
819 ntohl(match->nw_src),
820 ntohl(match->nw_dst),
821 ntohs(match->tp_src),
822 ntohs(match->tp_dst));
826 vendor_code_to_id(uint8_t code)
829 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
831 #undef OFPUTIL_VENDOR
837 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
838 * information taken from 'error', whose encoding must be as described in the
839 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
840 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
843 * Returns NULL if 'error' is not an OpenFlow error code. */
845 make_ofp_error_msg(int error, const struct ofp_header *oh)
847 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
857 if (!is_ofp_error(error)) {
858 /* We format 'error' with strerror() here since it seems likely to be
859 * a system errno value. */
860 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
861 error, strerror(error));
868 len = ntohs(oh->length);
878 vendor = get_ofp_err_vendor(error);
879 type = get_ofp_err_type(error);
880 code = get_ofp_err_code(error);
881 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
882 struct ofp_error_msg *oem;
884 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
885 oem->type = htons(type);
886 oem->code = htons(code);
888 struct ofp_error_msg *oem;
889 struct nx_vendor_error *nve;
892 vendor_id = vendor_code_to_id(vendor);
893 if (vendor_id == UINT32_MAX) {
894 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
899 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
900 OFPT_ERROR, xid, &buf);
901 oem->type = htons(NXET_VENDOR);
902 oem->code = htons(NXVC_VENDOR_ERROR);
904 nve = ofpbuf_put_uninit(buf, sizeof *nve);
905 nve->vendor = htonl(vendor_id);
906 nve->type = htons(type);
907 nve->code = htons(code);
911 ofpbuf_put(buf, data, len);
917 /* Attempts to pull 'actions_len' bytes from the front of 'b'. Returns 0 if
918 * successful, otherwise an OpenFlow error.
920 * If successful, the first action is stored in '*actionsp' and the number of
921 * "union ofp_action" size elements into '*n_actionsp'. Otherwise NULL and 0
922 * are stored, respectively.
924 * This function does not check that the actions are valid (the caller should
925 * do so, with validate_actions()). The caller is also responsible for making
926 * sure that 'b->data' is initially aligned appropriately for "union
929 ofputil_pull_actions(struct ofpbuf *b, unsigned int actions_len,
930 union ofp_action **actionsp, size_t *n_actionsp)
932 if (actions_len % OFP_ACTION_ALIGN != 0) {
933 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
934 "is not a multiple of %d", actions_len, OFP_ACTION_ALIGN);
938 *actionsp = ofpbuf_try_pull(b, actions_len);
939 if (*actionsp == NULL) {
940 VLOG_DBG_RL(&bad_ofmsg_rl, "OpenFlow message actions length %u "
941 "exceeds remaining message length (%zu)",
942 actions_len, b->size);
946 *n_actionsp = actions_len / OFP_ACTION_ALIGN;
952 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);