2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "vconn-provider.h"
22 #include <netinet/in.h>
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
30 #include "ofp-print.h"
32 #include "openflow/nicira-ext.h"
33 #include "openflow/openflow.h"
35 #include "poll-loop.h"
39 #define THIS_MODULE VLM_vconn
42 /* State of an active vconn.*/
44 /* This is the ordinary progression of states. */
45 VCS_CONNECTING, /* Underlying vconn is not connected. */
46 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
47 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
48 VCS_CONNECTED, /* Connection established. */
50 /* These states are entered only when something goes wrong. */
51 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
52 VCS_DISCONNECTED /* Connection failed or connection closed. */
55 static struct vconn_class *vconn_classes[] = {
63 static struct pvconn_class *pvconn_classes[] = {
71 /* Rate limit for individual OpenFlow messages going over the vconn, output at
72 * DBG level. This is very high because, if these are enabled, it is because
73 * we really need to see them. */
74 static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
76 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
77 * in the peer and so there's not much point in showing a lot of them. */
78 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
80 static int do_recv(struct vconn *, struct ofpbuf **);
81 static int do_send(struct vconn *, struct ofpbuf *);
83 /* Check the validity of the vconn class structures. */
85 check_vconn_classes(void)
90 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
91 struct vconn_class *class = vconn_classes[i];
92 assert(class->name != NULL);
93 assert(class->open != NULL);
94 if (class->close || class->recv || class->send
95 || class->run || class->run_wait || class->wait) {
96 assert(class->close != NULL);
97 assert(class->recv != NULL);
98 assert(class->send != NULL);
99 assert(class->wait != NULL);
101 /* This class delegates to another one. */
105 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
106 struct pvconn_class *class = pvconn_classes[i];
107 assert(class->name != NULL);
108 assert(class->listen != NULL);
109 if (class->close || class->accept || class->wait) {
110 assert(class->close != NULL);
111 assert(class->accept != NULL);
112 assert(class->wait != NULL);
114 /* This class delegates to another one. */
120 /* Prints information on active (if 'active') and passive (if 'passive')
121 * connection methods supported by the vconn. If 'bootstrap' is true, also
122 * advertises options to bootstrap the CA certificate. */
124 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
126 /* Really this should be implemented via callbacks into the vconn
127 * providers, but that seems too heavy-weight to bother with at the
132 printf("Active OpenFlow connection methods:\n");
133 printf(" tcp:IP[:PORT] "
134 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
136 printf(" ssl:IP[:PORT] "
137 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
139 printf(" unix:FILE Unix domain socket named FILE\n");
143 printf("Passive OpenFlow connection methods:\n");
144 printf(" ptcp:[PORT][:IP] "
145 "listen to TCP PORT (default: %d) on IP\n",
148 printf(" pssl:[PORT][:IP] "
149 "listen for SSL on PORT (default: %d) on IP\n",
152 printf(" punix:FILE "
153 "listen on Unix domain socket FILE\n");
157 printf("PKI configuration (required to use SSL):\n"
158 " -p, --private-key=FILE file with private key\n"
159 " -c, --certificate=FILE file with certificate for private key\n"
160 " -C, --ca-cert=FILE file with peer CA certificate\n");
162 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
163 "to read or create\n");
168 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
169 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
170 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
173 vconn_lookup_class(const char *name, struct vconn_class **classp)
177 prefix_len = strcspn(name, ":");
178 if (name[prefix_len] != '\0') {
181 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
182 struct vconn_class *class = vconn_classes[i];
183 if (strlen(class->name) == prefix_len
184 && !memcmp(class->name, name, prefix_len)) {
195 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
196 * a supported connection type, otherwise EAFNOSUPPORT. */
198 vconn_verify_name(const char *name)
200 struct vconn_class *class;
201 return vconn_lookup_class(name, &class);
204 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
205 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
206 * are vconn class-specific.
208 * The vconn will automatically negotiate an OpenFlow protocol version
209 * acceptable to both peers on the connection. The version negotiated will be
210 * no lower than 'min_version' and no higher than OFP_VERSION.
212 * Returns 0 if successful, otherwise a positive errno value. If successful,
213 * stores a pointer to the new connection in '*vconnp', otherwise a null
216 vconn_open(const char *name, int min_version, struct vconn **vconnp)
218 struct vconn_class *class;
223 COVERAGE_INC(vconn_open);
224 check_vconn_classes();
226 /* Look up the class. */
227 error = vconn_lookup_class(name, &class);
232 /* Call class's "open" function. */
233 suffix_copy = xstrdup(strchr(name, ':') + 1);
234 error = class->open(name, suffix_copy, &vconn);
241 assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
242 vconn->min_version = min_version;
251 /* Allows 'vconn' to perform maintenance activities, such as flushing output
254 vconn_run(struct vconn *vconn)
256 if (vconn->class->run) {
257 (vconn->class->run)(vconn);
261 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
262 * maintenance activities. */
264 vconn_run_wait(struct vconn *vconn)
266 if (vconn->class->run_wait) {
267 (vconn->class->run_wait)(vconn);
272 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
279 error = vconn_open(name, min_version, &vconn);
280 while (error == EAGAIN) {
282 vconn_run_wait(vconn);
283 vconn_connect_wait(vconn);
285 error = vconn_connect(vconn);
286 assert(error != EINPROGRESS);
297 /* Closes 'vconn'. */
299 vconn_close(struct vconn *vconn)
302 char *name = vconn->name;
303 (vconn->class->close)(vconn);
308 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
310 vconn_get_name(const struct vconn *vconn)
315 /* Returns the IP address of the peer, or 0 if the peer is not connected over
316 * an IP-based protocol or if its IP address is not yet known. */
318 vconn_get_remote_ip(const struct vconn *vconn)
320 return vconn->remote_ip;
323 /* Returns the transport port of the peer, or 0 if the connection does not
324 * contain a port or if the port is not yet known. */
326 vconn_get_remote_port(const struct vconn *vconn)
328 return vconn->remote_port;
331 /* Returns the IP address used to connect to the peer, or 0 if the
332 * connection is not an IP-based protocol or if its IP address is not
335 vconn_get_local_ip(const struct vconn *vconn)
337 return vconn->local_ip;
340 /* Returns the transport port used to connect to the peer, or 0 if the
341 * connection does not contain a port or if the port is not yet known. */
343 vconn_get_local_port(const struct vconn *vconn)
345 return vconn->local_port;
349 vcs_connecting(struct vconn *vconn)
351 int retval = (vconn->class->connect)(vconn);
352 assert(retval != EINPROGRESS);
354 vconn->state = VCS_SEND_HELLO;
355 } else if (retval != EAGAIN) {
356 vconn->state = VCS_DISCONNECTED;
357 vconn->error = retval;
362 vcs_send_hello(struct vconn *vconn)
367 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
368 retval = do_send(vconn, b);
370 vconn->state = VCS_RECV_HELLO;
373 if (retval != EAGAIN) {
374 vconn->state = VCS_DISCONNECTED;
375 vconn->error = retval;
381 vcs_recv_hello(struct vconn *vconn)
386 retval = do_recv(vconn, &b);
388 struct ofp_header *oh = b->data;
390 if (oh->type == OFPT_HELLO) {
391 if (b->size > sizeof *oh) {
392 struct ds msg = DS_EMPTY_INITIALIZER;
393 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
394 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
395 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
399 vconn->version = MIN(OFP_VERSION, oh->version);
400 if (vconn->version < vconn->min_version) {
401 VLOG_WARN_RL(&bad_ofmsg_rl,
402 "%s: version negotiation failed: we support "
403 "versions 0x%02x to 0x%02x inclusive but peer "
404 "supports no later than version 0x%02"PRIx8,
405 vconn->name, vconn->min_version, OFP_VERSION,
407 vconn->state = VCS_SEND_ERROR;
409 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
410 "(we support versions 0x%02x to 0x%02x inclusive, "
411 "peer no later than version 0x%02"PRIx8")",
412 vconn->name, vconn->version, vconn->min_version,
413 OFP_VERSION, oh->version);
414 vconn->state = VCS_CONNECTED;
419 char *s = ofp_to_string(b->data, b->size, 1);
420 VLOG_WARN_RL(&bad_ofmsg_rl,
421 "%s: received message while expecting hello: %s",
429 if (retval != EAGAIN) {
430 vconn->state = VCS_DISCONNECTED;
431 vconn->error = retval == EOF ? ECONNRESET : retval;
436 vcs_send_error(struct vconn *vconn)
438 struct ofp_error_msg *error;
443 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
444 "you support no later than version 0x%02"PRIx8".",
445 vconn->min_version, OFP_VERSION, vconn->version);
446 error = make_openflow(sizeof *error, OFPT_ERROR, &b);
447 error->type = htons(OFPET_HELLO_FAILED);
448 error->code = htons(OFPHFC_INCOMPATIBLE);
449 ofpbuf_put(b, s, strlen(s));
450 update_openflow_length(b);
451 retval = do_send(vconn, b);
455 if (retval != EAGAIN) {
456 vconn->state = VCS_DISCONNECTED;
457 vconn->error = retval ? retval : EPROTO;
461 /* Tries to complete the connection on 'vconn', which must be an active
462 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
463 * was successful or a positive errno value if it failed. If the
464 * connection is still in progress, returns EAGAIN. */
466 vconn_connect(struct vconn *vconn)
468 enum vconn_state last_state;
470 assert(vconn->min_version >= 0);
472 last_state = vconn->state;
473 switch (vconn->state) {
475 vcs_connecting(vconn);
479 vcs_send_hello(vconn);
483 vcs_recv_hello(vconn);
490 vcs_send_error(vconn);
493 case VCS_DISCONNECTED:
499 } while (vconn->state != last_state);
504 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
505 * vconn. If successful, stores the received message into '*msgp' and returns
506 * 0. The caller is responsible for destroying the message with
507 * ofpbuf_delete(). On failure, returns a positive errno value and stores a
508 * null pointer into '*msgp'. On normal connection close, returns EOF.
510 * vconn_recv will not block waiting for a packet to arrive. If no packets
511 * have been received, it returns EAGAIN immediately. */
513 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
515 int retval = vconn_connect(vconn);
517 retval = do_recv(vconn, msgp);
523 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
525 int retval = (vconn->class->recv)(vconn, msgp);
527 struct ofp_header *oh;
529 COVERAGE_INC(vconn_received);
530 if (VLOG_IS_DBG_ENABLED()) {
531 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
532 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
536 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
537 if (oh->version != vconn->version
538 && oh->type != OFPT_HELLO
539 && oh->type != OFPT_ERROR
540 && oh->type != OFPT_ECHO_REQUEST
541 && oh->type != OFPT_ECHO_REPLY
542 && oh->type != OFPT_VENDOR)
544 if (vconn->version < 0) {
545 VLOG_ERR_RL(&bad_ofmsg_rl,
546 "%s: received OpenFlow message type %"PRIu8" "
547 "before version negotiation complete",
548 vconn->name, oh->type);
550 VLOG_ERR_RL(&bad_ofmsg_rl,
551 "%s: received OpenFlow version 0x%02"PRIx8" "
553 vconn->name, oh->version, vconn->version);
555 ofpbuf_delete(*msgp);
565 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
566 * vconn. If successful, returns 0, in which case ownership of 'msg' is
567 * transferred to the vconn. Success does not guarantee that 'msg' has been or
568 * ever will be delivered to the peer, only that it has been queued for
571 * Returns a positive errno value on failure, in which case the caller
572 * retains ownership of 'msg'.
574 * vconn_send will not block. If 'msg' cannot be immediately accepted for
575 * transmission, it returns EAGAIN immediately. */
577 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
579 int retval = vconn_connect(vconn);
581 retval = do_send(vconn, msg);
587 do_send(struct vconn *vconn, struct ofpbuf *msg)
591 assert(msg->size >= sizeof(struct ofp_header));
592 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
593 if (!VLOG_IS_DBG_ENABLED()) {
594 COVERAGE_INC(vconn_sent);
595 retval = (vconn->class->send)(vconn, msg);
597 char *s = ofp_to_string(msg->data, msg->size, 1);
598 retval = (vconn->class->send)(vconn, msg);
599 if (retval != EAGAIN) {
600 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
601 vconn->name, strerror(retval), s);
608 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
610 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
616 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
618 vconn_run_wait(vconn);
619 vconn_send_wait(vconn);
625 /* Same as vconn_recv, except that it waits until a message is received. */
627 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
633 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
635 vconn_run_wait(vconn);
636 vconn_recv_wait(vconn);
642 /* Waits until a message with a transaction ID matching 'xid' is recived on
643 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
644 * '*replyp' for the caller to examine and free. Otherwise returns a positive
645 * errno value, or EOF, and sets '*replyp' to null.
647 * 'request' is always destroyed, regardless of the return value. */
649 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
653 struct ofpbuf *reply;
656 error = vconn_recv_block(vconn, &reply);
661 recv_xid = ((struct ofp_header *) reply->data)->xid;
662 if (xid == recv_xid) {
667 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
668 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
669 ofpbuf_delete(reply);
673 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
674 * matching transaction ID. Returns 0 if successful, in which case the reply
675 * is stored in '*replyp' for the caller to examine and free. Otherwise
676 * returns a positive errno value, or EOF, and sets '*replyp' to null.
678 * 'request' is always destroyed, regardless of the return value. */
680 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
681 struct ofpbuf **replyp)
683 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
687 error = vconn_send_block(vconn, request);
689 ofpbuf_delete(request);
691 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
695 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
697 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
699 switch (vconn->state) {
716 case VCS_DISCONNECTED:
717 poll_immediate_wake();
720 (vconn->class->wait)(vconn, wait);
724 vconn_connect_wait(struct vconn *vconn)
726 vconn_wait(vconn, WAIT_CONNECT);
730 vconn_recv_wait(struct vconn *vconn)
732 vconn_wait(vconn, WAIT_RECV);
736 vconn_send_wait(struct vconn *vconn)
738 vconn_wait(vconn, WAIT_SEND);
741 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
742 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
743 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
746 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
750 prefix_len = strcspn(name, ":");
751 if (name[prefix_len] != '\0') {
754 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
755 struct pvconn_class *class = pvconn_classes[i];
756 if (strlen(class->name) == prefix_len
757 && !memcmp(class->name, name, prefix_len)) {
768 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
769 * a supported connection type, otherwise EAFNOSUPPORT. */
771 pvconn_verify_name(const char *name)
773 struct pvconn_class *class;
774 return pvconn_lookup_class(name, &class);
777 /* Attempts to start listening for OpenFlow connections. 'name' is a
778 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
779 * class's name and ARGS are vconn class-specific.
781 * Returns 0 if successful, otherwise a positive errno value. If successful,
782 * stores a pointer to the new connection in '*pvconnp', otherwise a null
785 pvconn_open(const char *name, struct pvconn **pvconnp)
787 struct pvconn_class *class;
788 struct pvconn *pvconn;
792 check_vconn_classes();
794 /* Look up the class. */
795 error = pvconn_lookup_class(name, &class);
800 /* Call class's "open" function. */
801 suffix_copy = xstrdup(strchr(name, ':') + 1);
802 error = class->listen(name, suffix_copy, &pvconn);
817 /* Returns the name that was used to open 'pvconn'. The caller must not
818 * modify or free the name. */
820 pvconn_get_name(const struct pvconn *pvconn)
825 /* Closes 'pvconn'. */
827 pvconn_close(struct pvconn *pvconn)
829 if (pvconn != NULL) {
830 char *name = pvconn->name;
831 (pvconn->class->close)(pvconn);
836 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
837 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
840 * The new vconn will automatically negotiate an OpenFlow protocol version
841 * acceptable to both peers on the connection. The version negotiated will be
842 * no lower than 'min_version' and no higher than OFP_VERSION.
844 * pvconn_accept() will not block waiting for a connection. If no connection
845 * is ready to be accepted, it returns EAGAIN immediately. */
847 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
849 int retval = (pvconn->class->accept)(pvconn, new_vconn);
853 assert((*new_vconn)->state != VCS_CONNECTING
854 || (*new_vconn)->class->connect);
855 (*new_vconn)->min_version = min_version;
861 pvconn_wait(struct pvconn *pvconn)
863 (pvconn->class->wait)(pvconn);
866 /* XXX we should really use consecutive xids to avoid probabilistic
868 static inline uint32_t
871 return random_uint32();
874 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
875 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
876 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
879 * The caller is responsible for freeing '*bufferp' when it is no longer
882 * The OpenFlow header length is initially set to 'openflow_len'; if the
883 * message is later extended, the length should be updated with
884 * update_openflow_length() before sending.
886 * Returns the header. */
888 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
890 *bufferp = ofpbuf_new(openflow_len);
891 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
894 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
895 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
896 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
899 * The caller is responsible for freeing '*bufferp' when it is no longer
902 * The OpenFlow header length is initially set to 'openflow_len'; if the
903 * message is later extended, the length should be updated with
904 * update_openflow_length() before sending.
906 * Returns the header. */
908 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
909 struct ofpbuf **bufferp)
911 *bufferp = ofpbuf_new(openflow_len);
912 return put_openflow_xid(openflow_len, type, xid, *bufferp);
915 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
916 * with the given 'type' and an arbitrary transaction id. Allocated bytes
917 * beyond the header, if any, are zeroed.
919 * The OpenFlow header length is initially set to 'openflow_len'; if the
920 * message is later extended, the length should be updated with
921 * update_openflow_length() before sending.
923 * Returns the header. */
925 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
927 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
930 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
931 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
932 * the header, if any, are zeroed.
934 * The OpenFlow header length is initially set to 'openflow_len'; if the
935 * message is later extended, the length should be updated with
936 * update_openflow_length() before sending.
938 * Returns the header. */
940 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
941 struct ofpbuf *buffer)
943 struct ofp_header *oh;
945 assert(openflow_len >= sizeof *oh);
946 assert(openflow_len <= UINT16_MAX);
948 oh = ofpbuf_put_uninit(buffer, openflow_len);
949 oh->version = OFP_VERSION;
951 oh->length = htons(openflow_len);
953 memset(oh + 1, 0, openflow_len - sizeof *oh);
957 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
960 update_openflow_length(struct ofpbuf *buffer)
962 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
963 oh->length = htons(buffer->size);
967 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
969 struct ofp_flow_mod *ofm;
970 size_t size = sizeof *ofm + actions_len;
971 struct ofpbuf *out = ofpbuf_new(size);
972 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
973 ofm->header.version = OFP_VERSION;
974 ofm->header.type = OFPT_FLOW_MOD;
975 ofm->header.length = htons(size);
977 ofm->match.wildcards = htonl(0);
978 ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
980 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
981 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
982 ofm->match.dl_vlan = flow->dl_vlan;
983 ofm->match.dl_vlan_pcp = flow->dl_vlan_pcp;
984 ofm->match.dl_type = flow->dl_type;
985 ofm->match.nw_src = flow->nw_src;
986 ofm->match.nw_dst = flow->nw_dst;
987 ofm->match.nw_proto = flow->nw_proto;
988 ofm->match.nw_tos = flow->nw_tos;
989 ofm->match.tp_src = flow->tp_src;
990 ofm->match.tp_dst = flow->tp_dst;
991 ofm->command = htons(command);
996 make_add_flow(const flow_t *flow, uint32_t buffer_id,
997 uint16_t idle_timeout, size_t actions_len)
999 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
1000 struct ofp_flow_mod *ofm = out->data;
1001 ofm->idle_timeout = htons(idle_timeout);
1002 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
1003 ofm->buffer_id = htonl(buffer_id);
1008 make_del_flow(const flow_t *flow)
1010 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
1011 struct ofp_flow_mod *ofm = out->data;
1012 ofm->out_port = htons(OFPP_NONE);
1017 make_add_simple_flow(const flow_t *flow,
1018 uint32_t buffer_id, uint16_t out_port,
1019 uint16_t idle_timeout)
1021 struct ofp_action_output *oao;
1022 struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
1024 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
1025 oao->type = htons(OFPAT_OUTPUT);
1026 oao->len = htons(sizeof *oao);
1027 oao->port = htons(out_port);
1032 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
1033 const struct ofpbuf *payload, int max_send_len)
1035 struct ofp_packet_in *opi;
1039 send_len = MIN(max_send_len, payload->size);
1040 buf = ofpbuf_new(sizeof *opi + send_len);
1041 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1042 OFPT_PACKET_IN, 0, buf);
1043 opi->buffer_id = htonl(buffer_id);
1044 opi->total_len = htons(payload->size);
1045 opi->in_port = htons(in_port);
1046 opi->reason = reason;
1047 ofpbuf_put(buf, payload->data, send_len);
1048 update_openflow_length(buf);
1054 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1056 const struct ofp_action_header *actions, size_t n_actions)
1058 size_t actions_len = n_actions * sizeof *actions;
1059 struct ofp_packet_out *opo;
1060 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1061 struct ofpbuf *out = ofpbuf_new(size);
1063 opo = ofpbuf_put_uninit(out, sizeof *opo);
1064 opo->header.version = OFP_VERSION;
1065 opo->header.type = OFPT_PACKET_OUT;
1066 opo->header.length = htons(size);
1067 opo->header.xid = htonl(0);
1068 opo->buffer_id = htonl(buffer_id);
1069 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1070 opo->actions_len = htons(actions_len);
1071 ofpbuf_put(out, actions, actions_len);
1073 ofpbuf_put(out, packet->data, packet->size);
1079 make_unbuffered_packet_out(const struct ofpbuf *packet,
1080 uint16_t in_port, uint16_t out_port)
1082 struct ofp_action_output action;
1083 action.type = htons(OFPAT_OUTPUT);
1084 action.len = htons(sizeof action);
1085 action.port = htons(out_port);
1086 return make_packet_out(packet, UINT32_MAX, in_port,
1087 (struct ofp_action_header *) &action, 1);
1091 make_buffered_packet_out(uint32_t buffer_id,
1092 uint16_t in_port, uint16_t out_port)
1094 struct ofp_action_output action;
1095 action.type = htons(OFPAT_OUTPUT);
1096 action.len = htons(sizeof action);
1097 action.port = htons(out_port);
1098 return make_packet_out(NULL, buffer_id, in_port,
1099 (struct ofp_action_header *) &action, 1);
1102 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1104 make_echo_request(void)
1106 struct ofp_header *rq;
1107 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1108 rq = ofpbuf_put_uninit(out, sizeof *rq);
1109 rq->version = OFP_VERSION;
1110 rq->type = OFPT_ECHO_REQUEST;
1111 rq->length = htons(sizeof *rq);
1116 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1117 * OFPT_ECHO_REQUEST message in 'rq'. */
1119 make_echo_reply(const struct ofp_header *rq)
1121 size_t size = ntohs(rq->length);
1122 struct ofpbuf *out = ofpbuf_new(size);
1123 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1124 reply->type = OFPT_ECHO_REPLY;
1129 check_message_type(uint8_t got_type, uint8_t want_type)
1131 if (got_type != want_type) {
1132 char *want_type_name = ofp_message_type_to_string(want_type);
1133 char *got_type_name = ofp_message_type_to_string(got_type);
1134 VLOG_WARN_RL(&bad_ofmsg_rl,
1135 "received bad message type %s (expected %s)",
1136 got_type_name, want_type_name);
1137 free(want_type_name);
1138 free(got_type_name);
1139 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1144 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
1145 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
1146 * with ofp_mkerr()). */
1148 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
1153 error = check_message_type(msg->type, type);
1158 got_size = ntohs(msg->length);
1159 if (got_size != size) {
1160 char *type_name = ofp_message_type_to_string(type);
1161 VLOG_WARN_RL(&bad_ofmsg_rl,
1162 "received %s message of length %zu (expected %zu)",
1163 type_name, got_size, size);
1165 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1171 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
1172 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
1173 * the checks pass, otherwise an OpenFlow error code (produced with
1176 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
1177 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
1180 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
1181 size_t min_size, size_t array_elt_size,
1182 size_t *n_array_elts)
1187 assert(array_elt_size);
1189 error = check_message_type(msg->type, type);
1194 got_size = ntohs(msg->length);
1195 if (got_size < min_size) {
1196 char *type_name = ofp_message_type_to_string(type);
1197 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
1198 "(expected at least %zu)",
1199 type_name, got_size, min_size);
1201 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1203 if ((got_size - min_size) % array_elt_size) {
1204 char *type_name = ofp_message_type_to_string(type);
1205 VLOG_WARN_RL(&bad_ofmsg_rl,
1206 "received %s message of bad length %zu: the "
1207 "excess over %zu (%zu) is not evenly divisible by %zu "
1208 "(remainder is %zu)",
1209 type_name, got_size, min_size, got_size - min_size,
1210 array_elt_size, (got_size - min_size) % array_elt_size);
1212 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1215 *n_array_elts = (got_size - min_size) / array_elt_size;
1221 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
1222 int *n_actionsp, int max_ports)
1224 const struct ofp_packet_out *opo;
1225 unsigned int actions_len, n_actions;
1230 error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
1231 sizeof *opo, 1, &extra);
1235 opo = (const struct ofp_packet_out *) oh;
1237 actions_len = ntohs(opo->actions_len);
1238 if (actions_len > extra) {
1239 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
1240 "but message has room for only %zu bytes",
1241 actions_len, extra);
1242 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1244 if (actions_len % sizeof(union ofp_action)) {
1245 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
1246 "which is not a multiple of %zu",
1247 actions_len, sizeof(union ofp_action));
1248 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
1251 n_actions = actions_len / sizeof(union ofp_action);
1252 error = validate_actions((const union ofp_action *) opo->actions,
1253 n_actions, max_ports);
1258 data->data = (void *) &opo->actions[n_actions];
1259 data->size = extra - actions_len;
1260 *n_actionsp = n_actions;
1264 const struct ofp_flow_stats *
1265 flow_stats_first(struct flow_stats_iterator *iter,
1266 const struct ofp_stats_reply *osr)
1268 iter->pos = osr->body;
1269 iter->end = osr->body + (ntohs(osr->header.length)
1270 - offsetof(struct ofp_stats_reply, body));
1271 return flow_stats_next(iter);
1274 const struct ofp_flow_stats *
1275 flow_stats_next(struct flow_stats_iterator *iter)
1277 ptrdiff_t bytes_left = iter->end - iter->pos;
1278 const struct ofp_flow_stats *fs;
1281 if (bytes_left < sizeof *fs) {
1282 if (bytes_left != 0) {
1283 VLOG_WARN_RL(&bad_ofmsg_rl,
1284 "%td leftover bytes in flow stats reply", bytes_left);
1289 fs = (const void *) iter->pos;
1290 length = ntohs(fs->length);
1291 if (length < sizeof *fs) {
1292 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1293 "min %zu", length, sizeof *fs);
1295 } else if (length > bytes_left) {
1296 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1297 "bytes left", length, bytes_left);
1299 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1300 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1301 "left over in final action", length,
1302 (length - sizeof *fs) % sizeof fs->actions[0]);
1305 iter->pos += length;
1309 /* Alignment of ofp_actions. */
1310 #define ACTION_ALIGNMENT 8
1313 check_action_exact_len(const union ofp_action *a, unsigned int len,
1314 unsigned int required_len)
1316 if (len != required_len) {
1317 VLOG_DBG_RL(&bad_ofmsg_rl,
1318 "action %u has invalid length %"PRIu16" (must be %u)\n",
1319 a->type, ntohs(a->header.len), required_len);
1320 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1326 check_action_port(int port, int max_ports)
1334 case OFPP_CONTROLLER:
1339 if (port >= 0 && port < max_ports) {
1342 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1343 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1348 check_nicira_action(const union ofp_action *a, unsigned int len)
1350 const struct nx_action_header *nah;
1353 VLOG_DBG_RL(&bad_ofmsg_rl,
1354 "Nicira vendor action only %u bytes", len);
1355 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1357 nah = (const struct nx_action_header *) a;
1359 switch (ntohs(nah->subtype)) {
1360 case NXAST_RESUBMIT:
1361 return check_action_exact_len(a, len, 16);
1363 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1368 check_action(const union ofp_action *a, unsigned int len, int max_ports)
1372 switch (ntohs(a->type)) {
1374 error = check_action_port(ntohs(a->output.port), max_ports);
1375 return error ? error : check_action_exact_len(a, len, 8);
1377 case OFPAT_SET_VLAN_VID:
1378 case OFPAT_SET_VLAN_PCP:
1379 case OFPAT_STRIP_VLAN:
1380 case OFPAT_SET_NW_SRC:
1381 case OFPAT_SET_NW_DST:
1382 case OFPAT_SET_NW_TOS:
1383 case OFPAT_SET_TP_SRC:
1384 case OFPAT_SET_TP_DST:
1385 return check_action_exact_len(a, len, 8);
1387 case OFPAT_SET_DL_SRC:
1388 case OFPAT_SET_DL_DST:
1389 return check_action_exact_len(a, len, 16);
1392 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1393 ? check_nicira_action(a, len)
1394 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1397 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
1399 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1404 validate_actions(const union ofp_action *actions, size_t n_actions,
1407 const union ofp_action *a;
1409 for (a = actions; a < &actions[n_actions]; ) {
1410 unsigned int len = ntohs(a->header.len);
1411 unsigned int n_slots = len / ACTION_ALIGNMENT;
1412 unsigned int slots_left = &actions[n_actions] - a;
1415 if (n_slots > slots_left) {
1416 VLOG_DBG_RL(&bad_ofmsg_rl,
1417 "action requires %u slots but only %u remain",
1418 n_slots, slots_left);
1419 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1421 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
1422 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1423 } else if (len % ACTION_ALIGNMENT) {
1424 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1425 "of %d", len, ACTION_ALIGNMENT);
1426 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1429 error = check_action(a, len, max_ports);
1438 /* The set of actions must either come from a trusted source or have been
1439 * previously validated with validate_actions(). */
1440 const union ofp_action *
1441 actions_first(struct actions_iterator *iter,
1442 const union ofp_action *oa, size_t n_actions)
1445 iter->end = oa + n_actions;
1446 return actions_next(iter);
1449 const union ofp_action *
1450 actions_next(struct actions_iterator *iter)
1452 if (iter->pos < iter->end) {
1453 const union ofp_action *a = iter->pos;
1454 unsigned int len = ntohs(a->header.len);
1455 iter->pos += len / ACTION_ALIGNMENT;
1463 normalize_match(struct ofp_match *m)
1465 enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
1466 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1469 wc = ntohl(m->wildcards) & OFPFW_ALL;
1470 if (wc & OFPFW_DL_TYPE) {
1473 /* Can't sensibly match on network or transport headers if the
1474 * data link type is unknown. */
1475 wc |= OFPFW_NW | OFPFW_TP;
1476 m->nw_src = m->nw_dst = m->nw_proto = 0;
1477 m->tp_src = m->tp_dst = 0;
1478 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1479 if (wc & OFPFW_NW_PROTO) {
1482 /* Can't sensibly match on transport headers if the network
1483 * protocol is unknown. */
1485 m->tp_src = m->tp_dst = 0;
1486 } else if (m->nw_proto == IPPROTO_TCP ||
1487 m->nw_proto == IPPROTO_UDP ||
1488 m->nw_proto == IPPROTO_ICMP) {
1489 if (wc & OFPFW_TP_SRC) {
1492 if (wc & OFPFW_TP_DST) {
1496 /* Transport layer fields will always be extracted as zeros, so we
1497 * can do an exact-match on those values. */
1499 m->tp_src = m->tp_dst = 0;
1501 if (wc & OFPFW_NW_SRC_MASK) {
1502 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
1504 if (wc & OFPFW_NW_DST_MASK) {
1505 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
1507 } else if (m->dl_type == htons(ETH_TYPE_ARP)) {
1508 if (wc & OFPFW_NW_PROTO) {
1511 if (wc & OFPFW_NW_SRC_MASK) {
1512 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
1514 if (wc & OFPFW_NW_DST_MASK) {
1515 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
1517 m->tp_src = m->tp_dst = 0;
1519 /* Network and transport layer fields will always be extracted as
1520 * zeros, so we can do an exact-match on those values. */
1521 wc &= ~(OFPFW_NW | OFPFW_TP);
1522 m->nw_proto = m->nw_src = m->nw_dst = 0;
1523 m->tp_src = m->tp_dst = 0;
1525 if (wc & OFPFW_DL_SRC) {
1526 memset(m->dl_src, 0, sizeof m->dl_src);
1528 if (wc & OFPFW_DL_DST) {
1529 memset(m->dl_dst, 0, sizeof m->dl_dst);
1531 m->wildcards = htonl(wc);
1534 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1535 * The initial connection status, supplied as 'connect_status', is interpreted
1538 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1539 * called in the normal fashion.
1541 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1542 * function should be called to complete the connection.
1544 * - Other positive errno values indicate that the connection failed with
1545 * the specified error.
1547 * After calling this function, vconn_close() must be used to destroy 'vconn',
1548 * otherwise resources will be leaked.
1550 * The caller retains ownership of 'name'. */
1552 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
1555 vconn->class = class;
1556 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1557 : !connect_status ? VCS_SEND_HELLO
1558 : VCS_DISCONNECTED);
1559 vconn->error = connect_status;
1560 vconn->version = -1;
1561 vconn->min_version = -1;
1562 vconn->remote_ip = 0;
1563 vconn->remote_port = 0;
1564 vconn->local_ip = 0;
1565 vconn->local_port = 0;
1566 vconn->name = xstrdup(name);
1567 assert(vconn->state != VCS_CONNECTING || class->connect);
1571 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
1573 vconn->remote_ip = ip;
1577 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
1579 vconn->remote_port = port;
1583 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
1585 vconn->local_ip = ip;
1589 vconn_set_local_port(struct vconn *vconn, uint16_t port)
1591 vconn->local_port = port;
1595 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
1598 pvconn->class = class;
1599 pvconn->name = xstrdup(name);