X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fvconn.c;h=71e6d05244a224511513506464f6f849de3698ee;hb=a39c3ff9f586d3416976d3c3bd095fa81d73efa0;hp=a3792ece7ffa0eabe778bc53b67bab1e27d787ad;hpb=85b20fd6ee585f462e012fbcc7f966a81edab2ed;p=sliver-openvswitch.git diff --git a/lib/vconn.c b/lib/vconn.c index a3792ece7..71e6d0524 100644 --- a/lib/vconn.c +++ b/lib/vconn.c @@ -16,7 +16,6 @@ #include #include "vconn-provider.h" -#include #include #include #include @@ -60,7 +59,7 @@ enum vconn_state { VCS_DISCONNECTED /* Connection failed or connection closed. */ }; -static struct vconn_class *vconn_classes[] = { +static const struct vconn_class *vconn_classes[] = { &tcp_vconn_class, &unix_vconn_class, #ifdef HAVE_OPENSSL @@ -68,7 +67,7 @@ static struct vconn_class *vconn_classes[] = { #endif }; -static struct pvconn_class *pvconn_classes[] = { +static const struct pvconn_class *pvconn_classes[] = { &ptcp_pvconn_class, &punix_pvconn_class, #ifdef HAVE_OPENSSL @@ -96,28 +95,28 @@ check_vconn_classes(void) size_t i; for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { - struct vconn_class *class = vconn_classes[i]; - assert(class->name != NULL); - assert(class->open != NULL); + const struct vconn_class *class = vconn_classes[i]; + 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. */ } } for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { - struct pvconn_class *class = pvconn_classes[i]; - assert(class->name != NULL); - assert(class->listen != NULL); + const struct pvconn_class *class = pvconn_classes[i]; + 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. */ } @@ -178,7 +177,7 @@ vconn_usage(bool active, bool passive, bool bootstrap OVS_UNUSED) * a null pointer into '*classp' if 'name' is in the wrong form or if no such * class exists. */ static int -vconn_lookup_class(const char *name, struct vconn_class **classp) +vconn_lookup_class(const char *name, const struct vconn_class **classp) { size_t prefix_len; @@ -187,7 +186,7 @@ vconn_lookup_class(const char *name, struct vconn_class **classp) size_t i; for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) { - struct vconn_class *class = vconn_classes[i]; + const struct vconn_class *class = vconn_classes[i]; if (strlen(class->name) == prefix_len && !memcmp(class->name, name, prefix_len)) { *classp = class; @@ -205,7 +204,7 @@ vconn_lookup_class(const char *name, struct vconn_class **classp) int vconn_verify_name(const char *name) { - struct vconn_class *class; + const struct vconn_class *class; return vconn_lookup_class(name, &class); } @@ -226,7 +225,7 @@ int vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, struct vconn **vconnp) { - struct vconn_class *class; + const struct vconn_class *class; struct vconn *vconn; char *suffix_copy; int error; @@ -253,7 +252,7 @@ vconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, } /* Success. */ - assert(vconn->state != VCS_CONNECTING || vconn->class->connect); + ovs_assert(vconn->state != VCS_CONNECTING || vconn->class->connect); *vconnp = vconn; return 0; @@ -395,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) { @@ -601,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; @@ -664,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()) { @@ -695,7 +715,7 @@ vconn_connect_block(struct vconn *vconn) vconn_connect_wait(vconn); poll_block(); } - assert(error != EINPROGRESS); + ovs_assert(error != EINPROGRESS); return error; } @@ -892,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: @@ -941,7 +961,7 @@ vconn_send_wait(struct vconn *vconn) * a null pointer into '*classp' if 'name' is in the wrong form or if no such * class exists. */ static int -pvconn_lookup_class(const char *name, struct pvconn_class **classp) +pvconn_lookup_class(const char *name, const struct pvconn_class **classp) { size_t prefix_len; @@ -950,7 +970,7 @@ pvconn_lookup_class(const char *name, struct pvconn_class **classp) size_t i; for (i = 0; i < ARRAY_SIZE(pvconn_classes); i++) { - struct pvconn_class *class = pvconn_classes[i]; + const struct pvconn_class *class = pvconn_classes[i]; if (strlen(class->name) == prefix_len && !memcmp(class->name, name, prefix_len)) { *classp = class; @@ -968,7 +988,7 @@ pvconn_lookup_class(const char *name, struct pvconn_class **classp) int pvconn_verify_name(const char *name) { - struct pvconn_class *class; + const struct pvconn_class *class; return pvconn_lookup_class(name, &class); } @@ -989,7 +1009,7 @@ int pvconn_open(const char *name, uint32_t allowed_versions, uint8_t dscp, struct pvconn **pvconnp) { - struct pvconn_class *class; + const struct pvconn_class *class; struct pvconn *pvconn; char *suffix_copy; int error; @@ -1059,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; } @@ -1089,22 +1109,18 @@ pvconn_wait(struct pvconn *pvconn) * * The caller retains ownership of 'name'. */ void -vconn_init(struct vconn *vconn, struct vconn_class *class, int connect_status, - const char *name, uint32_t allowed_versions) +vconn_init(struct vconn *vconn, const 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 @@ -1132,7 +1148,7 @@ vconn_set_local_port(struct vconn *vconn, ovs_be16 port) } void -pvconn_init(struct pvconn *pvconn, struct pvconn_class *class, +pvconn_init(struct pvconn *pvconn, const struct pvconn_class *class, const char *name, uint32_t allowed_versions) { pvconn->class = class;