c3ffd6b77bc10cbea56f95e31c0c16aa692d3b2b
[cascardo/ovs.git] / lib / mcast-snooping.c
1 /*
2  * Copyright (c) 2014 Red Hat, Inc.
3  *
4  * Based on mac-learning implementation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <config.h>
20 #include "mcast-snooping.h"
21
22 #include <inttypes.h>
23 #include <stdlib.h>
24
25 #include "bitmap.h"
26 #include "byte-order.h"
27 #include "coverage.h"
28 #include "hash.h"
29 #include "list.h"
30 #include "poll-loop.h"
31 #include "timeval.h"
32 #include "entropy.h"
33 #include "unaligned.h"
34 #include "util.h"
35 #include "vlan-bitmap.h"
36 #include "openvswitch/vlog.h"
37
38 COVERAGE_DEFINE(mcast_snooping_learned);
39 COVERAGE_DEFINE(mcast_snooping_expired);
40
41 static struct mcast_port_bundle *
42 mcast_snooping_port_lookup(struct ovs_list *list, void *port);
43 static struct mcast_mrouter_bundle *
44 mcast_snooping_mrouter_lookup(struct mcast_snooping *ms, uint16_t vlan,
45                               void *port)
46     OVS_REQ_RDLOCK(ms->rwlock);
47
48 bool
49 mcast_snooping_enabled(const struct mcast_snooping *ms)
50 {
51     return !!ms;
52 }
53
54 bool
55 mcast_snooping_flood_unreg(const struct mcast_snooping *ms)
56 {
57     return ms->flood_unreg;
58 }
59
60 bool
61 mcast_snooping_is_query(ovs_be16 igmp_type)
62 {
63     return igmp_type == htons(IGMP_HOST_MEMBERSHIP_QUERY);
64 }
65
66 bool
67 mcast_snooping_is_membership(ovs_be16 igmp_type)
68 {
69     switch (ntohs(igmp_type)) {
70     case IGMP_HOST_MEMBERSHIP_REPORT:
71     case IGMPV2_HOST_MEMBERSHIP_REPORT:
72     case IGMP_HOST_LEAVE_MESSAGE:
73         return true;
74     }
75     return false;
76 }
77
78 /* Returns the number of seconds since multicast group 'b' was learned in a
79  * port on 'ms'. */
80 int
81 mcast_bundle_age(const struct mcast_snooping *ms,
82                  const struct mcast_group_bundle *b)
83 {
84     time_t remaining = b->expires - time_now();
85     return ms->idle_time - remaining;
86 }
87
88 static uint32_t
89 mcast_table_hash(const struct mcast_snooping *ms, ovs_be32 grp_ip4,
90                  uint16_t vlan)
91 {
92     return hash_3words((OVS_FORCE uint32_t) grp_ip4, vlan, ms->secret);
93 }
94
95 static struct mcast_group_bundle *
96 mcast_group_bundle_from_lru_node(struct ovs_list *list)
97 {
98     return CONTAINER_OF(list, struct mcast_group_bundle, bundle_node);
99 }
100
101 static struct mcast_group *
102 mcast_group_from_lru_node(struct ovs_list *list)
103 {
104     return CONTAINER_OF(list, struct mcast_group, group_node);
105 }
106
107 /* Searches 'ms' for and returns an mcast group for destination address
108  * 'dip' in 'vlan'. */
109 struct mcast_group *
110 mcast_snooping_lookup(const struct mcast_snooping *ms, ovs_be32 dip,
111                       uint16_t vlan)
112     OVS_REQ_RDLOCK(ms->rwlock)
113 {
114     struct mcast_group *grp;
115     uint32_t hash;
116
117     hash = mcast_table_hash(ms, dip, vlan);
118     HMAP_FOR_EACH_WITH_HASH (grp, hmap_node, hash, &ms->table) {
119         if (grp->vlan == vlan && grp->ip4 == dip) {
120            return grp;
121         }
122     }
123     return NULL;
124 }
125
126 /* If the LRU list is not empty, stores the least-recently-used entry
127  * in '*e' and returns true.  Otherwise, if the LRU list is empty,
128  * stores NULL in '*e' and return false. */
129 static bool
130 group_get_lru(const struct mcast_snooping *ms, struct mcast_group **grp)
131     OVS_REQ_RDLOCK(ms->rwlock)
132 {
133     if (!list_is_empty(&ms->group_lru)) {
134         *grp = mcast_group_from_lru_node(ms->group_lru.next);
135         return true;
136     } else {
137         *grp = NULL;
138         return false;
139     }
140 }
141
142 static unsigned int
143 normalize_idle_time(unsigned int idle_time)
144 {
145     return (idle_time < 15 ? 15
146             : idle_time > 3600 ? 3600
147             : idle_time);
148 }
149
150 /* Creates and returns a new mcast table with an initial mcast aging
151  * timeout of MCAST_ENTRY_DEFAULT_IDLE_TIME seconds and an initial maximum of
152  * MCAST_DEFAULT_MAX entries. */
153 struct mcast_snooping *
154 mcast_snooping_create(void)
155 {
156     struct mcast_snooping *ms;
157
158     ms = xmalloc(sizeof *ms);
159     hmap_init(&ms->table);
160     list_init(&ms->group_lru);
161     list_init(&ms->mrouter_lru);
162     list_init(&ms->fport_list);
163     list_init(&ms->rport_list);
164     ms->secret = random_uint32();
165     ms->idle_time = MCAST_ENTRY_DEFAULT_IDLE_TIME;
166     ms->max_entries = MCAST_DEFAULT_MAX_ENTRIES;
167     ms->need_revalidate = false;
168     ms->flood_unreg = true;
169     ovs_refcount_init(&ms->ref_cnt);
170     ovs_rwlock_init(&ms->rwlock);
171     return ms;
172 }
173
174 struct mcast_snooping *
175 mcast_snooping_ref(const struct mcast_snooping *ms_)
176 {
177     struct mcast_snooping *ms = CONST_CAST(struct mcast_snooping *, ms_);
178     if (ms) {
179         ovs_refcount_ref(&ms->ref_cnt);
180     }
181     return ms;
182 }
183
184 /* Unreferences (and possibly destroys) mcast snooping table 'ms'. */
185 void
186 mcast_snooping_unref(struct mcast_snooping *ms)
187 {
188     if (!mcast_snooping_enabled(ms)) {
189         return;
190     }
191
192     if (ovs_refcount_unref_relaxed(&ms->ref_cnt) == 1) {
193         mcast_snooping_flush(ms);
194         hmap_destroy(&ms->table);
195         ovs_rwlock_destroy(&ms->rwlock);
196         free(ms);
197     }
198 }
199
200 /* Changes the mcast aging timeout of 'ms' to 'idle_time' seconds. */
201 void
202 mcast_snooping_set_idle_time(struct mcast_snooping *ms, unsigned int idle_time)
203     OVS_REQ_WRLOCK(ms->rwlock)
204 {
205     struct mcast_group *grp;
206     struct mcast_group_bundle *b;
207     int delta;
208
209     idle_time = normalize_idle_time(idle_time);
210     if (idle_time != ms->idle_time) {
211         delta = (int) idle_time - (int) ms->idle_time;
212         LIST_FOR_EACH (grp, group_node, &ms->group_lru) {
213             LIST_FOR_EACH (b, bundle_node, &grp->bundle_lru) {
214                 b->expires += delta;
215             }
216         }
217         ms->idle_time = idle_time;
218     }
219 }
220
221 /* Sets the maximum number of entries in 'ms' to 'max_entries', adjusting it
222  * to be within a reasonable range. */
223 void
224 mcast_snooping_set_max_entries(struct mcast_snooping *ms,
225                                size_t max_entries)
226     OVS_REQ_WRLOCK(ms->rwlock)
227 {
228     ms->max_entries = (max_entries < 10 ? 10
229                        : max_entries > 1000 * 1000 ? 1000 * 1000
230                        : max_entries);
231 }
232
233 /* Sets if unregistered multicast packets should be flooded to
234  * all ports or only to ports connected to multicast routers
235  *
236  * Returns true if previous state differs from current state,
237  * false otherwise. */
238 bool
239 mcast_snooping_set_flood_unreg(struct mcast_snooping *ms, bool enable)
240     OVS_REQ_WRLOCK(ms->rwlock)
241 {
242     bool prev = ms->flood_unreg;
243     ms->flood_unreg = enable;
244     return prev != enable;
245 }
246
247 static struct mcast_group_bundle *
248 mcast_group_bundle_lookup(struct mcast_snooping *ms OVS_UNUSED,
249                           struct mcast_group *grp, void *port)
250     OVS_REQ_RDLOCK(ms->rwlock)
251 {
252     struct mcast_group_bundle *b;
253
254     LIST_FOR_EACH (b, bundle_node, &grp->bundle_lru) {
255         if (b->port == port) {
256             return b;
257         }
258     }
259     return NULL;
260 }
261
262 /* Insert a new bundle to the mcast group or update its
263  * position and expiration if it is already there. */
264 static struct mcast_group_bundle *
265 mcast_group_insert_bundle(struct mcast_snooping *ms OVS_UNUSED,
266                           struct mcast_group *grp, void *port, int idle_time)
267     OVS_REQ_WRLOCK(ms->rwlock)
268 {
269     struct mcast_group_bundle *b;
270
271     b = mcast_group_bundle_lookup(ms, grp, port);
272     if (b) {
273         list_remove(&b->bundle_node);
274     } else {
275         b = xmalloc(sizeof *b);
276         list_init(&b->bundle_node);
277         b->port = port;
278     }
279
280     b->expires = time_now() + idle_time;
281     list_push_back(&grp->bundle_lru, &b->bundle_node);
282     return b;
283 }
284
285 /* Return true if multicast still has bundles associated.
286  * Return false if there is no bundles. */
287 static bool
288 mcast_group_has_bundles(struct mcast_group *grp)
289 {
290     return !list_is_empty(&grp->bundle_lru);
291 }
292
293 /* Delete 'grp' from the 'ms' hash table.
294  * Caller is responsible to clean bundle lru first. */
295 static void
296 mcast_snooping_flush_group__(struct mcast_snooping *ms,
297                              struct mcast_group *grp)
298 {
299     ovs_assert(list_is_empty(&grp->bundle_lru));
300     hmap_remove(&ms->table, &grp->hmap_node);
301     list_remove(&grp->group_node);
302     free(grp);
303 }
304
305 /* Flush out mcast group and its bundles */
306 static void
307 mcast_snooping_flush_group(struct mcast_snooping *ms, struct mcast_group *grp)
308     OVS_REQ_WRLOCK(ms->rwlock)
309 {
310     struct mcast_group_bundle *b;
311
312     LIST_FOR_EACH_POP (b, bundle_node, &grp->bundle_lru) {
313         free(b);
314     }
315     mcast_snooping_flush_group__(ms, grp);
316     ms->need_revalidate = true;
317 }
318
319
320 /* Delete bundle returning true if it succeeds,
321  * false if it didn't find the group. */
322 static bool
323 mcast_group_delete_bundle(struct mcast_snooping *ms OVS_UNUSED,
324                           struct mcast_group *grp, void *port)
325     OVS_REQ_WRLOCK(ms->rwlock)
326 {
327     struct mcast_group_bundle *b;
328
329     LIST_FOR_EACH (b, bundle_node, &grp->bundle_lru) {
330         if (b->port == port) {
331             list_remove(&b->bundle_node);
332             free(b);
333             return true;
334         }
335     }
336     return false;
337 }
338
339 /* If any bundle has expired, delete it.  Returns the number of deleted
340  * bundles. */
341 static int
342 mcast_snooping_prune_expired(struct mcast_snooping *ms,
343                              struct mcast_group *grp)
344     OVS_REQ_WRLOCK(ms->rwlock)
345 {
346     int expired;
347     struct mcast_group_bundle *b, *next_b;
348     time_t timenow = time_now();
349
350     expired = 0;
351     LIST_FOR_EACH_SAFE (b, next_b, bundle_node, &grp->bundle_lru) {
352         /* This list is sorted on expiration time. */
353         if (b->expires > timenow) {
354             break;
355         }
356         list_remove(&b->bundle_node);
357         free(b);
358         expired++;
359     }
360
361     if (!mcast_group_has_bundles(grp)) {
362         mcast_snooping_flush_group__(ms, grp);
363         expired++;
364     }
365
366     if (expired) {
367         ms->need_revalidate = true;
368         COVERAGE_ADD(mcast_snooping_expired, expired);
369     }
370
371     return expired;
372 }
373
374 /* Add a multicast group to the mdb. If it exists, then
375  * move to the last position in the LRU list.
376  */
377 bool
378 mcast_snooping_add_group(struct mcast_snooping *ms, ovs_be32 ip4,
379                          uint16_t vlan, void *port)
380     OVS_REQ_WRLOCK(ms->rwlock)
381 {
382     bool learned;
383     struct mcast_group *grp;
384
385     /* Avoid duplicate packets. */
386     if (mcast_snooping_mrouter_lookup(ms, vlan, port)
387         || mcast_snooping_port_lookup(&ms->fport_list, port)) {
388         return false;
389     }
390
391     learned = false;
392     grp = mcast_snooping_lookup(ms, ip4, vlan);
393     if (!grp) {
394         uint32_t hash = mcast_table_hash(ms, ip4, vlan);
395
396         if (hmap_count(&ms->table) >= ms->max_entries) {
397             group_get_lru(ms, &grp);
398             mcast_snooping_flush_group(ms, grp);
399         }
400
401         grp = xmalloc(sizeof *grp);
402         hmap_insert(&ms->table, &grp->hmap_node, hash);
403         grp->ip4 = ip4;
404         grp->vlan = vlan;
405         list_init(&grp->bundle_lru);
406         learned = true;
407         ms->need_revalidate = true;
408         COVERAGE_INC(mcast_snooping_learned);
409     } else {
410         list_remove(&grp->group_node);
411     }
412     mcast_group_insert_bundle(ms, grp, port, ms->idle_time);
413
414     /* Mark 'grp' as recently used. */
415     list_push_back(&ms->group_lru, &grp->group_node);
416     return learned;
417 }
418
419 bool
420 mcast_snooping_leave_group(struct mcast_snooping *ms, ovs_be32 ip4,
421                            uint16_t vlan, void *port)
422     OVS_REQ_WRLOCK(ms->rwlock)
423 {
424     struct mcast_group *grp;
425
426     /* Ports flagged to forward Reports usually have more
427      * than one host behind it, so don't leave the group
428      * on the first message and just let it expire */
429     if (mcast_snooping_port_lookup(&ms->rport_list, port)) {
430         return false;
431     }
432
433     grp = mcast_snooping_lookup(ms, ip4, vlan);
434     if (grp && mcast_group_delete_bundle(ms, grp, port)) {
435         ms->need_revalidate = true;
436         return true;
437     }
438     return false;
439 }
440
441 \f
442 /* Router ports. */
443
444 /* Returns the number of seconds since the multicast router
445  * was learned in a port. */
446 int
447 mcast_mrouter_age(const struct mcast_snooping *ms OVS_UNUSED,
448                   const struct mcast_mrouter_bundle *mrouter)
449 {
450     time_t remaining = mrouter->expires - time_now();
451     return MCAST_MROUTER_PORT_IDLE_TIME - remaining;
452 }
453
454 static struct mcast_mrouter_bundle *
455 mcast_mrouter_from_lru_node(struct ovs_list *list)
456 {
457     return CONTAINER_OF(list, struct mcast_mrouter_bundle, mrouter_node);
458 }
459
460 /* If the LRU list is not empty, stores the least-recently-used mrouter
461  * in '*m' and returns true.  Otherwise, if the LRU list is empty,
462  * stores NULL in '*m' and return false. */
463 static bool
464 mrouter_get_lru(const struct mcast_snooping *ms,
465                 struct mcast_mrouter_bundle **m)
466     OVS_REQ_RDLOCK(ms->rwlock)
467 {
468     if (!list_is_empty(&ms->mrouter_lru)) {
469         *m = mcast_mrouter_from_lru_node(ms->mrouter_lru.next);
470         return true;
471     } else {
472         *m = NULL;
473         return false;
474     }
475 }
476
477 static struct mcast_mrouter_bundle *
478 mcast_snooping_mrouter_lookup(struct mcast_snooping *ms, uint16_t vlan,
479                               void *port)
480     OVS_REQ_RDLOCK(ms->rwlock)
481 {
482     struct mcast_mrouter_bundle *mrouter;
483
484     LIST_FOR_EACH (mrouter, mrouter_node, &ms->mrouter_lru) {
485         if (mrouter->vlan == vlan && mrouter->port == port) {
486             return mrouter;
487         }
488     }
489     return NULL;
490 }
491
492 bool
493 mcast_snooping_add_mrouter(struct mcast_snooping *ms, uint16_t vlan,
494                            void *port)
495     OVS_REQ_WRLOCK(ms->rwlock)
496 {
497     struct mcast_mrouter_bundle *mrouter;
498
499     /* Avoid duplicate packets. */
500     if (mcast_snooping_port_lookup(&ms->fport_list, port)) {
501         return false;
502     }
503
504     mrouter = mcast_snooping_mrouter_lookup(ms, vlan, port);
505     if (mrouter) {
506         list_remove(&mrouter->mrouter_node);
507     } else {
508         mrouter = xmalloc(sizeof *mrouter);
509         mrouter->vlan = vlan;
510         mrouter->port = port;
511         COVERAGE_INC(mcast_snooping_learned);
512         ms->need_revalidate = true;
513     }
514
515     mrouter->expires = time_now() + MCAST_MROUTER_PORT_IDLE_TIME;
516     list_push_back(&ms->mrouter_lru, &mrouter->mrouter_node);
517     return ms->need_revalidate;
518 }
519
520 static void
521 mcast_snooping_flush_mrouter(struct mcast_mrouter_bundle *mrouter)
522 {
523     list_remove(&mrouter->mrouter_node);
524     free(mrouter);
525 }
526 \f
527 /* Ports */
528
529 static struct mcast_port_bundle *
530 mcast_port_from_list_node(struct ovs_list *list)
531 {
532     return CONTAINER_OF(list, struct mcast_port_bundle, node);
533 }
534
535 /* If the list is not empty, stores the fport in '*f' and returns true.
536  * Otherwise, if the list is empty, stores NULL in '*f' and return false. */
537 static bool
538 mcast_snooping_port_get(const struct ovs_list *list,
539                         struct mcast_port_bundle **f)
540 {
541     if (!list_is_empty(list)) {
542         *f = mcast_port_from_list_node(list->next);
543         return true;
544     } else {
545         *f = NULL;
546         return false;
547     }
548 }
549
550 static struct mcast_port_bundle *
551 mcast_snooping_port_lookup(struct ovs_list *list, void *port)
552 {
553     struct mcast_port_bundle *pbundle;
554
555     LIST_FOR_EACH (pbundle, node, list) {
556         if (pbundle->port == port) {
557             return pbundle;
558         }
559     }
560     return NULL;
561 }
562
563 static void
564 mcast_snooping_add_port(struct ovs_list *list, void *port)
565 {
566     struct mcast_port_bundle *pbundle;
567
568     pbundle = xmalloc(sizeof *pbundle);
569     pbundle->port = port;
570     list_insert(list, &pbundle->node);
571 }
572
573 static void
574 mcast_snooping_flush_port(struct mcast_port_bundle *pbundle)
575 {
576     list_remove(&pbundle->node);
577     free(pbundle);
578 }
579
580 \f
581 /* Flood ports. */
582 void
583 mcast_snooping_set_port_flood(struct mcast_snooping *ms, void *port,
584                               bool flood)
585     OVS_REQ_WRLOCK(ms->rwlock)
586 {
587     struct mcast_port_bundle *fbundle;
588
589     fbundle = mcast_snooping_port_lookup(&ms->fport_list, port);
590     if (flood && !fbundle) {
591         mcast_snooping_add_port(&ms->fport_list, port);
592         ms->need_revalidate = true;
593     } else if (!flood && fbundle) {
594         mcast_snooping_flush_port(fbundle);
595         ms->need_revalidate = true;
596     }
597 }
598 \f
599 /* Flood Reports ports. */
600
601 void
602 mcast_snooping_set_port_flood_reports(struct mcast_snooping *ms, void *port,
603                                       bool flood)
604     OVS_REQ_WRLOCK(ms->rwlock)
605 {
606     struct mcast_port_bundle *pbundle;
607
608     pbundle = mcast_snooping_port_lookup(&ms->rport_list, port);
609     if (flood && !pbundle) {
610         mcast_snooping_add_port(&ms->rport_list, port);
611         ms->need_revalidate = true;
612     } else if (!flood && pbundle) {
613         mcast_snooping_flush_port(pbundle);
614         ms->need_revalidate = true;
615     }
616 }
617 \f
618 /* Run and flush. */
619
620 static void
621 mcast_snooping_mdb_flush__(struct mcast_snooping *ms)
622     OVS_REQ_WRLOCK(ms->rwlock)
623 {
624     struct mcast_group *grp;
625     struct mcast_mrouter_bundle *mrouter;
626
627     while (group_get_lru(ms, &grp)) {
628         mcast_snooping_flush_group(ms, grp);
629     }
630
631     hmap_shrink(&ms->table);
632
633     while (mrouter_get_lru(ms, &mrouter)) {
634         mcast_snooping_flush_mrouter(mrouter);
635     }
636 }
637
638 void
639 mcast_snooping_mdb_flush(struct mcast_snooping *ms)
640 {
641     if (!mcast_snooping_enabled(ms)) {
642         return;
643     }
644
645     ovs_rwlock_wrlock(&ms->rwlock);
646     mcast_snooping_mdb_flush__(ms);
647     ovs_rwlock_unlock(&ms->rwlock);
648 }
649
650 /* Flushes mdb and flood ports. */
651 static void
652 mcast_snooping_flush__(struct mcast_snooping *ms)
653     OVS_REQ_WRLOCK(ms->rwlock)
654 {
655     struct mcast_group *grp;
656     struct mcast_mrouter_bundle *mrouter;
657     struct mcast_port_bundle *pbundle;
658
659     while (group_get_lru(ms, &grp)) {
660         mcast_snooping_flush_group(ms, grp);
661     }
662
663     hmap_shrink(&ms->table);
664
665     /* flush multicast routers */
666     while (mrouter_get_lru(ms, &mrouter)) {
667         mcast_snooping_flush_mrouter(mrouter);
668     }
669
670     /* flush flood ports */
671     while (mcast_snooping_port_get(&ms->fport_list, &pbundle)) {
672         mcast_snooping_flush_port(pbundle);
673     }
674
675     /* flush flood report ports */
676     while (mcast_snooping_port_get(&ms->rport_list, &pbundle)) {
677         mcast_snooping_flush_port(pbundle);
678     }
679 }
680
681 void
682 mcast_snooping_flush(struct mcast_snooping *ms)
683 {
684     if (!mcast_snooping_enabled(ms)) {
685         return;
686     }
687
688     ovs_rwlock_wrlock(&ms->rwlock);
689     mcast_snooping_flush__(ms);
690     ovs_rwlock_unlock(&ms->rwlock);
691 }
692
693 static bool
694 mcast_snooping_run__(struct mcast_snooping *ms)
695     OVS_REQ_WRLOCK(ms->rwlock)
696 {
697     bool need_revalidate;
698     struct mcast_group *grp;
699     struct mcast_mrouter_bundle *mrouter;
700     int mrouter_expired;
701
702     while (group_get_lru(ms, &grp)) {
703         if (hmap_count(&ms->table) > ms->max_entries) {
704             mcast_snooping_flush_group(ms, grp);
705         } else {
706             if (!mcast_snooping_prune_expired(ms, grp)) {
707                 break;
708             }
709         }
710     }
711
712     hmap_shrink(&ms->table);
713
714     mrouter_expired = 0;
715     while (mrouter_get_lru(ms, &mrouter)
716            && time_now() >= mrouter->expires) {
717         mcast_snooping_flush_mrouter(mrouter);
718         mrouter_expired++;
719     }
720
721     if (mrouter_expired) {
722         ms->need_revalidate = true;
723         COVERAGE_ADD(mcast_snooping_expired, mrouter_expired);
724     }
725
726     need_revalidate = ms->need_revalidate;
727     ms->need_revalidate = false;
728     return need_revalidate;
729 }
730
731 /* Does periodic work required by 'ms'. Returns true if something changed
732  * that may require flow revalidation. */
733 bool
734 mcast_snooping_run(struct mcast_snooping *ms)
735 {
736     bool need_revalidate;
737
738     if (!mcast_snooping_enabled(ms)) {
739         return false;
740     }
741
742     ovs_rwlock_wrlock(&ms->rwlock);
743     need_revalidate = mcast_snooping_run__(ms);
744     ovs_rwlock_unlock(&ms->rwlock);
745
746     return need_revalidate;
747 }
748
749 static void
750 mcast_snooping_wait__(struct mcast_snooping *ms)
751     OVS_REQ_RDLOCK(ms->rwlock)
752 {
753     if (hmap_count(&ms->table) > ms->max_entries
754         || ms->need_revalidate) {
755         poll_immediate_wake();
756     } else {
757         struct mcast_group *grp;
758         struct mcast_group_bundle *bundle;
759         struct mcast_mrouter_bundle *mrouter;
760         long long int mrouter_msec;
761         long long int msec = 0;
762
763         if (!list_is_empty(&ms->group_lru)) {
764             grp = mcast_group_from_lru_node(ms->group_lru.next);
765             bundle = mcast_group_bundle_from_lru_node(grp->bundle_lru.next);
766             msec = bundle->expires * 1000LL;
767         }
768
769         if (!list_is_empty(&ms->mrouter_lru)) {
770             mrouter = mcast_mrouter_from_lru_node(ms->mrouter_lru.next);
771             mrouter_msec = mrouter->expires * 1000LL;
772             msec = msec ? MIN(msec, mrouter_msec) : mrouter_msec;
773         }
774
775         if (msec) {
776             poll_timer_wait_until(msec);
777         }
778     }
779 }
780
781 void
782 mcast_snooping_wait(struct mcast_snooping *ms)
783 {
784     if (!mcast_snooping_enabled(ms)) {
785         return;
786     }
787
788     ovs_rwlock_rdlock(&ms->rwlock);
789     mcast_snooping_wait__(ms);
790     ovs_rwlock_unlock(&ms->rwlock);
791 }