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"
29 #include "ofp-print.h"
31 #include "openflow/nicira-ext.h"
32 #include "openflow/openflow.h"
34 #include "poll-loop.h"
38 #define THIS_MODULE VLM_vconn
41 /* State of an active vconn.*/
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. */
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. */
54 static struct vconn_class *vconn_classes[] = {
62 static struct pvconn_class *pvconn_classes[] = {
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);
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);
79 static int do_recv(struct vconn *, struct ofpbuf **);
80 static int do_send(struct vconn *, struct ofpbuf *);
82 /* Check the validity of the vconn class structures. */
84 check_vconn_classes(void)
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);
99 /* This class delegates to another one. */
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);
112 /* This class delegates to another one. */
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. */
122 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
124 /* Really this should be implemented via callbacks into the vconn
125 * providers, but that seems too heavy-weight to bother with at the
130 printf("Active OpenFlow connection methods:\n");
131 printf(" tcp:IP[:PORT] "
132 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
134 printf(" ssl:IP[:PORT] "
135 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
137 printf(" unix:FILE Unix domain socket named FILE\n");
141 printf("Passive OpenFlow connection methods:\n");
142 printf(" ptcp:[PORT][:IP] "
143 "listen to TCP PORT (default: %d) on IP\n",
146 printf(" pssl:[PORT][:IP] "
147 "listen for SSL on PORT (default: %d) on IP\n",
150 printf(" punix:FILE "
151 "listen on Unix domain socket FILE\n");
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");
160 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
161 "to read or create\n");
166 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
167 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
168 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
171 vconn_lookup_class(const char *name, struct vconn_class **classp)
175 prefix_len = strcspn(name, ":");
176 if (name[prefix_len] != '\0') {
179 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
180 struct vconn_class *class = vconn_classes[i];
181 if (strlen(class->name) == prefix_len
182 && !memcmp(class->name, name, prefix_len)) {
193 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
194 * a supported connection type, otherwise EAFNOSUPPORT. */
196 vconn_verify_name(const char *name)
198 struct vconn_class *class;
199 return vconn_lookup_class(name, &class);
202 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
203 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
204 * are vconn class-specific.
206 * The vconn will automatically negotiate an OpenFlow protocol version
207 * acceptable to both peers on the connection. The version negotiated will be
208 * no lower than 'min_version' and no higher than OFP_VERSION.
210 * Returns 0 if successful, otherwise a positive errno value. If successful,
211 * stores a pointer to the new connection in '*vconnp', otherwise a null
214 vconn_open(const char *name, int min_version, struct vconn **vconnp)
216 struct vconn_class *class;
221 COVERAGE_INC(vconn_open);
222 check_vconn_classes();
224 /* Look up the class. */
225 error = vconn_lookup_class(name, &class);
230 /* Call class's "open" function. */
231 suffix_copy = xstrdup(strchr(name, ':') + 1);
232 error = class->open(name, suffix_copy, &vconn);
239 assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
240 vconn->min_version = min_version;
250 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
255 error = vconn_open(name, min_version, &vconn);
256 while (error == EAGAIN) {
257 vconn_connect_wait(vconn);
259 error = vconn_connect(vconn);
260 assert(error != EINPROGRESS);
271 /* Closes 'vconn'. */
273 vconn_close(struct vconn *vconn)
276 char *name = vconn->name;
277 (vconn->class->close)(vconn);
282 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
284 vconn_get_name(const struct vconn *vconn)
289 /* Returns the IP address of the peer, or 0 if the peer is not connected over
290 * an IP-based protocol or if its IP address is not yet known. */
292 vconn_get_remote_ip(const struct vconn *vconn)
294 return vconn->remote_ip;
297 /* Returns the transport port of the peer, or 0 if the connection does not
298 * contain a port or if the port is not yet known. */
300 vconn_get_remote_port(const struct vconn *vconn)
302 return vconn->remote_port;
305 /* Returns the IP address used to connect to the peer, or 0 if the
306 * connection is not an IP-based protocol or if its IP address is not
309 vconn_get_local_ip(const struct vconn *vconn)
311 return vconn->local_ip;
314 /* Returns the transport port used to connect to the peer, or 0 if the
315 * connection does not contain a port or if the port is not yet known. */
317 vconn_get_local_port(const struct vconn *vconn)
319 return vconn->local_port;
323 vcs_connecting(struct vconn *vconn)
325 int retval = (vconn->class->connect)(vconn);
326 assert(retval != EINPROGRESS);
328 vconn->state = VCS_SEND_HELLO;
329 } else if (retval != EAGAIN) {
330 vconn->state = VCS_DISCONNECTED;
331 vconn->error = retval;
336 vcs_send_hello(struct vconn *vconn)
341 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
342 retval = do_send(vconn, b);
344 vconn->state = VCS_RECV_HELLO;
347 if (retval != EAGAIN) {
348 vconn->state = VCS_DISCONNECTED;
349 vconn->error = retval;
355 vcs_recv_hello(struct vconn *vconn)
360 retval = do_recv(vconn, &b);
362 struct ofp_header *oh = b->data;
364 if (oh->type == OFPT_HELLO) {
365 if (b->size > sizeof *oh) {
366 struct ds msg = DS_EMPTY_INITIALIZER;
367 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
368 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
369 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
373 vconn->version = MIN(OFP_VERSION, oh->version);
374 if (vconn->version < vconn->min_version) {
375 VLOG_WARN_RL(&bad_ofmsg_rl,
376 "%s: version negotiation failed: we support "
377 "versions 0x%02x to 0x%02x inclusive but peer "
378 "supports no later than version 0x%02"PRIx8,
379 vconn->name, vconn->min_version, OFP_VERSION,
381 vconn->state = VCS_SEND_ERROR;
383 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
384 "(we support versions 0x%02x to 0x%02x inclusive, "
385 "peer no later than version 0x%02"PRIx8")",
386 vconn->name, vconn->version, vconn->min_version,
387 OFP_VERSION, oh->version);
388 vconn->state = VCS_CONNECTED;
393 char *s = ofp_to_string(b->data, b->size, 1);
394 VLOG_WARN_RL(&bad_ofmsg_rl,
395 "%s: received message while expecting hello: %s",
403 if (retval != EAGAIN) {
404 vconn->state = VCS_DISCONNECTED;
405 vconn->error = retval == EOF ? ECONNRESET : retval;
410 vcs_send_error(struct vconn *vconn)
412 struct ofp_error_msg *error;
417 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
418 "you support no later than version 0x%02"PRIx8".",
419 vconn->min_version, OFP_VERSION, vconn->version);
420 error = make_openflow(sizeof *error, OFPT_ERROR, &b);
421 error->type = htons(OFPET_HELLO_FAILED);
422 error->code = htons(OFPHFC_INCOMPATIBLE);
423 ofpbuf_put(b, s, strlen(s));
424 update_openflow_length(b);
425 retval = do_send(vconn, b);
429 if (retval != EAGAIN) {
430 vconn->state = VCS_DISCONNECTED;
431 vconn->error = retval ? retval : EPROTO;
435 /* Tries to complete the connection on 'vconn', which must be an active
436 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
437 * was successful or a positive errno value if it failed. If the
438 * connection is still in progress, returns EAGAIN. */
440 vconn_connect(struct vconn *vconn)
442 enum vconn_state last_state;
444 assert(vconn->min_version >= 0);
446 last_state = vconn->state;
447 switch (vconn->state) {
449 vcs_connecting(vconn);
453 vcs_send_hello(vconn);
457 vcs_recv_hello(vconn);
464 vcs_send_error(vconn);
467 case VCS_DISCONNECTED:
473 } while (vconn->state != last_state);
478 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
479 * vconn. If successful, stores the received message into '*msgp' and returns
480 * 0. The caller is responsible for destroying the message with
481 * ofpbuf_delete(). On failure, returns a positive errno value and stores a
482 * null pointer into '*msgp'. On normal connection close, returns EOF.
484 * vconn_recv will not block waiting for a packet to arrive. If no packets
485 * have been received, it returns EAGAIN immediately. */
487 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
489 int retval = vconn_connect(vconn);
491 retval = do_recv(vconn, msgp);
497 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
499 int retval = (vconn->class->recv)(vconn, msgp);
501 struct ofp_header *oh;
503 COVERAGE_INC(vconn_received);
504 if (VLOG_IS_DBG_ENABLED()) {
505 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
506 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
510 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
511 if (oh->version != vconn->version
512 && oh->type != OFPT_HELLO
513 && oh->type != OFPT_ERROR
514 && oh->type != OFPT_ECHO_REQUEST
515 && oh->type != OFPT_ECHO_REPLY
516 && oh->type != OFPT_VENDOR)
518 if (vconn->version < 0) {
519 VLOG_ERR_RL(&bad_ofmsg_rl,
520 "%s: received OpenFlow message type %"PRIu8" "
521 "before version negotiation complete",
522 vconn->name, oh->type);
524 VLOG_ERR_RL(&bad_ofmsg_rl,
525 "%s: received OpenFlow version 0x%02"PRIx8" "
527 vconn->name, oh->version, vconn->version);
529 ofpbuf_delete(*msgp);
539 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
540 * vconn. If successful, returns 0, in which case ownership of 'msg' is
541 * transferred to the vconn. Success does not guarantee that 'msg' has been or
542 * ever will be delivered to the peer, only that it has been queued for
545 * Returns a positive errno value on failure, in which case the caller
546 * retains ownership of 'msg'.
548 * vconn_send will not block. If 'msg' cannot be immediately accepted for
549 * transmission, it returns EAGAIN immediately. */
551 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
553 int retval = vconn_connect(vconn);
555 retval = do_send(vconn, msg);
561 do_send(struct vconn *vconn, struct ofpbuf *msg)
565 assert(msg->size >= sizeof(struct ofp_header));
566 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
567 if (!VLOG_IS_DBG_ENABLED()) {
568 COVERAGE_INC(vconn_sent);
569 retval = (vconn->class->send)(vconn, msg);
571 char *s = ofp_to_string(msg->data, msg->size, 1);
572 retval = (vconn->class->send)(vconn, msg);
573 if (retval != EAGAIN) {
574 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
575 vconn->name, strerror(retval), s);
582 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
584 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
587 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
588 vconn_send_wait(vconn);
594 /* Same as vconn_recv, except that it waits until a message is received. */
596 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
599 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
600 vconn_recv_wait(vconn);
606 /* Waits until a message with a transaction ID matching 'xid' is recived on
607 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
608 * '*replyp' for the caller to examine and free. Otherwise returns a positive
609 * errno value, or EOF, and sets '*replyp' to null.
611 * 'request' is always destroyed, regardless of the return value. */
613 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
617 struct ofpbuf *reply;
620 error = vconn_recv_block(vconn, &reply);
625 recv_xid = ((struct ofp_header *) reply->data)->xid;
626 if (xid == recv_xid) {
631 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
632 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
633 ofpbuf_delete(reply);
637 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
638 * matching transaction ID. Returns 0 if successful, in which case the reply
639 * is stored in '*replyp' for the caller to examine and free. Otherwise
640 * returns a positive errno value, or EOF, and sets '*replyp' to null.
642 * 'request' is always destroyed, regardless of the return value. */
644 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
645 struct ofpbuf **replyp)
647 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
651 error = vconn_send_block(vconn, request);
653 ofpbuf_delete(request);
655 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
659 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
661 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
663 switch (vconn->state) {
680 case VCS_DISCONNECTED:
681 poll_immediate_wake();
684 (vconn->class->wait)(vconn, wait);
688 vconn_connect_wait(struct vconn *vconn)
690 vconn_wait(vconn, WAIT_CONNECT);
694 vconn_recv_wait(struct vconn *vconn)
696 vconn_wait(vconn, WAIT_RECV);
700 vconn_send_wait(struct vconn *vconn)
702 vconn_wait(vconn, WAIT_SEND);
705 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
706 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
707 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
710 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
714 prefix_len = strcspn(name, ":");
715 if (name[prefix_len] != '\0') {
718 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
719 struct pvconn_class *class = pvconn_classes[i];
720 if (strlen(class->name) == prefix_len
721 && !memcmp(class->name, name, prefix_len)) {
732 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
733 * a supported connection type, otherwise EAFNOSUPPORT. */
735 pvconn_verify_name(const char *name)
737 struct pvconn_class *class;
738 return pvconn_lookup_class(name, &class);
741 /* Attempts to start listening for OpenFlow connections. 'name' is a
742 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
743 * class's name and ARGS are vconn class-specific.
745 * Returns 0 if successful, otherwise a positive errno value. If successful,
746 * stores a pointer to the new connection in '*pvconnp', otherwise a null
749 pvconn_open(const char *name, struct pvconn **pvconnp)
751 struct pvconn_class *class;
752 struct pvconn *pvconn;
756 check_vconn_classes();
758 /* Look up the class. */
759 error = pvconn_lookup_class(name, &class);
764 /* Call class's "open" function. */
765 suffix_copy = xstrdup(strchr(name, ':') + 1);
766 error = class->listen(name, suffix_copy, &pvconn);
781 /* Returns the name that was used to open 'pvconn'. The caller must not
782 * modify or free the name. */
784 pvconn_get_name(const struct pvconn *pvconn)
789 /* Closes 'pvconn'. */
791 pvconn_close(struct pvconn *pvconn)
793 if (pvconn != NULL) {
794 char *name = pvconn->name;
795 (pvconn->class->close)(pvconn);
800 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
801 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
804 * The new vconn will automatically negotiate an OpenFlow protocol version
805 * acceptable to both peers on the connection. The version negotiated will be
806 * no lower than 'min_version' and no higher than OFP_VERSION.
808 * pvconn_accept() will not block waiting for a connection. If no connection
809 * is ready to be accepted, it returns EAGAIN immediately. */
811 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
813 int retval = (pvconn->class->accept)(pvconn, new_vconn);
817 assert((*new_vconn)->state != VCS_CONNECTING
818 || (*new_vconn)->class->connect);
819 (*new_vconn)->min_version = min_version;
825 pvconn_wait(struct pvconn *pvconn)
827 (pvconn->class->wait)(pvconn);
830 /* XXX we should really use consecutive xids to avoid probabilistic
832 static inline uint32_t
835 return random_uint32();
838 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
839 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
840 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
843 * The caller is responsible for freeing '*bufferp' when it is no longer
846 * The OpenFlow header length is initially set to 'openflow_len'; if the
847 * message is later extended, the length should be updated with
848 * update_openflow_length() before sending.
850 * Returns the header. */
852 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
854 *bufferp = ofpbuf_new(openflow_len);
855 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
858 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
859 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
860 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
863 * The caller is responsible for freeing '*bufferp' when it is no longer
866 * The OpenFlow header length is initially set to 'openflow_len'; if the
867 * message is later extended, the length should be updated with
868 * update_openflow_length() before sending.
870 * Returns the header. */
872 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
873 struct ofpbuf **bufferp)
875 *bufferp = ofpbuf_new(openflow_len);
876 return put_openflow_xid(openflow_len, type, xid, *bufferp);
879 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
880 * with the given 'type' and an arbitrary transaction id. Allocated bytes
881 * beyond the header, if any, are zeroed.
883 * The OpenFlow header length is initially set to 'openflow_len'; if the
884 * message is later extended, the length should be updated with
885 * update_openflow_length() before sending.
887 * Returns the header. */
889 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
891 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
894 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
895 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
896 * the header, if any, are zeroed.
898 * The OpenFlow header length is initially set to 'openflow_len'; if the
899 * message is later extended, the length should be updated with
900 * update_openflow_length() before sending.
902 * Returns the header. */
904 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
905 struct ofpbuf *buffer)
907 struct ofp_header *oh;
909 assert(openflow_len >= sizeof *oh);
910 assert(openflow_len <= UINT16_MAX);
912 oh = ofpbuf_put_uninit(buffer, openflow_len);
913 oh->version = OFP_VERSION;
915 oh->length = htons(openflow_len);
917 memset(oh + 1, 0, openflow_len - sizeof *oh);
921 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
924 update_openflow_length(struct ofpbuf *buffer)
926 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
927 oh->length = htons(buffer->size);
931 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
933 struct ofp_flow_mod *ofm;
934 size_t size = sizeof *ofm + actions_len;
935 struct ofpbuf *out = ofpbuf_new(size);
936 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
937 ofm->header.version = OFP_VERSION;
938 ofm->header.type = OFPT_FLOW_MOD;
939 ofm->header.length = htons(size);
940 ofm->match.wildcards = htonl(0);
941 ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
943 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
944 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
945 ofm->match.dl_vlan = flow->dl_vlan;
946 ofm->match.dl_type = flow->dl_type;
947 ofm->match.nw_src = flow->nw_src;
948 ofm->match.nw_dst = flow->nw_dst;
949 ofm->match.nw_proto = flow->nw_proto;
950 ofm->match.tp_src = flow->tp_src;
951 ofm->match.tp_dst = flow->tp_dst;
952 ofm->command = htons(command);
957 make_add_flow(const flow_t *flow, uint32_t buffer_id,
958 uint16_t idle_timeout, size_t actions_len)
960 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
961 struct ofp_flow_mod *ofm = out->data;
962 ofm->idle_timeout = htons(idle_timeout);
963 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
964 ofm->buffer_id = htonl(buffer_id);
969 make_del_flow(const flow_t *flow)
971 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
972 struct ofp_flow_mod *ofm = out->data;
973 ofm->out_port = htons(OFPP_NONE);
978 make_add_simple_flow(const flow_t *flow,
979 uint32_t buffer_id, uint16_t out_port,
980 uint16_t idle_timeout)
982 struct ofp_action_output *oao;
983 struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
985 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
986 oao->type = htons(OFPAT_OUTPUT);
987 oao->len = htons(sizeof *oao);
988 oao->port = htons(out_port);
993 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
994 const struct ofpbuf *payload, int max_send_len)
996 struct ofp_packet_in *opi;
1000 send_len = MIN(max_send_len, payload->size);
1001 buf = ofpbuf_new(sizeof *opi + send_len);
1002 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
1003 OFPT_PACKET_IN, 0, buf);
1004 opi->buffer_id = htonl(buffer_id);
1005 opi->total_len = htons(payload->size);
1006 opi->in_port = htons(in_port);
1007 opi->reason = reason;
1008 ofpbuf_put(buf, payload->data, send_len);
1009 update_openflow_length(buf);
1015 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
1017 const struct ofp_action_header *actions, size_t n_actions)
1019 size_t actions_len = n_actions * sizeof *actions;
1020 struct ofp_packet_out *opo;
1021 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
1022 struct ofpbuf *out = ofpbuf_new(size);
1024 opo = ofpbuf_put_uninit(out, sizeof *opo);
1025 opo->header.version = OFP_VERSION;
1026 opo->header.type = OFPT_PACKET_OUT;
1027 opo->header.length = htons(size);
1028 opo->header.xid = htonl(0);
1029 opo->buffer_id = htonl(buffer_id);
1030 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
1031 opo->actions_len = htons(actions_len);
1032 ofpbuf_put(out, actions, actions_len);
1034 ofpbuf_put(out, packet->data, packet->size);
1040 make_unbuffered_packet_out(const struct ofpbuf *packet,
1041 uint16_t in_port, uint16_t out_port)
1043 struct ofp_action_output action;
1044 action.type = htons(OFPAT_OUTPUT);
1045 action.len = htons(sizeof action);
1046 action.port = htons(out_port);
1047 return make_packet_out(packet, UINT32_MAX, in_port,
1048 (struct ofp_action_header *) &action, 1);
1052 make_buffered_packet_out(uint32_t buffer_id,
1053 uint16_t in_port, uint16_t out_port)
1055 struct ofp_action_output action;
1056 action.type = htons(OFPAT_OUTPUT);
1057 action.len = htons(sizeof action);
1058 action.port = htons(out_port);
1059 return make_packet_out(NULL, buffer_id, in_port,
1060 (struct ofp_action_header *) &action, 1);
1063 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1065 make_echo_request(void)
1067 struct ofp_header *rq;
1068 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1069 rq = ofpbuf_put_uninit(out, sizeof *rq);
1070 rq->version = OFP_VERSION;
1071 rq->type = OFPT_ECHO_REQUEST;
1072 rq->length = htons(sizeof *rq);
1077 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1078 * OFPT_ECHO_REQUEST message in 'rq'. */
1080 make_echo_reply(const struct ofp_header *rq)
1082 size_t size = ntohs(rq->length);
1083 struct ofpbuf *out = ofpbuf_new(size);
1084 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1085 reply->type = OFPT_ECHO_REPLY;
1090 check_message_type(uint8_t got_type, uint8_t want_type)
1092 if (got_type != want_type) {
1093 char *want_type_name = ofp_message_type_to_string(want_type);
1094 char *got_type_name = ofp_message_type_to_string(got_type);
1095 VLOG_WARN_RL(&bad_ofmsg_rl,
1096 "received bad message type %s (expected %s)",
1097 got_type_name, want_type_name);
1098 free(want_type_name);
1099 free(got_type_name);
1100 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1105 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
1106 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
1107 * with ofp_mkerr()). */
1109 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
1114 error = check_message_type(msg->type, type);
1119 got_size = ntohs(msg->length);
1120 if (got_size != size) {
1121 char *type_name = ofp_message_type_to_string(type);
1122 VLOG_WARN_RL(&bad_ofmsg_rl,
1123 "received %s message of length %zu (expected %zu)",
1124 type_name, got_size, size);
1126 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1132 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
1133 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
1134 * the checks pass, otherwise an OpenFlow error code (produced with
1137 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
1138 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
1141 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
1142 size_t min_size, size_t array_elt_size,
1143 size_t *n_array_elts)
1148 assert(array_elt_size);
1150 error = check_message_type(msg->type, type);
1155 got_size = ntohs(msg->length);
1156 if (got_size < min_size) {
1157 char *type_name = ofp_message_type_to_string(type);
1158 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
1159 "(expected at least %zu)",
1160 type_name, got_size, min_size);
1162 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1164 if ((got_size - min_size) % array_elt_size) {
1165 char *type_name = ofp_message_type_to_string(type);
1166 VLOG_WARN_RL(&bad_ofmsg_rl,
1167 "received %s message of bad length %zu: the "
1168 "excess over %zu (%zu) is not evenly divisible by %zu "
1169 "(remainder is %zu)",
1170 type_name, got_size, min_size, got_size - min_size,
1171 array_elt_size, (got_size - min_size) % array_elt_size);
1173 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1176 *n_array_elts = (got_size - min_size) / array_elt_size;
1182 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
1183 int *n_actionsp, int max_ports)
1185 const struct ofp_packet_out *opo;
1186 unsigned int actions_len, n_actions;
1191 error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
1192 sizeof *opo, 1, &extra);
1196 opo = (const struct ofp_packet_out *) oh;
1198 actions_len = ntohs(opo->actions_len);
1199 if (actions_len > extra) {
1200 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
1201 "but message has room for only %zu bytes",
1202 actions_len, extra);
1203 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1205 if (actions_len % sizeof(union ofp_action)) {
1206 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
1207 "which is not a multiple of %zu",
1208 actions_len, sizeof(union ofp_action));
1209 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1212 n_actions = actions_len / sizeof(union ofp_action);
1213 error = validate_actions((const union ofp_action *) opo->actions,
1214 n_actions, max_ports);
1219 data->data = (void *) &opo->actions[n_actions];
1220 data->size = extra - actions_len;
1221 *n_actionsp = n_actions;
1225 const struct ofp_flow_stats *
1226 flow_stats_first(struct flow_stats_iterator *iter,
1227 const struct ofp_stats_reply *osr)
1229 iter->pos = osr->body;
1230 iter->end = osr->body + (ntohs(osr->header.length)
1231 - offsetof(struct ofp_stats_reply, body));
1232 return flow_stats_next(iter);
1235 const struct ofp_flow_stats *
1236 flow_stats_next(struct flow_stats_iterator *iter)
1238 ptrdiff_t bytes_left = iter->end - iter->pos;
1239 const struct ofp_flow_stats *fs;
1242 if (bytes_left < sizeof *fs) {
1243 if (bytes_left != 0) {
1244 VLOG_WARN_RL(&bad_ofmsg_rl,
1245 "%td leftover bytes in flow stats reply", bytes_left);
1250 fs = (const void *) iter->pos;
1251 length = ntohs(fs->length);
1252 if (length < sizeof *fs) {
1253 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1254 "min %zu", length, sizeof *fs);
1256 } else if (length > bytes_left) {
1257 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1258 "bytes left", length, bytes_left);
1260 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1261 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1262 "left over in final action", length,
1263 (length - sizeof *fs) % sizeof fs->actions[0]);
1266 iter->pos += length;
1270 /* Alignment of ofp_actions. */
1271 #define ACTION_ALIGNMENT 8
1274 check_action_exact_len(const union ofp_action *a, unsigned int len,
1275 unsigned int required_len)
1277 if (len != required_len) {
1278 VLOG_DBG_RL(&bad_ofmsg_rl,
1279 "action %u has invalid length %"PRIu16" (must be %u)\n",
1280 a->type, ntohs(a->header.len), required_len);
1281 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1287 check_action_port(int port, int max_ports)
1295 case OFPP_CONTROLLER:
1300 if (port >= 0 && port < max_ports) {
1303 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1304 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1309 check_nicira_action(const union ofp_action *a, unsigned int len)
1311 const struct nx_action_header *nah;
1314 VLOG_DBG_RL(&bad_ofmsg_rl,
1315 "Nicira vendor action only %u bytes", len);
1316 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1318 nah = (const struct nx_action_header *) a;
1320 switch (ntohs(nah->subtype)) {
1321 case NXAST_RESUBMIT:
1322 return check_action_exact_len(a, len, 16);
1324 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1329 check_action(const union ofp_action *a, unsigned int len, int max_ports)
1333 switch (ntohs(a->type)) {
1335 error = check_action_port(ntohs(a->output.port), max_ports);
1336 return error ? error : check_action_exact_len(a, len, 8);
1338 case OFPAT_SET_VLAN_VID:
1339 case OFPAT_SET_VLAN_PCP:
1340 case OFPAT_STRIP_VLAN:
1341 case OFPAT_SET_NW_SRC:
1342 case OFPAT_SET_NW_DST:
1343 case OFPAT_SET_TP_SRC:
1344 case OFPAT_SET_TP_DST:
1345 return check_action_exact_len(a, len, 8);
1347 case OFPAT_SET_DL_SRC:
1348 case OFPAT_SET_DL_DST:
1349 return check_action_exact_len(a, len, 16);
1352 return (a->vendor.vendor == htonl(NX_VENDOR_ID)
1353 ? check_nicira_action(a, len)
1354 : ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR));
1357 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16,
1359 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1364 validate_actions(const union ofp_action *actions, size_t n_actions,
1367 const union ofp_action *a;
1369 for (a = actions; a < &actions[n_actions]; ) {
1370 unsigned int len = ntohs(a->header.len);
1371 unsigned int n_slots = len / ACTION_ALIGNMENT;
1372 unsigned int slots_left = &actions[n_actions] - a;
1375 if (n_slots > slots_left) {
1376 VLOG_DBG_RL(&bad_ofmsg_rl,
1377 "action requires %u slots but only %u remain",
1378 n_slots, slots_left);
1379 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1381 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
1382 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1383 } else if (len % ACTION_ALIGNMENT) {
1384 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple "
1385 "of %d", len, ACTION_ALIGNMENT);
1386 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1389 error = check_action(a, len, max_ports);
1398 /* The set of actions must either come from a trusted source or have been
1399 * previously validated with validate_actions(). */
1400 const union ofp_action *
1401 actions_first(struct actions_iterator *iter,
1402 const union ofp_action *oa, size_t n_actions)
1405 iter->end = oa + n_actions;
1406 return actions_next(iter);
1409 const union ofp_action *
1410 actions_next(struct actions_iterator *iter)
1412 if (iter->pos < iter->end) {
1413 const union ofp_action *a = iter->pos;
1414 unsigned int len = ntohs(a->header.len);
1415 iter->pos += len / ACTION_ALIGNMENT;
1423 normalize_match(struct ofp_match *m)
1425 enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
1426 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1429 wc = ntohl(m->wildcards) & OFPFW_ALL;
1430 if (wc & OFPFW_DL_TYPE) {
1433 /* Can't sensibly match on network or transport headers if the
1434 * data link type is unknown. */
1435 wc |= OFPFW_NW | OFPFW_TP;
1436 m->nw_src = m->nw_dst = m->nw_proto = 0;
1437 m->tp_src = m->tp_dst = 0;
1438 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1439 if (wc & OFPFW_NW_PROTO) {
1442 /* Can't sensibly match on transport headers if the network
1443 * protocol is unknown. */
1445 m->tp_src = m->tp_dst = 0;
1446 } else if (m->nw_proto == IPPROTO_TCP ||
1447 m->nw_proto == IPPROTO_UDP ||
1448 m->nw_proto == IPPROTO_ICMP) {
1449 if (wc & OFPFW_TP_SRC) {
1452 if (wc & OFPFW_TP_DST) {
1456 /* Transport layer fields will always be extracted as zeros, so we
1457 * can do an exact-match on those values. */
1459 m->tp_src = m->tp_dst = 0;
1461 if (wc & OFPFW_NW_SRC_MASK) {
1462 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
1464 if (wc & OFPFW_NW_DST_MASK) {
1465 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
1468 /* Network and transport layer fields will always be extracted as
1469 * zeros, so we can do an exact-match on those values. */
1470 wc &= ~(OFPFW_NW | OFPFW_TP);
1471 m->nw_proto = m->nw_src = m->nw_dst = 0;
1472 m->tp_src = m->tp_dst = 0;
1474 if (wc & OFPFW_DL_SRC) {
1475 memset(m->dl_src, 0, sizeof m->dl_src);
1477 if (wc & OFPFW_DL_DST) {
1478 memset(m->dl_dst, 0, sizeof m->dl_dst);
1480 m->wildcards = htonl(wc);
1483 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
1484 * The initial connection status, supplied as 'connect_status', is interpreted
1487 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
1488 * called in the normal fashion.
1490 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
1491 * function should be called to complete the connection.
1493 * - Other positive errno values indicate that the connection failed with
1494 * the specified error.
1496 * After calling this function, vconn_close() must be used to destroy 'vconn',
1497 * otherwise resources will be leaked.
1499 * The caller retains ownership of 'name'. */
1501 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
1504 vconn->class = class;
1505 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1506 : !connect_status ? VCS_SEND_HELLO
1507 : VCS_DISCONNECTED);
1508 vconn->error = connect_status;
1509 vconn->version = -1;
1510 vconn->min_version = -1;
1511 vconn->remote_ip = 0;
1512 vconn->remote_port = 0;
1513 vconn->local_ip = 0;
1514 vconn->local_port = 0;
1515 vconn->name = xstrdup(name);
1519 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
1521 vconn->remote_ip = ip;
1525 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
1527 vconn->remote_port = port;
1531 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
1533 vconn->local_ip = ip;
1537 vconn_set_local_port(struct vconn *vconn, uint16_t port)
1539 vconn->local_port = port;
1543 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
1546 pvconn->class = class;
1547 pvconn->name = xstrdup(name);