ofproto: Check overlap, emerg flow cache, and error code sync (OpenFlow 0.9)
[cascardo/ovs.git] / lib / vconn.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 "vconn-provider.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <poll.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "coverage.h"
27 #include "dynamic-string.h"
28 #include "flow.h"
29 #include "ofp-print.h"
30 #include "ofpbuf.h"
31 #include "openflow/nicira-ext.h"
32 #include "openflow/openflow.h"
33 #include "packets.h"
34 #include "poll-loop.h"
35 #include "random.h"
36 #include "util.h"
37
38 #define THIS_MODULE VLM_vconn
39 #include "vlog.h"
40
41 /* State of an active vconn.*/
42 enum vconn_state {
43     /* This is the ordinary progression of states. */
44     VCS_CONNECTING,             /* Underlying vconn is not connected. */
45     VCS_SEND_HELLO,             /* Waiting to send OFPT_HELLO message. */
46     VCS_RECV_HELLO,             /* Waiting to receive OFPT_HELLO message. */
47     VCS_CONNECTED,              /* Connection established. */
48
49     /* These states are entered only when something goes wrong. */
50     VCS_SEND_ERROR,             /* Sending OFPT_ERROR message. */
51     VCS_DISCONNECTED            /* Connection failed or connection closed. */
52 };
53
54 static struct vconn_class *vconn_classes[] = {
55     &tcp_vconn_class,
56     &unix_vconn_class,
57 #ifdef HAVE_OPENSSL
58     &ssl_vconn_class,
59 #endif
60 };
61
62 static struct pvconn_class *pvconn_classes[] = {
63     &ptcp_pvconn_class,
64     &punix_pvconn_class,
65 #ifdef HAVE_OPENSSL
66     &pssl_pvconn_class,
67 #endif
68 };
69
70 /* Rate limit for individual OpenFlow messages going over the vconn, output at
71  * DBG level.  This is very high because, if these are enabled, it is because
72  * we really need to see them. */
73 static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
74
75 /* Rate limit for OpenFlow message parse errors.  These always indicate a bug
76  * in the peer and so there's not much point in showing a lot of them. */
77 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
78
79 static int do_recv(struct vconn *, struct ofpbuf **);
80 static int do_send(struct vconn *, struct ofpbuf *);
81
82 /* Check the validity of the vconn class structures. */
83 static void
84 check_vconn_classes(void)
85 {
86 #ifndef NDEBUG
87     size_t i;
88
89     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
90         struct vconn_class *class = vconn_classes[i];
91         assert(class->name != NULL);
92         assert(class->open != NULL);
93         if (class->close || class->recv || class->send
94             || class->run || class->run_wait || class->wait) {
95             assert(class->close != NULL);
96             assert(class->recv != NULL);
97             assert(class->send != NULL);
98             assert(class->wait != NULL);
99         } else {
100             /* This class delegates to another one. */
101         }
102     }
103
104     for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
105         struct pvconn_class *class = pvconn_classes[i];
106         assert(class->name != NULL);
107         assert(class->listen != NULL);
108         if (class->close || class->accept || class->wait) {
109             assert(class->close != NULL);
110             assert(class->accept != NULL);
111             assert(class->wait != NULL);
112         } else {
113             /* This class delegates to another one. */
114         }
115     }
116 #endif
117 }
118
119 /* Prints information on active (if 'active') and passive (if 'passive')
120  * connection methods supported by the vconn.  If 'bootstrap' is true, also
121  * advertises options to bootstrap the CA certificate. */
122 void
123 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
124 {
125     /* Really this should be implemented via callbacks into the vconn
126      * providers, but that seems too heavy-weight to bother with at the
127      * moment. */
128     
129     printf("\n");
130     if (active) {
131         printf("Active OpenFlow connection methods:\n");
132         printf("  tcp:IP[:PORT]         "
133                "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
134 #ifdef HAVE_OPENSSL
135         printf("  ssl:IP[:PORT]         "
136                "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
137 #endif
138         printf("  unix:FILE               Unix domain socket named FILE\n");
139     }
140
141     if (passive) {
142         printf("Passive OpenFlow connection methods:\n");
143         printf("  ptcp:[PORT][:IP]        "
144                "listen to TCP PORT (default: %d) on IP\n",
145                OFP_TCP_PORT);
146 #ifdef HAVE_OPENSSL
147         printf("  pssl:[PORT][:IP]        "
148                "listen for SSL on PORT (default: %d) on IP\n",
149                OFP_SSL_PORT);
150 #endif
151         printf("  punix:FILE              "
152                "listen on Unix domain socket FILE\n");
153     }
154
155 #ifdef HAVE_OPENSSL
156     printf("PKI configuration (required to use SSL):\n"
157            "  -p, --private-key=FILE  file with private key\n"
158            "  -c, --certificate=FILE  file with certificate for private key\n"
159            "  -C, --ca-cert=FILE      file with peer CA certificate\n");
160     if (bootstrap) {
161         printf("  --bootstrap-ca-cert=FILE  file with peer CA certificate "
162                "to read or create\n");
163     }
164 #endif
165 }
166
167 /* Attempts to connect to an OpenFlow device.  'name' is a connection name in
168  * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
169  * are vconn class-specific.
170  *
171  * The vconn will automatically negotiate an OpenFlow protocol version
172  * acceptable to both peers on the connection.  The version negotiated will be
173  * no lower than 'min_version' and no higher than OFP_VERSION.
174  *
175  * Returns 0 if successful, otherwise a positive errno value.  If successful,
176  * stores a pointer to the new connection in '*vconnp', otherwise a null
177  * pointer.  */
178 int
179 vconn_open(const char *name, int min_version, struct vconn **vconnp)
180 {
181     size_t prefix_len;
182     size_t i;
183
184     COVERAGE_INC(vconn_open);
185     check_vconn_classes();
186
187     *vconnp = NULL;
188     prefix_len = strcspn(name, ":");
189     if (prefix_len == strlen(name)) {
190         return EAFNOSUPPORT;
191     }
192     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
193         struct vconn_class *class = vconn_classes[i];
194         if (strlen(class->name) == prefix_len
195             && !memcmp(class->name, name, prefix_len)) {
196             struct vconn *vconn;
197             char *suffix_copy = xstrdup(name + prefix_len + 1);
198             int retval = class->open(name, suffix_copy, &vconn);
199             free(suffix_copy);
200             if (!retval) {
201                 assert(vconn->state != VCS_CONNECTING
202                        || vconn->class->connect);
203                 vconn->min_version = min_version;
204                 *vconnp = vconn;
205             }
206             return retval;
207         }
208     }
209     return EAFNOSUPPORT;
210 }
211
212 /* Allows 'vconn' to perform maintenance activities, such as flushing output
213  * buffers. */
214 void
215 vconn_run(struct vconn *vconn)
216 {
217     if (vconn->class->run) {
218         (vconn->class->run)(vconn);
219     }
220 }
221
222 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
223  * maintenance activities. */
224 void
225 vconn_run_wait(struct vconn *vconn)
226 {
227     if (vconn->class->run_wait) {
228         (vconn->class->run_wait)(vconn);
229     }
230 }
231
232 int
233 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
234 {
235     struct vconn *vconn;
236     int error;
237
238     error = vconn_open(name, min_version, &vconn);
239     while (error == EAGAIN) {
240         vconn_run(vconn);
241         vconn_run_wait(vconn);
242         vconn_connect_wait(vconn);
243         poll_block();
244         error = vconn_connect(vconn);
245         assert(error != EINPROGRESS);
246     }
247     if (error) {
248         vconn_close(vconn);
249         *vconnp = NULL;
250     } else {
251         *vconnp = vconn;
252     }
253     return error;
254 }
255
256 /* Closes 'vconn'. */
257 void
258 vconn_close(struct vconn *vconn)
259 {
260     if (vconn != NULL) {
261         char *name = vconn->name;
262         (vconn->class->close)(vconn);
263         free(name);
264     }
265 }
266
267 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
268 const char *
269 vconn_get_name(const struct vconn *vconn)
270 {
271     return vconn->name;
272 }
273
274 /* Returns the IP address of the peer, or 0 if the peer is not connected over
275  * an IP-based protocol or if its IP address is not yet known. */
276 uint32_t
277 vconn_get_remote_ip(const struct vconn *vconn) 
278 {
279     return vconn->remote_ip;
280 }
281
282 /* Returns the transport port of the peer, or 0 if the connection does not 
283  * contain a port or if the port is not yet known. */
284 uint16_t
285 vconn_get_remote_port(const struct vconn *vconn) 
286 {
287     return vconn->remote_port;
288 }
289
290 /* Returns the IP address used to connect to the peer, or 0 if the 
291  * connection is not an IP-based protocol or if its IP address is not 
292  * yet known. */
293 uint32_t
294 vconn_get_local_ip(const struct vconn *vconn) 
295 {
296     return vconn->local_ip;
297 }
298
299 /* Returns the transport port used to connect to the peer, or 0 if the 
300  * connection does not contain a port or if the port is not yet known. */
301 uint16_t
302 vconn_get_local_port(const struct vconn *vconn) 
303 {
304     return vconn->local_port;
305 }
306
307 static void
308 vcs_connecting(struct vconn *vconn) 
309 {
310     int retval = (vconn->class->connect)(vconn);
311     assert(retval != EINPROGRESS);
312     if (!retval) {
313         vconn->state = VCS_SEND_HELLO;
314     } else if (retval != EAGAIN) {
315         vconn->state = VCS_DISCONNECTED;
316         vconn->error = retval;
317     }
318 }
319
320 static void
321 vcs_send_hello(struct vconn *vconn)
322 {
323     struct ofpbuf *b;
324     int retval;
325
326     make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
327     retval = do_send(vconn, b);
328     if (!retval) {
329         vconn->state = VCS_RECV_HELLO;
330     } else {
331         ofpbuf_delete(b);
332         if (retval != EAGAIN) {
333             vconn->state = VCS_DISCONNECTED;
334             vconn->error = retval;
335         }
336     }
337 }
338
339 static void
340 vcs_recv_hello(struct vconn *vconn)
341 {
342     struct ofpbuf *b;
343     int retval;
344
345     retval = do_recv(vconn, &b);
346     if (!retval) {
347         struct ofp_header *oh = b->data;
348
349         if (oh->type == OFPT_HELLO) {
350             if (b->size > sizeof *oh) {
351                 struct ds msg = DS_EMPTY_INITIALIZER;
352                 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
353                 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
354                 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
355                 ds_destroy(&msg);
356             }
357
358             vconn->version = MIN(OFP_VERSION, oh->version);
359             if (vconn->version < vconn->min_version) {
360                 VLOG_WARN_RL(&bad_ofmsg_rl,
361                              "%s: version negotiation failed: we support "
362                              "versions 0x%02x to 0x%02x inclusive but peer "
363                              "supports no later than version 0x%02"PRIx8,
364                              vconn->name, vconn->min_version, OFP_VERSION,
365                              oh->version);
366                 vconn->state = VCS_SEND_ERROR;
367             } else {
368                 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
369                          "(we support versions 0x%02x to 0x%02x inclusive, "
370                          "peer no later than version 0x%02"PRIx8")",
371                          vconn->name, vconn->version, vconn->min_version,
372                          OFP_VERSION, oh->version);
373                 vconn->state = VCS_CONNECTED;
374             }
375             ofpbuf_delete(b);
376             return;
377         } else {
378             char *s = ofp_to_string(b->data, b->size, 1);
379             VLOG_WARN_RL(&bad_ofmsg_rl,
380                          "%s: received message while expecting hello: %s",
381                          vconn->name, s);
382             free(s);
383             retval = EPROTO;
384             ofpbuf_delete(b);
385         }
386     }
387
388     if (retval != EAGAIN) {
389         vconn->state = VCS_DISCONNECTED;
390         vconn->error = retval == EOF ? ECONNRESET : retval;
391     }
392 }
393
394 static void
395 vcs_send_error(struct vconn *vconn)
396 {
397     struct ofp_error_msg *error;
398     struct ofpbuf *b;
399     char s[128];
400     int retval;
401
402     snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
403              "you support no later than version 0x%02"PRIx8".",
404              vconn->min_version, OFP_VERSION, vconn->version);
405     error = make_openflow(sizeof *error, OFPT_ERROR, &b);
406     error->type = htons(OFPET_HELLO_FAILED);
407     error->code = htons(OFPHFC_INCOMPATIBLE);
408     ofpbuf_put(b, s, strlen(s));
409     update_openflow_length(b);
410     retval = do_send(vconn, b);
411     if (retval) {
412         ofpbuf_delete(b);
413     }
414     if (retval != EAGAIN) {
415         vconn->state = VCS_DISCONNECTED;
416         vconn->error = retval ? retval : EPROTO;
417     }
418 }
419
420 /* Tries to complete the connection on 'vconn', which must be an active
421  * vconn.  If 'vconn''s connection is complete, returns 0 if the connection
422  * was successful or a positive errno value if it failed.  If the
423  * connection is still in progress, returns EAGAIN. */
424 int
425 vconn_connect(struct vconn *vconn)
426 {
427     enum vconn_state last_state;
428
429     assert(vconn->min_version >= 0);
430     do {
431         last_state = vconn->state;
432         switch (vconn->state) {
433         case VCS_CONNECTING:
434             vcs_connecting(vconn);
435             break;
436
437         case VCS_SEND_HELLO:
438             vcs_send_hello(vconn);
439             break;
440
441         case VCS_RECV_HELLO:
442             vcs_recv_hello(vconn);
443             break;
444
445         case VCS_CONNECTED:
446             return 0;
447
448         case VCS_SEND_ERROR:
449             vcs_send_error(vconn);
450             break;
451
452         case VCS_DISCONNECTED:
453             return vconn->error;
454
455         default:
456             NOT_REACHED();
457         }
458     } while (vconn->state != last_state);
459
460     return EAGAIN;
461 }
462
463 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
464  * vconn.  If successful, stores the received message into '*msgp' and returns
465  * 0.  The caller is responsible for destroying the message with
466  * ofpbuf_delete().  On failure, returns a positive errno value and stores a
467  * null pointer into '*msgp'.  On normal connection close, returns EOF.
468  *
469  * vconn_recv will not block waiting for a packet to arrive.  If no packets
470  * have been received, it returns EAGAIN immediately. */
471 int
472 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
473 {
474     int retval = vconn_connect(vconn);
475     if (!retval) {
476         retval = do_recv(vconn, msgp);
477     }
478     return retval;
479 }
480
481 static int
482 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
483 {
484     int retval = (vconn->class->recv)(vconn, msgp);
485     if (!retval) {
486         struct ofp_header *oh;
487
488         COVERAGE_INC(vconn_received);
489         if (VLOG_IS_DBG_ENABLED()) {
490             char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
491             VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
492             free(s);
493         }
494
495         oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
496         if (oh->version != vconn->version
497             && oh->type != OFPT_HELLO
498             && oh->type != OFPT_ERROR
499             && oh->type != OFPT_ECHO_REQUEST
500             && oh->type != OFPT_ECHO_REPLY
501             && oh->type != OFPT_VENDOR)
502         {
503             if (vconn->version < 0) {
504                 VLOG_ERR_RL(&bad_ofmsg_rl,
505                             "%s: received OpenFlow message type %"PRIu8" "
506                             "before version negotiation complete",
507                             vconn->name, oh->type);
508             } else {
509                 VLOG_ERR_RL(&bad_ofmsg_rl,
510                             "%s: received OpenFlow version 0x%02"PRIx8" "
511                             "!= expected %02x",
512                             vconn->name, oh->version, vconn->version);
513             }
514             ofpbuf_delete(*msgp);
515             retval = EPROTO;
516         }
517     }
518     if (retval) {
519         *msgp = NULL;
520     }
521     return retval;
522 }
523
524 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
525  * vconn.  If successful, returns 0, in which case ownership of 'msg' is
526  * transferred to the vconn.  Success does not guarantee that 'msg' has been or
527  * ever will be delivered to the peer, only that it has been queued for
528  * transmission.
529  *
530  * Returns a positive errno value on failure, in which case the caller
531  * retains ownership of 'msg'.
532  *
533  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
534  * transmission, it returns EAGAIN immediately. */
535 int
536 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
537 {
538     int retval = vconn_connect(vconn);
539     if (!retval) {
540         retval = do_send(vconn, msg);
541     }
542     return retval;
543 }
544
545 static int
546 do_send(struct vconn *vconn, struct ofpbuf *msg)
547 {
548     int retval;
549
550     assert(msg->size >= sizeof(struct ofp_header));
551     assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
552     if (!VLOG_IS_DBG_ENABLED()) {
553         COVERAGE_INC(vconn_sent);
554         retval = (vconn->class->send)(vconn, msg);
555     } else {
556         char *s = ofp_to_string(msg->data, msg->size, 1);
557         retval = (vconn->class->send)(vconn, msg);
558         if (retval != EAGAIN) {
559             VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
560                         vconn->name, strerror(retval), s);
561         }
562         free(s);
563     }
564     return retval;
565 }
566
567 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
568 int
569 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
570 {
571     int retval;
572     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
573         vconn_run(vconn);
574         vconn_run_wait(vconn);
575         vconn_send_wait(vconn);
576         poll_block();
577     }
578     return retval;
579 }
580
581 /* Same as vconn_recv, except that it waits until a message is received. */
582 int
583 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
584 {
585     int retval;
586     while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
587         vconn_run(vconn);
588         vconn_run_wait(vconn);
589         vconn_recv_wait(vconn);
590         poll_block();
591     }
592     return retval;
593 }
594
595 /* Waits until a message with a transaction ID matching 'xid' is recived on
596  * 'vconn'.  Returns 0 if successful, in which case the reply is stored in
597  * '*replyp' for the caller to examine and free.  Otherwise returns a positive
598  * errno value, or EOF, and sets '*replyp' to null.
599  *
600  * 'request' is always destroyed, regardless of the return value. */
601 int
602 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
603 {
604     for (;;) {
605         uint32_t recv_xid;
606         struct ofpbuf *reply;
607         int error;
608
609         error = vconn_recv_block(vconn, &reply);
610         if (error) {
611             *replyp = NULL;
612             return error;
613         }
614         recv_xid = ((struct ofp_header *) reply->data)->xid;
615         if (xid == recv_xid) {
616             *replyp = reply;
617             return 0;
618         }
619
620         VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
621                     " != expected %08"PRIx32, vconn->name, recv_xid, xid);
622         ofpbuf_delete(reply);
623     }
624 }
625
626 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
627  * matching transaction ID.  Returns 0 if successful, in which case the reply
628  * is stored in '*replyp' for the caller to examine and free.  Otherwise
629  * returns a positive errno value, or EOF, and sets '*replyp' to null.
630  *
631  * 'request' is always destroyed, regardless of the return value. */
632 int
633 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
634                struct ofpbuf **replyp)
635 {
636     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
637     int error;
638
639     *replyp = NULL;
640     error = vconn_send_block(vconn, request);
641     if (error) {
642         ofpbuf_delete(request);
643     }
644     return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
645 }
646
647 void
648 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
649 {
650     assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
651
652     switch (vconn->state) {
653     case VCS_CONNECTING:
654         wait = WAIT_CONNECT;
655         break;
656
657     case VCS_SEND_HELLO:
658     case VCS_SEND_ERROR:
659         wait = WAIT_SEND;
660         break;
661
662     case VCS_RECV_HELLO:
663         wait = WAIT_RECV;
664         break;
665
666     case VCS_CONNECTED:
667         break;
668
669     case VCS_DISCONNECTED:
670         poll_immediate_wake();
671         return;
672     }
673     (vconn->class->wait)(vconn, wait);
674 }
675
676 void
677 vconn_connect_wait(struct vconn *vconn)
678 {
679     vconn_wait(vconn, WAIT_CONNECT);
680 }
681
682 void
683 vconn_recv_wait(struct vconn *vconn)
684 {
685     vconn_wait(vconn, WAIT_RECV);
686 }
687
688 void
689 vconn_send_wait(struct vconn *vconn)
690 {
691     vconn_wait(vconn, WAIT_SEND);
692 }
693
694 /* Attempts to start listening for OpenFlow connections.  'name' is a
695  * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
696  * class's name and ARGS are vconn class-specific.
697  *
698  * Returns 0 if successful, otherwise a positive errno value.  If successful,
699  * stores a pointer to the new connection in '*pvconnp', otherwise a null
700  * pointer.  */
701 int
702 pvconn_open(const char *name, struct pvconn **pvconnp)
703 {
704     size_t prefix_len;
705     size_t i;
706
707     check_vconn_classes();
708
709     *pvconnp = NULL;
710     prefix_len = strcspn(name, ":");
711     if (prefix_len == strlen(name)) {
712         return EAFNOSUPPORT;
713     }
714     for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
715         struct pvconn_class *class = pvconn_classes[i];
716         if (strlen(class->name) == prefix_len
717             && !memcmp(class->name, name, prefix_len)) {
718             char *suffix_copy = xstrdup(name + prefix_len + 1);
719             int retval = class->listen(name, suffix_copy, pvconnp);
720             free(suffix_copy);
721             if (retval) {
722                 *pvconnp = NULL;
723             }
724             return retval;
725         }
726     }
727     return EAFNOSUPPORT;
728 }
729
730 /* Returns the name that was used to open 'pvconn'.  The caller must not
731  * modify or free the name. */
732 const char *
733 pvconn_get_name(const struct pvconn *pvconn)
734 {
735     return pvconn->name;
736 }
737
738 /* Closes 'pvconn'. */
739 void
740 pvconn_close(struct pvconn *pvconn)
741 {
742     if (pvconn != NULL) {
743         char *name = pvconn->name;
744         (pvconn->class->close)(pvconn);
745         free(name);
746     }
747 }
748
749 /* Tries to accept a new connection on 'pvconn'.  If successful, stores the new
750  * connection in '*new_vconn' and returns 0.  Otherwise, returns a positive
751  * errno value.
752  *
753  * The new vconn will automatically negotiate an OpenFlow protocol version
754  * acceptable to both peers on the connection.  The version negotiated will be
755  * no lower than 'min_version' and no higher than OFP_VERSION.
756  *
757  * pvconn_accept() will not block waiting for a connection.  If no connection
758  * is ready to be accepted, it returns EAGAIN immediately. */
759 int
760 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
761 {
762     int retval = (pvconn->class->accept)(pvconn, new_vconn);
763     if (retval) {
764         *new_vconn = NULL;
765     } else {
766         assert((*new_vconn)->state != VCS_CONNECTING
767                || (*new_vconn)->class->connect);
768         (*new_vconn)->min_version = min_version;
769     }
770     return retval;
771 }
772
773 void
774 pvconn_wait(struct pvconn *pvconn)
775 {
776     (pvconn->class->wait)(pvconn);
777 }
778
779 /* XXX we should really use consecutive xids to avoid probabilistic
780  * failures. */
781 static inline uint32_t
782 alloc_xid(void)
783 {
784     return random_uint32();
785 }
786
787 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
788  * 'openflow_len', starting with an OpenFlow header with the given 'type' and
789  * an arbitrary transaction id.  Allocated bytes beyond the header, if any, are
790  * zeroed.
791  *
792  * The caller is responsible for freeing '*bufferp' when it is no longer
793  * needed.
794  *
795  * The OpenFlow header length is initially set to 'openflow_len'; if the
796  * message is later extended, the length should be updated with
797  * update_openflow_length() before sending.
798  *
799  * Returns the header. */
800 void *
801 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
802 {
803     *bufferp = ofpbuf_new(openflow_len);
804     return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
805 }
806
807 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
808  * 'openflow_len', starting with an OpenFlow header with the given 'type' and
809  * transaction id 'xid'.  Allocated bytes beyond the header, if any, are
810  * zeroed.
811  *
812  * The caller is responsible for freeing '*bufferp' when it is no longer
813  * needed.
814  *
815  * The OpenFlow header length is initially set to 'openflow_len'; if the
816  * message is later extended, the length should be updated with
817  * update_openflow_length() before sending.
818  *
819  * Returns the header. */
820 void *
821 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
822                   struct ofpbuf **bufferp)
823 {
824     *bufferp = ofpbuf_new(openflow_len);
825     return put_openflow_xid(openflow_len, type, xid, *bufferp);
826 }
827
828 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
829  * with the given 'type' and an arbitrary transaction id.  Allocated bytes
830  * beyond the header, if any, are zeroed.
831  *
832  * The OpenFlow header length is initially set to 'openflow_len'; if the
833  * message is later extended, the length should be updated with
834  * update_openflow_length() before sending.
835  *
836  * Returns the header. */
837 void *
838 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
839 {
840     return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
841 }
842
843 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
844  * with the given 'type' and an transaction id 'xid'.  Allocated bytes beyond
845  * the header, if any, are zeroed.
846  *
847  * The OpenFlow header length is initially set to 'openflow_len'; if the
848  * message is later extended, the length should be updated with
849  * update_openflow_length() before sending.
850  *
851  * Returns the header. */
852 void *
853 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
854                  struct ofpbuf *buffer)
855 {
856     struct ofp_header *oh;
857
858     assert(openflow_len >= sizeof *oh);
859     assert(openflow_len <= UINT16_MAX);
860
861     oh = ofpbuf_put_uninit(buffer, openflow_len);
862     oh->version = OFP_VERSION;
863     oh->type = type;
864     oh->length = htons(openflow_len);
865     oh->xid = xid;
866     memset(oh + 1, 0, openflow_len - sizeof *oh);
867     return oh;
868 }
869
870 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
871  * 'buffer->size'. */
872 void
873 update_openflow_length(struct ofpbuf *buffer) 
874 {
875     struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
876     oh->length = htons(buffer->size); 
877 }
878
879 struct ofpbuf *
880 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
881 {
882     struct ofp_flow_mod *ofm;
883     size_t size = sizeof *ofm + actions_len;
884     struct ofpbuf *out = ofpbuf_new(size);
885     ofm = ofpbuf_put_zeros(out, sizeof *ofm);
886     ofm->header.version = OFP_VERSION;
887     ofm->header.type = OFPT_FLOW_MOD;
888     ofm->header.length = htons(size);
889     ofm->match.wildcards = htonl(0);
890     ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
891                                : flow->in_port);
892     memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
893     memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
894     ofm->match.dl_vlan = flow->dl_vlan;
895     ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp;
896     ofm->match.dl_type = flow->dl_type;
897     ofm->match.nw_src = flow->nw_src;
898     ofm->match.nw_dst = flow->nw_dst;
899     ofm->match.nw_proto = flow->nw_proto;
900     ofm->match.tp_src = flow->tp_src;
901     ofm->match.tp_dst = flow->tp_dst;
902     ofm->command = htons(command);
903     return out;
904 }
905
906 struct ofpbuf *
907 make_add_flow(const flow_t *flow, uint32_t buffer_id,
908               uint16_t idle_timeout, size_t actions_len)
909 {
910     struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
911     struct ofp_flow_mod *ofm = out->data;
912     ofm->idle_timeout = htons(idle_timeout);
913     ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
914     ofm->buffer_id = htonl(buffer_id);
915     return out;
916 }
917
918 struct ofpbuf *
919 make_del_flow(const flow_t *flow)
920 {
921     struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
922     struct ofp_flow_mod *ofm = out->data;
923     ofm->out_port = htons(OFPP_NONE);
924     return out;
925 }
926
927 struct ofpbuf *
928 make_add_simple_flow(const flow_t *flow,
929                      uint32_t buffer_id, uint16_t out_port,
930                      uint16_t idle_timeout)
931 {
932     struct ofp_action_output *oao;
933     struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
934                                           sizeof *oao);
935     oao = ofpbuf_put_zeros(buffer, sizeof *oao);
936     oao->type = htons(OFPAT_OUTPUT);
937     oao->len = htons(sizeof *oao);
938     oao->port = htons(out_port);
939     return buffer;
940 }
941
942 struct ofpbuf *
943 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
944                const struct ofpbuf *payload, int max_send_len)
945 {
946     struct ofp_packet_in *opi;
947     struct ofpbuf *buf;
948     int send_len;
949
950     send_len = MIN(max_send_len, payload->size);
951     buf = ofpbuf_new(sizeof *opi + send_len);
952     opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
953                            OFPT_PACKET_IN, 0, buf);
954     opi->buffer_id = htonl(buffer_id);
955     opi->total_len = htons(payload->size);
956     opi->in_port = htons(in_port);
957     opi->reason = reason;
958     ofpbuf_put(buf, payload->data, send_len);
959     update_openflow_length(buf);
960
961     return buf;
962 }
963
964 struct ofpbuf *
965 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
966                 uint16_t in_port,
967                 const struct ofp_action_header *actions, size_t n_actions)
968 {
969     size_t actions_len = n_actions * sizeof *actions;
970     struct ofp_packet_out *opo;
971     size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
972     struct ofpbuf *out = ofpbuf_new(size);
973
974     opo = ofpbuf_put_uninit(out, sizeof *opo);
975     opo->header.version = OFP_VERSION;
976     opo->header.type = OFPT_PACKET_OUT;
977     opo->header.length = htons(size);
978     opo->header.xid = htonl(0);
979     opo->buffer_id = htonl(buffer_id);
980     opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
981     opo->actions_len = htons(actions_len);
982     ofpbuf_put(out, actions, actions_len);
983     if (packet) {
984         ofpbuf_put(out, packet->data, packet->size);
985     }
986     return out;
987 }
988
989 struct ofpbuf *
990 make_unbuffered_packet_out(const struct ofpbuf *packet,
991                            uint16_t in_port, uint16_t out_port)
992 {
993     struct ofp_action_output action;
994     action.type = htons(OFPAT_OUTPUT);
995     action.len = htons(sizeof action);
996     action.port = htons(out_port);
997     return make_packet_out(packet, UINT32_MAX, in_port,
998                            (struct ofp_action_header *) &action, 1);
999 }
1000
1001 struct ofpbuf *
1002 make_buffered_packet_out(uint32_t buffer_id,
1003                          uint16_t in_port, uint16_t out_port)
1004 {
1005     struct ofp_action_output action;
1006     action.type = htons(OFPAT_OUTPUT);
1007     action.len = htons(sizeof action);
1008     action.port = htons(out_port);
1009     return make_packet_out(NULL, buffer_id, in_port,
1010                            (struct ofp_action_header *) &action, 1);
1011 }
1012
1013 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1014 struct ofpbuf *
1015 make_echo_request(void)
1016 {
1017     struct ofp_header *rq;
1018     struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1019     rq = ofpbuf_put_uninit(out, sizeof *rq);
1020     rq->version = OFP_VERSION;
1021     rq->type = OFPT_ECHO_REQUEST;
1022     rq->length = htons(sizeof *rq);
1023     rq->xid = 0;
1024     return out;
1025 }
1026
1027 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1028  * OFPT_ECHO_REQUEST message in 'rq'. */
1029 struct ofpbuf *
1030 make_echo_reply(const struct ofp_header *rq)
1031 {
1032     size_t size = ntohs(rq->length);
1033     struct ofpbuf *out = ofpbuf_new(size);
1034     struct ofp_header *reply = ofpbuf_put(out, rq, size);
1035     reply->type = OFPT_ECHO_REPLY;
1036     return out;
1037 }
1038
1039 static int
1040 check_message_type(uint8_t got_type, uint8_t want_type) 
1041 {
1042     if (got_type != want_type) {
1043         char *want_type_name = ofp_message_type_to_string(want_type);
1044         char *got_type_name = ofp_message_type_to_string(got_type);
1045         VLOG_WARN_RL(&bad_ofmsg_rl,
1046                      "received bad message type %s (expected %s)",
1047                      got_type_name, want_type_name);
1048         free(want_type_name);
1049         free(got_type_name);
1050         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1051     }
1052     return 0;
1053 }
1054
1055 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
1056  * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
1057  * with ofp_mkerr()). */
1058 int
1059 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
1060 {
1061     size_t got_size;
1062     int error;
1063
1064     error = check_message_type(msg->type, type);
1065     if (error) {
1066         return error;
1067     }
1068
1069     got_size = ntohs(msg->length);
1070     if (got_size != size) {
1071         char *type_name = ofp_message_type_to_string(type);
1072         VLOG_WARN_RL(&bad_ofmsg_rl,
1073                      "received %s message of length %zu (expected %zu)",
1074                      type_name, got_size, size);
1075         free(type_name);
1076         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1077     }
1078
1079     return 0;
1080 }
1081
1082 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
1083  * nonnegative integer multiple of 'array_elt_size' bytes long.  Returns 0 if
1084  * the checks pass, otherwise an OpenFlow error code (produced with
1085  * ofp_mkerr()).
1086  *
1087  * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
1088  * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
1089  * successful. */
1090 int
1091 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
1092                         size_t min_size, size_t array_elt_size,
1093                         size_t *n_array_elts)
1094 {
1095     size_t got_size;
1096     int error;
1097
1098     assert(array_elt_size);
1099
1100     error = check_message_type(msg->type, type);
1101     if (error) {
1102         return error;
1103     }
1104
1105     got_size = ntohs(msg->length);
1106     if (got_size < min_size) {
1107         char *type_name = ofp_message_type_to_string(type);
1108         VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
1109                      "(expected at least %zu)",
1110                      type_name, got_size, min_size);
1111         free(type_name);
1112         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1113     }
1114     if ((got_size - min_size) % array_elt_size) {
1115         char *type_name = ofp_message_type_to_string(type);
1116         VLOG_WARN_RL(&bad_ofmsg_rl,
1117                      "received %s message of bad length %zu: the "
1118                      "excess over %zu (%zu) is not evenly divisible by %zu "
1119                      "(remainder is %zu)",
1120                      type_name, got_size, min_size, got_size - min_size,
1121                      array_elt_size, (got_size - min_size) % array_elt_size);
1122         free(type_name);
1123         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1124     }
1125     if (n_array_elts) {
1126         *n_array_elts = (got_size - min_size) / array_elt_size;
1127     }
1128     return 0;
1129 }
1130
1131 int
1132 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
1133                      int *n_actionsp, int max_ports)
1134 {
1135     const struct ofp_packet_out *opo;
1136     unsigned int actions_len, n_actions;
1137     size_t extra;
1138     int error;
1139
1140     *n_actionsp = 0;
1141     error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
1142                                     sizeof *opo, 1, &extra);
1143     if (error) {
1144         return error;
1145     }
1146     opo = (const struct ofp_packet_out *) oh;
1147
1148     actions_len = ntohs(opo->actions_len);
1149     if (actions_len > extra) {
1150         VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
1151                      "but message has room for only %zu bytes",
1152                      actions_len, extra);
1153         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1154     }
1155     if (actions_len % sizeof(union ofp_action)) {
1156         VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
1157                      "which is not a multiple of %zu",
1158                      actions_len, sizeof(union ofp_action));
1159         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1160     }
1161
1162     n_actions = actions_len / sizeof(union ofp_action);
1163     error = validate_actions((const union ofp_action *) opo->actions,
1164                              n_actions, max_ports);
1165     if (error) {
1166         return error;
1167     }
1168
1169     data->data = (void *) &opo->actions[n_actions];
1170     data->size = extra - actions_len;
1171     *n_actionsp = n_actions;
1172     return 0;
1173 }
1174
1175 const struct ofp_flow_stats *
1176 flow_stats_first(struct flow_stats_iterator *iter,
1177                  const struct ofp_stats_reply *osr)
1178 {
1179     iter->pos = osr->body;
1180     iter->end = osr->body + (ntohs(osr->header.length)
1181                              - offsetof(struct ofp_stats_reply, body));
1182     return flow_stats_next(iter);
1183 }
1184
1185 const struct ofp_flow_stats *
1186 flow_stats_next(struct flow_stats_iterator *iter)
1187 {
1188     ptrdiff_t bytes_left = iter->end - iter->pos;
1189     const struct ofp_flow_stats *fs;
1190     size_t length;
1191
1192     if (bytes_left < sizeof *fs) {
1193         if (bytes_left != 0) {
1194             VLOG_WARN_RL(&bad_ofmsg_rl,
1195                          "%td leftover bytes in flow stats reply", bytes_left);
1196         }
1197         return NULL;
1198     }
1199
1200     fs = (const void *) iter->pos;
1201     length = ntohs(fs->length);
1202     if (length < sizeof *fs) {
1203         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1204                      "min %zu", length, sizeof *fs);
1205         return NULL;
1206     } else if (length > bytes_left) {
1207         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1208                      "bytes left", length, bytes_left);
1209         return NULL;
1210     } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1211         VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1212                      "left over in final action", length,
1213                      (length - sizeof *fs) % sizeof fs->actions[0]);
1214         return NULL;
1215     }
1216     iter->pos += length;
1217     return fs;
1218 }
1219
1220 /* Alignment of ofp_actions. */
1221 #define ACTION_ALIGNMENT 8
1222
1223 static int
1224 check_action_exact_len(const union ofp_action *a, unsigned int len,
1225                        unsigned int required_len)
1226 {
1227     if (len != required_len) {
1228         VLOG_DBG_RL(&bad_ofmsg_rl,
1229                     "action %u has invalid length %"PRIu16" (must be %u)\n",
1230                     a->type, ntohs(a->header.len), required_len);
1231         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1232     }
1233     return 0;
1234 }
1235
1236 static int
1237 check_action_port(int port, int max_ports)
1238 {
1239     switch (port) {
1240     case OFPP_IN_PORT:
1241     case OFPP_TABLE:
1242     case OFPP_NORMAL:
1243     case OFPP_FLOOD:
1244     case OFPP_ALL:
1245     case OFPP_CONTROLLER:
1246     case OFPP_LOCAL:
1247         return 0;
1248
1249     default:
1250         if (port >= 0 && port < max_ports) {
1251             return 0;
1252         }
1253         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1254         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1255     }
1256 }
1257
1258 static int
1259 check_nicira_action(const union ofp_action *a, unsigned int len)
1260 {
1261     const struct nx_action_header *nah;
1262
1263     if (len < 16) {
1264         VLOG_DBG_RL(&bad_ofmsg_rl,
1265                     "Nicira vendor action only %u bytes", len);
1266         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1267     }
1268     nah = (const struct nx_action_header *) a;
1269
1270     switch (ntohs(nah->subtype)) {
1271     case NXAST_RESUBMIT:
1272         return check_action_exact_len(a, len, 16);
1273     default:
1274         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1275     }
1276 }
1277
1278 static int
1279 check_action(const union ofp_action *a, unsigned int len, int max_ports)
1280 {
1281     int error;
1282
1283     switch (ntohs(a->type)) {
1284     case OFPAT_OUTPUT:
1285         error = check_action_port(ntohs(a->output.port), max_ports);
1286         if (error) {
1287             return error;
1288         }
1289         return check_action_exact_len(a, len, 8);
1290
1291     case OFPAT_SET_VLAN_VID:
1292     case OFPAT_SET_VLAN_PCP:
1293     case OFPAT_STRIP_VLAN:
1294     case OFPAT_SET_NW_SRC:
1295     case OFPAT_SET_NW_DST:
1296     case OFPAT_SET_NW_TOS:
1297     case OFPAT_SET_TP_SRC:
1298     case OFPAT_SET_TP_DST:
1299         return check_action_exact_len(a, len, 8);
1300
1301     case OFPAT_SET_DL_SRC:
1302     case OFPAT_SET_DL_DST:
1303         return check_action_exact_len(a, len, 16);
1304
1305     case OFPAT_VENDOR:
1306         if (a->vendor.vendor == htonl(NX_VENDOR_ID)) {
1307             return check_nicira_action(a, len);
1308         } else {
1309             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR);
1310         }
1311         break;
1312
1313     default:
1314         VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
1315                 ntohs(a->type));
1316         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1317     }
1318
1319     if (!len) {
1320         VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
1321         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1322     }
1323     if (len % ACTION_ALIGNMENT) {
1324         VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple of %d",
1325                     len, ACTION_ALIGNMENT);
1326         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1327     }
1328     return 0;
1329 }
1330
1331 int
1332 validate_actions(const union ofp_action *actions, size_t n_actions,
1333                  int max_ports)
1334 {
1335     const union ofp_action *a;
1336
1337     for (a = actions; a < &actions[n_actions]; ) {
1338         unsigned int len = ntohs(a->header.len);
1339         unsigned int n_slots = len / ACTION_ALIGNMENT;
1340         unsigned int slots_left = &actions[n_actions] - a;
1341         int error;
1342
1343         if (n_slots > slots_left) {
1344             VLOG_DBG_RL(&bad_ofmsg_rl,
1345                         "action requires %u slots but only %u remain",
1346                         n_slots, slots_left);
1347             return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1348         }
1349         error = check_action(a, len, max_ports);
1350         if (error) {
1351             return error;
1352         }
1353         a += n_slots;
1354     }
1355     return 0;
1356 }
1357
1358 /* The set of actions must either come from a trusted source or have been
1359  * previously validated with validate_actions(). */
1360 const union ofp_action *
1361 actions_first(struct actions_iterator *iter,
1362               const union ofp_action *oa, size_t n_actions)
1363 {
1364     iter->pos = oa;
1365     iter->end = oa + n_actions;
1366     return actions_next(iter);
1367 }
1368
1369 const union ofp_action *
1370 actions_next(struct actions_iterator *iter)
1371 {
1372     if (iter->pos < iter->end) {
1373         const union ofp_action *a = iter->pos;
1374         unsigned int len = ntohs(a->header.len);
1375         iter->pos += len / ACTION_ALIGNMENT;
1376         return a;
1377     } else {
1378         return NULL;
1379     }
1380 }
1381
1382 void
1383 normalize_match(struct ofp_match *m)
1384 {
1385     enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
1386     enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1387     uint32_t wc;
1388
1389     wc = ntohl(m->wildcards) & OFPFW_ALL;
1390     if (wc & OFPFW_DL_TYPE) {
1391         m->dl_type = 0;
1392
1393         /* Can't sensibly match on network or transport headers if the
1394          * data link type is unknown. */
1395         wc |= OFPFW_NW | OFPFW_TP;
1396         m->nw_src = m->nw_dst = m->nw_proto = 0;
1397         m->tp_src = m->tp_dst = 0;
1398     } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1399         if (wc & OFPFW_NW_PROTO) {
1400             m->nw_proto = 0;
1401
1402             /* Can't sensibly match on transport headers if the network
1403              * protocol is unknown. */
1404             wc |= OFPFW_TP;
1405             m->tp_src = m->tp_dst = 0;
1406         } else if (m->nw_proto == IPPROTO_TCP ||
1407                    m->nw_proto == IPPROTO_UDP ||
1408                    m->nw_proto == IPPROTO_ICMP) {
1409             if (wc & OFPFW_TP_SRC) {
1410                 m->tp_src = 0;
1411             }
1412             if (wc & OFPFW_TP_DST) {
1413                 m->tp_dst = 0;
1414             }
1415         } else {
1416             /* Transport layer fields will always be extracted as zeros, so we
1417              * can do an exact-match on those values.  */
1418             wc &= ~OFPFW_TP;
1419             m->tp_src = m->tp_dst = 0;
1420         }
1421         if (wc & OFPFW_NW_SRC_MASK) {
1422             m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
1423         }
1424         if (wc & OFPFW_NW_DST_MASK) {
1425             m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
1426         }
1427     } else {
1428         /* Network and transport layer fields will always be extracted as
1429          * zeros, so we can do an exact-match on those values. */
1430         wc &= ~(OFPFW_NW | OFPFW_TP);
1431         m->nw_proto = m->nw_src = m->nw_dst = 0;
1432         m->tp_src = m->tp_dst = 0;
1433     }
1434     if (wc & OFPFW_DL_SRC) {
1435         memset(m->dl_src, 0, sizeof m->dl_src);
1436     }
1437     if (wc & OFPFW_DL_DST) {
1438         memset(m->dl_dst, 0, sizeof m->dl_dst);
1439     }
1440     m->wildcards = htonl(wc);
1441 }
1442
1443 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1444  * The initial connection status, supplied as 'connect_status', is interpreted
1445  * as follows:
1446  *
1447  *      - 0: 'vconn' is connected.  Its 'send' and 'recv' functions may be
1448  *        called in the normal fashion.
1449  *
1450  *      - EAGAIN: 'vconn' is trying to complete a connection.  Its 'connect'
1451  *        function should be called to complete the connection.
1452  *
1453  *      - Other positive errno values indicate that the connection failed with
1454  *        the specified error.
1455  *
1456  * After calling this function, vconn_close() must be used to destroy 'vconn',
1457  * otherwise resources will be leaked.
1458  *
1459  * The caller retains ownership of 'name'. */
1460 void
1461 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
1462            const char *name)
1463 {
1464     vconn->class = class;
1465     vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1466                     : !connect_status ? VCS_SEND_HELLO
1467                     : VCS_DISCONNECTED);
1468     vconn->error = connect_status;
1469     vconn->version = -1;
1470     vconn->min_version = -1;
1471     vconn->remote_ip = 0;
1472     vconn->remote_port = 0;
1473     vconn->local_ip = 0;
1474     vconn->local_port = 0;
1475     vconn->name = xstrdup(name);
1476     assert(vconn->state != VCS_CONNECTING || class->connect);
1477 }
1478
1479 void
1480 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
1481 {
1482     vconn->remote_ip = ip;
1483 }
1484
1485 void
1486 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
1487 {
1488     vconn->remote_port = port;
1489 }
1490
1491 void 
1492 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
1493 {
1494     vconn->local_ip = ip;
1495 }
1496
1497 void 
1498 vconn_set_local_port(struct vconn *vconn, uint16_t port)
1499 {
1500     vconn->local_port = port;
1501 }
1502
1503 void
1504 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
1505             const char *name)
1506 {
1507     pvconn->class = class;
1508     pvconn->name = xstrdup(name);
1509 }