32cf51b81b67818e3c79f369ca5b0cad49f32b35
[cascardo/ovs.git] / ofproto / ofproto-dpif-sflow.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  * Copyright (c) 2009 InMon Corp.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <config.h>
19 #include "ofproto-dpif-sflow.h"
20 #include <inttypes.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <stdlib.h>
24 #include "collectors.h"
25 #include "compiler.h"
26 #include "dpif.h"
27 #include "hash.h"
28 #include "hmap.h"
29 #include "netdev.h"
30 #include "netlink.h"
31 #include "ofpbuf.h"
32 #include "ofproto.h"
33 #include "packets.h"
34 #include "poll-loop.h"
35 #include "route-table.h"
36 #include "sflow_api.h"
37 #include "socket-util.h"
38 #include "timeval.h"
39 #include "vlog.h"
40 #include "lib/odp-util.h"
41 #include "ofproto-provider.h"
42
43 VLOG_DEFINE_THIS_MODULE(sflow);
44
45 static struct ovs_mutex mutex;
46
47 struct dpif_sflow_port {
48     struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
49     SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
50     struct ofport *ofport;      /* To retrive port stats. */
51     odp_port_t odp_port;
52 };
53
54 struct dpif_sflow {
55     struct collectors *collectors;
56     SFLAgent *sflow_agent;
57     struct ofproto_sflow_options *options;
58     time_t next_tick;
59     size_t n_flood, n_all;
60     struct hmap ports;          /* Contains "struct dpif_sflow_port"s. */
61     uint32_t probability;
62     struct ovs_refcount ref_cnt;
63 };
64
65 static void dpif_sflow_del_port__(struct dpif_sflow *,
66                                   struct dpif_sflow_port *);
67
68 #define RECEIVER_INDEX 1
69
70 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
71
72 static bool
73 nullable_string_is_equal(const char *a, const char *b)
74 {
75     return a ? b && !strcmp(a, b) : !b;
76 }
77
78 static bool
79 ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
80                          const struct ofproto_sflow_options *b)
81 {
82     return (sset_equals(&a->targets, &b->targets)
83             && a->sampling_rate == b->sampling_rate
84             && a->polling_interval == b->polling_interval
85             && a->header_len == b->header_len
86             && a->sub_id == b->sub_id
87             && nullable_string_is_equal(a->agent_device, b->agent_device)
88             && nullable_string_is_equal(a->control_ip, b->control_ip));
89 }
90
91 static struct ofproto_sflow_options *
92 ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
93 {
94     struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
95     sset_clone(&new->targets, &old->targets);
96     new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
97     new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
98     return new;
99 }
100
101 static void
102 ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
103 {
104     if (options) {
105         sset_destroy(&options->targets);
106         free(options->agent_device);
107         free(options->control_ip);
108         free(options);
109     }
110 }
111
112 /* sFlow library callback to allocate memory. */
113 static void *
114 sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
115                      size_t bytes)
116 {
117     return calloc(1, bytes);
118 }
119
120 /* sFlow library callback to free memory. */
121 static int
122 sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
123                     void *obj)
124 {
125     free(obj);
126     return 0;
127 }
128
129 /* sFlow library callback to report error. */
130 static void
131 sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
132                      char *msg)
133 {
134     VLOG_WARN("sFlow agent error: %s", msg);
135 }
136
137 /* sFlow library callback to send datagram. */
138 static void
139 sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
140                            SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
141                            uint32_t pktLen)
142 {
143     struct dpif_sflow *ds = ds_;
144     collectors_send(ds->collectors, pkt, pktLen);
145 }
146
147 static struct dpif_sflow_port *
148 dpif_sflow_find_port(const struct dpif_sflow *ds, odp_port_t odp_port)
149     OVS_REQUIRES(mutex)
150 {
151     struct dpif_sflow_port *dsp;
152
153     HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node, hash_odp_port(odp_port),
154                              &ds->ports) {
155         if (dsp->odp_port == odp_port) {
156             return dsp;
157         }
158     }
159     return NULL;
160 }
161
162 static void
163 sflow_agent_get_counters(void *ds_, SFLPoller *poller,
164                          SFL_COUNTERS_SAMPLE_TYPE *cs)
165     OVS_REQUIRES(mutex)
166 {
167     struct dpif_sflow *ds = ds_;
168     SFLCounters_sample_element elem;
169     enum netdev_features current;
170     struct dpif_sflow_port *dsp;
171     SFLIf_counters *counters;
172     struct netdev_stats stats;
173     enum netdev_flags flags;
174
175     dsp = dpif_sflow_find_port(ds, u32_to_odp(poller->bridgePort));
176     if (!dsp) {
177         return;
178     }
179
180     elem.tag = SFLCOUNTERS_GENERIC;
181     counters = &elem.counterBlock.generic;
182     counters->ifIndex = SFL_DS_INDEX(poller->dsi);
183     counters->ifType = 6;
184     if (!netdev_get_features(dsp->ofport->netdev, &current, NULL, NULL, NULL)) {
185         /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
186            1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
187         counters->ifSpeed = netdev_features_to_bps(current, 0);
188         counters->ifDirection = (netdev_features_is_full_duplex(current)
189                                  ? 1 : 2);
190     } else {
191         counters->ifSpeed = 100000000;
192         counters->ifDirection = 0;
193     }
194     if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) {
195         counters->ifStatus = 1; /* ifAdminStatus up. */
196         if (netdev_get_carrier(dsp->ofport->netdev)) {
197             counters->ifStatus |= 2; /* ifOperStatus us. */
198         }
199     } else {
200         counters->ifStatus = 0;  /* Down. */
201     }
202
203     /* XXX
204        1. Is the multicast counter filled in?
205        2. Does the multicast counter include broadcasts?
206        3. Does the rx_packets counter include multicasts/broadcasts?
207     */
208     ofproto_port_get_stats(dsp->ofport, &stats);
209     counters->ifInOctets = stats.rx_bytes;
210     counters->ifInUcastPkts = stats.rx_packets;
211     counters->ifInMulticastPkts = stats.multicast;
212     counters->ifInBroadcastPkts = -1;
213     counters->ifInDiscards = stats.rx_dropped;
214     counters->ifInErrors = stats.rx_errors;
215     counters->ifInUnknownProtos = -1;
216     counters->ifOutOctets = stats.tx_bytes;
217     counters->ifOutUcastPkts = stats.tx_packets;
218     counters->ifOutMulticastPkts = -1;
219     counters->ifOutBroadcastPkts = -1;
220     counters->ifOutDiscards = stats.tx_dropped;
221     counters->ifOutErrors = stats.tx_errors;
222     counters->ifPromiscuousMode = 0;
223
224     SFLADD_ELEMENT(cs, &elem);
225     sfl_poller_writeCountersSample(poller, cs);
226 }
227
228 /* Obtains an address to use for the local sFlow agent and stores it into
229  * '*agent_addr'.  Returns true if successful, false on failure.
230  *
231  * The sFlow agent address should be a local IP address that is persistent and
232  * reachable over the network, if possible.  The IP address associated with
233  * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
234  * address used to talk to the controller.  If the agent device is not
235  * specified then it is figured out by taking a look at the routing table based
236  * on 'targets'. */
237 static bool
238 sflow_choose_agent_address(const char *agent_device,
239                            const struct sset *targets,
240                            const char *control_ip,
241                            SFLAddress *agent_addr)
242 {
243     const char *target;
244     struct in_addr in4;
245
246     memset(agent_addr, 0, sizeof *agent_addr);
247     agent_addr->type = SFLADDRESSTYPE_IP_V4;
248
249     if (agent_device) {
250         if (!netdev_get_in4_by_name(agent_device, &in4)) {
251             goto success;
252         }
253     }
254
255     SSET_FOR_EACH (target, targets) {
256         union {
257             struct sockaddr_storage ss;
258             struct sockaddr_in sin;
259         } sa;
260         char name[IFNAMSIZ];
261
262         if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sa.ss)
263             && sa.ss.ss_family == AF_INET) {
264             if (route_table_get_name(sa.sin.sin_addr.s_addr, name)
265                 && !netdev_get_in4_by_name(name, &in4)) {
266                 goto success;
267             }
268         }
269     }
270
271     if (control_ip && !lookup_ip(control_ip, &in4)) {
272         goto success;
273     }
274
275     VLOG_ERR("could not determine IP address for sFlow agent");
276     return false;
277
278 success:
279     agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
280     return true;
281 }
282
283 static void
284 dpif_sflow_clear__(struct dpif_sflow *ds) OVS_REQUIRES(mutex)
285 {
286     if (ds->sflow_agent) {
287         sfl_agent_release(ds->sflow_agent);
288         free(ds->sflow_agent);
289         ds->sflow_agent = NULL;
290     }
291     collectors_destroy(ds->collectors);
292     ds->collectors = NULL;
293     ofproto_sflow_options_destroy(ds->options);
294     ds->options = NULL;
295
296     /* Turn off sampling to save CPU cycles. */
297     ds->probability = 0;
298 }
299
300 void
301 dpif_sflow_clear(struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
302 {
303     ovs_mutex_lock(&mutex);
304     dpif_sflow_clear__(ds);
305     ovs_mutex_unlock(&mutex);
306 }
307
308 bool
309 dpif_sflow_is_enabled(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
310 {
311     bool enabled;
312
313     ovs_mutex_lock(&mutex);
314     enabled = ds->collectors != NULL;
315     ovs_mutex_unlock(&mutex);
316     return enabled;
317 }
318
319 struct dpif_sflow *
320 dpif_sflow_create(void)
321 {
322     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
323     struct dpif_sflow *ds;
324
325     if (ovsthread_once_start(&once)) {
326         ovs_mutex_init_recursive(&mutex);
327         ovsthread_once_done(&once);
328     }
329
330     ds = xcalloc(1, sizeof *ds);
331     ds->next_tick = time_now() + 1;
332     hmap_init(&ds->ports);
333     ds->probability = 0;
334     route_table_register();
335     ovs_refcount_init(&ds->ref_cnt);
336
337     return ds;
338 }
339
340 struct dpif_sflow *
341 dpif_sflow_ref(const struct dpif_sflow *ds_)
342 {
343     struct dpif_sflow *ds = CONST_CAST(struct dpif_sflow *, ds_);
344     if (ds) {
345         ovs_refcount_ref(&ds->ref_cnt);
346     }
347     return ds;
348 }
349
350 /* 32-bit fraction of packets to sample with.  A value of 0 samples no packets,
351  * a value of %UINT32_MAX samples all packets and intermediate values sample
352  * intermediate fractions of packets. */
353 uint32_t
354 dpif_sflow_get_probability(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
355 {
356     uint32_t probability;
357     ovs_mutex_lock(&mutex);
358     probability = ds->probability;
359     ovs_mutex_unlock(&mutex);
360     return probability;
361 }
362
363 void
364 dpif_sflow_unref(struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
365 {
366     if (ds && ovs_refcount_unref_relaxed(&ds->ref_cnt) == 1) {
367         struct dpif_sflow_port *dsp, *next;
368
369         route_table_unregister();
370         dpif_sflow_clear(ds);
371         HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
372             dpif_sflow_del_port__(ds, dsp);
373         }
374         hmap_destroy(&ds->ports);
375         free(ds);
376     }
377 }
378
379 static void
380 dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
381     OVS_REQUIRES(mutex)
382 {
383     SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
384                                             sflow_agent_get_counters);
385     sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
386     sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
387     sfl_poller_set_bridgePort(poller, odp_to_u32(dsp->odp_port));
388 }
389
390 void
391 dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport,
392                     odp_port_t odp_port) OVS_EXCLUDED(mutex)
393 {
394     struct dpif_sflow_port *dsp;
395     int ifindex;
396
397     ovs_mutex_lock(&mutex);
398     dpif_sflow_del_port(ds, odp_port);
399
400     ifindex = netdev_get_ifindex(ofport->netdev);
401
402     if (ifindex <= 0) {
403         /* Not an ifindex port, so do not add a cross-reference to it here */
404         goto out;
405     }
406
407     /* Add to table of ports. */
408     dsp = xmalloc(sizeof *dsp);
409     dsp->ofport = ofport;
410     dsp->odp_port = odp_port;
411     SFL_DS_SET(dsp->dsi, SFL_DSCLASS_IFINDEX, ifindex, 0);
412     hmap_insert(&ds->ports, &dsp->hmap_node, hash_odp_port(odp_port));
413
414     /* Add poller. */
415     if (ds->sflow_agent) {
416         dpif_sflow_add_poller(ds, dsp);
417     }
418
419 out:
420     ovs_mutex_unlock(&mutex);
421 }
422
423 static void
424 dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
425     OVS_REQUIRES(mutex)
426 {
427     if (ds->sflow_agent) {
428         sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
429         sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
430     }
431     hmap_remove(&ds->ports, &dsp->hmap_node);
432     free(dsp);
433 }
434
435 void
436 dpif_sflow_del_port(struct dpif_sflow *ds, odp_port_t odp_port)
437     OVS_EXCLUDED(mutex)
438 {
439     struct dpif_sflow_port *dsp;
440
441     ovs_mutex_lock(&mutex);
442     dsp = dpif_sflow_find_port(ds, odp_port);
443     if (dsp) {
444         dpif_sflow_del_port__(ds, dsp);
445     }
446     ovs_mutex_unlock(&mutex);
447 }
448
449 void
450 dpif_sflow_set_options(struct dpif_sflow *ds,
451                        const struct ofproto_sflow_options *options)
452     OVS_EXCLUDED(mutex)
453 {
454     struct dpif_sflow_port *dsp;
455     bool options_changed;
456     SFLReceiver *receiver;
457     SFLAddress agentIP;
458     time_t now;
459     SFLDataSource_instance dsi;
460     uint32_t dsIndex;
461     SFLSampler *sampler;
462
463     ovs_mutex_lock(&mutex);
464     if (sset_is_empty(&options->targets) || !options->sampling_rate) {
465         /* No point in doing any work if there are no targets or nothing to
466          * sample. */
467         dpif_sflow_clear__(ds);
468         goto out;
469     }
470
471     options_changed = (!ds->options
472                        || !ofproto_sflow_options_equal(options, ds->options));
473
474     /* Configure collectors if options have changed or if we're shortchanged in
475      * collectors (which indicates that opening one or more of the configured
476      * collectors failed, so that we should retry). */
477     if (options_changed
478         || collectors_count(ds->collectors) < sset_count(&options->targets)) {
479         collectors_destroy(ds->collectors);
480         collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
481                           &ds->collectors);
482         if (ds->collectors == NULL) {
483             VLOG_WARN_RL(&rl, "no collectors could be initialized, "
484                          "sFlow disabled");
485             dpif_sflow_clear__(ds);
486             goto out;
487         }
488     }
489
490     /* Choose agent IP address and agent device (if not yet setup) */
491     if (!sflow_choose_agent_address(options->agent_device,
492                                     &options->targets,
493                                     options->control_ip, &agentIP)) {
494         dpif_sflow_clear__(ds);
495         goto out;
496     }
497
498     /* Avoid reconfiguring if options didn't change. */
499     if (!options_changed) {
500         goto out;
501     }
502     ofproto_sflow_options_destroy(ds->options);
503     ds->options = ofproto_sflow_options_clone(options);
504
505     /* Create agent. */
506     VLOG_INFO("creating sFlow agent %d", options->sub_id);
507     if (ds->sflow_agent) {
508         sfl_agent_release(ds->sflow_agent);
509     }
510     ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
511     now = time_wall();
512     sfl_agent_init(ds->sflow_agent,
513                    &agentIP,
514                    options->sub_id,
515                    now,         /* Boot time. */
516                    now,         /* Current time. */
517                    ds,          /* Pointer supplied to callbacks. */
518                    sflow_agent_alloc_cb,
519                    sflow_agent_free_cb,
520                    sflow_agent_error_cb,
521                    sflow_agent_send_packet_cb);
522
523     receiver = sfl_agent_addReceiver(ds->sflow_agent);
524     sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
525     sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
526
527     /* Set the sampling_rate down in the datapath. */
528     ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate);
529
530     /* Add a single sampler for the bridge. This appears as a PHYSICAL_ENTITY
531        because it is associated with the hypervisor, and interacts with the server
532        hardware directly.  The sub_id is used to distinguish this sampler from
533        others on other bridges within the same agent. */
534     dsIndex = 1000 + options->sub_id;
535     SFL_DS_SET(dsi, SFL_DSCLASS_PHYSICAL_ENTITY, dsIndex, 0);
536     sampler = sfl_agent_addSampler(ds->sflow_agent, &dsi);
537     sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
538     sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
539     sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
540
541     /* Add pollers for the currently known ifindex-ports */
542     HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
543         dpif_sflow_add_poller(ds, dsp);
544     }
545
546
547 out:
548     ovs_mutex_unlock(&mutex);
549 }
550
551 int
552 dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
553                                odp_port_t odp_port) OVS_EXCLUDED(mutex)
554 {
555     struct dpif_sflow_port *dsp;
556     int ret;
557
558     ovs_mutex_lock(&mutex);
559     dsp = dpif_sflow_find_port(ds, odp_port);
560     ret = dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
561     ovs_mutex_unlock(&mutex);
562     return ret;
563 }
564
565 void
566 dpif_sflow_received(struct dpif_sflow *ds, const struct ofpbuf *packet,
567                     const struct flow *flow, odp_port_t odp_in_port,
568                     const union user_action_cookie *cookie)
569     OVS_EXCLUDED(mutex)
570 {
571     SFL_FLOW_SAMPLE_TYPE fs;
572     SFLFlow_sample_element hdrElem;
573     SFLSampled_header *header;
574     SFLFlow_sample_element switchElem;
575     SFLSampler *sampler;
576     struct dpif_sflow_port *in_dsp;
577     ovs_be16 vlan_tci;
578
579     ovs_mutex_lock(&mutex);
580     sampler = ds->sflow_agent->samplers;
581     if (!sampler) {
582         goto out;
583     }
584
585     /* Build a flow sample. */
586     memset(&fs, 0, sizeof fs);
587
588     /* Look up the input ifIndex if this port has one. Otherwise just
589      * leave it as 0 (meaning 'unknown') and continue. */
590     in_dsp = dpif_sflow_find_port(ds, odp_in_port);
591     if (in_dsp) {
592         fs.input = SFL_DS_INDEX(in_dsp->dsi);
593     }
594
595     /* Make the assumption that the random number generator in the datapath converges
596      * to the configured mean, and just increment the samplePool by the configured
597      * sampling rate every time. */
598     sampler->samplePool += sfl_sampler_get_sFlowFsPacketSamplingRate(sampler);
599
600     /* Sampled header. */
601     memset(&hdrElem, 0, sizeof hdrElem);
602     hdrElem.tag = SFLFLOW_HEADER;
603     header = &hdrElem.flowType.header;
604     header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
605     /* The frame_length should include the Ethernet FCS (4 bytes),
606      * but it has already been stripped,  so we need to add 4 here. */
607     header->frame_length = ofpbuf_size(packet) + 4;
608     /* Ethernet FCS stripped off. */
609     header->stripped = 4;
610     header->header_length = MIN(ofpbuf_size(packet),
611                                 sampler->sFlowFsMaximumHeaderSize);
612     header->header_bytes = ofpbuf_data(packet);
613
614     /* Add extended switch element. */
615     memset(&switchElem, 0, sizeof(switchElem));
616     switchElem.tag = SFLFLOW_EX_SWITCH;
617     switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
618     switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
619
620     /* Retrieve data from user_action_cookie. */
621     vlan_tci = cookie->sflow.vlan_tci;
622     switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci);
623     switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci);
624
625     fs.output = cookie->sflow.output;
626
627     /* Submit the flow sample to be encoded into the next datagram. */
628     SFLADD_ELEMENT(&fs, &hdrElem);
629     SFLADD_ELEMENT(&fs, &switchElem);
630     sfl_sampler_writeFlowSample(sampler, &fs);
631
632 out:
633     ovs_mutex_unlock(&mutex);
634 }
635
636 void
637 dpif_sflow_run(struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
638 {
639     ovs_mutex_lock(&mutex);
640     if (ds->collectors != NULL) {
641         time_t now = time_now();
642         route_table_run();
643         if (now >= ds->next_tick) {
644             sfl_agent_tick(ds->sflow_agent, time_wall());
645             ds->next_tick = now + 1;
646         }
647     }
648     ovs_mutex_unlock(&mutex);
649 }
650
651 void
652 dpif_sflow_wait(struct dpif_sflow *ds) OVS_EXCLUDED(mutex)
653 {
654     ovs_mutex_lock(&mutex);
655     if (ds->collectors != NULL) {
656         poll_timer_wait_until(ds->next_tick * 1000LL);
657     }
658     ovs_mutex_unlock(&mutex);
659 }