X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fvconn.c;h=bf40b1b60630416d530b9270d0f6f9c55dc0456b;hb=7fa0f73fb284b4406bcd085ee62552891b3fa6cd;hp=24a369d8a1233f969cd78cca4fe1fc667646d8d2;hpb=7a25bd99246a548891427d62ccefb65f765bbc48;p=sliver-openvswitch.git diff --git a/lib/vconn.c b/lib/vconn.c index 24a369d8a..bf40b1b60 100644 --- a/lib/vconn.c +++ b/lib/vconn.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ #include #include "vconn-provider.h" -#include #include #include #include @@ -97,14 +96,14 @@ check_vconn_classes(void) for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { struct vconn_class *class = vconn_classes[i]; - assert(class->name != NULL); - assert(class->open != NULL); + ovs_assert(class->name != NULL); + ovs_assert(class->open != NULL); if (class->close || class->recv || class->send || class->run || class->run_wait || class->wait) { - assert(class->close != NULL); - assert(class->recv != NULL); - assert(class->send != NULL); - assert(class->wait != NULL); + ovs_assert(class->close != NULL); + ovs_assert(class->recv != NULL); + ovs_assert(class->send != NULL); + ovs_assert(class->wait != NULL); } else { /* This class delegates to another one. */ } @@ -112,12 +111,12 @@ check_vconn_classes(void) for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { struct pvconn_class *class = pvconn_classes[i]; - assert(class->name != NULL); - assert(class->listen != NULL); + ovs_assert(class->name != NULL); + ovs_assert(class->listen != NULL); if (class->close || class->accept || class->wait) { - assert(class->close != NULL); - assert(class->accept != NULL); - assert(class->wait != NULL); + ovs_assert(class->close != NULL); + ovs_assert(class->accept != NULL); + ovs_assert(class->wait != NULL); } else { /* This class delegates to another one. */ } @@ -223,8 +222,8 @@ vconn_verify_name(const char *name) * stores a pointer to the new connection in '*vconnp', otherwise a null * pointer. */ int -vconn_open(const char *name, uint32_t allowed_versions, - struct vconn **vconnp, uint8_t dscp) +vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, + struct vconn **vconnp) { struct vconn_class *class; struct vconn *vconn; @@ -253,7 +252,7 @@ vconn_open(const char *name, uint32_t allowed_versions, } /* Success. */ - assert(vconn->state != VCS_CONNECTING || vconn->class->connect); + ovs_assert(vconn->state != VCS_CONNECTING || vconn->class->connect); *vconnp = vconn; return 0; @@ -295,7 +294,7 @@ vconn_run_wait(struct vconn *vconn) } int -vconn_open_block(const char *name, uint32_t allowed_versions, +vconn_open_block(const char *name, uint32_t allowed_versions, uint8_t dscp, struct vconn **vconnp) { struct vconn *vconn; @@ -303,7 +302,7 @@ vconn_open_block(const char *name, uint32_t allowed_versions, fatal_signal_run(); - error = vconn_open(name, allowed_versions, &vconn, DSCP_DEFAULT); + error = vconn_open(name, allowed_versions, dscp, &vconn); if (!error) { error = vconn_connect_block(vconn); } @@ -343,6 +342,14 @@ vconn_get_allowed_versions(const struct vconn *vconn) return vconn->allowed_versions; } +/* Sets the allowed_versions of 'vconn', overriding + * the allowed_versions passed to vconn_open(). */ +void +vconn_set_allowed_versions(struct vconn *vconn, uint32_t allowed_versions) +{ + vconn->allowed_versions = allowed_versions; +} + /* Returns the IP address of the peer, or 0 if the peer is not connected over * an IP-based protocol or if its IP address is not yet known. */ ovs_be32 @@ -387,11 +394,32 @@ vconn_get_version(const struct vconn *vconn) return vconn->version ? vconn->version : -1; } +/* By default, a vconn accepts only OpenFlow messages whose version matches the + * one negotiated for the connection. A message received with a different + * version is an error that causes the vconn to drop the connection. + * + * This functions allows 'vconn' to accept messages with any OpenFlow version. + * This is useful in the special case where 'vconn' is used as an rconn + * "monitor" connection (see rconn_add_monitor()), that is, where 'vconn' is + * used as a target for mirroring OpenFlow messages for debugging and + * troubleshooting. + * + * This function should be called after a successful vconn_open() or + * pvconn_accept() but before the connection completes, that is, before + * vconn_connect() returns success. Otherwise, messages that arrive on 'vconn' + * beforehand with an unexpected version will the vconn to drop the + * connection. */ +void +vconn_set_recv_any_version(struct vconn *vconn) +{ + vconn->recv_any_version = true; +} + static void vcs_connecting(struct vconn *vconn) { int retval = (vconn->class->connect)(vconn); - assert(retval != EINPROGRESS); + ovs_assert(retval != EINPROGRESS); if (!retval) { vconn->state = VCS_SEND_HELLO; } else if (retval != EAGAIN) { @@ -406,8 +434,7 @@ vcs_send_hello(struct vconn *vconn) struct ofpbuf *b; int retval; - b = ofpraw_alloc(OFPRAW_OFPT_HELLO, - leftmost_1bit_idx(vconn->allowed_versions), 0); + b = ofputil_encode_hello(vconn->allowed_versions); retval = do_send(vconn, b); if (!retval) { vconn->state = VCS_RECV_HELLO; @@ -450,7 +477,6 @@ vcs_recv_hello(struct vconn *vconn) retval = do_recv(vconn, &b); if (!retval) { - const struct ofp_header *oh = b->data; enum ofptype type; enum ofperr error; @@ -459,9 +485,10 @@ vcs_recv_hello(struct vconn *vconn) char *peer_s, *local_s; uint32_t common_versions; - if (b->size > sizeof *oh) { + if (!ofputil_decode_hello(b->data, &vconn->peer_versions)) { struct ds msg = DS_EMPTY_INITIALIZER; - ds_put_format(&msg, "%s: extra-long hello:\n", vconn->name); + ds_put_format(&msg, "%s: unknown data in hello:\n", + vconn->name); ds_put_hex_dump(&msg, b->data, b->size, 0, true); VLOG_WARN_RL(&bad_ofmsg_rl, "%s", ds_cstr(&msg)); ds_destroy(&msg); @@ -594,7 +621,7 @@ vconn_recv(struct vconn *vconn, struct ofpbuf **msgp) if (!retval) { retval = do_recv(vconn, &msg); } - if (!retval) { + if (!retval && !vconn->recv_any_version) { const struct ofp_header *oh = msg->data; if (oh->version != vconn->version) { enum ofptype type; @@ -657,7 +684,7 @@ do_send(struct vconn *vconn, struct ofpbuf *msg) { int retval; - assert(msg->size >= sizeof(struct ofp_header)); + ovs_assert(msg->size >= sizeof(struct ofp_header)); ofpmsg_update_length(msg); if (!VLOG_IS_DBG_ENABLED()) { @@ -688,7 +715,7 @@ vconn_connect_block(struct vconn *vconn) vconn_connect_wait(vconn); poll_block(); } - assert(error != EINPROGRESS); + ovs_assert(error != EINPROGRESS); return error; } @@ -885,7 +912,7 @@ vconn_transact_multiple_noreply(struct vconn *vconn, struct list *requests, void vconn_wait(struct vconn *vconn, enum vconn_wait_type wait) { - assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND); + ovs_assert(wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND); switch (vconn->state) { case VCS_CONNECTING: @@ -979,8 +1006,8 @@ pvconn_verify_name(const char *name) * stores a pointer to the new connection in '*pvconnp', otherwise a null * pointer. */ int -pvconn_open(const char *name, uint32_t allowed_versions, - struct pvconn **pvconnp, uint8_t dscp) +pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, + struct pvconn **pvconnp) { struct pvconn_class *class; struct pvconn *pvconn; @@ -1052,8 +1079,8 @@ pvconn_accept(struct pvconn *pvconn, struct vconn **new_vconn) if (retval) { *new_vconn = NULL; } else { - assert((*new_vconn)->state != VCS_CONNECTING - || (*new_vconn)->class->connect); + ovs_assert((*new_vconn)->state != VCS_CONNECTING + || (*new_vconn)->class->connect); } return retval; } @@ -1085,19 +1112,15 @@ void vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status, const char *name, uint32_t allowed_versions) { + memset(vconn, 0, sizeof *vconn); vconn->class = class; vconn->state = (connect_status == EAGAIN ? VCS_CONNECTING : !connect_status ? VCS_SEND_HELLO : VCS_DISCONNECTED); vconn->error = connect_status; - vconn->version = 0; vconn->allowed_versions = allowed_versions; - vconn->remote_ip = 0; - vconn->remote_port = 0; - vconn->local_ip = 0; - vconn->local_port = 0; vconn->name = xstrdup(name); - assert(vconn->state != VCS_CONNECTING || class->connect); + ovs_assert(vconn->state != VCS_CONNECTING || class->connect); } void