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 "dp-packet.h"
25 #include "guarded-list.h"
31 #include "ofproto-dpif.h"
32 #include "ovs-thread.h"
33 #include "poll-loop.h"
37 #include "openvswitch/vlog.h"
39 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
41 /* Converts the time in millisecond to heap priority. */
42 #define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME))
43 /* Converts the heap priority to time in millisecond. */
44 #define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO))
46 /* Monitored port. It owns references to ofport, bfd, cfm structs. */
48 struct hmap_node hmap_node; /* In monitor_hmap. */
49 struct heap_node heap_node; /* In monitor_heap. */
50 const struct ofport_dpif *ofport; /* The corresponding ofport. */
52 struct cfm *cfm; /* Reference to cfm. */
53 struct bfd *bfd; /* Reference to bfd. */
54 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
57 /* Entry of the 'send_soon' list. Contains the pointer to the
58 * 'ofport_dpif'. Note, the pointed object is not protected, so
59 * users should always use the mport_find() to convert it to 'mport'. */
60 struct send_soon_entry {
61 struct ovs_list list_node; /* In send_soon. */
62 const struct ofport_dpif *ofport;
65 /* hmap that contains "struct mport"s. */
66 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
68 /* heap for ordering mport based on bfd/cfm wakeup time. */
69 static struct heap monitor_heap;
71 /* guarded-list for storing the mports that need to send bfd/cfm control
73 static struct guarded_list send_soon = GUARDED_OVS_LIST_INITIALIZER(&send_soon);
75 /* The monitor thread id. */
76 static pthread_t monitor_tid;
77 /* True if the monitor thread is running. */
78 static bool monitor_running;
80 static struct latch monitor_exit_latch;
81 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
83 static void *monitor_main(void *);
84 static void monitor_check_send_soon(struct dp_packet *);
85 static void monitor_run(void);
86 static void monitor_mport_run(struct mport *, struct dp_packet *);
88 static void mport_register(const struct ofport_dpif *, struct bfd *,
89 struct cfm *, uint8_t[ETH_ADDR_LEN])
90 OVS_REQUIRES(monitor_mutex);
91 static void mport_unregister(const struct ofport_dpif *)
92 OVS_REQUIRES(monitor_mutex);
93 static void mport_update(struct mport *, struct bfd *, struct cfm *,
94 uint8_t[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex);
95 static struct mport *mport_find(const struct ofport_dpif *)
96 OVS_REQUIRES(monitor_mutex);
98 /* Tries finding and returning the 'mport' from the monitor_hmap.
99 * If there is no such 'mport', returns NULL. */
100 static struct mport *
101 mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex)
105 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
107 if (node->ofport == ofport) {
114 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
115 * if it doesn't exist. Otherwise, just updates its fields. */
117 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
118 struct cfm *cfm, uint8_t *hw_addr)
119 OVS_REQUIRES(monitor_mutex)
121 struct mport *mport = mport_find(ofport);
124 mport = xzalloc(sizeof *mport);
125 mport->ofport = ofport;
126 hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
127 heap_insert(&monitor_heap, &mport->heap_node, 0);
129 mport_update(mport, bfd, cfm, hw_addr);
132 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
134 mport_unregister(const struct ofport_dpif *ofport)
135 OVS_REQUIRES(monitor_mutex)
137 struct mport *mport = mport_find(ofport);
140 mport_update(mport, NULL, NULL, NULL);
141 hmap_remove(&monitor_hmap, &mport->hmap_node);
142 heap_remove(&monitor_heap, &mport->heap_node);
147 /* Updates the fields of an existing mport struct. */
149 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
150 uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex)
154 if (mport->cfm != cfm) {
155 cfm_unref(mport->cfm);
156 mport->cfm = cfm_ref(cfm);
158 if (mport->bfd != bfd) {
159 bfd_unref(mport->bfd);
160 mport->bfd = bfd_ref(bfd);
162 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
163 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
165 /* If bfd/cfm is added or reconfigured, move the mport on top of the heap
166 * so that the monitor thread can run the mport next time it wakes up. */
167 if (mport->bfd || mport->cfm) {
168 heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
173 /* The 'main' function for the monitor thread. */
175 monitor_main(void * args OVS_UNUSED)
177 VLOG_INFO("monitor thread created");
178 while (!latch_is_set(&monitor_exit_latch)) {
180 latch_wait(&monitor_exit_latch);
183 VLOG_INFO("monitor thread terminated");
187 /* The monitor thread should wake up this often to ensure that newly added or
188 * reconfigured monitoring ports are run in a timely manner. */
189 #define MONITOR_INTERVAL_MSEC 100
191 /* Checks the 'send_soon' list and the heap for mports that have timed
192 * out bfd/cfm sessions. */
196 uint32_t stub[512 / 4];
197 long long int prio_now;
198 struct dp_packet packet;
200 dp_packet_use_stub(&packet, stub, sizeof stub);
201 ovs_mutex_lock(&monitor_mutex);
203 /* The monitor_check_send_soon() needs to be run twice. The first
204 * time is for preventing the same 'mport' from being processed twice
205 * (i.e. once from heap, the other from the 'send_soon' array).
206 * The second run is to cover the case when the control packet is sent
207 * via patch port and the other end needs to send back immediately. */
208 monitor_check_send_soon(&packet);
210 prio_now = MSEC_TO_PRIO(time_msec());
211 /* Peeks the top of heap and checks if we should run this mport. */
212 while (!heap_is_empty(&monitor_heap)
213 && heap_max(&monitor_heap)->priority >= prio_now) {
216 mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
217 monitor_mport_run(mport, &packet);
220 monitor_check_send_soon(&packet);
222 /* Waits on the earliest next wakeup time. */
223 if (!heap_is_empty(&monitor_heap)) {
224 long long int next_timeout, next_mport_wakeup;
226 next_timeout = time_msec() + MONITOR_INTERVAL_MSEC;
227 next_mport_wakeup = PRIO_TO_MSEC(heap_max(&monitor_heap)->priority);
228 poll_timer_wait_until(MIN(next_timeout, next_mport_wakeup));
230 ovs_mutex_unlock(&monitor_mutex);
231 dp_packet_uninit(&packet);
234 /* Checks the 'send_soon' list for any mport that needs to send cfm/bfd
235 * control packet immediately, and calls monitor_mport_run(). */
237 monitor_check_send_soon(struct dp_packet *packet)
238 OVS_REQUIRES(monitor_mutex)
240 while (!guarded_list_is_empty(&send_soon)) {
241 struct send_soon_entry *entry;
244 entry = CONTAINER_OF(guarded_list_pop_front(&send_soon),
245 struct send_soon_entry, list_node);
246 mport = mport_find(entry->ofport);
248 monitor_mport_run(mport, packet);
254 /* Checks the sending of control packet on 'mport'. Sends the control
255 * packet if needed. Executes bfd and cfm periodic functions (run, wait)
256 * on 'mport'. And changes the location of 'mport' in heap based on next
259 monitor_mport_run(struct mport *mport, struct dp_packet *packet)
260 OVS_REQUIRES(monitor_mutex)
262 long long int next_wake_time;
264 if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
265 dp_packet_clear(packet);
266 cfm_compose_ccm(mport->cfm, packet, mport->hw_addr);
267 ofproto_dpif_send_packet(mport->ofport, packet);
269 if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
270 dp_packet_clear(packet);
271 bfd_put_packet(mport->bfd, packet, mport->hw_addr);
272 ofproto_dpif_send_packet(mport->ofport, packet);
276 cfm_wait(mport->cfm);
280 bfd_wait(mport->bfd);
282 /* Computes the next wakeup time for this mport. */
283 next_wake_time = MIN(bfd_wake_time(mport->bfd),
284 cfm_wake_time(mport->cfm));
285 heap_change(&monitor_heap, &mport->heap_node,
286 MSEC_TO_PRIO(next_wake_time));
290 /* Creates the mport in monitor module if either bfd or cfm
291 * is configured. Otherwise, deletes the mport.
292 * Also checks whether the monitor thread should be started
295 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
296 struct bfd *bfd, struct cfm *cfm,
297 uint8_t hw_addr[ETH_ADDR_LEN])
299 ovs_mutex_lock(&monitor_mutex);
301 mport_unregister(ofport);
303 mport_register(ofport, bfd, cfm, hw_addr);
305 ovs_mutex_unlock(&monitor_mutex);
307 /* If the monitor thread is not running and the hmap
308 * is not empty, starts it. If it is and the hmap is empty,
310 if (!monitor_running && !hmap_is_empty(&monitor_hmap)) {
311 latch_init(&monitor_exit_latch);
312 monitor_tid = ovs_thread_create("monitor", monitor_main, NULL);
313 monitor_running = true;
314 } else if (monitor_running && hmap_is_empty(&monitor_hmap)) {
315 latch_set(&monitor_exit_latch);
316 xpthread_join(monitor_tid, NULL);
317 latch_destroy(&monitor_exit_latch);
318 monitor_running = false;
322 /* Registers the 'ofport' in the 'send_soon' list. We cannot directly
323 * insert the corresponding mport to the 'send_soon' list, since the
324 * 'send_soon' list is not updated when the mport is removed.
326 * Reader of the 'send_soon' list is responsible for freeing the entry. */
328 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
330 struct send_soon_entry *entry = xzalloc(sizeof *entry);
331 entry->ofport = ofport;
333 guarded_list_push_back(&send_soon, &entry->list_node, SIZE_MAX);