b5217357a8926793605f978d6674632abf182f69
[sliver-openvswitch.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 "hash.h"
25 #include "heap.h"
26 #include "hmap.h"
27 #include "latch.h"
28 #include "ofpbuf.h"
29 #include "ofproto-dpif.h"
30 #include "ovs-thread.h"
31 #include "poll-loop.h"
32 #include "seq.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
38
39 /* Converts the time in millisecond to heap priority. */
40 #define MSEC_TO_PRIO(TIME) (LLONG_MAX - (TIME))
41 /* Converts the heap priority to time in millisecond. */
42 #define PRIO_TO_MSEC(PRIO) (LLONG_MAX - (PRIO))
43
44 /* Monitored port.  It owns references to ofport, bfd, cfm structs. */
45 struct mport {
46     struct hmap_node hmap_node;       /* In monitor_hmap. */
47     struct heap_node heap_node;       /* In monitor_heap. */
48     const struct ofport_dpif *ofport; /* The corresponding ofport. */
49
50     struct cfm *cfm;                  /* Reference to cfm. */
51     struct bfd *bfd;                  /* Reference to bfd. */
52     uint8_t hw_addr[OFP_ETH_ALEN];    /* Hardware address. */
53 };
54
55 /* hmap that contains "struct mport"s. */
56 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
57
58 /* heap for ordering mport based on bfd/cfm wakeup time. */
59 static struct heap monitor_heap;
60
61 /* The monitor thread id. */
62 static pthread_t monitor_tid;
63 /* True if the monitor thread is running. */
64 static bool monitor_running;
65
66 static struct latch monitor_exit_latch;
67 static struct ovs_mutex monitor_mutex = OVS_MUTEX_INITIALIZER;
68
69 static void *monitor_main(void *);
70 static void monitor_run(void);
71
72 static void mport_register(const struct ofport_dpif *, struct bfd *,
73                            struct cfm *, uint8_t[ETH_ADDR_LEN])
74     OVS_REQUIRES(monitor_mutex);
75 static void mport_unregister(const struct ofport_dpif *)
76     OVS_REQUIRES(monitor_mutex);
77 static void mport_update(struct mport *, struct bfd *, struct cfm *,
78                          uint8_t[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex);
79 static struct mport *mport_find(const struct ofport_dpif *)
80     OVS_REQUIRES(monitor_mutex);
81
82 /* Tries finding and returning the 'mport' from the monitor_hmap.
83  * If there is no such 'mport', returns NULL. */
84 static struct mport *
85 mport_find(const struct ofport_dpif *ofport) OVS_REQUIRES(monitor_mutex)
86 {
87     struct mport *node;
88
89     HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
90                              &monitor_hmap) {
91         if (node->ofport == ofport) {
92             return node;
93         }
94     }
95     return NULL;
96 }
97
98 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
99  * if it doesn't exist.  Otherwise, just updates its fields. */
100 static void
101 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
102                struct cfm *cfm, uint8_t *hw_addr)
103     OVS_REQUIRES(monitor_mutex)
104 {
105     struct mport *mport = mport_find(ofport);
106
107     if (!mport) {
108         mport = xzalloc(sizeof *mport);
109         mport->ofport = ofport;
110         hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
111         heap_insert(&monitor_heap, &mport->heap_node, 0);
112     }
113     mport_update(mport, bfd, cfm, hw_addr);
114 }
115
116 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
117 static void
118 mport_unregister(const struct ofport_dpif *ofport)
119     OVS_REQUIRES(monitor_mutex)
120 {
121     struct mport *mport = mport_find(ofport);
122
123     if (mport) {
124         mport_update(mport, NULL, NULL, NULL);
125         hmap_remove(&monitor_hmap, &mport->hmap_node);
126         heap_remove(&monitor_heap, &mport->heap_node);
127         free(mport);
128     }
129 }
130
131 /* Updates the fields of an existing mport struct. */
132 static void
133 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
134              uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQUIRES(monitor_mutex)
135 {
136     ovs_assert(mport);
137
138     if (mport->cfm != cfm) {
139         cfm_unref(mport->cfm);
140         mport->cfm = cfm_ref(cfm);
141     }
142     if (mport->bfd != bfd) {
143         bfd_unref(mport->bfd);
144         mport->bfd = bfd_ref(bfd);
145     }
146     if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
147         memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
148     }
149     /* If bfd/cfm is added or reconfigured, move the mport on top of the heap
150      * so that the monitor thread can run the mport next time it wakes up. */
151     if (mport->bfd || mport->cfm) {
152         heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
153     }
154 }
155 \f
156
157 /* The 'main' function for the monitor thread. */
158 static void *
159 monitor_main(void * args OVS_UNUSED)
160 {
161     set_subprogram_name("monitor");
162     VLOG_INFO("monitor thread created");
163     while (!latch_is_set(&monitor_exit_latch)) {
164         monitor_run();
165         latch_wait(&monitor_exit_latch);
166         poll_block();
167     }
168     VLOG_INFO("monitor thread terminated");
169     return NULL;
170 }
171
172 /* The monitor thread should wake up this often to ensure that newly added or
173  * reconfigured monitoring ports are run in a timely manner. */
174 #define MONITOR_INTERVAL_MSEC 100
175
176 /* Checks the sending of control packets on mports that have timed out.
177  * Sends the control packets if needed.  Executes bfd and cfm periodic
178  * functions (run, wait) on those mports. */
179 static void
180 monitor_run(void)
181 {
182     uint32_t stub[512 / 4];
183     long long int prio_now;
184     struct ofpbuf packet;
185
186     ofpbuf_use_stub(&packet, stub, sizeof stub);
187     ovs_mutex_lock(&monitor_mutex);
188     prio_now = MSEC_TO_PRIO(time_msec());
189     /* Peeks the top of heap and checks if we should run this mport. */
190     while (!heap_is_empty(&monitor_heap)
191            && heap_max(&monitor_heap)->priority >= prio_now) {
192         long long int next_wake_time;
193         struct mport *mport;
194
195         mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
196         if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
197             ofpbuf_clear(&packet);
198             cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr);
199             ofproto_dpif_send_packet(mport->ofport, &packet);
200         }
201         if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
202             ofpbuf_clear(&packet);
203             bfd_put_packet(mport->bfd, &packet, mport->hw_addr);
204             ofproto_dpif_send_packet(mport->ofport, &packet);
205         }
206         if (mport->cfm) {
207             cfm_run(mport->cfm);
208             cfm_wait(mport->cfm);
209         }
210         if (mport->bfd) {
211             bfd_run(mport->bfd);
212             bfd_wait(mport->bfd);
213         }
214         /* Computes the next wakeup time for this mport. */
215         next_wake_time = MIN(bfd_wake_time(mport->bfd),
216                              cfm_wake_time(mport->cfm));
217         heap_change(&monitor_heap, &mport->heap_node,
218                     MSEC_TO_PRIO(next_wake_time));
219     }
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 \f
233
234 /* Creates the mport in monitor module if either bfd or cfm
235  * is configured.  Otherwise, deletes the mport.
236  * Also checks whether the monitor thread should be started
237  * or terminated. */
238 void
239 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
240                                  struct bfd *bfd, struct cfm *cfm,
241                                  uint8_t hw_addr[ETH_ADDR_LEN])
242 {
243     ovs_mutex_lock(&monitor_mutex);
244     if (!cfm && !bfd) {
245         mport_unregister(ofport);
246     } else {
247         mport_register(ofport, bfd, cfm, hw_addr);
248     }
249     ovs_mutex_unlock(&monitor_mutex);
250
251     /* If the monitor thread is not running and the hmap
252      * is not empty, starts it.  If it is and the hmap is empty,
253      * terminates it. */
254     if (!monitor_running && !hmap_is_empty(&monitor_hmap))  {
255         latch_init(&monitor_exit_latch);
256         xpthread_create(&monitor_tid, NULL, monitor_main, NULL);
257         monitor_running = true;
258     } else if (monitor_running && hmap_is_empty(&monitor_hmap))  {
259         latch_set(&monitor_exit_latch);
260         xpthread_join(monitor_tid, NULL);
261         latch_destroy(&monitor_exit_latch);
262         monitor_running = false;
263     }
264 }
265
266 /* Moves the mport on top of the heap.  This is necessary when
267  * for example, bfd POLL is received and the mport should
268  * immediately send FINAL back. */
269 void
270 ofproto_dpif_monitor_port_send_soon_safe(const struct ofport_dpif *ofport)
271 {
272     ovs_mutex_lock(&monitor_mutex);
273     ofproto_dpif_monitor_port_send_soon(ofport);
274     ovs_mutex_unlock(&monitor_mutex);
275 }
276
277 void
278 ofproto_dpif_monitor_port_send_soon(const struct ofport_dpif *ofport)
279     OVS_REQUIRES(monitor_mutex)
280 {
281     struct mport *mport;
282
283     mport = mport_find(ofport);
284     if (mport) {
285         heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
286     }
287 }