2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofproto-dpif-monitor.h"
28 #include "ofproto-dpif.h"
29 #include "ovs-thread.h"
30 #include "poll-loop.h"
35 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_monitor);
37 /* Monitored port. It owns references to ofport, bfd, cfm structs. */
39 struct hmap_node hmap_node; /* In monitor_hmap. */
40 const struct ofport_dpif *ofport; /* The corresponding ofport. */
42 struct cfm *cfm; /* Reference to cfm. */
43 struct bfd *bfd; /* Reference to bfd. */
44 uint8_t hw_addr[OFP_ETH_ALEN]; /* Hardware address. */
47 /* hmap that contains "struct mport"s. */
48 static struct hmap monitor_hmap = HMAP_INITIALIZER(&monitor_hmap);
50 /* The monitor thread id. */
51 static pthread_t monitor_tid;
52 /* True if the monitor thread is running. */
53 static bool monitor_running;
55 static struct seq *monitor_seq;
56 static struct latch monitor_exit_latch;
57 static struct ovs_rwlock monitor_rwlock = OVS_RWLOCK_INITIALIZER;
59 static void monitor_init(void);
60 static void *monitor_main(void *);
61 static void monitor_run(void);
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);
73 /* Tries finding and returning the 'mport' from the monitor_hmap.
74 * If there is no such 'mport', returns NULL. */
76 mport_find(const struct ofport_dpif *ofport) OVS_REQ_WRLOCK(monitor_rwlock)
80 HMAP_FOR_EACH_WITH_HASH (node, hmap_node, hash_pointer(ofport, 0),
82 if (node->ofport == ofport) {
89 /* Creates a new mport and inserts it into monitor_hmap, if it doesn't exist.
90 * Otherwise, just updates its fields. */
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)
96 struct mport *mport = mport_find(ofport);
99 mport = xzalloc(sizeof *mport);
100 mport->ofport = ofport;
101 hmap_insert(&monitor_hmap, &mport->hmap_node, hash_pointer(ofport, 0));
103 mport_update(mport, bfd, cfm, hw_addr);
106 /* Removes mport from monitor_hmap and frees it. */
108 mport_unregister(const struct ofport_dpif *ofport)
109 OVS_REQ_WRLOCK(monitor_rwlock)
111 struct mport *mport = mport_find(ofport);
114 mport_update(mport, NULL, NULL, NULL);
115 hmap_remove(&monitor_hmap, &mport->hmap_node);
120 /* Updates the fields of an existing mport struct. */
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)
127 if (mport->cfm != cfm) {
128 cfm_unref(mport->cfm);
129 mport->cfm = cfm_ref(cfm);
131 if (mport->bfd != bfd) {
132 bfd_unref(mport->bfd);
133 mport->bfd = bfd_ref(bfd);
135 if (hw_addr && memcmp(mport->hw_addr, hw_addr, ETH_ADDR_LEN)) {
136 memcpy(mport->hw_addr, hw_addr, ETH_ADDR_LEN);
138 /* If bfd/cfm is added or reconfigured, wakes up the monitor thread. */
139 if (mport->bfd || mport->cfm) {
140 seq_change(monitor_seq);
145 /* Initializes the global variables. This will only run once. */
149 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
151 if (ovsthread_once_start(&once)) {
152 hmap_init(&monitor_hmap);
153 monitor_seq = seq_create();
154 ovsthread_once_done(&once);
158 /* The 'main' function for the monitor thread. */
160 monitor_main(void * args OVS_UNUSED)
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);
168 latch_wait(&monitor_exit_latch);
169 seq_wait(monitor_seq, seq);
172 VLOG_INFO("monitor thread terminated");
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)
182 uint32_t stub[512 / 4];
183 struct ofpbuf packet;
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);
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);
201 cfm_wait(mport->cfm);
205 bfd_wait(mport->bfd);
208 ovs_rwlock_unlock(&monitor_rwlock);
209 ofpbuf_uninit(&packet);
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
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])
223 ovs_rwlock_wrlock(&monitor_rwlock);
225 mport_unregister(ofport);
227 mport_register(ofport, bfd, cfm, hw_addr);
229 ovs_rwlock_unlock(&monitor_rwlock);
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,
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;