dp-packet: Remove ofpbuf dependency.
[cascardo/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 "dp-packet.h"
25 #include "guarded-list.h"
26 #include "hash.h"
27 #include "heap.h"
28 #include "hmap.h"
29 #include "latch.h"
30 #include "ofpbuf.h"
31 #include "ofproto-dpif.h"
32 #include "ovs-thread.h"
33 #include "poll-loop.h"
34 #include "seq.h"
35 #include "timeval.h"
36 #include "util.h"
37 #include "openvswitch/vlog.h"
38
39 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
40
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))
45
46 /* Monitored port.  It owns references to ofport, bfd, cfm structs. */
47 struct mport {
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. */
51
52     struct cfm *cfm;                  /* Reference to cfm. */
53     struct bfd *bfd;                  /* Reference to bfd. */
54     uint8_t hw_addr[OFP_ETH_ALEN];    /* Hardware address. */
55 };
56
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;
63 };
64
65 /* hmap that contains "struct mport"s. */
66 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
67
68 /* heap for ordering mport based on bfd/cfm wakeup time. */
69 static struct heap monitor_heap;
70
71 /* guarded-list for storing the mports that need to send bfd/cfm control
72  * packet soon. */
73 static struct guarded_list send_soon = GUARDED_OVS_LIST_INITIALIZER(&send_soon);
74
75 /* The monitor thread id. */
76 static pthread_t monitor_tid;
77 /* True if the monitor thread is running. */
78 static bool monitor_running;
79
80 static struct latch monitor_exit_latch;
81 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
82
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 *);
87
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);
97
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)
102 {
103     struct mport *node;
104
105     HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
106                              &monitor_hmap) {
107         if (node->ofport == ofport) {
108             return node;
109         }
110     }
111     return NULL;
112 }
113
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. */
116 static void
117 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
118                struct cfm *cfm, uint8_t *hw_addr)
119     OVS_REQUIRES(monitor_mutex)
120 {
121     struct mport *mport = mport_find(ofport);
122
123     if (!mport) {
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);
128     }
129     mport_update(mport, bfd, cfm, hw_addr);
130 }
131
132 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
133 static void
134 mport_unregister(const struct ofport_dpif *ofport)
135     OVS_REQUIRES(monitor_mutex)
136 {
137     struct mport *mport = mport_find(ofport);
138
139     if (mport) {
140         mport_update(mport, NULL, NULL, NULL);
141         hmap_remove(&monitor_hmap, &mport->hmap_node);
142         heap_remove(&monitor_heap, &mport->heap_node);
143         free(mport);
144     }
145 }
146
147 /* Updates the fields of an existing mport struct. */
148 static void
149 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
150              uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex)
151 {
152     ovs_assert(mport);
153
154     if (mport->cfm != cfm) {
155         cfm_unref(mport->cfm);
156         mport->cfm = cfm_ref(cfm);
157     }
158     if (mport->bfd != bfd) {
159         bfd_unref(mport->bfd);
160         mport->bfd = bfd_ref(bfd);
161     }
162     if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
163         memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
164     }
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);
169     }
170 }
171 \f
172
173 /* The 'main' function for the monitor thread. */
174 static void *
175 monitor_main(void * args OVS_UNUSED)
176 {
177     VLOG_INFO("monitor thread created");
178     while (!latch_is_set(&monitor_exit_latch)) {
179         monitor_run();
180         latch_wait(&monitor_exit_latch);
181         poll_block();
182     }
183     VLOG_INFO("monitor thread terminated");
184     return NULL;
185 }
186
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
190
191 /* Checks the 'send_soon' list and the heap for mports that have timed
192  * out bfd/cfm sessions. */
193 static void
194 monitor_run(void)
195 {
196     uint32_t stub[512 / 4];
197     long long int prio_now;
198     struct dp_packet packet;
199
200     dp_packet_use_stub(&packet, stub, sizeof stub);
201     ovs_mutex_lock(&monitor_mutex);
202
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);
209
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) {
214         struct mport *mport;
215
216         mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
217         monitor_mport_run(mport, &packet);
218     }
219
220     monitor_check_send_soon(&packet);
221
222     /* Waits on the earliest next wakeup time. */
223     if (!heap_is_empty(&monitor_heap)) {
224         long long int next_timeout, next_mport_wakeup;
225
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));
229     }
230     ovs_mutex_unlock(&monitor_mutex);
231     dp_packet_uninit(&packet);
232 }
233
234 /* Checks the 'send_soon' list for any mport that needs to send cfm/bfd
235  * control packet immediately, and calls monitor_mport_run(). */
236 static void
237 monitor_check_send_soon(struct dp_packet *packet)
238     OVS_REQUIRES(monitor_mutex)
239 {
240     while (!guarded_list_is_empty(&send_soon)) {
241         struct send_soon_entry *entry;
242         struct mport *mport;
243
244         entry = CONTAINER_OF(guarded_list_pop_front(&send_soon),
245                              struct send_soon_entry, list_node);
246         mport = mport_find(entry->ofport);
247         if (mport) {
248             monitor_mport_run(mport, packet);
249         }
250         free(entry);
251     }
252 }
253
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
257  * timeout. */
258 static void
259 monitor_mport_run(struct mport *mport, struct dp_packet *packet)
260     OVS_REQUIRES(monitor_mutex)
261 {
262     long long int next_wake_time;
263
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);
268     }
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);
273     }
274     if (mport->cfm) {
275         cfm_run(mport->cfm);
276         cfm_wait(mport->cfm);
277     }
278     if (mport->bfd) {
279         bfd_run(mport->bfd);
280         bfd_wait(mport->bfd);
281     }
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));
287 }
288 \f
289
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
293  * or terminated. */
294 void
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])
298 {
299     ovs_mutex_lock(&monitor_mutex);
300     if (!cfm && !bfd) {
301         mport_unregister(ofport);
302     } else {
303         mport_register(ofport, bfd, cfm, hw_addr);
304     }
305     ovs_mutex_unlock(&monitor_mutex);
306
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,
309      * terminates it. */
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;
319     }
320 }
321
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.
325  *
326  * Reader of the 'send_soon' list is responsible for freeing the entry. */
327 void
328 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
329 {
330     struct send_soon_entry *entry = xzalloc(sizeof *entry);
331     entry->ofport = ofport;
332
333     guarded_list_push_back(&send_soon, &entry->list_node, SIZE_MAX);
334 }