ofproto-dpif-monitor: Run ofproto-dpif-monitor in a thread.
[sliver-openvswitch.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 "hmap.h"
26 #include "latch.h"
27 #include "ofpbuf.h"
28 #include "ofproto-dpif.h"
29 #include "ovs-thread.h"
30 #include "poll-loop.h"
31 #include "seq.h"
32 #include "util.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
36
37 /* Monitored port.  It owns references to ofport, bfd, cfm structs. */
38 struct mport {
39     struct hmap_node hmap_node;       /* In monitor_hmap. */
40     const struct ofport_dpif *ofport; /* The corresponding ofport. */
41
42     struct cfm *cfm;                  /* Reference to cfm. */
43     struct bfd *bfd;                  /* Reference to bfd. */
44     uint8_t hw_addr[OFP_ETH_ALEN];    /* Hardware address. */
45 };
46
47 /* hmap that contains "struct mport"s. */
48 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
49
50 /* The monitor thread id. */
51 static pthread_t monitor_tid;
52 /* True if the monitor thread is running. */
53 static bool monitor_running;
54
55 static struct seq *monitor_seq;
56 static struct latch monitor_exit_latch;
57 static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER;
58
59 static void monitor_init(void);
60 static void *monitor_main(void *);
61 static void monitor_run(void);
62
63 static void mport_register(const struct ofport_dpif *, struct bfd *,
64                            struct cfm *, uint8_t[ETH_ADDR_LEN])
65     OVS_REQ_WRLOCK(monitor_rwlock);
66 static void mport_unregister(const struct ofport_dpif *)
67     OVS_REQ_WRLOCK(monitor_rwlock);
68 static void mport_update(struct mport *, struct bfd *, struct cfm *,
69                          uint8_t[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock);
70 static struct mport *mport_find(const struct ofport_dpif *)
71     OVS_REQ_WRLOCK(monitor_rwlock);
72
73 /* Tries finding and returning the 'mport' from the monitor_hmap.
74  * If there is no such 'mport', returns NULL. */
75 static struct mport *
76 mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock)
77 {
78     struct mport *node;
79
80     HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
81                              &monitor_hmap) {
82         if (node->ofport == ofport) {
83             return node;
84         }
85     }
86     return NULL;
87 }
88
89 /* Creates a new mport and inserts it into monitor_hmap, if it doesn't exist.
90  * Otherwise, just updates its fields. */
91 static void
92 mport_register(const struct ofport_dpif *ofport, struct bfd *bfd,
93                struct cfm *cfm, uint8_t *hw_addr)
94     OVS_REQ_WRLOCK(monitor_rwlock)
95 {
96     struct mport *mport = mport_find(ofport);
97
98     if (!mport) {
99         mport = xzalloc(sizeof *mport);
100         mport->ofport = ofport;
101         hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
102     }
103     mport_update(mport, bfd, cfm, hw_addr);
104 }
105
106 /* Removes mport from monitor_hmap and frees it. */
107 static void
108 mport_unregister(const struct ofport_dpif *ofport)
109     OVS_REQ_WRLOCK(monitor_rwlock)
110 {
111     struct mport *mport = mport_find(ofport);
112
113     if (mport) {
114         mport_update(mport, NULL, NULL, NULL);
115         hmap_remove(&monitor_hmap, &mport->hmap_node);
116         free(mport);
117     }
118 }
119
120 /* Updates the fields of an existing mport struct. */
121 static void
122 mport_update(struct mport *mport, struct bfd *bfd, struct cfm *cfm,
123              uint8_t hw_addr[ETH_ADDR_LEN]) OVS_REQ_WRLOCK(monitor_rwlock)
124 {
125     ovs_assert(mport);
126
127     if (mport->cfm != cfm) {
128         cfm_unref(mport->cfm);
129         mport->cfm = cfm_ref(cfm);
130     }
131     if (mport->bfd != bfd) {
132         bfd_unref(mport->bfd);
133         mport->bfd = bfd_ref(bfd);
134     }
135     if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
136         memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
137     }
138     /* If bfd/cfm is added or reconfigured, wakes up the monitor thread. */
139     if (mport->bfd || mport->cfm) {
140         seq_change(monitor_seq);
141     }
142 }
143 \f
144
145 /* Initializes the global variables.  This will only run once. */
146 static void
147 monitor_init(void)
148 {
149     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
150
151     if (ovsthread_once_start(&once)) {
152         hmap_init(&monitor_hmap);
153         monitor_seq = seq_create();
154         ovsthread_once_done(&once);
155     }
156 }
157
158 /* The 'main' function for the monitor thread. */
159 static void *
160 monitor_main(void * args OVS_UNUSED)
161 {
162     set_subprogram_name("monitor");
163     VLOG_INFO("monitor thread created");
164     while (!latch_is_set(&monitor_exit_latch)) {
165         uint64_t seq = seq_read(monitor_seq);
166
167         monitor_run();
168         latch_wait(&monitor_exit_latch);
169         seq_wait(monitor_seq, seq);
170         poll_block();
171     }
172     VLOG_INFO("monitor thread terminated");
173     return NULL;
174 }
175
176 /* Checks the sending of control packets on all mports.  Sends the control
177  * packets if needed.  Executes bfd and cfm periodic functions (run, wait)
178  * on all mports. */
179 static void
180 monitor_run(void)
181 {
182     uint32_t stub[512 / 4];
183     struct ofpbuf packet;
184     struct mport *mport;
185
186     ofpbuf_use_stub(&packet, stub, sizeof stub);
187     ovs_rwlock_rdlock(&monitor_rwlock);
188     HMAP_FOR_EACH (mport, hmap_node, &monitor_hmap) {
189         if (mport->cfm && cfm_should_send_ccm(mport->cfm)) {
190             ofpbuf_clear(&packet);
191             cfm_compose_ccm(mport->cfm, &packet, mport->hw_addr);
192             ofproto_dpif_send_packet(mport->ofport, &packet);
193         }
194         if (mport->bfd && bfd_should_send_packet(mport->bfd)) {
195             ofpbuf_clear(&packet);
196             bfd_put_packet(mport->bfd, &packet, mport->hw_addr);
197             ofproto_dpif_send_packet(mport->ofport, &packet);
198         }
199         if (mport->cfm) {
200             cfm_run(mport->cfm);
201             cfm_wait(mport->cfm);
202         }
203         if (mport->bfd) {
204             bfd_run(mport->bfd);
205             bfd_wait(mport->bfd);
206         }
207     }
208     ovs_rwlock_unlock(&monitor_rwlock);
209     ofpbuf_uninit(&packet);
210 }
211 \f
212
213 /* Creates the mport in monitor module if either bfd or cfm
214  * is configured.  Otherwise, deletes the mport.
215  * Also checks whether the monitor thread should be started
216  * or terminated. */
217 void
218 ofproto_dpif_monitor_port_update(const struct ofport_dpif *ofport,
219                                  struct bfd *bfd, struct cfm *cfm,
220                                  uint8_t hw_addr[ETH_ADDR_LEN])
221 {
222     monitor_init();
223     ovs_rwlock_wrlock(&monitor_rwlock);
224     if (!cfm && !bfd) {
225         mport_unregister(ofport);
226     } else {
227         mport_register(ofport, bfd, cfm, hw_addr);
228     }
229     ovs_rwlock_unlock(&monitor_rwlock);
230
231     /* If the monitor thread is not running and the hmap
232      * is not empty, starts it.  If it is and the hmap is empty,
233      * terminates it. */
234     if (!monitor_running && !hmap_is_empty(&monitor_hmap))  {
235         latch_init(&monitor_exit_latch);
236         xpthread_create(&monitor_tid, NULL, monitor_main, NULL);
237         monitor_running = true;
238     } else if (monitor_running && hmap_is_empty(&monitor_hmap))  {
239         latch_set(&monitor_exit_latch);
240         xpthread_join(monitor_tid, NULL);
241         latch_destroy(&monitor_exit_latch);
242         monitor_running = false;
243     }
244 }