2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
24 #include "guarded-list.h"
30 #include "ofproto-dpif.h"
31 #include "ovs-thread.h"
32 #include "poll-loop.h"
38 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
40 /* Converts the time in millisecond to heap priority. */
41 #define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME))
42 /* Converts the heap priority to time in millisecond. */
43 #define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO))
45 /* Monitored port. It owns references to ofport, bfd, cfm structs. */
47 struct hmap_node hmap_node; /* In monitor_hmap. */
48 struct heap_node heap_node; /* In monitor_heap. */
49 const struct ofport_dpif *ofport; /* The corresponding ofport. */
51 struct cfm *cfm; /* Reference to cfm. */
52 struct bfd *bfd; /* Reference to bfd. */
53 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
56 /* Entry of the 'send_soon' list. Contains the pointer to the
57 * 'ofport_dpif'. Note, the pointed object is not protected, so
58 * users should always use the mport_find() to convert it to 'mport'. */
59 struct send_soon_entry {
60 struct list list_node; /* In send_soon. */
61 const struct ofport_dpif *ofport;
64 /* hmap that contains "struct mport"s. */
65 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
67 /* heap for ordering mport based on bfd/cfm wakeup time. */
68 static struct heap monitor_heap;
70 /* guarded-list for storing the mports that need to send bfd/cfm control
72 static struct guarded_list send_soon = GUARDED_LIST_INITIALIZER(&send_soon);
74 /* The monitor thread id. */
75 static pthread_t monitor_tid;
76 /* True if the monitor thread is running. */
77 static bool monitor_running;
79 static struct latch monitor_exit_latch;
80 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
82 static void *monitor_main(void *);
83 static void monitor_check_send_soon(struct ofpbuf *);
84 static void monitor_run(void);
85 static void monitor_mport_run(struct mport *, struct ofpbuf *);
87 static void mport_register(const struct ofport_dpif *, struct bfd *,
88 struct cfm *, uint8_t[ETH_ADDR_LEN])
89 OVS_REQUIRES(monitor_mutex);
90 static void mport_unregister(const struct ofport_dpif *)
91 OVS_REQUIRES(monitor_mutex);
92 static void mport_update(struct mport *, struct bfd *, struct cfm *,
93 uint8_t[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex);
94 static struct mport *mport_find(const struct ofport_dpif *)
95 OVS_REQUIRES(monitor_mutex);
97 /* Tries finding and returning the 'mport' from the monitor_hmap.
98 * If there is no such 'mport', returns NULL. */
100 mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex)
104 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
106 if (node->ofport == ofport) {
113 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
114 * if it doesn't exist. Otherwise, just updates its fields. */
116 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
117 struct cfm *cfm, uint8_t *hw_addr)
118 OVS_REQUIRES(monitor_mutex)
120 struct mport *mport = mport_find(ofport);
123 mport = xzalloc(sizeof *mport);
124 mport->ofport = ofport;
125 hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
126 heap_insert(&monitor_heap, &mport->heap_node, 0);
128 mport_update(mport, bfd, cfm, hw_addr);
131 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
133 mport_unregister(const struct ofport_dpif *ofport)
134 OVS_REQUIRES(monitor_mutex)
136 struct mport *mport = mport_find(ofport);
139 mport_update(mport, NULL, NULL, NULL);
140 hmap_remove(&monitor_hmap, &mport->hmap_node);
141 heap_remove(&monitor_heap, &mport->heap_node);
146 /* Updates the fields of an existing mport struct. */
148 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
149 uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex)
153 if (mport->cfm != cfm) {
154 cfm_unref(mport->cfm);
155 mport->cfm = cfm_ref(cfm);
157 if (mport->bfd != bfd) {
158 bfd_unref(mport->bfd);
159 mport->bfd = bfd_ref(bfd);
161 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
162 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
164 /* If bfd/cfm is added or reconfigured, move the mport on top of the heap
165 * so that the monitor thread can run the mport next time it wakes up. */
166 if (mport->bfd || mport->cfm) {
167 heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
172 /* The 'main' function for the monitor thread. */
174 monitor_main(void * args OVS_UNUSED)
176 VLOG_INFO("monitor thread created");
177 while (!latch_is_set(&monitor_exit_latch)) {
179 latch_wait(&monitor_exit_latch);
182 VLOG_INFO("monitor thread terminated");
186 /* The monitor thread should wake up this often to ensure that newly added or
187 * reconfigured monitoring ports are run in a timely manner. */
188 #define MONITOR_INTERVAL_MSEC 100
190 /* Checks the 'send_soon' list and the heap for mports that have timed
191 * out bfd/cfm sessions. */
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_mutex_lock(&monitor_mutex);
202 /* The monitor_check_send_soon() needs to be run twice. The first
203 * time is for preventing the same 'mport' from being processed twice
204 * (i.e. once from heap, the other from the 'send_soon' array).
205 * The second run is to cover the case when the control packet is sent
206 * via patch port and the other end needs to send back immediately. */
207 monitor_check_send_soon(&packet);
209 prio_now = MSEC_TO_PRIO(time_msec());
210 /* Peeks the top of heap and checks if we should run this mport. */
211 while (!heap_is_empty(&monitor_heap)
212 && heap_max(&monitor_heap)->priority >= prio_now) {
215 mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
216 monitor_mport_run(mport, &packet);
219 monitor_check_send_soon(&packet);
221 /* Waits on the earliest next wakeup time. */
222 if (!heap_is_empty(&monitor_heap)) {
223 long long int next_timeout, next_mport_wakeup;
225 next_timeout = time_msec() + MONITOR_INTERVAL_MSEC;
226 next_mport_wakeup = PRIO_TO_MSEC(heap_max(&monitor_heap)->priority);
227 poll_timer_wait_until(MIN(next_timeout, next_mport_wakeup));
229 ovs_mutex_unlock(&monitor_mutex);
230 ofpbuf_uninit(&packet);
233 /* Checks the 'send_soon' list for any mport that needs to send cfm/bfd
234 * control packet immediately, and calls monitor_mport_run(). */
236 monitor_check_send_soon(struct ofpbuf *packet)
237 OVS_REQUIRES(monitor_mutex)
239 while (!guarded_list_is_empty(&send_soon)) {
240 struct send_soon_entry *entry;
243 entry = CONTAINER_OF(guarded_list_pop_front(&send_soon),
244 struct send_soon_entry, list_node);
245 mport = mport_find(entry->ofport);
247 monitor_mport_run(mport, packet);
253 /* Checks the sending of control packet on 'mport'. Sends the control
254 * packet if needed. Executes bfd and cfm periodic functions (run, wait)
255 * on 'mport'. And changes the location of 'mport' in heap based on next
258 monitor_mport_run(struct mport *mport, struct ofpbuf *packet)
259 OVS_REQUIRES(monitor_mutex)
261 long long int next_wake_time;
263 if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
264 ofpbuf_clear(packet);
265 cfm_compose_ccm(mport->cfm, packet, mport->hw_addr);
266 ofproto_dpif_send_packet(mport->ofport, packet);
268 if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
269 ofpbuf_clear(packet);
270 bfd_put_packet(mport->bfd, packet, mport->hw_addr);
271 ofproto_dpif_send_packet(mport->ofport, packet);
275 cfm_wait(mport->cfm);
279 bfd_wait(mport->bfd);
281 /* Computes the next wakeup time for this mport. */
282 next_wake_time = MIN(bfd_wake_time(mport->bfd),
283 cfm_wake_time(mport->cfm));
284 heap_change(&monitor_heap, &mport->heap_node,
285 MSEC_TO_PRIO(next_wake_time));
289 /* Creates the mport in monitor module if either bfd or cfm
290 * is configured. Otherwise, deletes the mport.
291 * Also checks whether the monitor thread should be started
294 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
295 struct bfd *bfd, struct cfm *cfm,
296 uint8_t hw_addr[ETH_ADDR_LEN])
298 ovs_mutex_lock(&monitor_mutex);
300 mport_unregister(ofport);
302 mport_register(ofport, bfd, cfm, hw_addr);
304 ovs_mutex_unlock(&monitor_mutex);
306 /* If the monitor thread is not running and the hmap
307 * is not empty, starts it. If it is and the hmap is empty,
309 if (!monitor_running && !hmap_is_empty(&monitor_hmap)) {
310 latch_init(&monitor_exit_latch);
311 monitor_tid = ovs_thread_create("monitor", monitor_main, NULL);
312 monitor_running = true;
313 } else if (monitor_running && hmap_is_empty(&monitor_hmap)) {
314 latch_set(&monitor_exit_latch);
315 xpthread_join(monitor_tid, NULL);
316 latch_destroy(&monitor_exit_latch);
317 monitor_running = false;
321 /* Registers the 'ofport' in the 'send_soon' list. We cannot directly
322 * insert the corresponding mport to the 'send_soon' list, since the
323 * 'send_soon' list is not updated when the mport is removed.
325 * Reader of the 'send_soon' list is responsible for freeing the entry. */
327 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
329 struct send_soon_entry *entry = xzalloc(sizeof *entry);
330 entry->ofport = ofport;
332 guarded_list_push_back(&send_soon, &entry->list_node, SIZE_MAX);