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"
28 VLOG_DEFINE_THIS_MODULE(ofp_util)
30 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
31 * in the peer and so there's not much point in showing a lot of them. */
32 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
34 /* XXX we should really use consecutive xids to avoid probabilistic
36 static inline uint32_t
39 return random_uint32();
42 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
43 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
44 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
47 * The caller is responsible for freeing '*bufferp' when it is no longer
50 * The OpenFlow header length is initially set to 'openflow_len'; if the
51 * message is later extended, the length should be updated with
52 * update_openflow_length() before sending.
54 * Returns the header. */
56 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
58 *bufferp = ofpbuf_new(openflow_len);
59 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
62 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
63 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
64 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
67 * The caller is responsible for freeing '*bufferp' when it is no longer
70 * The OpenFlow header length is initially set to 'openflow_len'; if the
71 * message is later extended, the length should be updated with
72 * update_openflow_length() before sending.
74 * Returns the header. */
76 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
77 struct ofpbuf **bufferp)
79 *bufferp = ofpbuf_new(openflow_len);
80 return put_openflow_xid(openflow_len, type, xid, *bufferp);
83 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
84 * with the given 'type' and an arbitrary transaction id. Allocated bytes
85 * beyond the header, if any, are zeroed.
87 * The OpenFlow header length is initially set to 'openflow_len'; if the
88 * message is later extended, the length should be updated with
89 * update_openflow_length() before sending.
91 * Returns the header. */
93 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
95 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
98 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
99 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
100 * the header, if any, are zeroed.
102 * The OpenFlow header length is initially set to 'openflow_len'; if the
103 * message is later extended, the length should be updated with
104 * update_openflow_length() before sending.
106 * Returns the header. */
108 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
109 struct ofpbuf *buffer)
111 struct ofp_header *oh;
113 assert(openflow_len >= sizeof *oh);
114 assert(openflow_len <= UINT16_MAX);
116 oh = ofpbuf_put_uninit(buffer, openflow_len);
117 oh->version = OFP_VERSION;
119 oh->length = htons(openflow_len);
121 memset(oh + 1, 0, openflow_len - sizeof *oh);
125 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
128 update_openflow_length(struct ofpbuf *buffer)
130 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
131 oh->length = htons(buffer->size);
135 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
137 struct ofp_flow_mod *ofm;
138 size_t size = sizeof *ofm + actions_len;
139 struct ofpbuf *out = ofpbuf_new(size);
140 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
141 ofm->header.version = OFP_VERSION;
142 ofm->header.type = OFPT_FLOW_MOD;
143 ofm->header.length = htons(size);
145 ofm->match.wildcards = htonl(0);
146 ofm->match.in_port = htons(flow->in_port == XFLOWP_LOCAL ? OFPP_LOCAL
148 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
149 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
150 ofm->match.dl_vlan = flow->dl_vlan;
151 ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp;
152 ofm->match.dl_type = flow->dl_type;
153 ofm->match.nw_src = flow->nw_src;
154 ofm->match.nw_dst = flow->nw_dst;
155 ofm->match.nw_proto = flow->nw_proto;
156 ofm->match.nw_tos = flow->nw_tos;
157 ofm->match.tp_src = flow->tp_src;
158 ofm->match.tp_dst = flow->tp_dst;
159 ofm->command = htons(command);
164 make_add_flow(const flow_t *flow, uint32_t buffer_id,
165 uint16_t idle_timeout, size_t actions_len)
167 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
168 struct ofp_flow_mod *ofm = out->data;
169 ofm->idle_timeout = htons(idle_timeout);
170 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
171 ofm->buffer_id = htonl(buffer_id);
176 make_del_flow(const flow_t *flow)
178 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
179 struct ofp_flow_mod *ofm = out->data;
180 ofm->out_port = htons(OFPP_NONE);
185 make_add_simple_flow(const flow_t *flow,
186 uint32_t buffer_id, uint16_t out_port,
187 uint16_t idle_timeout)
189 if (out_port != OFPP_NONE) {
190 struct ofp_action_output *oao;
191 struct ofpbuf *buffer;
193 buffer = make_add_flow(flow, buffer_id, idle_timeout, sizeof *oao);
194 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
195 oao->type = htons(OFPAT_OUTPUT);
196 oao->len = htons(sizeof *oao);
197 oao->port = htons(out_port);
200 return make_add_flow(flow, buffer_id, idle_timeout, 0);
205 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
206 const struct ofpbuf *payload, int max_send_len)
208 struct ofp_packet_in *opi;
212 send_len = MIN(max_send_len, payload->size);
213 buf = ofpbuf_new(sizeof *opi + send_len);
214 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
215 OFPT_PACKET_IN, 0, buf);
216 opi->buffer_id = htonl(buffer_id);
217 opi->total_len = htons(payload->size);
218 opi->in_port = htons(in_port);
219 opi->reason = reason;
220 ofpbuf_put(buf, payload->data, send_len);
221 update_openflow_length(buf);
227 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
229 const struct ofp_action_header *actions, size_t n_actions)
231 size_t actions_len = n_actions * sizeof *actions;
232 struct ofp_packet_out *opo;
233 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
234 struct ofpbuf *out = ofpbuf_new(size);
236 opo = ofpbuf_put_uninit(out, sizeof *opo);
237 opo->header.version = OFP_VERSION;
238 opo->header.type = OFPT_PACKET_OUT;
239 opo->header.length = htons(size);
240 opo->header.xid = htonl(0);
241 opo->buffer_id = htonl(buffer_id);
242 opo->in_port = htons(in_port == XFLOWP_LOCAL ? XFLOWP_LOCAL : in_port);
243 opo->actions_len = htons(actions_len);
244 ofpbuf_put(out, actions, actions_len);
246 ofpbuf_put(out, packet->data, packet->size);
252 make_unbuffered_packet_out(const struct ofpbuf *packet,
253 uint16_t in_port, uint16_t out_port)
255 struct ofp_action_output action;
256 action.type = htons(OFPAT_OUTPUT);
257 action.len = htons(sizeof action);
258 action.port = htons(out_port);
259 return make_packet_out(packet, UINT32_MAX, in_port,
260 (struct ofp_action_header *) &action, 1);
264 make_buffered_packet_out(uint32_t buffer_id,
265 uint16_t in_port, uint16_t out_port)
267 if (out_port != OFPP_NONE) {
268 struct ofp_action_output action;
269 action.type = htons(OFPAT_OUTPUT);
270 action.len = htons(sizeof action);
271 action.port = htons(out_port);
272 return make_packet_out(NULL, buffer_id, in_port,
273 (struct ofp_action_header *) &action, 1);
275 return make_packet_out(NULL, buffer_id, in_port, NULL, 0);
279 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
281 make_echo_request(void)
283 struct ofp_header *rq;
284 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
285 rq = ofpbuf_put_uninit(out, sizeof *rq);
286 rq->version = OFP_VERSION;
287 rq->type = OFPT_ECHO_REQUEST;
288 rq->length = htons(sizeof *rq);
293 /* Creates and returns an OFPT_ECHO_REPLY message matching the
294 * OFPT_ECHO_REQUEST message in 'rq'. */
296 make_echo_reply(const struct ofp_header *rq)
298 size_t size = ntohs(rq->length);
299 struct ofpbuf *out = ofpbuf_new(size);
300 struct ofp_header *reply = ofpbuf_put(out, rq, size);
301 reply->type = OFPT_ECHO_REPLY;
306 check_message_type(uint8_t got_type, uint8_t want_type)
308 if (got_type != want_type) {
309 char *want_type_name = ofp_message_type_to_string(want_type);
310 char *got_type_name = ofp_message_type_to_string(got_type);
311 VLOG_WARN_RL(&bad_ofmsg_rl,
312 "received bad message type %s (expected %s)",
313 got_type_name, want_type_name);
314 free(want_type_name);
316 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
321 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
322 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
323 * with ofp_mkerr()). */
325 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
330 error = check_message_type(msg->type, type);
335 got_size = ntohs(msg->length);
336 if (got_size != size) {
337 char *type_name = ofp_message_type_to_string(type);
338 VLOG_WARN_RL(&bad_ofmsg_rl,
339 "received %s message of length %zu (expected %zu)",
340 type_name, got_size, size);
342 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
348 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
349 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
350 * the checks pass, otherwise an OpenFlow error code (produced with
353 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
354 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
357 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
358 size_t min_size, size_t array_elt_size,
359 size_t *n_array_elts)
364 assert(array_elt_size);
366 error = check_message_type(msg->type, type);
371 got_size = ntohs(msg->length);
372 if (got_size < min_size) {
373 char *type_name = ofp_message_type_to_string(type);
374 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
375 "(expected at least %zu)",
376 type_name, got_size, min_size);
378 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
380 if ((got_size - min_size) % array_elt_size) {
381 char *type_name = ofp_message_type_to_string(type);
382 VLOG_WARN_RL(&bad_ofmsg_rl,
383 "received %s message of bad length %zu: the "
384 "excess over %zu (%zu) is not evenly divisible by %zu "
385 "(remainder is %zu)",
386 type_name, got_size, min_size, got_size - min_size,
387 array_elt_size, (got_size - min_size) % array_elt_size);
389 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
392 *n_array_elts = (got_size - min_size) / array_elt_size;
398 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
399 int *n_actionsp, int max_ports)
401 const struct ofp_packet_out *opo;
402 unsigned int actions_len, n_actions;
407 error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
408 sizeof *opo, 1, &extra);
412 opo = (const struct ofp_packet_out *) oh;
414 actions_len = ntohs(opo->actions_len);
415 if (actions_len > extra) {
416 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
417 "but message has room for only %zu bytes",
419 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
421 if (actions_len % sizeof(union ofp_action)) {
422 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
423 "which is not a multiple of %zu",
424 actions_len, sizeof(union ofp_action));
425 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
428 n_actions = actions_len / sizeof(union ofp_action);
429 error = validate_actions((const union ofp_action *) opo->actions,
430 n_actions, max_ports);
435 data->data = (void *) &opo->actions[n_actions];
436 data->size = extra - actions_len;
437 *n_actionsp = n_actions;
442 make_nxt_flow_mod_table_id(bool enable)
444 struct nxt_flow_mod_table_id *flow_mod_table_id;
445 struct ofpbuf *buffer;
447 flow_mod_table_id = make_openflow(sizeof *flow_mod_table_id, OFPT_VENDOR,
450 flow_mod_table_id->vendor = htonl(NX_VENDOR_ID);
451 flow_mod_table_id->subtype = htonl(NXT_FLOW_MOD_TABLE_ID);
452 flow_mod_table_id->set = enable;
456 const struct ofp_flow_stats *
457 flow_stats_first(struct flow_stats_iterator *iter,
458 const struct ofp_stats_reply *osr)
460 iter->pos = osr->body;
461 iter->end = osr->body + (ntohs(osr->header.length)
462 - offsetof(struct ofp_stats_reply, body));
463 return flow_stats_next(iter);
466 const struct ofp_flow_stats *
467 flow_stats_next(struct flow_stats_iterator *iter)
469 ptrdiff_t bytes_left = iter->end - iter->pos;
470 const struct ofp_flow_stats *fs;
473 if (bytes_left < sizeof *fs) {
474 if (bytes_left != 0) {
475 VLOG_WARN_RL(&bad_ofmsg_rl,
476 "%td leftover bytes in flow stats reply", bytes_left);
481 fs = (const void *) iter->pos;
482 length = ntohs(fs->length);
483 if (length < sizeof *fs) {
484 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
485 "min %zu", length, sizeof *fs);
487 } else if (length > bytes_left) {
488 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
489 "bytes left", length, bytes_left);
491 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
492 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
493 "left over in final action", length,
494 (length - sizeof *fs) % sizeof fs->actions[0]);
501 /* Alignment of ofp_actions. */
502 #define ACTION_ALIGNMENT 8
505 check_action_exact_len(const union ofp_action *a, unsigned int len,
506 unsigned int required_len)
508 if (len != required_len) {
509 VLOG_DBG_RL(&bad_ofmsg_rl,
510 "action %u has invalid length %"PRIu16" (must be %u)\n",
511 a->type, ntohs(a->header.len), required_len);
512 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
517 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
518 * that the switch will never have more than 'max_ports' ports. Returns 0 if
519 * 'port' is valid, otherwise an ofp_mkerr() return code. */
521 check_output_port(uint16_t port, int max_ports)
529 case OFPP_CONTROLLER:
534 if (port < max_ports) {
537 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
538 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
542 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
543 * will never have more than 'max_ports' ports. Returns 0 if 'port' is valid,
544 * otherwise an ofp_mkerr() return code. */
546 check_enqueue_action(const union ofp_action *a, unsigned int len,
549 const struct ofp_action_enqueue *oae;
553 error = check_action_exact_len(a, len, 16);
558 oae = (const struct ofp_action_enqueue *) a;
559 port = ntohs(oae->port);
560 if (port < max_ports || port == OFPP_IN_PORT) {
563 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
564 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
568 check_nicira_action(const union ofp_action *a, unsigned int len)
570 const struct nx_action_header *nah;
573 VLOG_DBG_RL(&bad_ofmsg_rl,
574 "Nicira vendor action only %u bytes", len);
575 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
577 nah = (const struct nx_action_header *) a;
579 switch (ntohs(nah->subtype)) {
581 case NXAST_SET_TUNNEL:
582 case NXAST_DROP_SPOOFED_ARP:
583 case NXAST_SET_QUEUE:
584 case NXAST_POP_QUEUE:
585 return check_action_exact_len(a, len, 16);
587 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
592 check_action(const union ofp_action *a, unsigned int len, int max_ports)
596 switch (ntohs(a->type)) {
598 error = check_action_exact_len(a, len, 8);
602 return check_output_port(ntohs(a->output.port), max_ports);
604 case OFPAT_SET_VLAN_VID:
605 case OFPAT_SET_VLAN_PCP:
606 case OFPAT_STRIP_VLAN:
607 case OFPAT_SET_NW_SRC:
608 case OFPAT_SET_NW_DST:
609 case OFPAT_SET_NW_TOS:
610 case OFPAT_SET_TP_SRC:
611 case OFPAT_SET_TP_DST:
612 return check_action_exact_len(a, len, 8);
614 case OFPAT_SET_DL_SRC:
615 case OFPAT_SET_DL_DST:
616 return check_action_exact_len(a, len, 16);
619 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
620 ? check_nicira_action(a, len)
621 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
624 return check_enqueue_action(a, len, max_ports);
627 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
629 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
634 validate_actions(const union ofp_action *actions, size_t n_actions,
637 const union ofp_action *a;
639 for (a = actions; a < &actions[n_actions]; ) {
640 unsigned int len = ntohs(a->header.len);
641 unsigned int n_slots = len / ACTION_ALIGNMENT;
642 unsigned int slots_left = &actions[n_actions] - a;
645 if (n_slots > slots_left) {
646 VLOG_DBG_RL(&bad_ofmsg_rl,
647 "action requires %u slots but only %u remain",
648 n_slots, slots_left);
649 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
651 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
652 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
653 } else if (len % ACTION_ALIGNMENT) {
654 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
655 "of %d", len, ACTION_ALIGNMENT);
656 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
659 error = check_action(a, len, max_ports);
668 /* Returns true if 'action' outputs to 'port' (which must be in network byte
669 * order), false otherwise. */
671 action_outputs_to_port(const union ofp_action *action, uint16_t port)
673 switch (ntohs(action->type)) {
675 return action->output.port == port;
677 return ((const struct ofp_action_enqueue *) action)->port == port;
683 /* The set of actions must either come from a trusted source or have been
684 * previously validated with validate_actions(). */
685 const union ofp_action *
686 actions_first(struct actions_iterator *iter,
687 const union ofp_action *oa, size_t n_actions)
690 iter->end = oa + n_actions;
691 return actions_next(iter);
694 const union ofp_action *
695 actions_next(struct actions_iterator *iter)
697 if (iter->pos < iter->end) {
698 const union ofp_action *a = iter->pos;
699 unsigned int len = ntohs(a->header.len);
700 iter->pos += len / ACTION_ALIGNMENT;
708 normalize_match(struct ofp_match *m)
710 enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
711 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
714 wc = ntohl(m->wildcards) & OVSFW_ALL;
715 if (wc & OFPFW_DL_TYPE) {
718 /* Can't sensibly match on network or transport headers if the
719 * data link type is unknown. */
720 wc |= OFPFW_NW | OFPFW_TP;
721 m->nw_src = m->nw_dst = m->nw_proto = m->nw_tos = 0;
722 m->tp_src = m->tp_dst = 0;
723 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
724 if (wc & OFPFW_NW_PROTO) {
727 /* Can't sensibly match on transport headers if the network
728 * protocol is unknown. */
730 m->tp_src = m->tp_dst = 0;
731 } else if (m->nw_proto == IPPROTO_TCP ||
732 m->nw_proto == IPPROTO_UDP ||
733 m->nw_proto == IPPROTO_ICMP) {
734 if (wc & OFPFW_TP_SRC) {
737 if (wc & OFPFW_TP_DST) {
741 /* Transport layer fields will always be extracted as zeros, so we
742 * can do an exact-match on those values. */
744 m->tp_src = m->tp_dst = 0;
746 if (wc & OFPFW_NW_SRC_MASK) {
747 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
749 if (wc & OFPFW_NW_DST_MASK) {
750 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
752 if (wc & OFPFW_NW_TOS) {
755 m->nw_tos &= IP_DSCP_MASK;
757 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
758 if (wc & OFPFW_NW_PROTO) {
761 if (wc & OFPFW_NW_SRC_MASK) {
762 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
764 if (wc & OFPFW_NW_DST_MASK) {
765 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
767 m->tp_src = m->tp_dst = m->nw_tos = 0;
769 /* Network and transport layer fields will always be extracted as
770 * zeros, so we can do an exact-match on those values. */
771 wc &= ~(OFPFW_NW | OFPFW_TP);
772 m->nw_proto = m->nw_src = m->nw_dst = m->nw_tos = 0;
773 m->tp_src = m->tp_dst = 0;
775 if (wc & OFPFW_DL_SRC) {
776 memset(m->dl_src, 0, sizeof m->dl_src);
778 if (wc & OFPFW_DL_DST) {
779 memset(m->dl_dst, 0, sizeof m->dl_dst);
781 m->wildcards = htonl(wc);
784 /* Converts all of the fields in 'opp' from host to native byte-order. */
786 hton_ofp_phy_port(struct ofp_phy_port *opp)
788 opp->port_no = htons(opp->port_no);
789 opp->config = htonl(opp->config);
790 opp->state = htonl(opp->state);
791 opp->curr = htonl(opp->curr);
792 opp->advertised = htonl(opp->advertised);
793 opp->supported = htonl(opp->supported);
794 opp->peer = htonl(opp->peer);
797 /* Converts all of the fields in 'opp' from native to host byte-order. */
799 ntoh_ofp_phy_port(struct ofp_phy_port *opp)
801 /* ntohX and htonX are really the same functions. */
802 hton_ofp_phy_port(opp);
805 /* Returns a string that describes 'match' in a very literal way, without
806 * interpreting its contents except in a very basic fashion. The returned
807 * string is intended to be fixed-length, so that it is easy to see differences
808 * between two such strings if one is put above another. This is useful for
809 * describing changes made by normalize_match().
811 * The caller must free the returned string (with free()). */
813 ofp_match_to_literal_string(const struct ofp_match *match)
815 return xasprintf("wildcards=%#10"PRIx32" "
816 " in_port=%5"PRId16" "
817 " dl_src="ETH_ADDR_FMT" "
818 " dl_dst="ETH_ADDR_FMT" "
819 " dl_vlan=%5"PRId16" "
820 " dl_vlan_pcp=%3"PRId8" "
821 " dl_type=%#6"PRIx16" "
822 " nw_tos=%#4"PRIx8" "
823 " nw_proto=%#4"PRIx16" "
824 " nw_src=%#10"PRIx32" "
825 " nw_dst=%#10"PRIx32" "
826 " tp_src=%5"PRId16" "
828 ntohl(match->wildcards),
829 ntohs(match->in_port),
830 ETH_ADDR_ARGS(match->dl_src),
831 ETH_ADDR_ARGS(match->dl_dst),
832 ntohs(match->dl_vlan),
834 ntohs(match->dl_type),
837 ntohl(match->nw_src),
838 ntohl(match->nw_dst),
839 ntohs(match->tp_src),
840 ntohs(match->tp_dst));
844 vendor_code_to_id(uint8_t code)
847 #define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
849 #undef OFPUTIL_VENDOR
855 /* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
856 * information taken from 'error', whose encoding must be as described in the
857 * large comment in ofp-util.h. If 'oh' is nonnull, then the error will use
858 * oh->xid as its transaction ID, and it will include up to the first 64 bytes
861 * Returns NULL if 'error' is not an OpenFlow error code. */
863 make_ofp_error_msg(int error, const struct ofp_header *oh)
865 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
875 if (!is_ofp_error(error)) {
876 /* We format 'error' with strerror() here since it seems likely to be
877 * a system errno value. */
878 VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
879 error, strerror(error));
886 len = ntohs(oh->length);
896 vendor = get_ofp_err_vendor(error);
897 type = get_ofp_err_type(error);
898 code = get_ofp_err_code(error);
899 if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
900 struct ofp_error_msg *oem;
902 oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
903 oem->type = htons(type);
904 oem->code = htons(code);
906 struct ofp_error_msg *oem;
907 struct nx_vendor_error *nve;
910 vendor_id = vendor_code_to_id(vendor);
911 if (vendor_id == UINT32_MAX) {
912 VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
917 oem = make_openflow_xid(len + sizeof *oem + sizeof *nve,
918 OFPT_ERROR, xid, &buf);
919 oem->type = htons(NXET_VENDOR);
920 oem->code = htons(NXVC_VENDOR_ERROR);
922 nve = ofpbuf_put_uninit(buf, sizeof *nve);
923 nve->vendor = htonl(vendor_id);
924 nve->type = htons(type);
925 nve->code = htons(code);
929 ofpbuf_put(buf, data, len);