Implement QoS framework.
[sliver-openvswitch.git] / lib / ofp-util.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "ofp-print.h"
19 #include <inttypes.h>
20 #include <stdlib.h>
21 #include "ofp-util.h"
22 #include "ofpbuf.h"
23 #include "packets.h"
24 #include "random.h"
25 #include "xtoxll.h"
26
27 #define THIS_MODULE VLM_ofp_util
28 #include "vlog.h"
29
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);
33
34 /* XXX we should really use consecutive xids to avoid probabilistic
35  * failures. */
36 static inline uint32_t
37 alloc_xid(void)
38 {
39     return random_uint32();
40 }
41
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
45  * zeroed.
46  *
47  * The caller is responsible for freeing '*bufferp' when it is no longer
48  * needed.
49  *
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.
53  *
54  * Returns the header. */
55 void *
56 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
57 {
58     *bufferp = ofpbuf_new(openflow_len);
59     return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
60 }
61
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
65  * zeroed.
66  *
67  * The caller is responsible for freeing '*bufferp' when it is no longer
68  * needed.
69  *
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.
73  *
74  * Returns the header. */
75 void *
76 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
77                   struct ofpbuf **bufferp)
78 {
79     *bufferp = ofpbuf_new(openflow_len);
80     return put_openflow_xid(openflow_len, type, xid, *bufferp);
81 }
82
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.
86  *
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.
90  *
91  * Returns the header. */
92 void *
93 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
94 {
95     return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
96 }
97
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.
101  *
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.
105  *
106  * Returns the header. */
107 void *
108 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
109                  struct ofpbuf *buffer)
110 {
111     struct ofp_header *oh;
112
113     assert(openflow_len >= sizeof *oh);
114     assert(openflow_len <= UINT16_MAX);
115
116     oh = ofpbuf_put_uninit(buffer, openflow_len);
117     oh->version = OFP_VERSION;
118     oh->type = type;
119     oh->length = htons(openflow_len);
120     oh->xid = xid;
121     memset(oh + 1, 0, openflow_len - sizeof *oh);
122     return oh;
123 }
124
125 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
126  * 'buffer->size'. */
127 void
128 update_openflow_length(struct ofpbuf *buffer) 
129 {
130     struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
131     oh->length = htons(buffer->size); 
132 }
133
134 struct ofpbuf *
135 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
136 {
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);
144     ofm->cookie = 0;
145     ofm->match.wildcards = htonl(0);
146     ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
147                                : flow->in_port);
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);
160     return out;
161 }
162
163 struct ofpbuf *
164 make_add_flow(const flow_t *flow, uint32_t buffer_id,
165               uint16_t idle_timeout, size_t actions_len)
166 {
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);
172     return out;
173 }
174
175 struct ofpbuf *
176 make_del_flow(const flow_t *flow)
177 {
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);
181     return out;
182 }
183
184 struct ofpbuf *
185 make_add_simple_flow(const flow_t *flow,
186                      uint32_t buffer_id, uint16_t out_port,
187                      uint16_t idle_timeout)
188 {
189     struct ofp_action_output *oao;
190     struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
191                                           sizeof *oao);
192     oao = ofpbuf_put_zeros(buffer, sizeof *oao);
193     oao->type = htons(OFPAT_OUTPUT);
194     oao->len = htons(sizeof *oao);
195     oao->port = htons(out_port);
196     return buffer;
197 }
198
199 struct ofpbuf *
200 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
201                const struct ofpbuf *payload, int max_send_len)
202 {
203     struct ofp_packet_in *opi;
204     struct ofpbuf *buf;
205     int send_len;
206
207     send_len = MIN(max_send_len, payload->size);
208     buf = ofpbuf_new(sizeof *opi + send_len);
209     opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
210                            OFPT_PACKET_IN, 0, buf);
211     opi->buffer_id = htonl(buffer_id);
212     opi->total_len = htons(payload->size);
213     opi->in_port = htons(in_port);
214     opi->reason = reason;
215     ofpbuf_put(buf, payload->data, send_len);
216     update_openflow_length(buf);
217
218     return buf;
219 }
220
221 struct ofpbuf *
222 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
223                 uint16_t in_port,
224                 const struct ofp_action_header *actions, size_t n_actions)
225 {
226     size_t actions_len = n_actions * sizeof *actions;
227     struct ofp_packet_out *opo;
228     size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
229     struct ofpbuf *out = ofpbuf_new(size);
230
231     opo = ofpbuf_put_uninit(out, sizeof *opo);
232     opo->header.version = OFP_VERSION;
233     opo->header.type = OFPT_PACKET_OUT;
234     opo->header.length = htons(size);
235     opo->header.xid = htonl(0);
236     opo->buffer_id = htonl(buffer_id);
237     opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
238     opo->actions_len = htons(actions_len);
239     ofpbuf_put(out, actions, actions_len);
240     if (packet) {
241         ofpbuf_put(out, packet->data, packet->size);
242     }
243     return out;
244 }
245
246 struct ofpbuf *
247 make_unbuffered_packet_out(const struct ofpbuf *packet,
248                            uint16_t in_port, uint16_t out_port)
249 {
250     struct ofp_action_output action;
251     action.type = htons(OFPAT_OUTPUT);
252     action.len = htons(sizeof action);
253     action.port = htons(out_port);
254     return make_packet_out(packet, UINT32_MAX, in_port,
255                            (struct ofp_action_header *) &action, 1);
256 }
257
258 struct ofpbuf *
259 make_buffered_packet_out(uint32_t buffer_id,
260                          uint16_t in_port, uint16_t out_port)
261 {
262     struct ofp_action_output action;
263     action.type = htons(OFPAT_OUTPUT);
264     action.len = htons(sizeof action);
265     action.port = htons(out_port);
266     return make_packet_out(NULL, buffer_id, in_port,
267                            (struct ofp_action_header *) &action, 1);
268 }
269
270 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
271 struct ofpbuf *
272 make_echo_request(void)
273 {
274     struct ofp_header *rq;
275     struct ofpbuf *out = ofpbuf_new(sizeof *rq);
276     rq = ofpbuf_put_uninit(out, sizeof *rq);
277     rq->version = OFP_VERSION;
278     rq->type = OFPT_ECHO_REQUEST;
279     rq->length = htons(sizeof *rq);
280     rq->xid = 0;
281     return out;
282 }
283
284 /* Creates and returns an OFPT_ECHO_REPLY message matching the
285  * OFPT_ECHO_REQUEST message in 'rq'. */
286 struct ofpbuf *
287 make_echo_reply(const struct ofp_header *rq)
288 {
289     size_t size = ntohs(rq->length);
290     struct ofpbuf *out = ofpbuf_new(size);
291     struct ofp_header *reply = ofpbuf_put(out, rq, size);
292     reply->type = OFPT_ECHO_REPLY;
293     return out;
294 }
295
296 static int
297 check_message_type(uint8_t got_type, uint8_t want_type) 
298 {
299     if (got_type != want_type) {
300         char *want_type_name = ofp_message_type_to_string(want_type);
301         char *got_type_name = ofp_message_type_to_string(got_type);
302         VLOG_WARN_RL(&bad_ofmsg_rl,
303                      "received bad message type %s (expected %s)",
304                      got_type_name, want_type_name);
305         free(want_type_name);
306         free(got_type_name);
307         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
308     }
309     return 0;
310 }
311
312 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
313  * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
314  * with ofp_mkerr()). */
315 int
316 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
317 {
318     size_t got_size;
319     int error;
320
321     error = check_message_type(msg->type, type);
322     if (error) {
323         return error;
324     }
325
326     got_size = ntohs(msg->length);
327     if (got_size != size) {
328         char *type_name = ofp_message_type_to_string(type);
329         VLOG_WARN_RL(&bad_ofmsg_rl,
330                      "received %s message of length %zu (expected %zu)",
331                      type_name, got_size, size);
332         free(type_name);
333         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
334     }
335
336     return 0;
337 }
338
339 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
340  * nonnegative integer multiple of 'array_elt_size' bytes long.  Returns 0 if
341  * the checks pass, otherwise an OpenFlow error code (produced with
342  * ofp_mkerr()).
343  *
344  * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
345  * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
346  * successful. */
347 int
348 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
349                         size_t min_size, size_t array_elt_size,
350                         size_t *n_array_elts)
351 {
352     size_t got_size;
353     int error;
354
355     assert(array_elt_size);
356
357     error = check_message_type(msg->type, type);
358     if (error) {
359         return error;
360     }
361
362     got_size = ntohs(msg->length);
363     if (got_size < min_size) {
364         char *type_name = ofp_message_type_to_string(type);
365         VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
366                      "(expected at least %zu)",
367                      type_name, got_size, min_size);
368         free(type_name);
369         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
370     }
371     if ((got_size - min_size) % array_elt_size) {
372         char *type_name = ofp_message_type_to_string(type);
373         VLOG_WARN_RL(&bad_ofmsg_rl,
374                      "received %s message of bad length %zu: the "
375                      "excess over %zu (%zu) is not evenly divisible by %zu "
376                      "(remainder is %zu)",
377                      type_name, got_size, min_size, got_size - min_size,
378                      array_elt_size, (got_size - min_size) % array_elt_size);
379         free(type_name);
380         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
381     }
382     if (n_array_elts) {
383         *n_array_elts = (got_size - min_size) / array_elt_size;
384     }
385     return 0;
386 }
387
388 int
389 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
390                      int *n_actionsp, int max_ports)
391 {
392     const struct ofp_packet_out *opo;
393     unsigned int actions_len, n_actions;
394     size_t extra;
395     int error;
396
397     *n_actionsp = 0;
398     error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
399                                     sizeof *opo, 1, &extra);
400     if (error) {
401         return error;
402     }
403     opo = (const struct ofp_packet_out *) oh;
404
405     actions_len = ntohs(opo->actions_len);
406     if (actions_len > extra) {
407         VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
408                      "but message has room for only %zu bytes",
409                      actions_len, extra);
410         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
411     }
412     if (actions_len % sizeof(union ofp_action)) {
413         VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
414                      "which is not a multiple of %zu",
415                      actions_len, sizeof(union ofp_action));
416         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
417     }
418
419     n_actions = actions_len / sizeof(union ofp_action);
420     error = validate_actions((const union ofp_action *) opo->actions,
421                              n_actions, max_ports);
422     if (error) {
423         return error;
424     }
425
426     data->data = (void *) &opo->actions[n_actions];
427     data->size = extra - actions_len;
428     *n_actionsp = n_actions;
429     return 0;
430 }
431
432 const struct ofp_flow_stats *
433 flow_stats_first(struct flow_stats_iterator *iter,
434                  const struct ofp_stats_reply *osr)
435 {
436     iter->pos = osr->body;
437     iter->end = osr->body + (ntohs(osr->header.length)
438                              - offsetof(struct ofp_stats_reply, body));
439     return flow_stats_next(iter);
440 }
441
442 const struct ofp_flow_stats *
443 flow_stats_next(struct flow_stats_iterator *iter)
444 {
445     ptrdiff_t bytes_left = iter->end - iter->pos;
446     const struct ofp_flow_stats *fs;
447     size_t length;
448
449     if (bytes_left < sizeof *fs) {
450         if (bytes_left != 0) {
451             VLOG_WARN_RL(&bad_ofmsg_rl,
452                          "%td leftover bytes in flow stats reply", bytes_left);
453         }
454         return NULL;
455     }
456
457     fs = (const void *) iter->pos;
458     length = ntohs(fs->length);
459     if (length < sizeof *fs) {
460         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
461                      "min %zu", length, sizeof *fs);
462         return NULL;
463     } else if (length > bytes_left) {
464         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
465                      "bytes left", length, bytes_left);
466         return NULL;
467     } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
468         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
469                      "left over in final action", length,
470                      (length - sizeof *fs) % sizeof fs->actions[0]);
471         return NULL;
472     }
473     iter->pos += length;
474     return fs;
475 }
476
477 /* Alignment of ofp_actions. */
478 #define ACTION_ALIGNMENT 8
479
480 static int
481 check_action_exact_len(const union ofp_action *a, unsigned int len,
482                        unsigned int required_len)
483 {
484     if (len != required_len) {
485         VLOG_DBG_RL(&bad_ofmsg_rl,
486                     "action %u has invalid length %"PRIu16" (must be %u)\n",
487                     a->type, ntohs(a->header.len), required_len);
488         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
489     }
490     return 0;
491 }
492
493 /* Checks that 'port' is a valid output port for the OFPAT_OUTPUT action, given
494  * that the switch will never have more than 'max_ports' ports.  Returns 0 if
495  * 'port' is valid, otherwise an ofp_mkerr() return code. */
496 static int
497 check_output_port(uint16_t port, int max_ports)
498 {
499     switch (port) {
500     case OFPP_IN_PORT:
501     case OFPP_TABLE:
502     case OFPP_NORMAL:
503     case OFPP_FLOOD:
504     case OFPP_ALL:
505     case OFPP_CONTROLLER:
506     case OFPP_LOCAL:
507         return 0;
508
509     default:
510         if (port < max_ports) {
511             return 0;
512         }
513         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
514         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
515     }
516 }
517
518 /* Checks that 'action' is a valid OFPAT_ENQUEUE action, given that the switch
519  * will never have more than 'max_ports' ports.  Returns 0 if 'port' is valid,
520  * otherwise an ofp_mkerr() return code. */
521 static int
522 check_enqueue_action(const union ofp_action *a, unsigned int len,
523                      int max_ports)
524 {
525     const struct ofp_action_enqueue *oae;
526     uint16_t port;
527     int error;
528
529     error = check_action_exact_len(a, len, 16);
530     if (error) {
531         return error;
532     }
533
534     oae = (const struct ofp_action_enqueue *) a;
535     port = ntohs(oae->port);
536     if (port < max_ports || port == OFPP_IN_PORT) {
537         return 0;
538     }
539     VLOG_WARN_RL(&bad_ofmsg_rl, "unknown enqueue port %x", port);
540     return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
541 }
542
543 static int
544 check_nicira_action(const union ofp_action *a, unsigned int len)
545 {
546     const struct nx_action_header *nah;
547
548     if (len < 16) {
549         VLOG_DBG_RL(&bad_ofmsg_rl,
550                     "Nicira vendor action only %u bytes", len);
551         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
552     }
553     nah = (const struct nx_action_header *) a;
554
555     switch (ntohs(nah->subtype)) {
556     case NXAST_RESUBMIT:
557     case NXAST_SET_TUNNEL:
558         return check_action_exact_len(a, len, 16);
559     default:
560         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
561     }
562 }
563
564 static int
565 check_action(const union ofp_action *a, unsigned int len, int max_ports)
566 {
567     int error;
568
569     switch (ntohs(a->type)) {
570     case OFPAT_OUTPUT:
571         error = check_action_exact_len(a, len, 8);
572         if (error) {
573             return error;
574         }
575         return check_output_port(ntohs(a->output.port), max_ports);
576
577     case OFPAT_SET_VLAN_VID:
578     case OFPAT_SET_VLAN_PCP:
579     case OFPAT_STRIP_VLAN:
580     case OFPAT_SET_NW_SRC:
581     case OFPAT_SET_NW_DST:
582     case OFPAT_SET_NW_TOS:
583     case OFPAT_SET_TP_SRC:
584     case OFPAT_SET_TP_DST:
585         return check_action_exact_len(a, len, 8);
586
587     case OFPAT_SET_DL_SRC:
588     case OFPAT_SET_DL_DST:
589         return check_action_exact_len(a, len, 16);
590
591     case OFPAT_VENDOR:
592         return (a->vendor.vendor == htonl(NX_VENDOR_ID)
593                 ? check_nicira_action(a, len)
594                 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
595
596     case OFPAT_ENQUEUE:
597         return check_enqueue_action(a, len, max_ports);
598
599     default:
600         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
601                 ntohs(a->type));
602         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
603     }
604 }
605
606 int
607 validate_actions(const union ofp_action *actions, size_t n_actions,
608                  int max_ports)
609 {
610     const union ofp_action *a;
611
612     for (a = actions; a < &actions[n_actions]; ) {
613         unsigned int len = ntohs(a->header.len);
614         unsigned int n_slots = len / ACTION_ALIGNMENT;
615         unsigned int slots_left = &actions[n_actions] - a;
616         int error;
617
618         if (n_slots > slots_left) {
619             VLOG_DBG_RL(&bad_ofmsg_rl,
620                         "action requires %u slots but only %u remain",
621                         n_slots, slots_left);
622             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
623         } else if (!len) {
624             VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
625             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
626         } else if (len % ACTION_ALIGNMENT) {
627             VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
628                         "of %d", len, ACTION_ALIGNMENT);
629             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
630         }
631
632         error = check_action(a, len, max_ports);
633         if (error) {
634             return error;
635         }
636         a += n_slots;
637     }
638     return 0;
639 }
640
641 /* Returns true if 'action' outputs to 'port' (which must be in network byte
642  * order), false otherwise. */
643 bool
644 action_outputs_to_port(const union ofp_action *action, uint16_t port)
645 {
646     switch (ntohs(action->type)) {
647     case OFPAT_OUTPUT:
648         return action->output.port == port;
649     case OFPAT_ENQUEUE:
650         return ((const struct ofp_action_enqueue *) action)->port == port;
651     default:
652         return false;
653     }
654 }
655
656 /* The set of actions must either come from a trusted source or have been
657  * previously validated with validate_actions(). */
658 const union ofp_action *
659 actions_first(struct actions_iterator *iter,
660               const union ofp_action *oa, size_t n_actions)
661 {
662     iter->pos = oa;
663     iter->end = oa + n_actions;
664     return actions_next(iter);
665 }
666
667 const union ofp_action *
668 actions_next(struct actions_iterator *iter)
669 {
670     if (iter->pos < iter->end) {
671         const union ofp_action *a = iter->pos;
672         unsigned int len = ntohs(a->header.len);
673         iter->pos += len / ACTION_ALIGNMENT;
674         return a;
675     } else {
676         return NULL;
677     }
678 }
679
680 void
681 normalize_match(struct ofp_match *m)
682 {
683     enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
684     enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
685     uint32_t wc;
686
687     wc = ntohl(m->wildcards) & OVSFW_ALL;
688     if (wc & OFPFW_DL_TYPE) {
689         m->dl_type = 0;
690
691         /* Can't sensibly match on network or transport headers if the
692          * data link type is unknown. */
693         wc |= OFPFW_NW | OFPFW_TP;
694         m->nw_src = m->nw_dst = m->nw_proto = 0;
695         m->tp_src = m->tp_dst = 0;
696     } else if (m->dl_type == htons(ETH_TYPE_IP)) {
697         if (wc & OFPFW_NW_PROTO) {
698             m->nw_proto = 0;
699
700             /* Can't sensibly match on transport headers if the network
701              * protocol is unknown. */
702             wc |= OFPFW_TP;
703             m->tp_src = m->tp_dst = 0;
704         } else if (m->nw_proto == IPPROTO_TCP ||
705                    m->nw_proto == IPPROTO_UDP ||
706                    m->nw_proto == IPPROTO_ICMP) {
707             if (wc & OFPFW_TP_SRC) {
708                 m->tp_src = 0;
709             }
710             if (wc & OFPFW_TP_DST) {
711                 m->tp_dst = 0;
712             }
713         } else {
714             /* Transport layer fields will always be extracted as zeros, so we
715              * can do an exact-match on those values.  */
716             wc &= ~OFPFW_TP;
717             m->tp_src = m->tp_dst = 0;
718         }
719         if (wc & OFPFW_NW_SRC_MASK) {
720             m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
721         }
722         if (wc & OFPFW_NW_DST_MASK) {
723             m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
724         }
725     } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
726         if (wc & OFPFW_NW_PROTO) {
727             m->nw_proto = 0;
728         }
729         if (wc & OFPFW_NW_SRC_MASK) {
730             m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
731         }
732         if (wc & OFPFW_NW_DST_MASK) {
733             m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
734         }
735         m->tp_src = m->tp_dst = 0;
736     } else {
737         /* Network and transport layer fields will always be extracted as
738          * zeros, so we can do an exact-match on those values. */
739         wc &= ~(OFPFW_NW | OFPFW_TP);
740         m->nw_proto = m->nw_src = m->nw_dst = 0;
741         m->tp_src = m->tp_dst = 0;
742     }
743     if (wc & OFPFW_DL_SRC) {
744         memset(m->dl_src, 0, sizeof m->dl_src);
745     }
746     if (wc & OFPFW_DL_DST) {
747         memset(m->dl_dst, 0, sizeof m->dl_dst);
748     }
749     m->wildcards = htonl(wc);
750 }
751