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.
38 #include <netinet/in.h>
44 #include "ofp-print.h"
46 #include "poll-loop.h"
50 #define THIS_MODULE VLM_vconn
53 static struct vconn_class *vconn_classes[] = {
65 /* Check the validity of the vconn class structures. */
67 check_vconn_classes(void)
72 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
73 struct vconn_class *class = vconn_classes[i];
74 assert(class->name != NULL);
75 assert(class->open != NULL);
76 assert(class->close != NULL);
78 ? !class->recv && !class->send
79 : class->recv && class->send);
80 assert(class->wait != NULL);
85 /* Prints information on active (if 'active') and passive (if 'passive')
86 * connection methods supported by the vconn. */
88 vconn_usage(bool active, bool passive)
90 /* Really this should be implemented via callbacks into the vconn
91 * providers, but that seems too heavy-weight to bother with at the
96 printf("Active OpenFlow connection methods:\n");
99 "local datapath DP_IDX\n");
101 printf(" tcp:HOST[:PORT] "
102 "PORT (default: %d) on remote TCP HOST\n", OFP_TCP_PORT);
104 printf(" ssl:HOST[:PORT] "
105 "SSL PORT (default: %d) on remote HOST\n", OFP_SSL_PORT);
110 printf("Passive OpenFlow connection methods:\n");
111 printf(" ptcp:[PORT] "
112 "listen to TCP PORT (default: %d)\n",
115 printf(" pssl:[PORT] "
116 "listen for SSL on PORT (default: %d)\n",
122 printf("PKI configuration (required to use SSL):\n"
123 " -p, --private-key=FILE file with private key\n"
124 " -c, --certificate=FILE file with certificate for private key\n"
125 " -C, --ca-cert=FILE file with peer CA certificate\n");
129 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
130 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
131 * vconn class-specific.
133 * Returns 0 if successful, otherwise a positive errno value. If successful,
134 * stores a pointer to the new connection in '*vconnp', otherwise a null
137 vconn_open(const char *name, struct vconn **vconnp)
142 check_vconn_classes();
144 prefix_len = strcspn(name, ":");
145 if (prefix_len == strlen(name)) {
146 error(0, "`%s' not correct format for peer name", name);
149 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
150 struct vconn_class *class = vconn_classes[i];
151 if (strlen(class->name) == prefix_len
152 && !memcmp(class->name, name, prefix_len)) {
153 char *suffix_copy = xstrdup(name + prefix_len + 1);
154 int retval = class->open(name, suffix_copy, vconnp);
159 assert((*vconnp)->connect_status != EAGAIN
160 || (*vconnp)->class->connect);
165 error(0, "unknown peer type `%.*s'", (int) prefix_len, name);
170 vconn_open_block(const char *name, struct vconn **vconnp)
175 error = vconn_open(name, &vconn);
176 while (error == EAGAIN) {
177 vconn_connect_wait(vconn);
179 error = vconn_connect(vconn);
180 assert(error != EINPROGRESS);
191 /* Closes 'vconn'. */
193 vconn_close(struct vconn *vconn)
196 (vconn->class->close)(vconn);
200 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
201 * wait for connections to arrive, not to transfer data. Returns false if
202 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
203 * to wait for new connections to arrive. */
205 vconn_is_passive(const struct vconn *vconn)
207 return vconn->class->accept != NULL;
210 /* Returns the IP address of the peer, or 0 if the peer is not connected over
211 * an IP-based protocol or if its IP address is not yet known. */
213 vconn_get_ip(const struct vconn *vconn)
218 /* Tries to complete the connection on 'vconn', which must be an active
219 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
220 * was successful or a positive errno value if it failed. If the
221 * connection is still in progress, returns EAGAIN. */
223 vconn_connect(struct vconn *vconn)
225 if (vconn->connect_status == EAGAIN) {
226 vconn->connect_status = (vconn->class->connect)(vconn);
227 assert(vconn->connect_status != EINPROGRESS);
229 return vconn->connect_status;
232 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
233 * If successful, stores the new connection in '*new_vconn' and returns 0.
234 * Otherwise, returns a positive errno value.
236 * vconn_accept will not block waiting for a connection. If no connection is
237 * ready to be accepted, it returns EAGAIN immediately. */
239 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
243 retval = (vconn->class->accept)(vconn, new_vconn);
248 assert((*new_vconn)->connect_status != EAGAIN
249 || (*new_vconn)->class->connect);
254 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
255 * vconn. If successful, stores the received message into '*msgp' and returns
256 * 0. The caller is responsible for destroying the message with
257 * buffer_delete(). On failure, returns a positive errno value and stores a
258 * null pointer into '*msgp'. On normal connection close, returns EOF.
260 * vconn_recv will not block waiting for a packet to arrive. If no packets
261 * have been received, it returns EAGAIN immediately. */
263 vconn_recv(struct vconn *vconn, struct buffer **msgp)
265 int retval = vconn_connect(vconn);
267 retval = (vconn->class->recv)(vconn, msgp);
268 if (VLOG_IS_DBG_ENABLED() && !retval) {
269 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
270 VLOG_DBG("received: %s", s);
280 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
281 * vconn. If successful, returns 0, in which case ownership of 'msg' is
282 * transferred to the vconn. Success does not guarantee that 'msg' has been or
283 * ever will be delivered to the peer, only that it has been queued for
286 * Returns a positive errno value on failure, in which case the caller
287 * retains ownership of 'msg'.
289 * vconn_send will not block. If 'msg' cannot be immediately accepted for
290 * transmission, it returns EAGAIN immediately. */
292 vconn_send(struct vconn *vconn, struct buffer *msg)
294 int retval = vconn_connect(vconn);
296 assert(msg->size >= sizeof(struct ofp_header));
297 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
298 if (!VLOG_IS_DBG_ENABLED()) {
299 retval = (vconn->class->send)(vconn, msg);
301 char *s = ofp_to_string(msg->data, msg->size, 1);
302 retval = (vconn->class->send)(vconn, msg);
303 if (retval != EAGAIN) {
304 VLOG_DBG("sent (%s): %s", strerror(retval), s);
312 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
314 vconn_send_block(struct vconn *vconn, struct buffer *msg)
317 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
318 vconn_send_wait(vconn);
319 VLOG_DBG("blocking on vconn send");
325 /* Same as vconn_recv, except that it waits until a message is received. */
327 vconn_recv_block(struct vconn *vconn, struct buffer **msgp)
330 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
331 vconn_recv_wait(vconn);
332 VLOG_DBG("blocking on vconn receive");
338 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
339 * matching transaction ID. Returns 0 if successful, in which case the reply
340 * is stored in '*replyp' for the caller to examine and free. Otherwise
341 * returns a positive errno value, or EOF, and sets '*replyp' to null.
343 * 'request' is always destroyed, regardless of the return value. */
345 vconn_transact(struct vconn *vconn, struct buffer *request,
346 struct buffer **replyp)
348 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
352 error = vconn_send_block(vconn, request);
354 buffer_delete(request);
359 struct buffer *reply;
361 error = vconn_recv_block(vconn, &reply);
365 recv_xid = ((struct ofp_header *) reply->data)->xid;
366 if (send_xid == recv_xid) {
371 VLOG_DBG("received reply with xid %08"PRIx32" != expected %08"PRIx32,
373 buffer_delete(reply);
378 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
382 assert(vconn_is_passive(vconn)
383 ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
384 : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
386 connect_status = vconn_connect(vconn);
387 if (connect_status) {
388 if (connect_status == EAGAIN) {
391 poll_immediate_wake();
396 (vconn->class->wait)(vconn, wait);
400 vconn_connect_wait(struct vconn *vconn)
402 vconn_wait(vconn, WAIT_CONNECT);
406 vconn_accept_wait(struct vconn *vconn)
408 vconn_wait(vconn, WAIT_ACCEPT);
412 vconn_recv_wait(struct vconn *vconn)
414 vconn_wait(vconn, WAIT_RECV);
418 vconn_send_wait(struct vconn *vconn)
420 vconn_wait(vconn, WAIT_SEND);
423 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
424 * containing an OpenFlow header with the given 'type' and a random transaction
425 * id. Stores the new buffer in '*bufferp'. The caller must free the buffer
426 * when it is no longer needed. */
428 make_openflow(size_t openflow_len, uint8_t type, struct buffer **bufferp)
430 return make_openflow_xid(openflow_len, type, random_uint32(), bufferp);
433 /* Allocates and returns the first byte of a buffer 'openflow_len' bytes long,
434 * containing an OpenFlow header with the given 'type' and transaction id
435 * 'xid'. Stores the new buffer in '*bufferp'. The caller must free the
436 * buffer when it is no longer needed. */
438 make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid,
439 struct buffer **bufferp)
441 struct buffer *buffer;
442 struct ofp_header *oh;
444 assert(openflow_len >= sizeof *oh);
445 assert(openflow_len <= UINT16_MAX);
446 buffer = *bufferp = buffer_new(openflow_len);
447 oh = buffer_put_uninit(buffer, openflow_len);
448 memset(oh, 0, openflow_len);
449 oh->version = OFP_VERSION;
451 oh->length = htons(openflow_len);
456 /* Updates the 'length' field of the OpenFlow message in 'buffer' to
459 update_openflow_length(struct buffer *buffer)
461 struct ofp_header *oh = buffer_at_assert(buffer, 0, sizeof *oh);
462 oh->length = htons(buffer->size);
466 make_add_simple_flow(const struct flow *flow,
467 uint32_t buffer_id, uint16_t out_port, uint16_t max_idle)
469 struct ofp_flow_mod *ofm;
470 size_t size = sizeof *ofm + sizeof ofm->actions[0];
471 struct buffer *out = buffer_new(size);
472 ofm = buffer_put_uninit(out, size);
473 memset(ofm, 0, size);
474 ofm->header.version = OFP_VERSION;
475 ofm->header.type = OFPT_FLOW_MOD;
476 ofm->header.length = htons(size);
477 ofm->match.wildcards = htons(0);
478 ofm->match.in_port = flow->in_port;
479 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
480 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
481 ofm->match.dl_vlan = flow->dl_vlan;
482 ofm->match.dl_type = flow->dl_type;
483 ofm->match.nw_src = flow->nw_src;
484 ofm->match.nw_dst = flow->nw_dst;
485 ofm->match.nw_proto = flow->nw_proto;
486 ofm->match.tp_src = flow->tp_src;
487 ofm->match.tp_dst = flow->tp_dst;
488 ofm->command = htons(OFPFC_ADD);
489 ofm->max_idle = htons(max_idle);
490 ofm->buffer_id = htonl(buffer_id);
491 ofm->actions[0].type = htons(OFPAT_OUTPUT);
492 ofm->actions[0].arg.output.max_len = htons(0);
493 ofm->actions[0].arg.output.port = htons(out_port);
498 make_unbuffered_packet_out(const struct buffer *packet,
499 uint16_t in_port, uint16_t out_port)
501 struct ofp_packet_out *opo;
502 size_t size = sizeof *opo + packet->size;
503 struct buffer *out = buffer_new(size);
504 opo = buffer_put_uninit(out, size);
505 memset(opo, 0, sizeof *opo);
506 opo->header.version = OFP_VERSION;
507 opo->header.type = OFPT_PACKET_OUT;
508 opo->header.length = htons(size);
509 opo->buffer_id = htonl(UINT32_MAX);
510 opo->in_port = htons(in_port);
511 opo->out_port = htons(out_port);
512 memcpy(opo->u.data, packet->data, packet->size);
517 make_buffered_packet_out(uint32_t buffer_id,
518 uint16_t in_port, uint16_t out_port)
520 struct ofp_packet_out *opo;
521 size_t size = sizeof *opo + sizeof opo->u.actions[0];
522 struct buffer *out = buffer_new(size);
523 opo = buffer_put_uninit(out, size);
524 memset(opo, 0, size);
525 opo->header.version = OFP_VERSION;
526 opo->header.type = OFPT_PACKET_OUT;
527 opo->header.length = htons(size);
528 opo->buffer_id = htonl(buffer_id);
529 opo->in_port = htons(in_port);
530 opo->out_port = htons(out_port);
531 opo->u.actions[0].type = htons(OFPAT_OUTPUT);
532 opo->u.actions[0].arg.output.max_len = htons(0);
533 opo->u.actions[0].arg.output.port = htons(out_port);
537 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
539 make_echo_request(void)
541 struct ofp_header *rq;
542 struct buffer *out = buffer_new(sizeof *rq);
543 rq = buffer_put_uninit(out, sizeof *rq);
544 rq->version = OFP_VERSION;
545 rq->type = OFPT_ECHO_REQUEST;
546 rq->length = htons(sizeof *rq);
551 /* Creates and returns an OFPT_ECHO_REPLY message matching the
552 * OFPT_ECHO_REQUEST message in 'rq'. */
554 make_echo_reply(const struct ofp_header *rq)
556 size_t size = ntohs(rq->length);
557 struct buffer *out = buffer_new(size);
558 struct ofp_header *reply = buffer_put(out, rq, size);
559 reply->type = OFPT_ECHO_REPLY;