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"
33 #include "ovs-thread.h"
34 #include "poll-loop.h"
38 #include "openvswitch/vlog.h"
40 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
42 /* Converts the time in millisecond to heap priority. */
43 #define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME))
44 /* Converts the heap priority to time in millisecond. */
45 #define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO))
47 /* Monitored port. It owns references to ofport, bfd, cfm, and lldp structs. */
49 struct hmap_node hmap_node; /* In monitor_hmap. */
50 struct heap_node heap_node; /* In monitor_heap. */
51 const struct ofport_dpif *ofport; /* The corresponding ofport. */
53 struct cfm *cfm; /* Reference to cfm. */
54 struct bfd *bfd; /* Reference to bfd. */
55 struct lldp *lldp; /* Reference to lldp. */
56 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
59 /* Entry of the 'send_soon' list. Contains the pointer to the
60 * 'ofport_dpif'. Note, the pointed object is not protected, so
61 * users should always use the mport_find() to convert it to 'mport'. */
62 struct send_soon_entry {
63 struct ovs_list list_node; /* In send_soon. */
64 const struct ofport_dpif *ofport;
67 /* hmap that contains "struct mport"s. */
68 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
70 /* heap for ordering mport based on bfd/cfm wakeup time. */
71 static struct heap monitor_heap;
73 /* guarded-list for storing the mports that need to send bfd/cfm control
75 static struct guarded_list send_soon = GUARDED_OVS_LIST_INITIALIZER(&send_soon);
77 /* The monitor thread id. */
78 static pthread_t monitor_tid;
79 /* True if the monitor thread is running. */
80 static bool monitor_running;
82 static struct latch monitor_exit_latch;
83 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
85 static void *monitor_main(void *);
86 static void monitor_check_send_soon(struct dp_packet *);
87 static void monitor_run(void);
88 static void monitor_mport_run(struct mport *, struct dp_packet *);
90 static void mport_register(const struct ofport_dpif *, struct bfd *,
91 struct cfm *, struct lldp *, uint8_t[ETH_ADDR_LEN])
92 OVS_REQUIRES(monitor_mutex);
93 static void mport_unregister(const struct ofport_dpif *)
94 OVS_REQUIRES(monitor_mutex);
95 static void mport_update(struct mport *, struct bfd *, struct cfm *,
96 struct lldp *, uint8_t[ETH_ADDR_LEN])
97 OVS_REQUIRES(monitor_mutex);
98 static struct mport *mport_find(const struct ofport_dpif *)
99 OVS_REQUIRES(monitor_mutex);
101 /* Tries finding and returning the 'mport' from the monitor_hmap.
102 * If there is no such 'mport', returns NULL. */
103 static struct mport *
104 mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex)
108 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
110 if (node->ofport == ofport) {
117 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
118 * if it doesn't exist. Otherwise, just updates its fields. */
120 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
121 struct cfm *cfm, struct lldp *lldp, uint8_t *hw_addr)
122 OVS_REQUIRES(monitor_mutex)
124 struct mport *mport = mport_find(ofport);
127 mport = xzalloc(sizeof *mport);
128 mport->ofport = ofport;
129 hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
130 heap_insert(&monitor_heap, &mport->heap_node, 0);
132 mport_update(mport, bfd, cfm, lldp, hw_addr);
135 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
137 mport_unregister(const struct ofport_dpif *ofport)
138 OVS_REQUIRES(monitor_mutex)
140 struct mport *mport = mport_find(ofport);
143 mport_update(mport, NULL, NULL, NULL, NULL);
144 hmap_remove(&monitor_hmap, &mport->hmap_node);
145 heap_remove(&monitor_heap, &mport->heap_node);
150 /* Updates the fields of an existing mport struct. */
152 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
153 struct lldp *lldp, uint8_t hw_addr[ETH_ADDR_LEN])
154 OVS_REQUIRES(monitor_mutex)
158 if (mport->cfm != cfm) {
159 cfm_unref(mport->cfm);
160 mport->cfm = cfm_ref(cfm);
162 if (mport->bfd != bfd) {
163 bfd_unref(mport->bfd);
164 mport->bfd = bfd_ref(bfd);
166 if (mport->lldp != lldp) {
167 lldp_unref(mport->lldp);
168 mport->lldp = lldp_ref(lldp);
170 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
171 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
173 /* If bfd/cfm/lldp is added or reconfigured, move the mport on top of the heap
174 * so that the monitor thread can run the mport next time it wakes up. */
175 if (mport->bfd || mport->cfm || mport->lldp) {
176 heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
181 /* The 'main' function for the monitor thread. */
183 monitor_main(void * args OVS_UNUSED)
185 VLOG_INFO("monitor thread created");
186 while (!latch_is_set(&monitor_exit_latch)) {
188 latch_wait(&monitor_exit_latch);
191 VLOG_INFO("monitor thread terminated");
195 /* The monitor thread should wake up this often to ensure that newly added or
196 * reconfigured monitoring ports are run in a timely manner. */
197 #define MONITOR_INTERVAL_MSEC 100
199 /* Checks the 'send_soon' list and the heap for mports that have timed
200 * out bfd/cfm sessions. */
204 uint32_t stub[512 / 4];
205 long long int prio_now;
206 struct dp_packet packet;
208 dp_packet_use_stub(&packet, stub, sizeof stub);
209 ovs_mutex_lock(&monitor_mutex);
211 /* The monitor_check_send_soon() needs to be run twice. The first
212 * time is for preventing the same 'mport' from being processed twice
213 * (i.e. once from heap, the other from the 'send_soon' array).
214 * The second run is to cover the case when the control packet is sent
215 * via patch port and the other end needs to send back immediately. */
216 monitor_check_send_soon(&packet);
218 prio_now = MSEC_TO_PRIO(time_msec());
219 /* Peeks the top of heap and checks if we should run this mport. */
220 while (!heap_is_empty(&monitor_heap)
221 && heap_max(&monitor_heap)->priority >= prio_now) {
224 mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
225 monitor_mport_run(mport, &packet);
228 monitor_check_send_soon(&packet);
230 /* Waits on the earliest next wakeup time. */
231 if (!heap_is_empty(&monitor_heap)) {
232 long long int next_timeout, next_mport_wakeup;
234 next_timeout = time_msec() + MONITOR_INTERVAL_MSEC;
235 next_mport_wakeup = PRIO_TO_MSEC(heap_max(&monitor_heap)->priority);
236 poll_timer_wait_until(MIN(next_timeout, next_mport_wakeup));
238 ovs_mutex_unlock(&monitor_mutex);
239 dp_packet_uninit(&packet);
242 /* Checks the 'send_soon' list for any mport that needs to send cfm/bfd
243 * control packet immediately, and calls monitor_mport_run(). */
245 monitor_check_send_soon(struct dp_packet *packet)
246 OVS_REQUIRES(monitor_mutex)
248 while (!guarded_list_is_empty(&send_soon)) {
249 struct send_soon_entry *entry;
252 entry = CONTAINER_OF(guarded_list_pop_front(&send_soon),
253 struct send_soon_entry, list_node);
254 mport = mport_find(entry->ofport);
256 monitor_mport_run(mport, packet);
262 /* Checks the sending of control packet on 'mport'. Sends the control
263 * packet if needed. Executes bfd and cfm periodic functions (run, wait)
264 * on 'mport'. And changes the location of 'mport' in heap based on next
267 monitor_mport_run(struct mport *mport, struct dp_packet *packet)
268 OVS_REQUIRES(monitor_mutex)
270 long long int next_wake_time;
271 long long int bfd_wake_time = LLONG_MAX;
272 long long int cfm_wake_time = LLONG_MAX;
273 long long int lldp_wake_time = LLONG_MAX;
275 if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
276 dp_packet_clear(packet);
277 cfm_compose_ccm(mport->cfm, packet, mport->hw_addr);
278 ofproto_dpif_send_packet(mport->ofport, packet);
280 if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
281 dp_packet_clear(packet);
282 bfd_put_packet(mport->bfd, packet, mport->hw_addr);
283 ofproto_dpif_send_packet(mport->ofport, packet);
285 if (mport->lldp && lldp_should_send_packet(mport->lldp)) {
286 dp_packet_clear(packet);
287 lldp_put_packet(mport->lldp, packet, mport->hw_addr);
288 ofproto_dpif_send_packet(mport->ofport, packet);
293 cfm_wake_time = cfm_wait(mport->cfm);
297 bfd_wake_time = bfd_wait(mport->bfd);
300 lldp_wake_time = lldp_wait(mport->lldp);
302 /* Computes the next wakeup time for this mport. */
303 next_wake_time = MIN(bfd_wake_time,
305 next_wake_time = MIN(next_wake_time, lldp_wake_time);
306 heap_change(&monitor_heap, &mport->heap_node,
307 MSEC_TO_PRIO(next_wake_time));
311 /* Creates the mport in monitor module if either bfd or cfm
312 * is configured. Otherwise, deletes the mport.
313 * Also checks whether the monitor thread should be started
316 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
317 struct bfd *bfd, struct cfm *cfm,
319 uint8_t hw_addr[ETH_ADDR_LEN])
321 ovs_mutex_lock(&monitor_mutex);
322 if (!cfm && !bfd && !lldp) {
323 mport_unregister(ofport);
325 mport_register(ofport, bfd, cfm, lldp, hw_addr);
327 ovs_mutex_unlock(&monitor_mutex);
329 /* If the monitor thread is not running and the hmap
330 * is not empty, starts it. If it is and the hmap is empty,
332 if (!monitor_running && !hmap_is_empty(&monitor_hmap)) {
333 latch_init(&monitor_exit_latch);
334 monitor_tid = ovs_thread_create("monitor", monitor_main, NULL);
335 monitor_running = true;
336 } else if (monitor_running && hmap_is_empty(&monitor_hmap)) {
337 latch_set(&monitor_exit_latch);
338 xpthread_join(monitor_tid, NULL);
339 latch_destroy(&monitor_exit_latch);
340 monitor_running = false;
344 /* Registers the 'ofport' in the 'send_soon' list. We cannot directly
345 * insert the corresponding mport to the 'send_soon' list, since the
346 * 'send_soon' list is not updated when the mport is removed.
348 * Reader of the 'send_soon' list is responsible for freeing the entry. */
350 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
352 struct send_soon_entry *entry = xzalloc(sizeof *entry);
353 entry->ofport = ofport;
355 guarded_list_push_back(&send_soon, &entry->list_node, SIZE_MAX);