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 /* Check the validity of the vconn class structures. */
70 check_vconn_classes(void)
75 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
76 struct vconn_class *class = vconn_classes[i];
77 assert(class->name != NULL);
78 assert(class->open != NULL);
79 if (class->close || class->accept || class->recv || class->send
81 assert(class->close != NULL);
83 ? !class->recv && !class->send
84 : class->recv && class->send);
85 assert(class->wait != NULL);
87 /* This class delegates to another one. */
93 /* Prints information on active (if 'active') and passive (if 'passive')
94 * connection methods supported by the vconn. */
96 vconn_usage(bool active, bool passive)
98 /* Really this should be implemented via callbacks into the vconn
99 * providers, but that seems too heavy-weight to bother with at the
104 printf("Active OpenFlow connection methods:\n");
107 "local datapath DP_IDX\n");
109 printf(" tcp:HOST[:PORT] "
110 "PORT (default: %d) on remote TCP HOST\n", OFP_TCP_PORT);
112 printf(" ssl:HOST[:PORT] "
113 "SSL PORT (default: %d) on remote HOST\n", OFP_SSL_PORT);
115 printf(" unix:FILE Unix domain socket named FILE\n");
119 printf("Passive OpenFlow connection methods:\n");
120 printf(" ptcp:[PORT] "
121 "listen to TCP PORT (default: %d)\n",
124 printf(" pssl:[PORT] "
125 "listen for SSL on PORT (default: %d)\n",
128 printf(" punix:FILE "
129 "listen on Unix domain socket FILE\n");
133 printf("PKI configuration (required to use SSL):\n"
134 " -p, --private-key=FILE file with private key\n"
135 " -c, --certificate=FILE file with certificate for private key\n"
136 " -C, --ca-cert=FILE file with peer CA certificate\n");
140 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
141 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
142 * vconn class-specific.
144 * Returns 0 if successful, otherwise a positive errno value. If successful,
145 * stores a pointer to the new connection in '*vconnp', otherwise a null
148 vconn_open(const char *name, struct vconn **vconnp)
153 check_vconn_classes();
155 prefix_len = strcspn(name, ":");
156 if (prefix_len == strlen(name)) {
157 error(0, "`%s' not correct format for peer name", name);
160 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
161 struct vconn_class *class = vconn_classes[i];
162 if (strlen(class->name) == prefix_len
163 && !memcmp(class->name, name, prefix_len)) {
164 char *suffix_copy = xstrdup(name + prefix_len + 1);
165 int retval = class->open(name, suffix_copy, vconnp);
170 assert((*vconnp)->connect_status != EAGAIN
171 || (*vconnp)->class->connect);
176 error(0, "unknown peer type `%.*s'", (int) prefix_len, name);
181 vconn_open_block(const char *name, struct vconn **vconnp)
186 error = vconn_open(name, &vconn);
187 while (error == EAGAIN) {
188 vconn_connect_wait(vconn);
190 error = vconn_connect(vconn);
191 assert(error != EINPROGRESS);
202 /* Closes 'vconn'. */
204 vconn_close(struct vconn *vconn)
207 (vconn->class->close)(vconn);
211 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
212 * wait for connections to arrive, not to transfer data. Returns false if
213 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
214 * to wait for new connections to arrive. */
216 vconn_is_passive(const struct vconn *vconn)
218 return vconn->class->accept != NULL;
221 /* Returns the IP address of the peer, or 0 if the peer is not connected over
222 * an IP-based protocol or if its IP address is not yet known. */
224 vconn_get_ip(const struct vconn *vconn)
229 /* Tries to complete the connection on 'vconn', which must be an active
230 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
231 * was successful or a positive errno value if it failed. If the
232 * connection is still in progress, returns EAGAIN. */
234 vconn_connect(struct vconn *vconn)
236 if (vconn->connect_status == EAGAIN) {
237 vconn->connect_status = (vconn->class->connect)(vconn);
238 assert(vconn->connect_status != EINPROGRESS);
240 return vconn->connect_status;
243 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
244 * If successful, stores the new connection in '*new_vconn' and returns 0.
245 * Otherwise, returns a positive errno value.
247 * vconn_accept will not block waiting for a connection. If no connection is
248 * ready to be accepted, it returns EAGAIN immediately. */
250 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
254 retval = (vconn->class->accept)(vconn, new_vconn);
259 assert((*new_vconn)->connect_status != EAGAIN
260 || (*new_vconn)->class->connect);
265 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
266 * vconn. If successful, stores the received message into '*msgp' and returns
267 * 0. The caller is responsible for destroying the message with
268 * buffer_delete(). On failure, returns a positive errno value and stores a
269 * null pointer into '*msgp'. On normal connection close, returns EOF.
271 * vconn_recv will not block waiting for a packet to arrive. If no packets
272 * have been received, it returns EAGAIN immediately. */
274 vconn_recv(struct vconn *vconn, struct buffer **msgp)
276 int retval = vconn_connect(vconn);
278 retval = (vconn->class->recv)(vconn, msgp);
280 struct ofp_header *oh;
282 if (VLOG_IS_DBG_ENABLED()) {
283 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
284 VLOG_DBG("received: %s", s);
288 oh = buffer_at_assert(*msgp, 0, sizeof *oh);
289 if (oh->version != OFP_VERSION) {
290 VLOG_ERR("received OpenFlow version %02"PRIx8" "
292 oh->version, OFP_VERSION);
293 buffer_delete(*msgp);
305 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
306 * vconn. If successful, returns 0, in which case ownership of 'msg' is
307 * transferred to the vconn. Success does not guarantee that 'msg' has been or
308 * ever will be delivered to the peer, only that it has been queued for
311 * Returns a positive errno value on failure, in which case the caller
312 * retains ownership of 'msg'.
314 * vconn_send will not block. If 'msg' cannot be immediately accepted for
315 * transmission, it returns EAGAIN immediately. */
317 vconn_send(struct vconn *vconn, struct buffer *msg)
319 int retval = vconn_connect(vconn);
321 assert(msg->size >= sizeof(struct ofp_header));
322 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
323 if (!VLOG_IS_DBG_ENABLED()) {
324 retval = (vconn->class->send)(vconn, msg);
326 char *s = ofp_to_string(msg->data, msg->size, 1);
327 retval = (vconn->class->send)(vconn, msg);
328 if (retval != EAGAIN) {
329 VLOG_DBG("sent (%s): %s", strerror(retval), s);
337 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
339 vconn_send_block(struct vconn *vconn, struct buffer *msg)
342 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
343 vconn_send_wait(vconn);
344 VLOG_DBG("blocking on vconn send");
350 /* Same as vconn_recv, except that it waits until a message is received. */
352 vconn_recv_block(struct vconn *vconn, struct buffer **msgp)
355 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
356 vconn_recv_wait(vconn);
357 VLOG_DBG("blocking on vconn receive");
363 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
364 * matching transaction ID. Returns 0 if successful, in which case the reply
365 * is stored in '*replyp' for the caller to examine and free. Otherwise
366 * returns a positive errno value, or EOF, and sets '*replyp' to null.
368 * 'request' is always destroyed, regardless of the return value. */
370 vconn_transact(struct vconn *vconn, struct buffer *request,
371 struct buffer **replyp)
373 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
377 error = vconn_send_block(vconn, request);
379 buffer_delete(request);
384 struct buffer *reply;
386 error = vconn_recv_block(vconn, &reply);
390 recv_xid = ((struct ofp_header *) reply->data)->xid;
391 if (send_xid == recv_xid) {
396 VLOG_DBG("received reply with xid %08"PRIx32" != expected %08"PRIx32,
398 buffer_delete(reply);
403 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
407 assert(vconn_is_passive(vconn)
408 ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
409 : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
411 connect_status = vconn_connect(vconn);
412 if (connect_status) {
413 if (connect_status == EAGAIN) {
416 poll_immediate_wake();
421 (vconn->class->wait)(vconn, wait);
425 vconn_connect_wait(struct vconn *vconn)
427 vconn_wait(vconn, WAIT_CONNECT);
431 vconn_accept_wait(struct vconn *vconn)
433 vconn_wait(vconn, WAIT_ACCEPT);
437 vconn_recv_wait(struct vconn *vconn)
439 vconn_wait(vconn, WAIT_RECV);
443 vconn_send_wait(struct vconn *vconn)
445 vconn_wait(vconn, WAIT_SEND);
448 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
449 * containing an OpenFlow header with the given 'type' and a random transaction
450 * id. Stores the new buffer in '*bufferp'. The caller must free the buffer
451 * when it is no longer needed. */
453 make_openflow(size_t openflow_len, uint8_t type, struct buffer **bufferp)
455 return make_openflow_xid(openflow_len, type, random_uint32(), bufferp);
458 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
459 * containing an OpenFlow header with the given 'type' and transaction id
460 * 'xid'. Stores the new buffer in '*bufferp'. The caller must free the
461 * buffer when it is no longer needed. */
463 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
464 struct buffer **bufferp)
466 struct buffer *buffer;
467 struct ofp_header *oh;
469 assert(openflow_len >= sizeof *oh);
470 assert(openflow_len <= UINT16_MAX);
471 buffer = *bufferp = buffer_new(openflow_len);
472 oh = buffer_put_uninit(buffer, openflow_len);
473 memset(oh, 0, openflow_len);
474 oh->version = OFP_VERSION;
476 oh->length = htons(openflow_len);
481 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
484 update_openflow_length(struct buffer *buffer)
486 struct ofp_header *oh = buffer_at_assert(buffer, 0, sizeof *oh);
487 oh->length = htons(buffer->size);
491 make_add_flow(const struct flow *flow, uint32_t buffer_id, uint16_t max_idle,
494 struct ofp_flow_mod *ofm;
495 size_t size = sizeof *ofm + n_actions * sizeof ofm->actions[0];
496 struct buffer *out = buffer_new(size);
497 ofm = buffer_put_uninit(out, size);
498 memset(ofm, 0, size);
499 ofm->header.version = OFP_VERSION;
500 ofm->header.type = OFPT_FLOW_MOD;
501 ofm->header.length = htons(size);
502 ofm->match.wildcards = htons(0);
503 ofm->match.in_port = flow->in_port;
504 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
505 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
506 ofm->match.dl_vlan = flow->dl_vlan;
507 ofm->match.dl_type = flow->dl_type;
508 ofm->match.nw_src = flow->nw_src;
509 ofm->match.nw_dst = flow->nw_dst;
510 ofm->match.nw_proto = flow->nw_proto;
511 ofm->match.tp_src = flow->tp_src;
512 ofm->match.tp_dst = flow->tp_dst;
513 ofm->command = htons(OFPFC_ADD);
514 ofm->max_idle = htons(max_idle);
515 ofm->buffer_id = htonl(buffer_id);
520 make_add_simple_flow(const struct flow *flow,
521 uint32_t buffer_id, uint16_t out_port, uint16_t max_idle)
523 struct buffer *buffer = make_add_flow(flow, buffer_id, max_idle, 1);
524 struct ofp_flow_mod *ofm = buffer->data;
525 ofm->actions[0].type = htons(OFPAT_OUTPUT);
526 ofm->actions[0].arg.output.max_len = htons(0);
527 ofm->actions[0].arg.output.port = htons(out_port);
532 make_unbuffered_packet_out(const struct buffer *packet,
533 uint16_t in_port, uint16_t out_port)
535 struct ofp_packet_out *opo;
536 size_t size = sizeof *opo + packet->size;
537 struct buffer *out = buffer_new(size);
538 opo = buffer_put_uninit(out, size);
539 memset(opo, 0, sizeof *opo);
540 opo->header.version = OFP_VERSION;
541 opo->header.type = OFPT_PACKET_OUT;
542 opo->header.length = htons(size);
543 opo->buffer_id = htonl(UINT32_MAX);
544 opo->in_port = htons(in_port);
545 opo->out_port = htons(out_port);
546 memcpy(opo->u.data, packet->data, packet->size);
551 make_buffered_packet_out(uint32_t buffer_id,
552 uint16_t in_port, uint16_t out_port)
554 struct ofp_packet_out *opo;
555 size_t size = sizeof *opo + sizeof opo->u.actions[0];
556 struct buffer *out = buffer_new(size);
557 opo = buffer_put_uninit(out, size);
558 memset(opo, 0, size);
559 opo->header.version = OFP_VERSION;
560 opo->header.type = OFPT_PACKET_OUT;
561 opo->header.length = htons(size);
562 opo->buffer_id = htonl(buffer_id);
563 opo->in_port = htons(in_port);
564 opo->out_port = htons(out_port);
565 opo->u.actions[0].type = htons(OFPAT_OUTPUT);
566 opo->u.actions[0].arg.output.max_len = htons(0);
567 opo->u.actions[0].arg.output.port = htons(out_port);
571 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
573 make_echo_request(void)
575 struct ofp_header *rq;
576 struct buffer *out = buffer_new(sizeof *rq);
577 rq = buffer_put_uninit(out, sizeof *rq);
578 rq->version = OFP_VERSION;
579 rq->type = OFPT_ECHO_REQUEST;
580 rq->length = htons(sizeof *rq);
585 /* Creates and returns an OFPT_ECHO_REPLY message matching the
586 * OFPT_ECHO_REQUEST message in 'rq'. */
588 make_echo_reply(const struct ofp_header *rq)
590 size_t size = ntohs(rq->length);
591 struct buffer *out = buffer_new(size);
592 struct ofp_header *reply = buffer_put(out, rq, size);
593 reply->type = OFPT_ECHO_REPLY;