]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-monitor.c
lib: Add API to set program name and version
[mirror_ovs.git] / ofproto / ofproto-dpif-monitor.c
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <config.h>
18 #include "ofproto-dpif-monitor.h"
19
20 #include <string.h>
21
22 #include "bfd.h"
23 #include "cfm.h"
24 #include "guarded-list.h"
25 #include "hash.h"
26 #include "heap.h"
27 #include "hmap.h"
28 #include "latch.h"
29 #include "ofpbuf.h"
30 #include "ofproto-dpif.h"
31 #include "ovs-thread.h"
32 #include "poll-loop.h"
33 #include "seq.h"
34 #include "timeval.h"
35 #include "util.h"
36 #include "vlog.h"
37
38 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
39
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))
44
45 /* Monitored port. It owns references to ofport, bfd, cfm structs. */
46 struct mport {
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. */
50
51 struct cfm *cfm; /* Reference to cfm. */
52 struct bfd *bfd; /* Reference to bfd. */
53 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
54 };
55
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;
62 };
63
64 /* hmap that contains "struct mport"s. */
65 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
66
67 /* heap for ordering mport based on bfd/cfm wakeup time. */
68 static struct heap monitor_heap;
69
70 /* guarded-list for storing the mports that need to send bfd/cfm control
71 * packet soon. */
72 static struct guarded_list send_soon = GUARDED_LIST_INITIALIZER(&send_soon);
73
74 /* The monitor thread id. */
75 static pthread_t monitor_tid;
76 /* True if the monitor thread is running. */
77 static bool monitor_running;
78
79 static struct latch monitor_exit_latch;
80 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
81
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 *);
86
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);
96
97 /* Tries finding and returning the 'mport' from the monitor_hmap.
98 * If there is no such 'mport', returns NULL. */
99 static struct mport *
100 mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex)
101 {
102 struct mport *node;
103
104 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
105 &monitor_hmap) {
106 if (node->ofport == ofport) {
107 return node;
108 }
109 }
110 return NULL;
111 }
112
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. */
115 static void
116 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
117 struct cfm *cfm, uint8_t *hw_addr)
118 OVS_REQUIRES(monitor_mutex)
119 {
120 struct mport *mport = mport_find(ofport);
121
122 if (!mport) {
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);
127 }
128 mport_update(mport, bfd, cfm, hw_addr);
129 }
130
131 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
132 static void
133 mport_unregister(const struct ofport_dpif *ofport)
134 OVS_REQUIRES(monitor_mutex)
135 {
136 struct mport *mport = mport_find(ofport);
137
138 if (mport) {
139 mport_update(mport, NULL, NULL, NULL);
140 hmap_remove(&monitor_hmap, &mport->hmap_node);
141 heap_remove(&monitor_heap, &mport->heap_node);
142 free(mport);
143 }
144 }
145
146 /* Updates the fields of an existing mport struct. */
147 static void
148 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
149 uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex)
150 {
151 ovs_assert(mport);
152
153 if (mport->cfm != cfm) {
154 cfm_unref(mport->cfm);
155 mport->cfm = cfm_ref(cfm);
156 }
157 if (mport->bfd != bfd) {
158 bfd_unref(mport->bfd);
159 mport->bfd = bfd_ref(bfd);
160 }
161 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
162 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
163 }
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);
168 }
169 }
170 \f
171
172 /* The 'main' function for the monitor thread. */
173 static void *
174 monitor_main(void * args OVS_UNUSED)
175 {
176 VLOG_INFO("monitor thread created");
177 while (!latch_is_set(&monitor_exit_latch)) {
178 monitor_run();
179 latch_wait(&monitor_exit_latch);
180 poll_block();
181 }
182 VLOG_INFO("monitor thread terminated");
183 return NULL;
184 }
185
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
189
190 /* Checks the 'send_soon' list and the heap for mports that have timed
191 * out bfd/cfm sessions. */
192 static void
193 monitor_run(void)
194 {
195 uint32_t stub[512 / 4];
196 long long int prio_now;
197 struct ofpbuf packet;
198
199 ofpbuf_use_stub(&packet, stub, sizeof stub);
200 ovs_mutex_lock(&monitor_mutex);
201
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);
208
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) {
213 struct mport *mport;
214
215 mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
216 monitor_mport_run(mport, &packet);
217 }
218
219 monitor_check_send_soon(&packet);
220
221 /* Waits on the earliest next wakeup time. */
222 if (!heap_is_empty(&monitor_heap)) {
223 long long int next_timeout, next_mport_wakeup;
224
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));
228 }
229 ovs_mutex_unlock(&monitor_mutex);
230 ofpbuf_uninit(&packet);
231 }
232
233 /* Checks the 'send_soon' list for any mport that needs to send cfm/bfd
234 * control packet immediately, and calls monitor_mport_run(). */
235 static void
236 monitor_check_send_soon(struct ofpbuf *packet)
237 OVS_REQUIRES(monitor_mutex)
238 {
239 while (!guarded_list_is_empty(&send_soon)) {
240 struct send_soon_entry *entry;
241 struct mport *mport;
242
243 entry = CONTAINER_OF(guarded_list_pop_front(&send_soon),
244 struct send_soon_entry, list_node);
245 mport = mport_find(entry->ofport);
246 if (mport) {
247 monitor_mport_run(mport, packet);
248 }
249 free(entry);
250 }
251 }
252
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
256 * timeout. */
257 static void
258 monitor_mport_run(struct mport *mport, struct ofpbuf *packet)
259 OVS_REQUIRES(monitor_mutex)
260 {
261 long long int next_wake_time;
262
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);
267 }
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);
272 }
273 if (mport->cfm) {
274 cfm_run(mport->cfm);
275 cfm_wait(mport->cfm);
276 }
277 if (mport->bfd) {
278 bfd_run(mport->bfd);
279 bfd_wait(mport->bfd);
280 }
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));
286 }
287 \f
288
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
292 * or terminated. */
293 void
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])
297 {
298 ovs_mutex_lock(&monitor_mutex);
299 if (!cfm && !bfd) {
300 mport_unregister(ofport);
301 } else {
302 mport_register(ofport, bfd, cfm, hw_addr);
303 }
304 ovs_mutex_unlock(&monitor_mutex);
305
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,
308 * terminates it. */
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;
318 }
319 }
320
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.
324 *
325 * Reader of the 'send_soon' list is responsible for freeing the entry. */
326 void
327 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
328 {
329 struct send_soon_entry *entry = xzalloc(sizeof *entry);
330 entry->ofport = ofport;
331
332 guarded_list_push_back(&send_soon, &entry->list_node, SIZE_MAX);
333 }