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