2 * Copyright (c) 2008, 2009 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 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] "
143 "listen to TCP PORT (default: %d)\n",
146 printf(" pssl:[PORT] "
147 "listen for SSL on PORT (default: %d)\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 /* 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.
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.
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
178 vconn_open(const char *name, int min_version, struct vconn **vconnp)
183 COVERAGE_INC(vconn_open);
184 check_vconn_classes();
187 prefix_len = strcspn(name, ":");
188 if (prefix_len == strlen(name)) {
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)) {
196 char *suffix_copy = xstrdup(name + prefix_len + 1);
197 int retval = class->open(name, suffix_copy, &vconn);
200 assert(vconn->state != VCS_CONNECTING
201 || vconn->class->connect);
202 vconn->min_version = min_version;
212 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
217 error = vconn_open(name, min_version, &vconn);
218 while (error == EAGAIN) {
219 vconn_connect_wait(vconn);
221 error = vconn_connect(vconn);
222 assert(error != EINPROGRESS);
233 /* Closes 'vconn'. */
235 vconn_close(struct vconn *vconn)
238 char *name = vconn->name;
239 (vconn->class->close)(vconn);
244 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
246 vconn_get_name(const struct vconn *vconn)
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. */
254 vconn_get_remote_ip(const struct vconn *vconn)
256 return vconn->remote_ip;
259 /* Returns the transport port of the peer, or 0 if the connection does not
260 * contain a port or if the port is not yet known. */
262 vconn_get_remote_port(const struct vconn *vconn)
264 return vconn->remote_port;
267 /* Returns the IP address used to connect to the peer, or 0 if the
268 * connection is not an IP-based protocol or if its IP address is not
271 vconn_get_local_ip(const struct vconn *vconn)
273 return vconn->local_ip;
276 /* Returns the transport port used to connect to the peer, or 0 if the
277 * connection does not contain a port or if the port is not yet known. */
279 vconn_get_local_port(const struct vconn *vconn)
281 return vconn->local_port;
285 vcs_connecting(struct vconn *vconn)
287 int retval = (vconn->class->connect)(vconn);
288 assert(retval != EINPROGRESS);
290 vconn->state = VCS_SEND_HELLO;
291 } else if (retval != EAGAIN) {
292 vconn->state = VCS_DISCONNECTED;
293 vconn->error = retval;
298 vcs_send_hello(struct vconn *vconn)
303 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
304 retval = do_send(vconn, b);
306 vconn->state = VCS_RECV_HELLO;
309 if (retval != EAGAIN) {
310 vconn->state = VCS_DISCONNECTED;
311 vconn->error = retval;
317 vcs_recv_hello(struct vconn *vconn)
322 retval = do_recv(vconn, &b);
324 struct ofp_header *oh = b->data;
326 if (oh->type == OFPT_HELLO) {
327 if (b->size > sizeof *oh) {
328 struct ds msg = DS_EMPTY_INITIALIZER;
329 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
330 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
331 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
335 vconn->version = MIN(OFP_VERSION, oh->version);
336 if (vconn->version < vconn->min_version) {
337 VLOG_WARN_RL(&bad_ofmsg_rl,
338 "%s: version negotiation failed: we support "
339 "versions 0x%02x to 0x%02x inclusive but peer "
340 "supports no later than version 0x%02"PRIx8,
341 vconn->name, vconn->min_version, OFP_VERSION,
343 vconn->state = VCS_SEND_ERROR;
345 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
346 "(we support versions 0x%02x to 0x%02x inclusive, "
347 "peer no later than version 0x%02"PRIx8")",
348 vconn->name, vconn->version, vconn->min_version,
349 OFP_VERSION, oh->version);
350 vconn->state = VCS_CONNECTED;
355 char *s = ofp_to_string(b->data, b->size, 1);
356 VLOG_WARN_RL(&bad_ofmsg_rl,
357 "%s: received message while expecting hello: %s",
365 if (retval != EAGAIN) {
366 vconn->state = VCS_DISCONNECTED;
367 vconn->error = retval;
372 vcs_send_error(struct vconn *vconn)
374 struct ofp_error_msg *error;
379 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
380 "you support no later than version 0x%02"PRIx8".",
381 vconn->min_version, OFP_VERSION, vconn->version);
382 error = make_openflow(sizeof *error, OFPT_ERROR, &b);
383 error->type = htons(OFPET_HELLO_FAILED);
384 error->code = htons(OFPHFC_INCOMPATIBLE);
385 ofpbuf_put(b, s, strlen(s));
386 update_openflow_length(b);
387 retval = do_send(vconn, b);
391 if (retval != EAGAIN) {
392 vconn->state = VCS_DISCONNECTED;
393 vconn->error = retval ? retval : EPROTO;
397 /* Tries to complete the connection on 'vconn', which must be an active
398 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
399 * was successful or a positive errno value if it failed. If the
400 * connection is still in progress, returns EAGAIN. */
402 vconn_connect(struct vconn *vconn)
404 enum vconn_state last_state;
406 assert(vconn->min_version >= 0);
408 last_state = vconn->state;
409 switch (vconn->state) {
411 vcs_connecting(vconn);
415 vcs_send_hello(vconn);
419 vcs_recv_hello(vconn);
426 vcs_send_error(vconn);
429 case VCS_DISCONNECTED:
435 } while (vconn->state != last_state);
440 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
441 * vconn. If successful, stores the received message into '*msgp' and returns
442 * 0. The caller is responsible for destroying the message with
443 * ofpbuf_delete(). On failure, returns a positive errno value and stores a
444 * null pointer into '*msgp'. On normal connection close, returns EOF.
446 * vconn_recv will not block waiting for a packet to arrive. If no packets
447 * have been received, it returns EAGAIN immediately. */
449 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
451 int retval = vconn_connect(vconn);
453 retval = do_recv(vconn, msgp);
459 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
464 retval = (vconn->class->recv)(vconn, msgp);
466 struct ofp_header *oh;
468 COVERAGE_INC(vconn_received);
469 if (VLOG_IS_DBG_ENABLED()) {
470 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
471 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
475 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
476 if (oh->version != vconn->version
477 && oh->type != OFPT_HELLO
478 && oh->type != OFPT_ERROR
479 && oh->type != OFPT_ECHO_REQUEST
480 && oh->type != OFPT_ECHO_REPLY
481 && oh->type != OFPT_VENDOR)
483 if (vconn->version < 0) {
484 if (oh->type == OFPT_PACKET_IN
485 || oh->type == OFPT_FLOW_EXPIRED
486 || oh->type == OFPT_PORT_STATUS) {
487 /* The kernel datapath is stateless and doesn't really
488 * support version negotiation, so it can end up sending
489 * these asynchronous message before version negotiation
490 * is complete. Just ignore them.
492 * (After we move OFPT_PORT_STATUS messages from the kernel
493 * into secchan, we won't get those here, since secchan
494 * does proper version negotiation.) */
495 ofpbuf_delete(*msgp);
498 VLOG_ERR_RL(&bad_ofmsg_rl,
499 "%s: received OpenFlow message type %"PRIu8" "
500 "before version negotiation complete",
501 vconn->name, oh->type);
503 VLOG_ERR_RL(&bad_ofmsg_rl,
504 "%s: received OpenFlow version 0x%02"PRIx8" "
506 vconn->name, oh->version, vconn->version);
508 ofpbuf_delete(*msgp);
518 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
519 * vconn. If successful, returns 0, in which case ownership of 'msg' is
520 * transferred to the vconn. Success does not guarantee that 'msg' has been or
521 * ever will be delivered to the peer, only that it has been queued for
524 * Returns a positive errno value on failure, in which case the caller
525 * retains ownership of 'msg'.
527 * vconn_send will not block. If 'msg' cannot be immediately accepted for
528 * transmission, it returns EAGAIN immediately. */
530 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
532 int retval = vconn_connect(vconn);
534 retval = do_send(vconn, msg);
540 do_send(struct vconn *vconn, struct ofpbuf *msg)
544 assert(msg->size >= sizeof(struct ofp_header));
545 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
546 if (!VLOG_IS_DBG_ENABLED()) {
547 COVERAGE_INC(vconn_sent);
548 retval = (vconn->class->send)(vconn, msg);
550 char *s = ofp_to_string(msg->data, msg->size, 1);
551 retval = (vconn->class->send)(vconn, msg);
552 if (retval != EAGAIN) {
553 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
554 vconn->name, strerror(retval), s);
561 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
563 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
566 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
567 vconn_send_wait(vconn);
573 /* Same as vconn_recv, except that it waits until a message is received. */
575 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
578 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
579 vconn_recv_wait(vconn);
585 /* Waits until a message with a transaction ID matching 'xid' is recived on
586 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
587 * '*replyp' for the caller to examine and free. Otherwise returns a positive
588 * errno value, or EOF, and sets '*replyp' to null.
590 * 'request' is always destroyed, regardless of the return value. */
592 vconn_recv_xid(struct vconn *vconn, uint32_t xid, struct ofpbuf **replyp)
596 struct ofpbuf *reply;
599 error = vconn_recv_block(vconn, &reply);
604 recv_xid = ((struct ofp_header *) reply->data)->xid;
605 if (xid == recv_xid) {
610 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
611 " != expected %08"PRIx32, vconn->name, recv_xid, xid);
612 ofpbuf_delete(reply);
616 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
617 * matching transaction ID. Returns 0 if successful, in which case the reply
618 * is stored in '*replyp' for the caller to examine and free. Otherwise
619 * returns a positive errno value, or EOF, and sets '*replyp' to null.
621 * 'request' is always destroyed, regardless of the return value. */
623 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
624 struct ofpbuf **replyp)
626 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
630 error = vconn_send_block(vconn, request);
632 ofpbuf_delete(request);
634 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
638 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
640 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
642 switch (vconn->state) {
659 case VCS_DISCONNECTED:
660 poll_immediate_wake();
663 (vconn->class->wait)(vconn, wait);
667 vconn_connect_wait(struct vconn *vconn)
669 vconn_wait(vconn, WAIT_CONNECT);
673 vconn_recv_wait(struct vconn *vconn)
675 vconn_wait(vconn, WAIT_RECV);
679 vconn_send_wait(struct vconn *vconn)
681 vconn_wait(vconn, WAIT_SEND);
684 /* Attempts to start listening for OpenFlow connections. 'name' is a
685 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
686 * class's name and ARGS are vconn class-specific.
688 * Returns 0 if successful, otherwise a positive errno value. If successful,
689 * stores a pointer to the new connection in '*pvconnp', otherwise a null
692 pvconn_open(const char *name, struct pvconn **pvconnp)
697 check_vconn_classes();
700 prefix_len = strcspn(name, ":");
701 if (prefix_len == strlen(name)) {
704 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
705 struct pvconn_class *class = pvconn_classes[i];
706 if (strlen(class->name) == prefix_len
707 && !memcmp(class->name, name, prefix_len)) {
708 char *suffix_copy = xstrdup(name + prefix_len + 1);
709 int retval = class->listen(name, suffix_copy, pvconnp);
720 /* Returns the name that was used to open 'pvconn'. The caller must not
721 * modify or free the name. */
723 pvconn_get_name(const struct pvconn *pvconn)
728 /* Closes 'pvconn'. */
730 pvconn_close(struct pvconn *pvconn)
732 if (pvconn != NULL) {
733 char *name = pvconn->name;
734 (pvconn->class->close)(pvconn);
739 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
740 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
743 * The new vconn will automatically negotiate an OpenFlow protocol version
744 * acceptable to both peers on the connection. The version negotiated will be
745 * no lower than 'min_version' and no higher than OFP_VERSION.
747 * pvconn_accept() will not block waiting for a connection. If no connection
748 * is ready to be accepted, it returns EAGAIN immediately. */
750 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
752 int retval = (pvconn->class->accept)(pvconn, new_vconn);
756 assert((*new_vconn)->state != VCS_CONNECTING
757 || (*new_vconn)->class->connect);
758 (*new_vconn)->min_version = min_version;
764 pvconn_wait(struct pvconn *pvconn)
766 (pvconn->class->wait)(pvconn);
769 /* XXX we should really use consecutive xids to avoid probabilistic
771 static inline uint32_t
774 return random_uint32();
777 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
778 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
779 * an arbitrary transaction id. Allocated bytes beyond the header, if any, are
782 * The caller is responsible for freeing '*bufferp' when it is no longer
785 * The OpenFlow header length is initially set to 'openflow_len'; if the
786 * message is later extended, the length should be updated with
787 * update_openflow_length() before sending.
789 * Returns the header. */
791 make_openflow(size_t openflow_len, uint8_t type, struct ofpbuf **bufferp)
793 *bufferp = ofpbuf_new(openflow_len);
794 return put_openflow_xid(openflow_len, type, alloc_xid(), *bufferp);
797 /* Allocates and stores in '*bufferp' a new ofpbuf with a size of
798 * 'openflow_len', starting with an OpenFlow header with the given 'type' and
799 * transaction id 'xid'. Allocated bytes beyond the header, if any, are
802 * The caller is responsible for freeing '*bufferp' when it is no longer
805 * The OpenFlow header length is initially set to 'openflow_len'; if the
806 * message is later extended, the length should be updated with
807 * update_openflow_length() before sending.
809 * Returns the header. */
811 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
812 struct ofpbuf **bufferp)
814 *bufferp = ofpbuf_new(openflow_len);
815 return put_openflow_xid(openflow_len, type, xid, *bufferp);
818 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
819 * with the given 'type' and an arbitrary transaction id. Allocated bytes
820 * beyond the header, if any, are zeroed.
822 * The OpenFlow header length is initially set to 'openflow_len'; if the
823 * message is later extended, the length should be updated with
824 * update_openflow_length() before sending.
826 * Returns the header. */
828 put_openflow(size_t openflow_len, uint8_t type, struct ofpbuf *buffer)
830 return put_openflow_xid(openflow_len, type, alloc_xid(), buffer);
833 /* Appends 'openflow_len' bytes to 'buffer', starting with an OpenFlow header
834 * with the given 'type' and an transaction id 'xid'. Allocated bytes beyond
835 * the header, if any, are zeroed.
837 * The OpenFlow header length is initially set to 'openflow_len'; if the
838 * message is later extended, the length should be updated with
839 * update_openflow_length() before sending.
841 * Returns the header. */
843 put_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
844 struct ofpbuf *buffer)
846 struct ofp_header *oh;
848 assert(openflow_len >= sizeof *oh);
849 assert(openflow_len <= UINT16_MAX);
851 oh = ofpbuf_put_uninit(buffer, openflow_len);
852 oh->version = OFP_VERSION;
854 oh->length = htons(openflow_len);
856 memset(oh + 1, 0, openflow_len - sizeof *oh);
860 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
863 update_openflow_length(struct ofpbuf *buffer)
865 struct ofp_header *oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
866 oh->length = htons(buffer->size);
870 make_flow_mod(uint16_t command, const flow_t *flow, size_t actions_len)
872 struct ofp_flow_mod *ofm;
873 size_t size = sizeof *ofm + actions_len;
874 struct ofpbuf *out = ofpbuf_new(size);
875 ofm = ofpbuf_put_zeros(out, sizeof *ofm);
876 ofm->header.version = OFP_VERSION;
877 ofm->header.type = OFPT_FLOW_MOD;
878 ofm->header.length = htons(size);
879 ofm->match.wildcards = htonl(0);
880 ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
882 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
883 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
884 ofm->match.dl_vlan = flow->dl_vlan;
885 ofm->match.dl_type = flow->dl_type;
886 ofm->match.nw_src = flow->nw_src;
887 ofm->match.nw_dst = flow->nw_dst;
888 ofm->match.nw_proto = flow->nw_proto;
889 ofm->match.tp_src = flow->tp_src;
890 ofm->match.tp_dst = flow->tp_dst;
891 ofm->command = htons(command);
896 make_add_flow(const flow_t *flow, uint32_t buffer_id,
897 uint16_t idle_timeout, size_t actions_len)
899 struct ofpbuf *out = make_flow_mod(OFPFC_ADD, flow, actions_len);
900 struct ofp_flow_mod *ofm = out->data;
901 ofm->idle_timeout = htons(idle_timeout);
902 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
903 ofm->buffer_id = htonl(buffer_id);
908 make_del_flow(const flow_t *flow)
910 struct ofpbuf *out = make_flow_mod(OFPFC_DELETE_STRICT, flow, 0);
911 struct ofp_flow_mod *ofm = out->data;
912 ofm->out_port = htons(OFPP_NONE);
917 make_add_simple_flow(const flow_t *flow,
918 uint32_t buffer_id, uint16_t out_port,
919 uint16_t idle_timeout)
921 struct ofp_action_output *oao;
922 struct ofpbuf *buffer = make_add_flow(flow, buffer_id, idle_timeout,
924 oao = ofpbuf_put_zeros(buffer, sizeof *oao);
925 oao->type = htons(OFPAT_OUTPUT);
926 oao->len = htons(sizeof *oao);
927 oao->port = htons(out_port);
932 make_packet_in(uint32_t buffer_id, uint16_t in_port, uint8_t reason,
933 const struct ofpbuf *payload, int max_send_len)
935 struct ofp_packet_in *opi;
939 send_len = MIN(max_send_len, payload->size);
940 buf = ofpbuf_new(sizeof *opi + send_len);
941 opi = put_openflow_xid(offsetof(struct ofp_packet_in, data),
942 OFPT_PACKET_IN, 0, buf);
943 opi->buffer_id = htonl(buffer_id);
944 opi->total_len = htons(payload->size);
945 opi->in_port = htons(in_port);
946 opi->reason = reason;
947 ofpbuf_put(buf, payload->data, send_len);
948 update_openflow_length(buf);
954 make_packet_out(const struct ofpbuf *packet, uint32_t buffer_id,
956 const struct ofp_action_header *actions, size_t n_actions)
958 size_t actions_len = n_actions * sizeof *actions;
959 struct ofp_packet_out *opo;
960 size_t size = sizeof *opo + actions_len + (packet ? packet->size : 0);
961 struct ofpbuf *out = ofpbuf_new(size);
963 opo = ofpbuf_put_uninit(out, sizeof *opo);
964 opo->header.version = OFP_VERSION;
965 opo->header.type = OFPT_PACKET_OUT;
966 opo->header.length = htons(size);
967 opo->header.xid = htonl(0);
968 opo->buffer_id = htonl(buffer_id);
969 opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
970 opo->actions_len = htons(actions_len);
971 ofpbuf_put(out, actions, actions_len);
973 ofpbuf_put(out, packet->data, packet->size);
979 make_unbuffered_packet_out(const struct ofpbuf *packet,
980 uint16_t in_port, uint16_t out_port)
982 struct ofp_action_output action;
983 action.type = htons(OFPAT_OUTPUT);
984 action.len = htons(sizeof action);
985 action.port = htons(out_port);
986 return make_packet_out(packet, UINT32_MAX, in_port,
987 (struct ofp_action_header *) &action, 1);
991 make_buffered_packet_out(uint32_t buffer_id,
992 uint16_t in_port, uint16_t out_port)
994 struct ofp_action_output action;
995 action.type = htons(OFPAT_OUTPUT);
996 action.len = htons(sizeof action);
997 action.port = htons(out_port);
998 return make_packet_out(NULL, buffer_id, in_port,
999 (struct ofp_action_header *) &action, 1);
1002 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
1004 make_echo_request(void)
1006 struct ofp_header *rq;
1007 struct ofpbuf *out = ofpbuf_new(sizeof *rq);
1008 rq = ofpbuf_put_uninit(out, sizeof *rq);
1009 rq->version = OFP_VERSION;
1010 rq->type = OFPT_ECHO_REQUEST;
1011 rq->length = htons(sizeof *rq);
1016 /* Creates and returns an OFPT_ECHO_REPLY message matching the
1017 * OFPT_ECHO_REQUEST message in 'rq'. */
1019 make_echo_reply(const struct ofp_header *rq)
1021 size_t size = ntohs(rq->length);
1022 struct ofpbuf *out = ofpbuf_new(size);
1023 struct ofp_header *reply = ofpbuf_put(out, rq, size);
1024 reply->type = OFPT_ECHO_REPLY;
1029 check_message_type(uint8_t got_type, uint8_t want_type)
1031 if (got_type != want_type) {
1032 char *want_type_name = ofp_message_type_to_string(want_type);
1033 char *got_type_name = ofp_message_type_to_string(got_type);
1034 VLOG_WARN_RL(&bad_ofmsg_rl,
1035 "received bad message type %s (expected %s)",
1036 got_type_name, want_type_name);
1037 free(want_type_name);
1038 free(got_type_name);
1039 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
1044 /* Checks that 'msg' has type 'type' and that it is exactly 'size' bytes long.
1045 * Returns 0 if the checks pass, otherwise an OpenFlow error code (produced
1046 * with ofp_mkerr()). */
1048 check_ofp_message(const struct ofp_header *msg, uint8_t type, size_t size)
1053 error = check_message_type(msg->type, type);
1058 got_size = ntohs(msg->length);
1059 if (got_size != size) {
1060 char *type_name = ofp_message_type_to_string(type);
1061 VLOG_WARN_RL(&bad_ofmsg_rl,
1062 "received %s message of length %zu (expected %zu)",
1063 type_name, got_size, size);
1065 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1071 /* Checks that 'msg' has type 'type' and that 'msg' is 'size' plus a
1072 * nonnegative integer multiple of 'array_elt_size' bytes long. Returns 0 if
1073 * the checks pass, otherwise an OpenFlow error code (produced with
1076 * If 'n_array_elts' is nonnull, then '*n_array_elts' is set to the number of
1077 * 'array_elt_size' blocks in 'msg' past the first 'min_size' bytes, when
1080 check_ofp_message_array(const struct ofp_header *msg, uint8_t type,
1081 size_t min_size, size_t array_elt_size,
1082 size_t *n_array_elts)
1087 assert(array_elt_size);
1089 error = check_message_type(msg->type, type);
1094 got_size = ntohs(msg->length);
1095 if (got_size < min_size) {
1096 char *type_name = ofp_message_type_to_string(type);
1097 VLOG_WARN_RL(&bad_ofmsg_rl, "received %s message of length %zu "
1098 "(expected at least %zu)",
1099 type_name, got_size, min_size);
1101 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1103 if ((got_size - min_size) % array_elt_size) {
1104 char *type_name = ofp_message_type_to_string(type);
1105 VLOG_WARN_RL(&bad_ofmsg_rl,
1106 "received %s message of bad length %zu: the "
1107 "excess over %zu (%zu) is not evenly divisible by %zu "
1108 "(remainder is %zu)",
1109 type_name, got_size, min_size, got_size - min_size,
1110 array_elt_size, (got_size - min_size) % array_elt_size);
1112 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1115 *n_array_elts = (got_size - min_size) / array_elt_size;
1121 check_ofp_packet_out(const struct ofp_header *oh, struct ofpbuf *data,
1122 int *n_actionsp, int max_ports)
1124 const struct ofp_packet_out *opo;
1125 unsigned int actions_len, n_actions;
1130 error = check_ofp_message_array(oh, OFPT_PACKET_OUT,
1131 sizeof *opo, 1, &extra);
1135 opo = (const struct ofp_packet_out *) oh;
1137 actions_len = ntohs(opo->actions_len);
1138 if (actions_len > extra) {
1139 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions "
1140 "but message has room for only %zu bytes",
1141 actions_len, extra);
1142 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1144 if (actions_len % sizeof(union ofp_action)) {
1145 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out claims %u bytes of actions, "
1146 "which is not a multiple of %zu",
1147 actions_len, sizeof(union ofp_action));
1148 return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH);
1151 n_actions = actions_len / sizeof(union ofp_action);
1152 error = validate_actions((const union ofp_action *) opo->actions,
1153 n_actions, max_ports);
1158 data->data = (void *) &opo->actions[n_actions];
1159 data->size = extra - actions_len;
1160 *n_actionsp = n_actions;
1164 const struct ofp_flow_stats *
1165 flow_stats_first(struct flow_stats_iterator *iter,
1166 const struct ofp_stats_reply *osr)
1168 iter->pos = osr->body;
1169 iter->end = osr->body + (ntohs(osr->header.length)
1170 - offsetof(struct ofp_stats_reply, body));
1171 return flow_stats_next(iter);
1174 const struct ofp_flow_stats *
1175 flow_stats_next(struct flow_stats_iterator *iter)
1177 ptrdiff_t bytes_left = iter->end - iter->pos;
1178 const struct ofp_flow_stats *fs;
1181 if (bytes_left < sizeof *fs) {
1182 if (bytes_left != 0) {
1183 VLOG_WARN_RL(&bad_ofmsg_rl,
1184 "%td leftover bytes in flow stats reply", bytes_left);
1189 fs = (const void *) iter->pos;
1190 length = ntohs(fs->length);
1191 if (length < sizeof *fs) {
1192 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu is shorter than "
1193 "min %zu", length, sizeof *fs);
1195 } else if (length > bytes_left) {
1196 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu but only %td "
1197 "bytes left", length, bytes_left);
1199 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1200 VLOG_WARN_RL(&bad_ofmsg_rl, "flow stats length %zu has %zu bytes "
1201 "left over in final action", length,
1202 (length - sizeof *fs) % sizeof fs->actions[0]);
1205 iter->pos += length;
1209 /* Alignment of ofp_actions. */
1210 #define ACTION_ALIGNMENT 8
1213 check_action_exact_len(const union ofp_action *a, unsigned int len,
1214 unsigned int required_len)
1216 if (len != required_len) {
1217 VLOG_DBG_RL(&bad_ofmsg_rl,
1218 "action %u has invalid length %"PRIu16" (must be %u)\n",
1219 a->type, ntohs(a->header.len), required_len);
1220 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1226 check_action_port(int port, int max_ports)
1234 case OFPP_CONTROLLER:
1239 if (port >= 0 && port < max_ports) {
1242 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown output port %x", port);
1243 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT);
1248 check_nicira_action(const union ofp_action *a, unsigned int len)
1250 const struct nx_action_header *nah;
1253 VLOG_DBG_RL(&bad_ofmsg_rl,
1254 "Nicira vendor action only %u bytes", len);
1255 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1257 nah = (const struct nx_action_header *) a;
1259 switch (ntohs(nah->subtype)) {
1260 case NXAST_RESUBMIT:
1261 return check_action_exact_len(a, len, 16);
1263 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
1268 check_action(const union ofp_action *a, unsigned int len, int max_ports)
1272 switch (ntohs(a->type)) {
1274 error = check_action_port(ntohs(a->output.port), max_ports);
1278 return check_action_exact_len(a, len, 8);
1280 case OFPAT_SET_VLAN_VID:
1281 case OFPAT_SET_VLAN_PCP:
1282 case OFPAT_STRIP_VLAN:
1283 case OFPAT_SET_NW_SRC:
1284 case OFPAT_SET_NW_DST:
1285 case OFPAT_SET_TP_SRC:
1286 case OFPAT_SET_TP_DST:
1287 return check_action_exact_len(a, len, 8);
1289 case OFPAT_SET_DL_SRC:
1290 case OFPAT_SET_DL_DST:
1291 return check_action_exact_len(a, len, 16);
1294 if (a->vendor.vendor == htonl(NX_VENDOR_ID)) {
1295 return check_nicira_action(a, len);
1297 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR);
1302 VLOG_WARN_RL(&bad_ofmsg_rl, "unknown action type %"PRIu16, a->type);
1303 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE);
1307 VLOG_DBG_RL(&bad_ofmsg_rl, "action has invalid length 0");
1308 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1310 if (len % ACTION_ALIGNMENT) {
1311 VLOG_DBG_RL(&bad_ofmsg_rl, "action length %u is not a multiple of %d",
1312 len, ACTION_ALIGNMENT);
1313 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1319 validate_actions(const union ofp_action *actions, size_t n_actions,
1322 const union ofp_action *a;
1324 for (a = actions; a < &actions[n_actions]; ) {
1325 unsigned int len = ntohs(a->header.len);
1326 unsigned int n_slots = len / ACTION_ALIGNMENT;
1327 unsigned int slots_left = &actions[n_actions] - a;
1330 if (n_slots > slots_left) {
1331 VLOG_DBG_RL(&bad_ofmsg_rl,
1332 "action requires %u slots but only %u remain",
1333 n_slots, slots_left);
1334 return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN);
1336 error = check_action(a, len, max_ports);
1345 /* The set of actions must either come from a trusted source or have been
1346 * previously validated with validate_actions(). */
1347 const union ofp_action *
1348 actions_first(struct actions_iterator *iter,
1349 const union ofp_action *oa, size_t n_actions)
1352 iter->end = oa + n_actions;
1353 return actions_next(iter);
1356 const union ofp_action *
1357 actions_next(struct actions_iterator *iter)
1359 if (iter->pos < iter->end) {
1360 const union ofp_action *a = iter->pos;
1361 unsigned int len = ntohs(a->header.len);
1362 iter->pos += len / ACTION_ALIGNMENT;
1370 normalize_match(struct ofp_match *m)
1372 enum { OFPFW_NW = OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO };
1373 enum { OFPFW_TP = OFPFW_TP_SRC | OFPFW_TP_DST };
1376 wc = ntohl(m->wildcards) & OFPFW_ALL;
1377 if (wc & OFPFW_DL_TYPE) {
1380 /* Can't sensibly m on network or transport headers if the
1381 * data link type is unknown. */
1382 wc |= OFPFW_NW | OFPFW_TP;
1383 m->nw_src = m->nw_dst = m->nw_proto = 0;
1384 m->tp_src = m->tp_dst = 0;
1385 } else if (m->dl_type == htons(ETH_TYPE_IP)) {
1386 if (wc & OFPFW_NW_PROTO) {
1389 /* Can't sensibly m on transport headers if the network
1390 * protocol is unknown. */
1392 m->tp_src = m->tp_dst = 0;
1393 } else if (m->nw_proto == IPPROTO_TCP ||
1394 m->nw_proto == IPPROTO_UDP ||
1395 m->nw_proto == IPPROTO_ICMP) {
1396 if (wc & OFPFW_TP_SRC) {
1399 if (wc & OFPFW_TP_DST) {
1403 /* Transport layer fields will always be extracted as zeros, so we
1404 * can do an exact-m on those values. */
1406 m->tp_src = m->tp_dst = 0;
1408 if (wc & OFPFW_NW_SRC_MASK) {
1409 m->nw_src &= flow_nw_bits_to_mask(wc, OFPFW_NW_SRC_SHIFT);
1411 if (wc & OFPFW_NW_DST_MASK) {
1412 m->nw_dst &= flow_nw_bits_to_mask(wc, OFPFW_NW_DST_SHIFT);
1415 /* Network and transport layer fields will always be extracted as
1416 * zeros, so we can do an exact-m on those values. */
1417 wc &= ~(OFPFW_NW | OFPFW_TP);
1418 m->nw_proto = m->nw_src = m->nw_dst = 0;
1419 m->tp_src = m->tp_dst = 0;
1421 if (wc & OFPFW_DL_SRC) {
1422 memset(m->dl_src, 0, sizeof m->dl_src);
1424 if (wc & OFPFW_DL_DST) {
1425 memset(m->dl_dst, 0, sizeof m->dl_dst);
1427 m->wildcards = htonl(wc);
1431 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
1432 const char *name, bool reconnectable)
1434 vconn->class = class;
1435 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1436 : !connect_status ? VCS_SEND_HELLO
1437 : VCS_DISCONNECTED);
1438 vconn->error = connect_status;
1439 vconn->version = -1;
1440 vconn->min_version = -1;
1441 vconn->remote_ip = 0;
1442 vconn->remote_port = 0;
1443 vconn->local_ip = 0;
1444 vconn->local_port = 0;
1445 vconn->name = xstrdup(name);
1446 vconn->reconnectable = reconnectable;
1450 vconn_set_remote_ip(struct vconn *vconn, uint32_t ip)
1452 vconn->remote_ip = ip;
1456 vconn_set_remote_port(struct vconn *vconn, uint16_t port)
1458 vconn->remote_port = port;
1462 vconn_set_local_ip(struct vconn *vconn, uint32_t ip)
1464 vconn->local_ip = ip;
1468 vconn_set_local_port(struct vconn *vconn, uint16_t port)
1470 vconn->local_port = port;
1474 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
1477 pvconn->class = class;
1478 pvconn->name = xstrdup(name);