]> git.proxmox.com Git - mirror_ovs.git/blob - lib/vconn.c
Begin breaking openflow-1.0.h into common and version-specific definitions.
[mirror_ovs.git] / lib / vconn.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "vconn-provider.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <poll.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "coverage.h"
27 #include "dynamic-string.h"
28 #include "fatal-signal.h"
29 #include "flow.h"
30 #include "ofp-errors.h"
31 #include "ofp-print.h"
32 #include "ofp-util.h"
33 #include "ofpbuf.h"
34 #include "openflow/nicira-ext.h"
35 #include "openflow/openflow.h"
36 #include "packets.h"
37 #include "poll-loop.h"
38 #include "random.h"
39 #include "util.h"
40 #include "vlog.h"
41
42 VLOG_DEFINE_THIS_MODULE(vconn);
43
44 COVERAGE_DEFINE(vconn_open);
45 COVERAGE_DEFINE(vconn_received);
46 COVERAGE_DEFINE(vconn_sent);
47
48 /* State of an active vconn.*/
49 enum vconn_state {
50 /* This is the ordinary progression of states. */
51 VCS_CONNECTING, /* Underlying vconn is not connected. */
52 VCS_SEND_HELLO, /* Waiting to send OFPT_HELLO message. */
53 VCS_RECV_HELLO, /* Waiting to receive OFPT_HELLO message. */
54 VCS_CONNECTED, /* Connection established. */
55
56 /* These states are entered only when something goes wrong. */
57 VCS_SEND_ERROR, /* Sending OFPT_ERROR message. */
58 VCS_DISCONNECTED /* Connection failed or connection closed. */
59 };
60
61 static struct vconn_class *vconn_classes[] = {
62 &tcp_vconn_class,
63 &unix_vconn_class,
64 #ifdef HAVE_OPENSSL
65 &ssl_vconn_class,
66 #endif
67 };
68
69 static struct pvconn_class *pvconn_classes[] = {
70 &ptcp_pvconn_class,
71 &punix_pvconn_class,
72 #ifdef HAVE_OPENSSL
73 &pssl_pvconn_class,
74 #endif
75 };
76
77 /* Rate limit for individual OpenFlow messages going over the vconn, output at
78 * DBG level. This is very high because, if these are enabled, it is because
79 * we really need to see them. */
80 static struct vlog_rate_limit ofmsg_rl = VLOG_RATE_LIMIT_INIT(600, 600);
81
82 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
83 * in the peer and so there's not much point in showing a lot of them. */
84 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
85
86 static int do_recv(struct vconn *, struct ofpbuf **);
87 static int do_send(struct vconn *, struct ofpbuf *);
88
89 /* Check the validity of the vconn class structures. */
90 static void
91 check_vconn_classes(void)
92 {
93 #ifndef NDEBUG
94 size_t i;
95
96 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
97 struct vconn_class *class = vconn_classes[i];
98 assert(class->name != NULL);
99 assert(class->open != NULL);
100 if (class->close || class->recv || class->send
101 || class->run || class->run_wait || class->wait) {
102 assert(class->close != NULL);
103 assert(class->recv != NULL);
104 assert(class->send != NULL);
105 assert(class->wait != NULL);
106 } else {
107 /* This class delegates to another one. */
108 }
109 }
110
111 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
112 struct pvconn_class *class = pvconn_classes[i];
113 assert(class->name != NULL);
114 assert(class->listen != NULL);
115 if (class->close || class->accept || class->wait) {
116 assert(class->close != NULL);
117 assert(class->accept != NULL);
118 assert(class->wait != NULL);
119 } else {
120 /* This class delegates to another one. */
121 }
122 }
123 #endif
124 }
125
126 /* Prints information on active (if 'active') and passive (if 'passive')
127 * connection methods supported by the vconn. If 'bootstrap' is true, also
128 * advertises options to bootstrap the CA certificate. */
129 void
130 vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED)
131 {
132 /* Really this should be implemented via callbacks into the vconn
133 * providers, but that seems too heavy-weight to bother with at the
134 * moment. */
135
136 printf("\n");
137 if (active) {
138 printf("Active OpenFlow connection methods:\n");
139 printf(" tcp:IP[:PORT] "
140 "PORT (default: %d) at remote IP\n", OFP_TCP_PORT);
141 #ifdef HAVE_OPENSSL
142 printf(" ssl:IP[:PORT] "
143 "SSL PORT (default: %d) at remote IP\n", OFP_SSL_PORT);
144 #endif
145 printf(" unix:FILE Unix domain socket named FILE\n");
146 }
147
148 if (passive) {
149 printf("Passive OpenFlow connection methods:\n");
150 printf(" ptcp:[PORT][:IP] "
151 "listen to TCP PORT (default: %d) on IP\n",
152 OFP_TCP_PORT);
153 #ifdef HAVE_OPENSSL
154 printf(" pssl:[PORT][:IP] "
155 "listen for SSL on PORT (default: %d) on IP\n",
156 OFP_SSL_PORT);
157 #endif
158 printf(" punix:FILE "
159 "listen on Unix domain socket FILE\n");
160 }
161
162 #ifdef HAVE_OPENSSL
163 printf("PKI configuration (required to use SSL):\n"
164 " -p, --private-key=FILE file with private key\n"
165 " -c, --certificate=FILE file with certificate for private key\n"
166 " -C, --ca-cert=FILE file with peer CA certificate\n");
167 if (bootstrap) {
168 printf(" --bootstrap-ca-cert=FILE file with peer CA certificate "
169 "to read or create\n");
170 }
171 #endif
172 }
173
174 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
175 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
176 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
177 * class exists. */
178 static int
179 vconn_lookup_class(const char *name, struct vconn_class **classp)
180 {
181 size_t prefix_len;
182
183 prefix_len = strcspn(name, ":");
184 if (name[prefix_len] != '\0') {
185 size_t i;
186
187 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
188 struct vconn_class *class = vconn_classes[i];
189 if (strlen(class->name) == prefix_len
190 && !memcmp(class->name, name, prefix_len)) {
191 *classp = class;
192 return 0;
193 }
194 }
195 }
196
197 *classp = NULL;
198 return EAFNOSUPPORT;
199 }
200
201 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
202 * a supported connection type, otherwise EAFNOSUPPORT. */
203 int
204 vconn_verify_name(const char *name)
205 {
206 struct vconn_class *class;
207 return vconn_lookup_class(name, &class);
208 }
209
210 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
211 * the form "TYPE:ARGS", where TYPE is an active vconn class's name and ARGS
212 * are vconn class-specific.
213 *
214 * The vconn will automatically negotiate an OpenFlow protocol version
215 * acceptable to both peers on the connection. The version negotiated will be
216 * no lower than 'min_version' and no higher than OFP10_VERSION.
217 *
218 * Returns 0 if successful, otherwise a positive errno value. If successful,
219 * stores a pointer to the new connection in '*vconnp', otherwise a null
220 * pointer. */
221 int
222 vconn_open(const char *name, int min_version, struct vconn **vconnp)
223 {
224 struct vconn_class *class;
225 struct vconn *vconn;
226 char *suffix_copy;
227 int error;
228
229 COVERAGE_INC(vconn_open);
230 check_vconn_classes();
231
232 /* Look up the class. */
233 error = vconn_lookup_class(name, &class);
234 if (!class) {
235 goto error;
236 }
237
238 /* Call class's "open" function. */
239 suffix_copy = xstrdup(strchr(name, ':') + 1);
240 error = class->open(name, suffix_copy, &vconn);
241 free(suffix_copy);
242 if (error) {
243 goto error;
244 }
245
246 /* Success. */
247 assert(vconn->state != VCS_CONNECTING || vconn->class->connect);
248 vconn->min_version = min_version;
249 *vconnp = vconn;
250 return 0;
251
252 error:
253 *vconnp = NULL;
254 return error;
255 }
256
257 /* Allows 'vconn' to perform maintenance activities, such as flushing output
258 * buffers. */
259 void
260 vconn_run(struct vconn *vconn)
261 {
262 if (vconn->class->run) {
263 (vconn->class->run)(vconn);
264 }
265 }
266
267 /* Arranges for the poll loop to wake up when 'vconn' needs to perform
268 * maintenance activities. */
269 void
270 vconn_run_wait(struct vconn *vconn)
271 {
272 if (vconn->class->run_wait) {
273 (vconn->class->run_wait)(vconn);
274 }
275 }
276
277 int
278 vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
279 {
280 struct vconn *vconn;
281 int error;
282
283 fatal_signal_run();
284
285 error = vconn_open(name, min_version, &vconn);
286 if (!error) {
287 while ((error = vconn_connect(vconn)) == EAGAIN) {
288 vconn_run(vconn);
289 vconn_run_wait(vconn);
290 vconn_connect_wait(vconn);
291 poll_block();
292 }
293 assert(error != EINPROGRESS);
294 }
295
296 if (error) {
297 vconn_close(vconn);
298 *vconnp = NULL;
299 } else {
300 *vconnp = vconn;
301 }
302 return error;
303 }
304
305 /* Closes 'vconn'. */
306 void
307 vconn_close(struct vconn *vconn)
308 {
309 if (vconn != NULL) {
310 char *name = vconn->name;
311 (vconn->class->close)(vconn);
312 free(name);
313 }
314 }
315
316 /* Returns the name of 'vconn', that is, the string passed to vconn_open(). */
317 const char *
318 vconn_get_name(const struct vconn *vconn)
319 {
320 return vconn->name;
321 }
322
323 /* Returns the IP address of the peer, or 0 if the peer is not connected over
324 * an IP-based protocol or if its IP address is not yet known. */
325 ovs_be32
326 vconn_get_remote_ip(const struct vconn *vconn)
327 {
328 return vconn->remote_ip;
329 }
330
331 /* Returns the transport port of the peer, or 0 if the connection does not
332 * contain a port or if the port is not yet known. */
333 ovs_be16
334 vconn_get_remote_port(const struct vconn *vconn)
335 {
336 return vconn->remote_port;
337 }
338
339 /* Returns the IP address used to connect to the peer, or 0 if the
340 * connection is not an IP-based protocol or if its IP address is not
341 * yet known. */
342 ovs_be32
343 vconn_get_local_ip(const struct vconn *vconn)
344 {
345 return vconn->local_ip;
346 }
347
348 /* Returns the transport port used to connect to the peer, or 0 if the
349 * connection does not contain a port or if the port is not yet known. */
350 ovs_be16
351 vconn_get_local_port(const struct vconn *vconn)
352 {
353 return vconn->local_port;
354 }
355
356 /* Returns the OpenFlow version negotiated with the peer, or -1 if version
357 * negotiation is not yet complete.
358 *
359 * A vconn that has successfully connected (that is, vconn_connect() or
360 * vconn_send() or vconn_recv() has returned 0) always negotiated a version. */
361 int
362 vconn_get_version(const struct vconn *vconn)
363 {
364 return vconn->version;
365 }
366
367 static void
368 vcs_connecting(struct vconn *vconn)
369 {
370 int retval = (vconn->class->connect)(vconn);
371 assert(retval != EINPROGRESS);
372 if (!retval) {
373 vconn->state = VCS_SEND_HELLO;
374 } else if (retval != EAGAIN) {
375 vconn->state = VCS_DISCONNECTED;
376 vconn->error = retval;
377 }
378 }
379
380 static void
381 vcs_send_hello(struct vconn *vconn)
382 {
383 struct ofpbuf *b;
384 int retval;
385
386 make_openflow(sizeof(struct ofp_header), OFPT_HELLO, &b);
387 retval = do_send(vconn, b);
388 if (!retval) {
389 vconn->state = VCS_RECV_HELLO;
390 } else {
391 ofpbuf_delete(b);
392 if (retval != EAGAIN) {
393 vconn->state = VCS_DISCONNECTED;
394 vconn->error = retval;
395 }
396 }
397 }
398
399 static void
400 vcs_recv_hello(struct vconn *vconn)
401 {
402 struct ofpbuf *b;
403 int retval;
404
405 retval = do_recv(vconn, &b);
406 if (!retval) {
407 struct ofp_header *oh = b->data;
408
409 if (oh->type == OFPT_HELLO) {
410 if (b->size > sizeof *oh) {
411 struct ds msg = DS_EMPTY_INITIALIZER;
412 ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name);
413 ds_put_hex_dump(&msg, b->data, b->size, 0, true);
414 VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg));
415 ds_destroy(&msg);
416 }
417
418 vconn->version = MIN(OFP10_VERSION, oh->version);
419 if (vconn->version < vconn->min_version) {
420 VLOG_WARN_RL(&bad_ofmsg_rl,
421 "%s: version negotiation failed: we support "
422 "versions 0x%02x to 0x%02x inclusive but peer "
423 "supports no later than version 0x%02"PRIx8,
424 vconn->name, vconn->min_version, OFP10_VERSION,
425 oh->version);
426 vconn->state = VCS_SEND_ERROR;
427 } else {
428 VLOG_DBG("%s: negotiated OpenFlow version 0x%02x "
429 "(we support versions 0x%02x to 0x%02x inclusive, "
430 "peer no later than version 0x%02"PRIx8")",
431 vconn->name, vconn->version, vconn->min_version,
432 OFP10_VERSION, oh->version);
433 vconn->state = VCS_CONNECTED;
434 }
435 ofpbuf_delete(b);
436 return;
437 } else {
438 char *s = ofp_to_string(b->data, b->size, 1);
439 VLOG_WARN_RL(&bad_ofmsg_rl,
440 "%s: received message while expecting hello: %s",
441 vconn->name, s);
442 free(s);
443 retval = EPROTO;
444 ofpbuf_delete(b);
445 }
446 }
447
448 if (retval != EAGAIN) {
449 vconn->state = VCS_DISCONNECTED;
450 vconn->error = retval == EOF ? ECONNRESET : retval;
451 }
452 }
453
454 static void
455 vcs_send_error(struct vconn *vconn)
456 {
457 struct ofpbuf *b;
458 char s[128];
459 int retval;
460
461 snprintf(s, sizeof s, "We support versions 0x%02x to 0x%02x inclusive but "
462 "you support no later than version 0x%02"PRIx8".",
463 vconn->min_version, OFP10_VERSION, vconn->version);
464 b = ofperr_encode_hello(OFPERR_OFPHFC_INCOMPATIBLE,
465 ofperr_domain_from_version(vconn->version), s);
466 retval = do_send(vconn, b);
467 if (retval) {
468 ofpbuf_delete(b);
469 }
470 if (retval != EAGAIN) {
471 vconn->state = VCS_DISCONNECTED;
472 vconn->error = retval ? retval : EPROTO;
473 }
474 }
475
476 /* Tries to complete the connection on 'vconn'. If 'vconn''s connection is
477 * complete, returns 0 if the connection was successful or a positive errno
478 * value if it failed. If the connection is still in progress, returns
479 * EAGAIN. */
480 int
481 vconn_connect(struct vconn *vconn)
482 {
483 enum vconn_state last_state;
484
485 assert(vconn->min_version > 0);
486 do {
487 last_state = vconn->state;
488 switch (vconn->state) {
489 case VCS_CONNECTING:
490 vcs_connecting(vconn);
491 break;
492
493 case VCS_SEND_HELLO:
494 vcs_send_hello(vconn);
495 break;
496
497 case VCS_RECV_HELLO:
498 vcs_recv_hello(vconn);
499 break;
500
501 case VCS_CONNECTED:
502 return 0;
503
504 case VCS_SEND_ERROR:
505 vcs_send_error(vconn);
506 break;
507
508 case VCS_DISCONNECTED:
509 return vconn->error;
510
511 default:
512 NOT_REACHED();
513 }
514 } while (vconn->state != last_state);
515
516 return EAGAIN;
517 }
518
519 /* Tries to receive an OpenFlow message from 'vconn'. If successful, stores
520 * the received message into '*msgp' and returns 0. The caller is responsible
521 * for destroying the message with ofpbuf_delete(). On failure, returns a
522 * positive errno value and stores a null pointer into '*msgp'. On normal
523 * connection close, returns EOF.
524 *
525 * vconn_recv will not block waiting for a packet to arrive. If no packets
526 * have been received, it returns EAGAIN immediately. */
527 int
528 vconn_recv(struct vconn *vconn, struct ofpbuf **msgp)
529 {
530 int retval = vconn_connect(vconn);
531 if (!retval) {
532 retval = do_recv(vconn, msgp);
533 }
534 return retval;
535 }
536
537 static int
538 do_recv(struct vconn *vconn, struct ofpbuf **msgp)
539 {
540 int retval = (vconn->class->recv)(vconn, msgp);
541 if (!retval) {
542 struct ofp_header *oh;
543
544 COVERAGE_INC(vconn_received);
545 if (VLOG_IS_DBG_ENABLED()) {
546 char *s = ofp_to_string((*msgp)->data, (*msgp)->size, 1);
547 VLOG_DBG_RL(&ofmsg_rl, "%s: received: %s", vconn->name, s);
548 free(s);
549 }
550
551 oh = ofpbuf_at_assert(*msgp, 0, sizeof *oh);
552 if ((oh->version != vconn->version || oh->version == 0)
553 && oh->type != OFPT_HELLO
554 && oh->type != OFPT_ERROR
555 && oh->type != OFPT_ECHO_REQUEST
556 && oh->type != OFPT_ECHO_REPLY
557 && oh->type != OFPT_VENDOR)
558 {
559 if (vconn->version == 0) {
560 VLOG_ERR_RL(&bad_ofmsg_rl,
561 "%s: received OpenFlow message type %"PRIu8" "
562 "before version negotiation complete",
563 vconn->name, oh->type);
564 } else {
565 VLOG_ERR_RL(&bad_ofmsg_rl,
566 "%s: received OpenFlow version 0x%02"PRIx8" "
567 "!= expected %02x",
568 vconn->name, oh->version, vconn->version);
569 }
570 ofpbuf_delete(*msgp);
571 retval = EPROTO;
572 }
573 }
574 if (retval) {
575 *msgp = NULL;
576 }
577 return retval;
578 }
579
580 /* Tries to queue 'msg' for transmission on 'vconn'. If successful, returns 0,
581 * in which case ownership of 'msg' is transferred to the vconn. Success does
582 * not guarantee that 'msg' has been or ever will be delivered to the peer,
583 * only that it has been queued for transmission.
584 *
585 * Returns a positive errno value on failure, in which case the caller
586 * retains ownership of 'msg'.
587 *
588 * vconn_send will not block. If 'msg' cannot be immediately accepted for
589 * transmission, it returns EAGAIN immediately. */
590 int
591 vconn_send(struct vconn *vconn, struct ofpbuf *msg)
592 {
593 int retval = vconn_connect(vconn);
594 if (!retval) {
595 retval = do_send(vconn, msg);
596 }
597 return retval;
598 }
599
600 static int
601 do_send(struct vconn *vconn, struct ofpbuf *msg)
602 {
603 int retval;
604
605 assert(msg->size >= sizeof(struct ofp_header));
606 assert(((struct ofp_header *) msg->data)->length == htons(msg->size));
607 if (!VLOG_IS_DBG_ENABLED()) {
608 COVERAGE_INC(vconn_sent);
609 retval = (vconn->class->send)(vconn, msg);
610 } else {
611 char *s = ofp_to_string(msg->data, msg->size, 1);
612 retval = (vconn->class->send)(vconn, msg);
613 if (retval != EAGAIN) {
614 VLOG_DBG_RL(&ofmsg_rl, "%s: sent (%s): %s",
615 vconn->name, strerror(retval), s);
616 }
617 free(s);
618 }
619 return retval;
620 }
621
622 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
623 int
624 vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
625 {
626 int retval;
627
628 fatal_signal_run();
629
630 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
631 vconn_run(vconn);
632 vconn_run_wait(vconn);
633 vconn_send_wait(vconn);
634 poll_block();
635 }
636 return retval;
637 }
638
639 /* Same as vconn_recv, except that it waits until a message is received. */
640 int
641 vconn_recv_block(struct vconn *vconn, struct ofpbuf **msgp)
642 {
643 int retval;
644
645 fatal_signal_run();
646
647 while ((retval = vconn_recv(vconn, msgp)) == EAGAIN) {
648 vconn_run(vconn);
649 vconn_run_wait(vconn);
650 vconn_recv_wait(vconn);
651 poll_block();
652 }
653 return retval;
654 }
655
656 /* Waits until a message with a transaction ID matching 'xid' is recived on
657 * 'vconn'. Returns 0 if successful, in which case the reply is stored in
658 * '*replyp' for the caller to examine and free. Otherwise returns a positive
659 * errno value, or EOF, and sets '*replyp' to null.
660 *
661 * 'request' is always destroyed, regardless of the return value. */
662 int
663 vconn_recv_xid(struct vconn *vconn, ovs_be32 xid, struct ofpbuf **replyp)
664 {
665 for (;;) {
666 ovs_be32 recv_xid;
667 struct ofpbuf *reply;
668 int error;
669
670 error = vconn_recv_block(vconn, &reply);
671 if (error) {
672 *replyp = NULL;
673 return error;
674 }
675 recv_xid = ((struct ofp_header *) reply->data)->xid;
676 if (xid == recv_xid) {
677 *replyp = reply;
678 return 0;
679 }
680
681 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: received reply with xid %08"PRIx32
682 " != expected %08"PRIx32,
683 vconn->name, ntohl(recv_xid), ntohl(xid));
684 ofpbuf_delete(reply);
685 }
686 }
687
688 /* Sends 'request' to 'vconn' and blocks until it receives a reply with a
689 * matching transaction ID. Returns 0 if successful, in which case the reply
690 * is stored in '*replyp' for the caller to examine and free. Otherwise
691 * returns a positive errno value, or EOF, and sets '*replyp' to null.
692 *
693 * 'request' should be an OpenFlow request that requires a reply. Otherwise,
694 * if there is no reply, this function can end up blocking forever (or until
695 * the peer drops the connection).
696 *
697 * 'request' is always destroyed, regardless of the return value. */
698 int
699 vconn_transact(struct vconn *vconn, struct ofpbuf *request,
700 struct ofpbuf **replyp)
701 {
702 ovs_be32 send_xid = ((struct ofp_header *) request->data)->xid;
703 int error;
704
705 *replyp = NULL;
706 error = vconn_send_block(vconn, request);
707 if (error) {
708 ofpbuf_delete(request);
709 }
710 return error ? error : vconn_recv_xid(vconn, send_xid, replyp);
711 }
712
713 /* Sends 'request' followed by a barrier request to 'vconn', then blocks until
714 * it receives a reply to the barrier. If successful, stores the reply to
715 * 'request' in '*replyp', if one was received, and otherwise NULL, then
716 * returns 0. Otherwise returns a positive errno value, or EOF, and sets
717 * '*replyp' to null.
718 *
719 * This function is useful for sending an OpenFlow request that doesn't
720 * ordinarily include a reply but might report an error in special
721 * circumstances.
722 *
723 * 'request' is always destroyed, regardless of the return value. */
724 int
725 vconn_transact_noreply(struct vconn *vconn, struct ofpbuf *request,
726 struct ofpbuf **replyp)
727 {
728 ovs_be32 request_xid;
729 ovs_be32 barrier_xid;
730 struct ofpbuf *barrier;
731 int error;
732
733 *replyp = NULL;
734
735 /* Send request. */
736 request_xid = ((struct ofp_header *) request->data)->xid;
737 error = vconn_send_block(vconn, request);
738 if (error) {
739 ofpbuf_delete(request);
740 return error;
741 }
742
743 /* Send barrier. */
744 barrier = ofputil_encode_barrier_request();
745 barrier_xid = ((struct ofp_header *) barrier->data)->xid;
746 error = vconn_send_block(vconn, barrier);
747 if (error) {
748 ofpbuf_delete(barrier);
749 return error;
750 }
751
752 for (;;) {
753 struct ofpbuf *msg;
754 ovs_be32 msg_xid;
755 int error;
756
757 error = vconn_recv_block(vconn, &msg);
758 if (error) {
759 ofpbuf_delete(*replyp);
760 *replyp = NULL;
761 return error;
762 }
763
764 msg_xid = ((struct ofp_header *) msg->data)->xid;
765 if (msg_xid == request_xid) {
766 if (*replyp) {
767 VLOG_WARN_RL(&bad_ofmsg_rl, "%s: duplicate replies with "
768 "xid %08"PRIx32, vconn->name, ntohl(msg_xid));
769 ofpbuf_delete(*replyp);
770 }
771 *replyp = msg;
772 } else {
773 ofpbuf_delete(msg);
774 if (msg_xid == barrier_xid) {
775 return 0;
776 } else {
777 VLOG_DBG_RL(&bad_ofmsg_rl, "%s: reply with xid %08"PRIx32
778 " != expected %08"PRIx32" or %08"PRIx32,
779 vconn->name, ntohl(msg_xid),
780 ntohl(request_xid), ntohl(barrier_xid));
781 }
782 }
783 }
784 }
785
786 /* vconn_transact_noreply() for a list of "struct ofpbuf"s, sent one by one.
787 * All of the requests on 'requests' are always destroyed, regardless of the
788 * return value. */
789 int
790 vconn_transact_multiple_noreply(struct vconn *vconn, struct list *requests,
791 struct ofpbuf **replyp)
792 {
793 struct ofpbuf *request, *next;
794
795 LIST_FOR_EACH_SAFE (request, next, list_node, requests) {
796 int error;
797
798 list_remove(&request->list_node);
799
800 error = vconn_transact_noreply(vconn, request, replyp);
801 if (error || *replyp) {
802 ofpbuf_list_delete(requests);
803 return error;
804 }
805 }
806
807 *replyp = NULL;
808 return 0;
809 }
810
811 void
812 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
813 {
814 assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
815
816 switch (vconn->state) {
817 case VCS_CONNECTING:
818 wait = WAIT_CONNECT;
819 break;
820
821 case VCS_SEND_HELLO:
822 case VCS_SEND_ERROR:
823 wait = WAIT_SEND;
824 break;
825
826 case VCS_RECV_HELLO:
827 wait = WAIT_RECV;
828 break;
829
830 case VCS_CONNECTED:
831 break;
832
833 case VCS_DISCONNECTED:
834 poll_immediate_wake();
835 return;
836 }
837 (vconn->class->wait)(vconn, wait);
838 }
839
840 void
841 vconn_connect_wait(struct vconn *vconn)
842 {
843 vconn_wait(vconn, WAIT_CONNECT);
844 }
845
846 void
847 vconn_recv_wait(struct vconn *vconn)
848 {
849 vconn_wait(vconn, WAIT_RECV);
850 }
851
852 void
853 vconn_send_wait(struct vconn *vconn)
854 {
855 vconn_wait(vconn, WAIT_SEND);
856 }
857
858 /* Given 'name', a connection name in the form "TYPE:ARGS", stores the class
859 * named "TYPE" into '*classp' and returns 0. Returns EAFNOSUPPORT and stores
860 * a null pointer into '*classp' if 'name' is in the wrong form or if no such
861 * class exists. */
862 static int
863 pvconn_lookup_class(const char *name, struct pvconn_class **classp)
864 {
865 size_t prefix_len;
866
867 prefix_len = strcspn(name, ":");
868 if (name[prefix_len] != '\0') {
869 size_t i;
870
871 for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) {
872 struct pvconn_class *class = pvconn_classes[i];
873 if (strlen(class->name) == prefix_len
874 && !memcmp(class->name, name, prefix_len)) {
875 *classp = class;
876 return 0;
877 }
878 }
879 }
880
881 *classp = NULL;
882 return EAFNOSUPPORT;
883 }
884
885 /* Returns 0 if 'name' is a connection name in the form "TYPE:ARGS" and TYPE is
886 * a supported connection type, otherwise EAFNOSUPPORT. */
887 int
888 pvconn_verify_name(const char *name)
889 {
890 struct pvconn_class *class;
891 return pvconn_lookup_class(name, &class);
892 }
893
894 /* Attempts to start listening for OpenFlow connections. 'name' is a
895 * connection name in the form "TYPE:ARGS", where TYPE is an passive vconn
896 * class's name and ARGS are vconn class-specific.
897 *
898 * Returns 0 if successful, otherwise a positive errno value. If successful,
899 * stores a pointer to the new connection in '*pvconnp', otherwise a null
900 * pointer. */
901 int
902 pvconn_open(const char *name, struct pvconn **pvconnp)
903 {
904 struct pvconn_class *class;
905 struct pvconn *pvconn;
906 char *suffix_copy;
907 int error;
908
909 check_vconn_classes();
910
911 /* Look up the class. */
912 error = pvconn_lookup_class(name, &class);
913 if (!class) {
914 goto error;
915 }
916
917 /* Call class's "open" function. */
918 suffix_copy = xstrdup(strchr(name, ':') + 1);
919 error = class->listen(name, suffix_copy, &pvconn);
920 free(suffix_copy);
921 if (error) {
922 goto error;
923 }
924
925 /* Success. */
926 *pvconnp = pvconn;
927 return 0;
928
929 error:
930 *pvconnp = NULL;
931 return error;
932 }
933
934 /* Returns the name that was used to open 'pvconn'. The caller must not
935 * modify or free the name. */
936 const char *
937 pvconn_get_name(const struct pvconn *pvconn)
938 {
939 return pvconn->name;
940 }
941
942 /* Closes 'pvconn'. */
943 void
944 pvconn_close(struct pvconn *pvconn)
945 {
946 if (pvconn != NULL) {
947 char *name = pvconn->name;
948 (pvconn->class->close)(pvconn);
949 free(name);
950 }
951 }
952
953 /* Tries to accept a new connection on 'pvconn'. If successful, stores the new
954 * connection in '*new_vconn' and returns 0. Otherwise, returns a positive
955 * errno value.
956 *
957 * The new vconn will automatically negotiate an OpenFlow protocol version
958 * acceptable to both peers on the connection. The version negotiated will be
959 * no lower than 'min_version' and no higher than OFP10_VERSION.
960 *
961 * pvconn_accept() will not block waiting for a connection. If no connection
962 * is ready to be accepted, it returns EAGAIN immediately. */
963 int
964 pvconn_accept(struct pvconn *pvconn, int min_version, struct vconn **new_vconn)
965 {
966 int retval = (pvconn->class->accept)(pvconn, new_vconn);
967 if (retval) {
968 *new_vconn = NULL;
969 } else {
970 assert((*new_vconn)->state != VCS_CONNECTING
971 || (*new_vconn)->class->connect);
972 (*new_vconn)->min_version = min_version;
973 }
974 return retval;
975 }
976
977 void
978 pvconn_wait(struct pvconn *pvconn)
979 {
980 (pvconn->class->wait)(pvconn);
981 }
982
983 /* Initializes 'vconn' as a new vconn named 'name', implemented via 'class'.
984 * The initial connection status, supplied as 'connect_status', is interpreted
985 * as follows:
986 *
987 * - 0: 'vconn' is connected. Its 'send' and 'recv' functions may be
988 * called in the normal fashion.
989 *
990 * - EAGAIN: 'vconn' is trying to complete a connection. Its 'connect'
991 * function should be called to complete the connection.
992 *
993 * - Other positive errno values indicate that the connection failed with
994 * the specified error.
995 *
996 * After calling this function, vconn_close() must be used to destroy 'vconn',
997 * otherwise resources will be leaked.
998 *
999 * The caller retains ownership of 'name'. */
1000 void
1001 vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status,
1002 const char *name)
1003 {
1004 vconn->class = class;
1005 vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING
1006 : !connect_status ? VCS_SEND_HELLO
1007 : VCS_DISCONNECTED);
1008 vconn->error = connect_status;
1009 vconn->version = 0;
1010 vconn->min_version = 0;
1011 vconn->remote_ip = 0;
1012 vconn->remote_port = 0;
1013 vconn->local_ip = 0;
1014 vconn->local_port = 0;
1015 vconn->name = xstrdup(name);
1016 assert(vconn->state != VCS_CONNECTING || class->connect);
1017 }
1018
1019 void
1020 vconn_set_remote_ip(struct vconn *vconn, ovs_be32 ip)
1021 {
1022 vconn->remote_ip = ip;
1023 }
1024
1025 void
1026 vconn_set_remote_port(struct vconn *vconn, ovs_be16 port)
1027 {
1028 vconn->remote_port = port;
1029 }
1030
1031 void
1032 vconn_set_local_ip(struct vconn *vconn, ovs_be32 ip)
1033 {
1034 vconn->local_ip = ip;
1035 }
1036
1037 void
1038 vconn_set_local_port(struct vconn *vconn, ovs_be16 port)
1039 {
1040 vconn->local_port = port;
1041 }
1042
1043 void
1044 pvconn_init(struct pvconn *pvconn, struct pvconn_class *class,
1045 const char *name)
1046 {
1047 pvconn->class = class;
1048 pvconn->name = xstrdup(name);
1049 }