1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
39 #include <netinet/in.h>
45 #include "ofp-print.h"
47 #include "poll-loop.h"
51 #define THIS_MODULE VLM_vconn
54 static struct vconn_class *vconn_classes[] = {
68 /* High rate limit because most of the rate-limiting here is individual
69 * OpenFlow messages going over the vconn. If those are enabled then we
70 * really need to see them. */
71 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(600, 600);
73 /* Check the validity of the vconn class structures. */
75 check_vconn_classes(void)
80 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
81 struct vconn_class *class = vconn_classes[i];
82 assert(class->name != NULL);
83 assert(class->open != NULL);
84 if (class->close || class->accept || class->recv || class->send
86 assert(class->close != NULL);
88 ? !class->recv && !class->send
89 : class->recv && class->send);
90 assert(class->wait != NULL);
92 /* This class delegates to another one. */
98 /* Prints information on active (if 'active') and passive (if 'passive')
99 * connection methods supported by the vconn. */
101 vconn_usage(bool active, bool passive)
103 /* Really this should be implemented via callbacks into the vconn
104 * providers, but that seems too heavy-weight to bother with at the
109 printf("Active OpenFlow connection methods:\n");
112 "local datapath DP_IDX\n");
114 printf(" tcp:HOST[:PORT] "
115 "PORT (default: %d) on remote TCP HOST\n", OFP_TCP_PORT);
117 printf(" ssl:HOST[:PORT] "
118 "SSL PORT (default: %d) on remote HOST\n", OFP_SSL_PORT);
120 printf(" unix:FILE Unix domain socket named FILE\n");
124 printf("Passive OpenFlow connection methods:\n");
125 printf(" ptcp:[PORT] "
126 "listen to TCP PORT (default: %d)\n",
129 printf(" pssl:[PORT] "
130 "listen for SSL on PORT (default: %d)\n",
133 printf(" punix:FILE "
134 "listen on Unix domain socket FILE\n");
138 printf("PKI configuration (required to use SSL):\n"
139 " -p, --private-key=FILE file with private key\n"
140 " -c, --certificate=FILE file with certificate for private key\n"
141 " -C, --ca-cert=FILE file with peer CA certificate\n");
145 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
146 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
147 * vconn class-specific.
149 * Returns 0 if successful, otherwise a positive errno value. If successful,
150 * stores a pointer to the new connection in '*vconnp', otherwise a null
153 vconn_open(const char *name, struct vconn **vconnp)
158 check_vconn_classes();
161 prefix_len = strcspn(name, ":");
162 if (prefix_len == strlen(name)) {
163 error(0, "`%s' not correct format for peer name", name);
166 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
167 struct vconn_class *class = vconn_classes[i];
168 if (strlen(class->name) == prefix_len
169 && !memcmp(class->name, name, prefix_len)) {
171 char *suffix_copy = xstrdup(name + prefix_len + 1);
172 int retval = class->open(name, suffix_copy, &vconn);
175 assert(vconn->connect_status != EAGAIN
176 || vconn->class->connect);
182 error(0, "unknown peer type `%.*s'", (int) prefix_len, name);
187 vconn_open_block(const char *name, struct vconn **vconnp)
192 error = vconn_open(name, &vconn);
193 while (error == EAGAIN) {
194 vconn_connect_wait(vconn);
196 error = vconn_connect(vconn);
197 assert(error != EINPROGRESS);
208 /* Closes 'vconn'. */
210 vconn_close(struct vconn *vconn)
213 (vconn->class->close)(vconn);
217 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
218 * wait for connections to arrive, not to transfer data. Returns false if
219 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
220 * to wait for new connections to arrive. */
222 vconn_is_passive(const struct vconn *vconn)
224 return vconn->class->accept != NULL;
227 /* Returns the IP address of the peer, or 0 if the peer is not connected over
228 * an IP-based protocol or if its IP address is not yet known. */
230 vconn_get_ip(const struct vconn *vconn)
235 /* Tries to complete the connection on 'vconn', which must be an active
236 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
237 * was successful or a positive errno value if it failed. If the
238 * connection is still in progress, returns EAGAIN. */
240 vconn_connect(struct vconn *vconn)
242 if (vconn->connect_status == EAGAIN) {
243 vconn->connect_status = (vconn->class->connect)(vconn);
244 assert(vconn->connect_status != EINPROGRESS);
246 return vconn->connect_status;
249 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
250 * If successful, stores the new connection in '*new_vconn' and returns 0.
251 * Otherwise, returns a positive errno value.
253 * vconn_accept will not block waiting for a connection. If no connection is
254 * ready to be accepted, it returns EAGAIN immediately. */
256 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
260 retval = (vconn->class->accept)(vconn, new_vconn);
265 assert((*new_vconn)->connect_status != EAGAIN
266 || (*new_vconn)->class->connect);
271 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
272 * vconn. If successful, stores the received message into '*msgp' and returns
273 * 0. The caller is responsible for destroying the message with
274 * buffer_delete(). On failure, returns a positive errno value and stores a
275 * null pointer into '*msgp'. On normal connection close, returns EOF.
277 * vconn_recv will not block waiting for a packet to arrive. If no packets
278 * have been received, it returns EAGAIN immediately. */
280 vconn_recv(struct vconn *vconn, struct buffer **msgp)
282 int retval = vconn_connect(vconn);
284 retval = (vconn->class->recv)(vconn, msgp);
286 struct ofp_header *oh;
288 if (VLOG_IS_DBG_ENABLED()) {
289 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
290 VLOG_DBG_RL(&rl, "received: %s", s);
294 oh = buffer_at_assert(*msgp, 0, sizeof *oh);
295 if (oh->version != OFP_VERSION) {
296 VLOG_ERR_RL(&rl, "received OpenFlow version %02"PRIx8" "
298 oh->version, OFP_VERSION);
299 buffer_delete(*msgp);
311 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
312 * vconn. If successful, returns 0, in which case ownership of 'msg' is
313 * transferred to the vconn. Success does not guarantee that 'msg' has been or
314 * ever will be delivered to the peer, only that it has been queued for
317 * Returns a positive errno value on failure, in which case the caller
318 * retains ownership of 'msg'.
320 * vconn_send will not block. If 'msg' cannot be immediately accepted for
321 * transmission, it returns EAGAIN immediately. */
323 vconn_send(struct vconn *vconn, struct buffer *msg)
325 int retval = vconn_connect(vconn);
327 assert(msg->size >= sizeof(struct ofp_header));
328 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
329 if (!VLOG_IS_DBG_ENABLED()) {
330 retval = (vconn->class->send)(vconn, msg);
332 char *s = ofp_to_string(msg->data, msg->size, 1);
333 retval = (vconn->class->send)(vconn, msg);
334 if (retval != EAGAIN) {
335 VLOG_DBG_RL(&rl, "sent (%s): %s", strerror(retval), s);
343 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
345 vconn_send_block(struct vconn *vconn, struct buffer *msg)
348 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
349 vconn_send_wait(vconn);
355 /* Same as vconn_recv, except that it waits until a message is received. */
357 vconn_recv_block(struct vconn *vconn, struct buffer **msgp)
360 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
361 vconn_recv_wait(vconn);
367 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
368 * matching transaction ID. Returns 0 if successful, in which case the reply
369 * is stored in '*replyp' for the caller to examine and free. Otherwise
370 * returns a positive errno value, or EOF, and sets '*replyp' to null.
372 * 'request' is always destroyed, regardless of the return value. */
374 vconn_transact(struct vconn *vconn, struct buffer *request,
375 struct buffer **replyp)
377 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
381 error = vconn_send_block(vconn, request);
383 buffer_delete(request);
388 struct buffer *reply;
390 error = vconn_recv_block(vconn, &reply);
394 recv_xid = ((struct ofp_header *) reply->data)->xid;
395 if (send_xid == recv_xid) {
400 VLOG_DBG_RL(&rl, "received reply with xid %08"PRIx32" != expected "
401 "%08"PRIx32, recv_xid, send_xid);
402 buffer_delete(reply);
407 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
411 assert(vconn_is_passive(vconn)
412 ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
413 : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
415 connect_status = vconn_connect(vconn);
416 if (connect_status) {
417 if (connect_status == EAGAIN) {
420 poll_immediate_wake();
425 (vconn->class->wait)(vconn, wait);
429 vconn_connect_wait(struct vconn *vconn)
431 vconn_wait(vconn, WAIT_CONNECT);
435 vconn_accept_wait(struct vconn *vconn)
437 vconn_wait(vconn, WAIT_ACCEPT);
441 vconn_recv_wait(struct vconn *vconn)
443 vconn_wait(vconn, WAIT_RECV);
447 vconn_send_wait(struct vconn *vconn)
449 vconn_wait(vconn, WAIT_SEND);
452 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
453 * containing an OpenFlow header with the given 'type' and a random transaction
454 * id. Stores the new buffer in '*bufferp'. The caller must free the buffer
455 * when it is no longer needed. */
457 make_openflow(size_t openflow_len, uint8_t type, struct buffer **bufferp)
459 return make_openflow_xid(openflow_len, type, random_uint32(), bufferp);
462 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
463 * containing an OpenFlow header with the given 'type' and transaction id
464 * 'xid'. Stores the new buffer in '*bufferp'. The caller must free the
465 * buffer when it is no longer needed. */
467 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
468 struct buffer **bufferp)
470 struct buffer *buffer;
471 struct ofp_header *oh;
473 assert(openflow_len >= sizeof *oh);
474 assert(openflow_len <= UINT16_MAX);
475 buffer = *bufferp = buffer_new(openflow_len);
476 oh = buffer_put_uninit(buffer, openflow_len);
477 memset(oh, 0, openflow_len);
478 oh->version = OFP_VERSION;
480 oh->length = htons(openflow_len);
485 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
488 update_openflow_length(struct buffer *buffer)
490 struct ofp_header *oh = buffer_at_assert(buffer, 0, sizeof *oh);
491 oh->length = htons(buffer->size);
495 make_add_flow(const struct flow *flow, uint32_t buffer_id,
496 uint16_t idle_timeout, size_t n_actions)
498 struct ofp_flow_mod *ofm;
499 size_t size = sizeof *ofm + n_actions * sizeof ofm->actions[0];
500 struct buffer *out = buffer_new(size);
501 ofm = buffer_put_uninit(out, size);
502 memset(ofm, 0, size);
503 ofm->header.version = OFP_VERSION;
504 ofm->header.type = OFPT_FLOW_MOD;
505 ofm->header.length = htons(size);
506 ofm->match.wildcards = htonl(0);
507 ofm->match.in_port = flow->in_port;
508 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
509 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
510 ofm->match.dl_vlan = flow->dl_vlan;
511 ofm->match.dl_type = flow->dl_type;
512 ofm->match.nw_src = flow->nw_src;
513 ofm->match.nw_dst = flow->nw_dst;
514 ofm->match.nw_proto = flow->nw_proto;
515 ofm->match.tp_src = flow->tp_src;
516 ofm->match.tp_dst = flow->tp_dst;
517 ofm->command = htons(OFPFC_ADD);
518 ofm->idle_timeout = htons(idle_timeout);
519 ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
520 ofm->buffer_id = htonl(buffer_id);
525 make_add_simple_flow(const struct flow *flow,
526 uint32_t buffer_id, uint16_t out_port,
527 uint16_t idle_timeout)
529 struct buffer *buffer = make_add_flow(flow, buffer_id, idle_timeout, 1);
530 struct ofp_flow_mod *ofm = buffer->data;
531 ofm->actions[0].type = htons(OFPAT_OUTPUT);
532 ofm->actions[0].arg.output.max_len = htons(0);
533 ofm->actions[0].arg.output.port = htons(out_port);
538 make_unbuffered_packet_out(const struct buffer *packet,
539 uint16_t in_port, uint16_t out_port)
541 struct ofp_packet_out *opo;
542 size_t size = sizeof *opo + sizeof opo->actions[0];
543 struct buffer *out = buffer_new(size + packet->size);
544 opo = buffer_put_uninit(out, size);
545 memset(opo, 0, sizeof *opo);
546 opo->header.version = OFP_VERSION;
547 opo->header.type = OFPT_PACKET_OUT;
548 opo->buffer_id = htonl(UINT32_MAX);
549 opo->in_port = htons(in_port);
550 opo->n_actions = htons(1);
551 opo->actions[0].type = htons(OFPAT_OUTPUT);
552 opo->actions[0].arg.output.max_len = htons(0);
553 opo->actions[0].arg.output.port = htons(out_port);
554 buffer_put(out, packet->data, packet->size);
555 update_openflow_length(out);
560 make_buffered_packet_out(uint32_t buffer_id,
561 uint16_t in_port, uint16_t out_port)
563 struct ofp_packet_out *opo;
564 size_t size = sizeof *opo + sizeof opo->actions[0];
565 struct buffer *out = buffer_new(size);
566 opo = buffer_put_uninit(out, size);
567 memset(opo, 0, size);
568 opo->header.version = OFP_VERSION;
569 opo->header.type = OFPT_PACKET_OUT;
570 opo->header.length = htons(size);
571 opo->buffer_id = htonl(buffer_id);
572 opo->in_port = htons(in_port);
573 opo->n_actions = htons(1);
574 opo->actions[0].type = htons(OFPAT_OUTPUT);
575 opo->actions[0].arg.output.max_len = htons(0);
576 opo->actions[0].arg.output.port = htons(out_port);
580 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
582 make_echo_request(void)
584 struct ofp_header *rq;
585 struct buffer *out = buffer_new(sizeof *rq);
586 rq = buffer_put_uninit(out, sizeof *rq);
587 rq->version = OFP_VERSION;
588 rq->type = OFPT_ECHO_REQUEST;
589 rq->length = htons(sizeof *rq);
594 /* Creates and returns an OFPT_ECHO_REPLY message matching the
595 * OFPT_ECHO_REQUEST message in 'rq'. */
597 make_echo_reply(const struct ofp_header *rq)
599 size_t size = ntohs(rq->length);
600 struct buffer *out = buffer_new(size);
601 struct ofp_header *reply = buffer_put(out, rq, size);
602 reply->type = OFPT_ECHO_REPLY;