From: Alex Wang Date: Wed, 9 Oct 2013 04:30:37 +0000 (+0000) Subject: ofproto-dpif-monitor: Add ofproto-dpif-monitor module. X-Git-Tag: sliver-openvswitch-2.0.90-1~10^2 X-Git-Url: http://git.onelab.eu/?p=sliver-openvswitch.git;a=commitdiff_plain;h=635c5db93df8c69db6289c58ebda0e64cea85c9b ofproto-dpif-monitor: Add ofproto-dpif-monitor module. This commit adds a new module ofproto-dpif-monitor in ofproto directory. This module is in charge of executing the periodic functions of monitoring code (e.g. bfd and cfm). Signed-off-by: Alex Wang Signed-off-by: Ethan Jackson Acked-by: Ethan Jackson --- diff --git a/ofproto/automake.mk b/ofproto/automake.mk index 47ca1b81f..432f08371 100644 --- a/ofproto/automake.mk +++ b/ofproto/automake.mk @@ -28,6 +28,8 @@ ofproto_libofproto_a_SOURCES = \ ofproto/ofproto-dpif-ipfix.h \ ofproto/ofproto-dpif-mirror.c \ ofproto/ofproto-dpif-mirror.h \ + ofproto/ofproto-dpif-monitor.c \ + ofproto/ofproto-dpif-monitor.h \ ofproto/ofproto-dpif-sflow.c \ ofproto/ofproto-dpif-sflow.h \ ofproto/ofproto-dpif-upcall.c \ diff --git a/ofproto/ofproto-dpif-monitor.c b/ofproto/ofproto-dpif-monitor.c new file mode 100644 index 000000000..a0c3843f1 --- /dev/null +++ b/ofproto/ofproto-dpif-monitor.c @@ -0,0 +1,199 @@ +/* + * Copyright (c) 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. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "ofproto-dpif-monitor.h" + +#include + +#include "bfd.h" +#include "cfm.h" +#include "hash.h" +#include "hmap.h" +#include "ofpbuf.h" +#include "ofproto-dpif.h" +#include "util.h" +#include "vlog.h" + +/* Monitored port. It owns references to ofport, bfd, cfm structs. */ +struct mport { + struct hmap_node hmap_node; /* In monitor_hmap. */ + const struct ofport_dpif *ofport; /* The corresponding ofport. */ + + struct cfm *cfm; /* Reference to cfm. */ + struct bfd *bfd; /* Reference to bfd. */ + uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */ +}; + +/* hmap that contains "struct mport"s. */ +static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap); + +static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER; + +static void mport_register(const struct ofport_dpif *, struct bfd *, + struct cfm *, uint8_t[ETH_ADDR_LEN]) + OVS_REQ_WRLOCK(monitor_rwlock); +static void mport_unregister(const struct ofport_dpif *) + OVS_REQ_WRLOCK(monitor_rwlock); +static void mport_update(struct mport *, struct bfd *, struct cfm *, + uint8_t[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock); +static struct mport *mport_find(const struct ofport_dpif *) + OVS_REQ_WRLOCK(monitor_rwlock); + +/* Tries finding and returning the 'mport' from the monitor_hmap. + * If there is no such 'mport', returns NULL. */ +static struct mport * +mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock) +{ + struct mport *node; + + HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0), + &monitor_hmap) { + if (node->ofport == ofport) { + return node; + } + } + return NULL; +} + +/* Creates a new mport and inserts it into monitor_hmap, if it doesn't exist. + * Otherwise, just updates its fields. */ +static void +mport_register(const struct ofport_dpif *ofport, struct bfd *bfd, + struct cfm *cfm, uint8_t *hw_addr) + OVS_REQ_WRLOCK(monitor_rwlock) +{ + struct mport *mport = mport_find(ofport); + + if (!mport) { + mport = xzalloc(sizeof *mport); + mport->ofport = ofport; + hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0)); + } + mport_update(mport, bfd, cfm, hw_addr); +} + +/* Removes mport from monitor_hmap and frees it. */ +static void +mport_unregister(const struct ofport_dpif *ofport) + OVS_REQ_WRLOCK(monitor_rwlock) +{ + struct mport *mport = mport_find(ofport); + + if (mport) { + mport_update(mport, NULL, NULL, NULL); + hmap_remove(&monitor_hmap, &mport->hmap_node); + free(mport); + } +} + +/* Updates the fields of an existing mport struct. */ +static void +mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm, + uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock) +{ + ovs_assert(mport); + + if (mport->cfm != cfm) { + cfm_unref(mport->cfm); + mport->cfm = cfm_ref(cfm); + } + if (mport->bfd != bfd) { + bfd_unref(mport->bfd); + mport->bfd = bfd_ref(bfd); + } + if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) { + memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN); + } +} + + +/* Creates the mport in monitor module if either bfd or cfm + * is configured. Otherwise, deletes the mport. */ +void +ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport, + struct bfd *bfd, struct cfm *cfm, + uint8_t hw_addr[ETH_ADDR_LEN]) +{ + ovs_rwlock_wrlock(&monitor_rwlock); + if (!cfm && !bfd) { + mport_unregister(ofport); + } else { + mport_register(ofport, bfd, cfm, hw_addr); + } + ovs_rwlock_unlock(&monitor_rwlock); +} + +/* Checks the sending of control packets on all mports. Sends the control + * packets if needed. */ +void +ofproto_dpif_monitor_run_fast(void) +{ + struct mport *mport; + static uint32_t buf_stub[128 / 4]; + struct ofpbuf packet; + + ovs_rwlock_rdlock(&monitor_rwlock); + HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) { + if (mport->cfm && cfm_should_send_ccm(mport->cfm)) { + ofpbuf_use_stub(&packet, buf_stub, sizeof buf_stub); + cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr); + ofproto_dpif_send_packet(mport->ofport, &packet); + } + if (mport->bfd && bfd_should_send_packet(mport->bfd)) { + ofpbuf_use_stub(&packet, buf_stub, sizeof buf_stub); + bfd_put_packet(mport->bfd, &packet, mport->hw_addr); + ofproto_dpif_send_packet(mport->ofport, &packet); + } + } + ovs_rwlock_unlock(&monitor_rwlock); +} + +/* Executes bfd_run(), cfm_run() on all mports. */ +void +ofproto_dpif_monitor_run(void) +{ + struct mport *mport; + + ovs_rwlock_rdlock(&monitor_rwlock); + HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) { + if (mport->cfm) { + cfm_run(mport->cfm); + } + if (mport->bfd) { + bfd_run(mport->bfd); + } + } + ovs_rwlock_unlock(&monitor_rwlock); +} + +/* Executes the bfd_wait() and cfm_wait() functions on all mports. */ +void +ofproto_dpif_monitor_wait(void) +{ + struct mport *mport; + + ovs_rwlock_rdlock(&monitor_rwlock); + HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) { + if (mport->cfm) { + cfm_wait(mport->cfm); + } + if (mport->bfd) { + bfd_wait(mport->bfd); + } + } + ovs_rwlock_unlock(&monitor_rwlock); +} diff --git a/ofproto/ofproto-dpif-monitor.h b/ofproto/ofproto-dpif-monitor.h new file mode 100644 index 000000000..8e2681468 --- /dev/null +++ b/ofproto/ofproto-dpif-monitor.h @@ -0,0 +1,34 @@ +/* Copyright (c) 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. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ + +#ifndef OFPROTO_DPIF_MONITOR_H +#define OFPROTO_DPIF_MONITOR_H 1 + +#include + +#include "packets.h" + +struct bfd; +struct cfm; +struct ofport_dpif; + +void ofproto_dpif_monitor_run(void); +void ofproto_dpif_monitor_run_fast(void); +void ofproto_dpif_monitor_wait(void); + +void ofproto_dpif_monitor_port_update(const struct ofport_dpif *, + struct bfd *, struct cfm *, + uint8_t[OFP_ETH_ALEN]); + +#endif /* ofproto-dpif-monitor.h */ diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index 9d7e97a9b..7eff3263a 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -52,6 +52,7 @@ #include "ofproto-dpif-governor.h" #include "ofproto-dpif-ipfix.h" #include "ofproto-dpif-mirror.h" +#include "ofproto-dpif-monitor.h" #include "ofproto-dpif-sflow.h" #include "ofproto-dpif-upcall.h" #include "ofproto-dpif-xlate.h" @@ -361,8 +362,6 @@ ofport_dpif_cast(const struct ofport *ofport) } static void port_run(struct ofport_dpif *); -static void port_run_fast(struct ofport_dpif *); -static void port_wait(struct ofport_dpif *); static int set_bfd(struct ofport *, const struct smap *); static int set_cfm(struct ofport *, const struct cfm_settings *); static void ofport_update_peer(struct ofport_dpif *); @@ -1430,7 +1429,6 @@ run_fast(struct ofproto *ofproto_) { struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); struct ofputil_packet_in *pin, *next_pin; - struct ofport_dpif *ofport; struct list pins; /* Do not perform any periodic activity required by 'ofproto' while @@ -1447,10 +1445,7 @@ run_fast(struct ofproto *ofproto_) free(pin); } - HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { - port_run_fast(ofport); - } - + ofproto_dpif_monitor_run_fast(); return 0; } @@ -1492,6 +1487,9 @@ run(struct ofproto *ofproto_) dpif_ipfix_run(ofproto->ipfix); } + ofproto_dpif_monitor_run_fast(); + ofproto_dpif_monitor_run(); + HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { port_run(ofport); } @@ -1536,7 +1534,6 @@ static void wait(struct ofproto *ofproto_) { struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); - struct ofport_dpif *ofport; struct ofbundle *bundle; if (ofproto_get_flow_restore_wait()) { @@ -1549,9 +1546,7 @@ wait(struct ofproto *ofproto_) if (ofproto->ipfix) { dpif_ipfix_wait(ofproto->ipfix); } - HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { - port_wait(ofport); - } + ofproto_dpif_monitor_wait(); HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { bundle_wait(bundle); } @@ -1814,6 +1809,9 @@ port_modified(struct ofport *port_) bfd_set_netdev(port->bfd, port->up.netdev); } + ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm, + port->up.pp.hw_addr); + if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev, port->odp_port)) { ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate = @@ -1904,11 +1902,9 @@ static int set_cfm(struct ofport *ofport_, const struct cfm_settings *s) { struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); - int error; + int error = 0; - if (!s) { - error = 0; - } else { + if (s) { if (!ofport->cfm) { struct ofproto_dpif *ofproto; @@ -1918,13 +1914,17 @@ set_cfm(struct ofport *ofport_, const struct cfm_settings *s) } if (cfm_configure(ofport->cfm, s)) { - return 0; + error = 0; + goto out; } error = EINVAL; } cfm_unref(ofport->cfm); ofport->cfm = NULL; +out: + ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm, + ofport->up.pp.hw_addr); return error; } @@ -1958,7 +1958,8 @@ set_bfd(struct ofport *ofport_, const struct smap *cfg) if (ofport->bfd != old) { ofproto->backer->need_revalidate = REV_RECONFIGURE; } - + ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm, + ofport->up.pp.hw_addr); return 0; } @@ -2843,28 +2844,6 @@ ofport_update_peer(struct ofport_dpif *ofport) free(peer_name); } -static void -port_run_fast(struct ofport_dpif *ofport) -{ - if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) { - struct ofpbuf packet; - - ofpbuf_init(&packet, 0); - cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr); - ofproto_dpif_send_packet(ofport, &packet); - ofpbuf_uninit(&packet); - } - - if (ofport->bfd && bfd_should_send_packet(ofport->bfd)) { - struct ofpbuf packet; - - ofpbuf_init(&packet, 0); - bfd_put_packet(ofport->bfd, &packet, ofport->up.pp.hw_addr); - ofproto_dpif_send_packet(ofport, &packet); - ofpbuf_uninit(&packet); - } -} - static void port_run(struct ofport_dpif *ofport) { @@ -2876,12 +2855,9 @@ port_run(struct ofport_dpif *ofport) ofport->carrier_seq = carrier_seq; - port_run_fast(ofport); - if (ofport->cfm) { int cfm_opup = cfm_get_opup(ofport->cfm); - cfm_run(ofport->cfm); cfm_enable = !cfm_get_fault(ofport->cfm); if (cfm_opup >= 0) { @@ -2890,7 +2866,6 @@ port_run(struct ofport_dpif *ofport) } if (ofport->bfd) { - bfd_run(ofport->bfd); bfd_enable = bfd_forwarding(ofport->bfd); } @@ -2913,18 +2888,6 @@ port_run(struct ofport_dpif *ofport) ofport->may_enable = enable; } -static void -port_wait(struct ofport_dpif *ofport) -{ - if (ofport->cfm) { - cfm_wait(ofport->cfm); - } - - if (ofport->bfd) { - bfd_wait(ofport->bfd); - } -} - static int port_query_by_name(const struct ofproto *ofproto_, const char *devname, struct ofproto_port *ofproto_port) diff --git a/tests/bfd.at b/tests/bfd.at index 0b2b7ccf4..3154909e6 100644 --- a/tests/bfd.at +++ b/tests/bfd.at @@ -259,10 +259,25 @@ OVS_VSWITCHD_START([add-br br1 -- set bridge br1 datapath-type=dummy -- \ set Interface p1 bfd:enable=true bfd:min_tx=500 bfd:min_rx=500]) ovs-appctl time/stop - -# wait for local session state to go from down to up. for i in `seq 0 1`; do ovs-appctl time/warp 500; done -BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [init], [No Diagnostic]) + +# figuring out which port initiates the bfd session is important, +# since this whole unit test is based on exact timing sequence. +# for example, if p0 starts the bfd session, the p0 should have gone +# [up] now, and it will decay after 3000ms. if p1 starts the bfd +# session, we should wait for another 1000ms for p0 to go [up], and +# then 3000ms for it to decay. + +# check which port sends the first bfd control packet. +if [ ovs-appctl bfd/show p0 | grep "Remote Session State: init" ] +then +# if p0 sends first, it should have gone up already. + BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [init], [No Diagnostic]) +else +# if p1 sends first, wait 1000ms for p0 to go up. + BFD_CHECK([p0], [false], [false], [none], [init], [No Diagnostic], [none], [down], [No Diagnostic]) + for i in `seq 0 1`; do ovs-appctl time/warp 500; done +fi # Test-1 BFD decay: decay to decay_min_rx @@ -450,7 +465,7 @@ done AT_CHECK([ovs-vsctl set Interface p0 bfd:decay_min_rx=3000 -- set interface p1 bfd:min_tx=5000]) # there will be poll sequences from both sides. and it is hard to determine the # order. so just skip 10000ms and check the RX/TX. at that time, p0 should be in decay already. -for i in `seq 0 19`; do echo $i; ovs-appctl bfd/show; ovs-appctl time/warp 500; done +for i in `seq 0 19`; do ovs-appctl time/warp 500; done BFD_CHECK([p0], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic]) BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [none], [up], [No Diagnostic]) BFD_CHECK_TX([p0], [500ms], [300ms], [5000ms]) @@ -493,15 +508,12 @@ BFD_CHECK_RX([p0], [300ms], [300ms], [1ms]) # resume the bfd on p1. the bfd should not go to decay mode direclty. AT_CHECK([ovs-vsctl set Interface p1 bfd:enable=true]) -for i in `seq 0 1`; do ovs-appctl time/warp 500; done -BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [none], [up], [No Diagnostic]) +for i in `seq 0 3`; do ovs-appctl time/warp 500; done BFD_CHECK_TX([p0], [500ms], [300ms], [500ms]) BFD_CHECK_RX([p0], [500ms], [300ms], [500ms]) -# since the decay_min_rx is still 3000ms, so after 3000ms, there should be the decay and poll sequence. +# since the decay_min_rx is still 3000ms, so after 3000ms, there should be the decay. for i in `seq 0 5`; do ovs-appctl time/warp 500; done -BFD_CHECK([p0], [true], [false], [none], [up], [Control Detection Time Expired], [final], [up], [No Diagnostic]) -BFD_CHECK([p1], [true], [false], [none], [up], [No Diagnostic], [poll], [up], [Control Detection Time Expired]) BFD_CHECK_TX([p0], [500ms], [300ms], [500ms]) BFD_CHECK_RX([p0], [3000ms], [3000ms], [500ms]) # End of Test-8 ################################################################