X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=ofproto%2Fofproto-dpif-monitor.c;h=7c84e4b3a42c8d594b8a838d8bdd277f55bcb661;hb=8ba0a5227f6e6b50838c157bd303c2d5bf6f4e59;hp=75cf206cf05e74253b3e75295fa6eddc2638a3df;hpb=30fe866b46dd6b5658dfa7c2fb60b8f756ea6f95;p=sliver-openvswitch.git diff --git a/ofproto/ofproto-dpif-monitor.c b/ofproto/ofproto-dpif-monitor.c index 75cf206cf..7c84e4b3a 100644 --- a/ofproto/ofproto-dpif-monitor.c +++ b/ofproto/ofproto-dpif-monitor.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,15 +22,29 @@ #include "bfd.h" #include "cfm.h" #include "hash.h" +#include "heap.h" #include "hmap.h" +#include "latch.h" #include "ofpbuf.h" #include "ofproto-dpif.h" +#include "ovs-thread.h" +#include "poll-loop.h" +#include "seq.h" +#include "timeval.h" #include "util.h" #include "vlog.h" +VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor); + +/* Converts the time in millisecond to heap priority. */ +#define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME)) +/* Converts the heap priority to time in millisecond. */ +#define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO)) + /* Monitored port. It owns references to ofport, bfd, cfm structs. */ struct mport { struct hmap_node hmap_node; /* In monitor_hmap. */ + struct heap_node heap_node; /* In monitor_heap. */ const struct ofport_dpif *ofport; /* The corresponding ofport. */ struct cfm *cfm; /* Reference to cfm. */ @@ -41,22 +55,34 @@ struct mport { /* hmap that contains "struct mport"s. */ static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap); -static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER; +/* heap for ordering mport based on bfd/cfm wakeup time. */ +static struct heap monitor_heap; + +/* The monitor thread id. */ +static pthread_t monitor_tid; +/* True if the monitor thread is running. */ +static bool monitor_running; + +static struct latch monitor_exit_latch; +static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER; + +static void *monitor_main(void *); +static void monitor_run(void); static void mport_register(const struct ofport_dpif *, struct bfd *, struct cfm *, uint8_t[ETH_ADDR_LEN]) - OVS_REQ_WRLOCK(monitor_rwlock); + OVS_REQUIRES(monitor_mutex); static void mport_unregister(const struct ofport_dpif *) - OVS_REQ_WRLOCK(monitor_rwlock); + OVS_REQUIRES(monitor_mutex); static void mport_update(struct mport *, struct bfd *, struct cfm *, - uint8_t[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock); + uint8_t[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex); static struct mport *mport_find(const struct ofport_dpif *) - OVS_REQ_WRLOCK(monitor_rwlock); + OVS_REQUIRES(monitor_mutex); /* 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) +mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex) { struct mport *node; @@ -69,12 +95,12 @@ mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock) return NULL; } -/* Creates a new mport and inserts it into monitor_hmap, if it doesn't exist. - * Otherwise, just updates its fields. */ +/* Creates a new mport and inserts it into monitor_hmap and monitor_heap, + * 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) + OVS_REQUIRES(monitor_mutex) { struct mport *mport = mport_find(ofport); @@ -82,20 +108,22 @@ mport_register(const struct ofport_dpif *ofport, struct bfd *bfd, mport = xzalloc(sizeof *mport); mport->ofport = ofport; hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0)); + heap_insert(&monitor_heap, &mport->heap_node, 0); } mport_update(mport, bfd, cfm, hw_addr); } -/* Removes mport from monitor_hmap and frees it. */ +/* Removes mport from monitor_hmap and monitor_heap and frees it. */ static void mport_unregister(const struct ofport_dpif *ofport) - OVS_REQ_WRLOCK(monitor_rwlock) + OVS_REQUIRES(monitor_mutex) { struct mport *mport = mport_find(ofport); if (mport) { mport_update(mport, NULL, NULL, NULL); hmap_remove(&monitor_hmap, &mport->hmap_node); + heap_remove(&monitor_heap, &mport->heap_node); free(mport); } } @@ -103,7 +131,7 @@ mport_unregister(const struct ofport_dpif *ofport) /* 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) + uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex) { ovs_assert(mport); @@ -118,37 +146,52 @@ mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm, if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) { memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN); } + /* If bfd/cfm is added or reconfigured, move the mport on top of the heap + * so that the monitor thread can run the mport next time it wakes up. */ + if (mport->bfd || mport->cfm) { + heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX); + } } -/* 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]) +/* The 'main' function for the monitor thread. */ +static void * +monitor_main(void * args OVS_UNUSED) { - ovs_rwlock_wrlock(&monitor_rwlock); - if (!cfm && !bfd) { - mport_unregister(ofport); - } else { - mport_register(ofport, bfd, cfm, hw_addr); + VLOG_INFO("monitor thread created"); + while (!latch_is_set(&monitor_exit_latch)) { + monitor_run(); + latch_wait(&monitor_exit_latch); + poll_block(); } - ovs_rwlock_unlock(&monitor_rwlock); + VLOG_INFO("monitor thread terminated"); + return NULL; } -/* Checks the sending of control packets on all mports. Sends the control - * packets if needed. */ -void -ofproto_dpif_monitor_run_fast(void) +/* The monitor thread should wake up this often to ensure that newly added or + * reconfigured monitoring ports are run in a timely manner. */ +#define MONITOR_INTERVAL_MSEC 100 + +/* Checks the sending of control packets on mports that have timed out. + * Sends the control packets if needed. Executes bfd and cfm periodic + * functions (run, wait) on those mports. */ +static void +monitor_run(void) { uint32_t stub[512 / 4]; + long long int prio_now; struct ofpbuf packet; - struct mport *mport; ofpbuf_use_stub(&packet, stub, sizeof stub); - ovs_rwlock_rdlock(&monitor_rwlock); - HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) { + ovs_mutex_lock(&monitor_mutex); + prio_now = MSEC_TO_PRIO(time_msec()); + /* Peeks the top of heap and checks if we should run this mport. */ + while (!heap_is_empty(&monitor_heap) + && heap_max(&monitor_heap)->priority >= prio_now) { + long long int next_wake_time; + struct mport *mport; + + mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node); if (mport->cfm && cfm_should_send_ccm(mport->cfm)) { ofpbuf_clear(&packet); cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr); @@ -159,43 +202,85 @@ ofproto_dpif_monitor_run_fast(void) bfd_put_packet(mport->bfd, &packet, mport->hw_addr); ofproto_dpif_send_packet(mport->ofport, &packet); } + if (mport->cfm) { + cfm_run(mport->cfm); + cfm_wait(mport->cfm); + } + if (mport->bfd) { + bfd_run(mport->bfd); + bfd_wait(mport->bfd); + } + /* Computes the next wakeup time for this mport. */ + next_wake_time = MIN(bfd_wake_time(mport->bfd), + cfm_wake_time(mport->cfm)); + heap_change(&monitor_heap, &mport->heap_node, + MSEC_TO_PRIO(next_wake_time)); + } + + /* Waits on the earliest next wakeup time. */ + if (!heap_is_empty(&monitor_heap)) { + long long int next_timeout, next_mport_wakeup; + + next_timeout = time_msec() + MONITOR_INTERVAL_MSEC; + next_mport_wakeup = PRIO_TO_MSEC(heap_max(&monitor_heap)->priority); + poll_timer_wait_until(MIN(next_timeout, next_mport_wakeup)); } - ovs_rwlock_unlock(&monitor_rwlock); + ovs_mutex_unlock(&monitor_mutex); ofpbuf_uninit(&packet); } + -/* Executes bfd_run(), cfm_run() on all mports. */ +/* Creates the mport in monitor module if either bfd or cfm + * is configured. Otherwise, deletes the mport. + * Also checks whether the monitor thread should be started + * or terminated. */ void -ofproto_dpif_monitor_run(void) +ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport, + struct bfd *bfd, struct cfm *cfm, + uint8_t hw_addr[ETH_ADDR_LEN]) { - struct mport *mport; + ovs_mutex_lock(&monitor_mutex); + if (!cfm && !bfd) { + mport_unregister(ofport); + } else { + mport_register(ofport, bfd, cfm, hw_addr); + } + ovs_mutex_unlock(&monitor_mutex); - 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); - } + /* If the monitor thread is not running and the hmap + * is not empty, starts it. If it is and the hmap is empty, + * terminates it. */ + if (!monitor_running && !hmap_is_empty(&monitor_hmap)) { + latch_init(&monitor_exit_latch); + monitor_tid = ovs_thread_create("monitor", monitor_main, NULL); + monitor_running = true; + } else if (monitor_running && hmap_is_empty(&monitor_hmap)) { + latch_set(&monitor_exit_latch); + xpthread_join(monitor_tid, NULL); + latch_destroy(&monitor_exit_latch); + monitor_running = false; } - ovs_rwlock_unlock(&monitor_rwlock); } -/* Executes the bfd_wait() and cfm_wait() functions on all mports. */ +/* Moves the mport on top of the heap. This is necessary when + * for example, bfd POLL is received and the mport should + * immediately send FINAL back. */ +void +ofproto_dpif_monitor_port_send_soon_safe(const struct ofport_dpif *ofport) +{ + ovs_mutex_lock(&monitor_mutex); + ofproto_dpif_monitor_port_send_soon(ofport); + ovs_mutex_unlock(&monitor_mutex); +} + void -ofproto_dpif_monitor_wait(void) +ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport) + OVS_REQUIRES(monitor_mutex) { 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); - } + mport = mport_find(ofport); + if (mport) { + heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX); } - ovs_rwlock_unlock(&monitor_rwlock); }