ofproto-dpif-monitor: Use heap to order the mport wakeup time.
[cascardo/ovs.git] / ofproto / ofproto-dpif-monitor.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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;
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 seq *monitor_seq;
67 static struct latch monitor_exit_latch;
68 static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER;
69
70 static void monitor_init(void);
71 static void *monitor_main(void *);
72 static void monitor_run(void);
73
74 static void mport_register(const struct ofport_dpif *, struct bfd *,
75                            struct cfm *, uint8_t[ETH_ADDR_LEN])
76     OVS_REQ_WRLOCK(monitor_rwlock);
77 static void mport_unregister(const struct ofport_dpif *)
78     OVS_REQ_WRLOCK(monitor_rwlock);
79 static void mport_update(struct mport *, struct bfd *, struct cfm *,
80                          uint8_t[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock);
81 static struct mport *mport_find(const struct ofport_dpif *)
82     OVS_REQ_WRLOCK(monitor_rwlock);
83
84 /* Tries finding and returning the 'mport' from the monitor_hmap.
85  * If there is no such 'mport', returns NULL. */
86 static struct mport *
87 mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock)
88 {
89     struct mport *node;
90
91     HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
92                              &monitor_hmap) {
93         if (node->ofport == ofport) {
94             return node;
95         }
96     }
97     return NULL;
98 }
99
100 /* Creates a new mport and inserts it into monitor_hmap and monitor_heap,
101  * if it doesn't exist.  Otherwise, just updates its fields. */
102 static void
103 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
104                struct cfm *cfm, uint8_t *hw_addr)
105     OVS_REQ_WRLOCK(monitor_rwlock)
106 {
107     struct mport *mport = mport_find(ofport);
108
109     if (!mport) {
110         mport = xzalloc(sizeof *mport);
111         mport->ofport = ofport;
112         hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
113         heap_insert(&monitor_heap, &mport->heap_node, 0);
114     }
115     mport_update(mport, bfd, cfm, hw_addr);
116 }
117
118 /* Removes mport from monitor_hmap and monitor_heap and frees it. */
119 static void
120 mport_unregister(const struct ofport_dpif *ofport)
121     OVS_REQ_WRLOCK(monitor_rwlock)
122 {
123     struct mport *mport = mport_find(ofport);
124
125     if (mport) {
126         mport_update(mport, NULL, NULL, NULL);
127         hmap_remove(&monitor_hmap, &mport->hmap_node);
128         heap_remove(&monitor_heap, &mport->heap_node);
129         free(mport);
130     }
131 }
132
133 /* Updates the fields of an existing mport struct. */
134 static void
135 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
136              uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock)
137 {
138     ovs_assert(mport);
139
140     if (mport->cfm != cfm) {
141         cfm_unref(mport->cfm);
142         mport->cfm = cfm_ref(cfm);
143     }
144     if (mport->bfd != bfd) {
145         bfd_unref(mport->bfd);
146         mport->bfd = bfd_ref(bfd);
147     }
148     if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
149         memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
150     }
151     /* If bfd/cfm is added or reconfigured, move the mport on top of the heap
152      * and wakes up the monitor thread. */
153     if (mport->bfd || mport->cfm) {
154         heap_change(&monitor_heap, &mport->heap_node, LLONG_MAX);
155         seq_change(monitor_seq);
156     }
157 }
158 \f
159
160 /* Initializes the global variables.  This will only run once. */
161 static void
162 monitor_init(void)
163 {
164     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
165
166     if (ovsthread_once_start(&once)) {
167         hmap_init(&monitor_hmap);
168         monitor_seq = seq_create();
169         ovsthread_once_done(&once);
170     }
171 }
172
173 /* The 'main' function for the monitor thread. */
174 static void *
175 monitor_main(void * args OVS_UNUSED)
176 {
177     set_subprogram_name("monitor");
178     VLOG_INFO("monitor thread created");
179     while (!latch_is_set(&monitor_exit_latch)) {
180         uint64_t seq = seq_read(monitor_seq);
181
182         monitor_run();
183         latch_wait(&monitor_exit_latch);
184         seq_wait(monitor_seq, seq);
185         poll_block();
186     }
187     VLOG_INFO("monitor thread terminated");
188     return NULL;
189 }
190
191 /* Checks the sending of control packets on mports that have timed out.
192  * Sends the control packets if needed.  Executes bfd and cfm periodic
193  * functions (run, wait) on those mports. */
194 static void
195 monitor_run(void)
196 {
197     uint32_t stub[512 / 4];
198     long long int prio_now;
199     struct ofpbuf packet;
200
201     ofpbuf_use_stub(&packet, stub, sizeof stub);
202     ovs_rwlock_rdlock(&monitor_rwlock);
203     prio_now = MSEC_TO_PRIO(time_msec());
204     /* Peeks the top of heap and checks if we should run this mport. */
205     while (!heap_is_empty(&monitor_heap)
206            && heap_max(&monitor_heap)->priority >= prio_now) {
207         long long int next_wake_time;
208         struct mport *mport;
209
210         mport = CONTAINER_OF(heap_max(&monitor_heap), struct mport, heap_node);
211         if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
212             ofpbuf_clear(&packet);
213             cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr);
214             ofproto_dpif_send_packet(mport->ofport, &packet);
215         }
216         if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
217             ofpbuf_clear(&packet);
218             bfd_put_packet(mport->bfd, &packet, mport->hw_addr);
219             ofproto_dpif_send_packet(mport->ofport, &packet);
220         }
221         if (mport->cfm) {
222             cfm_run(mport->cfm);
223             cfm_wait(mport->cfm);
224         }
225         if (mport->bfd) {
226             bfd_run(mport->bfd);
227             bfd_wait(mport->bfd);
228         }
229         /* Computes the next wakeup time for this mport. */
230         next_wake_time = MIN(bfd_wake_time(mport->bfd), cfm_wake_time(mport->cfm));
231         heap_change(&monitor_heap, heap_max(&monitor_heap),
232                     MSEC_TO_PRIO(next_wake_time));
233     }
234
235     /* Waits on the earliest next wakeup time. */
236     if (!heap_is_empty(&monitor_heap)) {
237         poll_timer_wait_until(PRIO_TO_MSEC(heap_max(&monitor_heap)->priority));
238     }
239     ovs_rwlock_unlock(&monitor_rwlock);
240     ofpbuf_uninit(&packet);
241 }
242 \f
243
244 /* Creates the mport in monitor module if either bfd or cfm
245  * is configured.  Otherwise, deletes the mport.
246  * Also checks whether the monitor thread should be started
247  * or terminated. */
248 void
249 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
250                                  struct bfd *bfd, struct cfm *cfm,
251                                  uint8_t hw_addr[ETH_ADDR_LEN])
252 {
253     monitor_init();
254     ovs_rwlock_wrlock(&monitor_rwlock);
255     if (!cfm && !bfd) {
256         mport_unregister(ofport);
257     } else {
258         mport_register(ofport, bfd, cfm, hw_addr);
259     }
260     ovs_rwlock_unlock(&monitor_rwlock);
261
262     /* If the monitor thread is not running and the hmap
263      * is not empty, starts it.  If it is and the hmap is empty,
264      * terminates it. */
265     if (!monitor_running && !hmap_is_empty(&monitor_hmap))  {
266         latch_init(&monitor_exit_latch);
267         xpthread_create(&monitor_tid, NULL, monitor_main, NULL);
268         monitor_running = true;
269     } else if (monitor_running && hmap_is_empty(&monitor_hmap))  {
270         latch_set(&monitor_exit_latch);
271         xpthread_join(monitor_tid, NULL);
272         latch_destroy(&monitor_exit_latch);
273         monitor_running = false;
274     }
275 }