2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofproto-dpif-monitor.h"
29 #include "ofproto-dpif.h"
30 #include "ovs-thread.h"
31 #include "poll-loop.h"
37 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
39 /* Converts the time in millisecond to heap priority. */
40 #define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME))
41 /* Converts the heap priority to time in millisecond. */
42 #define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO))
44 /* Monitored port. It owns references to ofport, bfd, cfm structs. */
46 struct hmap_node hmap_node; /* In monitor_hmap. */
47 struct heap_node heap_node; /* In monitor_heap. */
48 const struct ofport_dpif *ofport; /* The corresponding ofport. */
50 struct cfm *cfm; /* Reference to cfm. */
51 struct bfd *bfd; /* Reference to bfd. */
52 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
55 /* hmap that contains "struct mport"s. */
56 static struct hmap monitor_hmap;
58 /* heap for ordering mport based on bfd/cfm wakeup time. */
59 static struct heap monitor_heap;
61 /* The monitor thread id. */
62 static pthread_t monitor_tid;
63 /* True if the monitor thread is running. */
64 static bool monitor_running;
66 static struct latch monitor_exit_latch;
67 static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER;
69 static void monitor_init(void);
70 static void *monitor_main(void *);
71 static void monitor_run(void);
73 static void mport_register(const struct ofport_dpif *, struct bfd *,
74 struct cfm *, uint8_t[ETH_ADDR_LEN])
75 OVS_REQ_WRLOCK(monitor_rwlock);
76 static void mport_unregister(const struct ofport_dpif *)
77 OVS_REQ_WRLOCK(monitor_rwlock);
78 static void mport_update(struct mport *, struct bfd *, struct cfm *,
79 uint8_t[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock);
80 static struct mport *mport_find(const struct ofport_dpif *)
81 OVS_REQ_WRLOCK(monitor_rwlock);
83 /* Tries finding and returning the 'mport' from the monitor_hmap.
84 * If there is no such 'mport', returns NULL. */
86 mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock)
90 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
92 if (node->ofport == ofport) {
99 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
100 * if it doesn't exist. Otherwise, just updates its fields. */
102 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
103 struct cfm *cfm, uint8_t *hw_addr)
104 OVS_REQ_WRLOCK(monitor_rwlock)
106 struct mport *mport = mport_find(ofport);
109 mport = xzalloc(sizeof *mport);
110 mport->ofport = ofport;
111 hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
112 heap_insert(&monitor_heap, &mport->heap_node, 0);
114 mport_update(mport, bfd, cfm, hw_addr);
117 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
119 mport_unregister(const struct ofport_dpif *ofport)
120 OVS_REQ_WRLOCK(monitor_rwlock)
122 struct mport *mport = mport_find(ofport);
125 mport_update(mport, NULL, NULL, NULL);
126 hmap_remove(&monitor_hmap, &mport->hmap_node);
127 heap_remove(&monitor_heap, &mport->heap_node);
132 /* Updates the fields of an existing mport struct. */
134 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
135 uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock)
139 if (mport->cfm != cfm) {
140 cfm_unref(mport->cfm);
141 mport->cfm = cfm_ref(cfm);
143 if (mport->bfd != bfd) {
144 bfd_unref(mport->bfd);
145 mport->bfd = bfd_ref(bfd);
147 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
148 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
150 /* If bfd/cfm is added or reconfigured, move the mport on top of the heap
151 * so that the monitor thread can run the mport next time it wakes up. */
152 if (mport->bfd || mport->cfm) {
153 heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
158 /* Initializes the global variables. This will only run once. */
162 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
164 if (ovsthread_once_start(&once)) {
165 hmap_init(&monitor_hmap);
166 ovsthread_once_done(&once);
170 /* The 'main' function for the monitor thread. */
172 monitor_main(void * args OVS_UNUSED)
174 set_subprogram_name("monitor");
175 VLOG_INFO("monitor thread created");
176 while (!latch_is_set(&monitor_exit_latch)) {
178 latch_wait(&monitor_exit_latch);
181 VLOG_INFO("monitor thread terminated");
185 /* The monitor thread should wake up this often to ensure that newly added or
186 * reconfigured monitoring ports are run in a timely manner. */
187 #define MONITOR_INTERVAL_MSEC 100
189 /* Checks the sending of control packets on mports that have timed out.
190 * Sends the control packets if needed. Executes bfd and cfm periodic
191 * functions (run, wait) on those mports. */
195 uint32_t stub[512 / 4];
196 long long int prio_now;
197 struct ofpbuf packet;
199 ofpbuf_use_stub(&packet, stub, sizeof stub);
200 ovs_rwlock_rdlock(&monitor_rwlock);
201 prio_now = MSEC_TO_PRIO(time_msec());
202 /* Peeks the top of heap and checks if we should run this mport. */
203 while (!heap_is_empty(&monitor_heap)
204 && heap_max(&monitor_heap)->priority >= prio_now) {
205 long long int next_wake_time;
208 mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
209 if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
210 ofpbuf_clear(&packet);
211 cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr);
212 ofproto_dpif_send_packet(mport->ofport, &packet);
214 if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
215 ofpbuf_clear(&packet);
216 bfd_put_packet(mport->bfd, &packet, mport->hw_addr);
217 ofproto_dpif_send_packet(mport->ofport, &packet);
221 cfm_wait(mport->cfm);
225 bfd_wait(mport->bfd);
227 /* Computes the next wakeup time for this mport. */
228 next_wake_time = MIN(bfd_wake_time(mport->bfd), cfm_wake_time(mport->cfm));
229 heap_change(&monitor_heap, heap_max(&monitor_heap),
230 MSEC_TO_PRIO(next_wake_time));
233 /* Waits on the earliest next wakeup time. */
234 if (!heap_is_empty(&monitor_heap)) {
235 long long int next_timeout, next_mport_wakeup;
237 next_timeout = time_msec() + MONITOR_INTERVAL_MSEC;
238 next_mport_wakeup = PRIO_TO_MSEC(heap_max(&monitor_heap)->priority);
239 poll_timer_wait_until(MIN(next_timeout, next_mport_wakeup));
241 ovs_rwlock_unlock(&monitor_rwlock);
242 ofpbuf_uninit(&packet);
246 /* Creates the mport in monitor module if either bfd or cfm
247 * is configured. Otherwise, deletes the mport.
248 * Also checks whether the monitor thread should be started
251 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
252 struct bfd *bfd, struct cfm *cfm,
253 uint8_t hw_addr[ETH_ADDR_LEN])
256 ovs_rwlock_wrlock(&monitor_rwlock);
258 mport_unregister(ofport);
260 mport_register(ofport, bfd, cfm, hw_addr);
262 ovs_rwlock_unlock(&monitor_rwlock);
264 /* If the monitor thread is not running and the hmap
265 * is not empty, starts it. If it is and the hmap is empty,
267 if (!monitor_running && !hmap_is_empty(&monitor_hmap)) {
268 latch_init(&monitor_exit_latch);
269 xpthread_create(&monitor_tid, NULL, monitor_main, NULL);
270 monitor_running = true;
271 } else if (monitor_running && hmap_is_empty(&monitor_hmap)) {
272 latch_set(&monitor_exit_latch);
273 xpthread_join(monitor_tid, NULL);
274 latch_destroy(&monitor_exit_latch);
275 monitor_running = false;