]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif-sflow.c
hash: Introduce an implementation of murmurhash.
[ovs.git] / ofproto / ofproto-dpif-sflow.c
CommitLineData
72b06300 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
f1588b1f 3 * Copyright (c) 2009 InMon Corp.
72b06300
BP
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>
bae473fe 19#include "ofproto-dpif-sflow.h"
72b06300 20#include <inttypes.h>
f6eb6b20 21#include <sys/socket.h>
733adf2a 22#include <net/if.h>
72b06300
BP
23#include <stdlib.h>
24#include "collectors.h"
72b06300 25#include "compiler.h"
bae473fe 26#include "dpif.h"
0cc96e48
BP
27#include "hash.h"
28#include "hmap.h"
72b06300 29#include "netdev.h"
cdee00fd 30#include "netlink.h"
72b06300
BP
31#include "ofpbuf.h"
32#include "ofproto.h"
26233bb4 33#include "packets.h"
72b06300 34#include "poll-loop.h"
733adf2a 35#include "route-table.h"
72b06300
BP
36#include "sflow_api.h"
37#include "socket-util.h"
38#include "timeval.h"
72b06300 39#include "vlog.h"
975a704c 40#include "lib/odp-util.h"
392c7182 41#include "ofproto-provider.h"
72b06300 42
d98e6007 43VLOG_DEFINE_THIS_MODULE(sflow);
5136ce49 44
bae473fe
JP
45struct dpif_sflow_port {
46 struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */
72b06300 47 SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
392c7182 48 struct ofport *ofport; /* To retrive port stats. */
72b06300
BP
49};
50
bae473fe 51struct dpif_sflow {
72b06300
BP
52 struct collectors *collectors;
53 SFLAgent *sflow_agent;
54 struct ofproto_sflow_options *options;
55 struct dpif *dpif;
56 time_t next_tick;
57 size_t n_flood, n_all;
bae473fe 58 struct hmap ports; /* Contains "struct dpif_sflow_port"s. */
6ff686f2 59 uint32_t probability;
72b06300
BP
60};
61
bae473fe
JP
62static void dpif_sflow_del_port__(struct dpif_sflow *,
63 struct dpif_sflow_port *);
0cc96e48 64
72b06300
BP
65#define RECEIVER_INDEX 1
66
67static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
68
fa49ca80
BP
69static bool
70nullable_string_is_equal(const char *a, const char *b)
71{
72 return a ? b && !strcmp(a, b) : !b;
73}
74
72b06300
BP
75static bool
76ofproto_sflow_options_equal(const struct ofproto_sflow_options *a,
bae473fe 77 const struct ofproto_sflow_options *b)
72b06300 78{
81e2083f 79 return (sset_equals(&a->targets, &b->targets)
72b06300
BP
80 && a->sampling_rate == b->sampling_rate
81 && a->polling_interval == b->polling_interval
82 && a->header_len == b->header_len
83 && a->sub_id == b->sub_id
fa49ca80
BP
84 && nullable_string_is_equal(a->agent_device, b->agent_device)
85 && nullable_string_is_equal(a->control_ip, b->control_ip));
72b06300
BP
86}
87
88static struct ofproto_sflow_options *
89ofproto_sflow_options_clone(const struct ofproto_sflow_options *old)
90{
91 struct ofproto_sflow_options *new = xmemdup(old, sizeof *old);
81e2083f 92 sset_clone(&new->targets, &old->targets);
72b06300
BP
93 new->agent_device = old->agent_device ? xstrdup(old->agent_device) : NULL;
94 new->control_ip = old->control_ip ? xstrdup(old->control_ip) : NULL;
95 return new;
96}
97
98static void
99ofproto_sflow_options_destroy(struct ofproto_sflow_options *options)
100{
101 if (options) {
81e2083f 102 sset_destroy(&options->targets);
72b06300
BP
103 free(options->agent_device);
104 free(options->control_ip);
105 free(options);
106 }
107}
108
109/* sFlow library callback to allocate memory. */
110static void *
67a4917b
BP
111sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
112 size_t bytes)
72b06300
BP
113{
114 return calloc(1, bytes);
115}
116
117/* sFlow library callback to free memory. */
118static int
67a4917b
BP
119sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
120 void *obj)
72b06300
BP
121{
122 free(obj);
123 return 0;
124}
125
126/* sFlow library callback to report error. */
127static void
67a4917b
BP
128sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED,
129 char *msg)
72b06300
BP
130{
131 VLOG_WARN("sFlow agent error: %s", msg);
132}
133
134/* sFlow library callback to send datagram. */
135static void
bae473fe 136sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED,
67a4917b 137 SFLReceiver *receiver OVS_UNUSED, u_char *pkt,
72b06300
BP
138 uint32_t pktLen)
139{
bae473fe
JP
140 struct dpif_sflow *ds = ds_;
141 collectors_send(ds->collectors, pkt, pktLen);
72b06300
BP
142}
143
bae473fe
JP
144static struct dpif_sflow_port *
145dpif_sflow_find_port(const struct dpif_sflow *ds, uint16_t odp_port)
0cc96e48 146{
bae473fe 147 struct dpif_sflow_port *dsp;
0cc96e48 148
bae473fe
JP
149 HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node,
150 hash_int(odp_port, 0), &ds->ports) {
392c7182 151 if (ofp_port_to_odp_port(dsp->ofport->ofp_port) == odp_port) {
bae473fe 152 return dsp;
0cc96e48
BP
153 }
154 }
155 return NULL;
156}
157
72b06300 158static void
bae473fe 159sflow_agent_get_counters(void *ds_, SFLPoller *poller,
72b06300
BP
160 SFL_COUNTERS_SAMPLE_TYPE *cs)
161{
bae473fe 162 struct dpif_sflow *ds = ds_;
72b06300 163 SFLCounters_sample_element elem;
6c038611 164 enum netdev_features current;
bae473fe 165 struct dpif_sflow_port *dsp;
72b06300
BP
166 SFLIf_counters *counters;
167 struct netdev_stats stats;
168 enum netdev_flags flags;
72b06300 169
bae473fe
JP
170 dsp = dpif_sflow_find_port(ds, poller->bridgePort);
171 if (!dsp) {
72b06300
BP
172 return;
173 }
174
175 elem.tag = SFLCOUNTERS_GENERIC;
176 counters = &elem.counterBlock.generic;
177 counters->ifIndex = SFL_DS_INDEX(poller->dsi);
178 counters->ifType = 6;
392c7182 179 if (!netdev_get_features(dsp->ofport->netdev, &current, NULL, NULL, NULL)) {
bae473fe
JP
180 /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown,
181 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */
72b06300
BP
182 counters->ifSpeed = netdev_features_to_bps(current);
183 counters->ifDirection = (netdev_features_is_full_duplex(current)
184 ? 1 : 2);
185 } else {
186 counters->ifSpeed = 100000000;
b6dab095 187 counters->ifDirection = 0;
72b06300 188 }
392c7182 189 if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) {
72b06300 190 counters->ifStatus = 1; /* ifAdminStatus up. */
392c7182 191 if (netdev_get_carrier(dsp->ofport->netdev)) {
72b06300
BP
192 counters->ifStatus |= 2; /* ifOperStatus us. */
193 }
194 } else {
195 counters->ifStatus = 0; /* Down. */
196 }
197
198 /* XXX
199 1. Is the multicast counter filled in?
200 2. Does the multicast counter include broadcasts?
201 3. Does the rx_packets counter include multicasts/broadcasts?
202 */
392c7182 203 ofproto_port_get_stats(dsp->ofport, &stats);
72b06300
BP
204 counters->ifInOctets = stats.rx_bytes;
205 counters->ifInUcastPkts = stats.rx_packets;
206 counters->ifInMulticastPkts = stats.multicast;
207 counters->ifInBroadcastPkts = -1;
208 counters->ifInDiscards = stats.rx_dropped;
209 counters->ifInErrors = stats.rx_errors;
210 counters->ifInUnknownProtos = -1;
211 counters->ifOutOctets = stats.tx_bytes;
212 counters->ifOutUcastPkts = stats.tx_packets;
213 counters->ifOutMulticastPkts = -1;
214 counters->ifOutBroadcastPkts = -1;
215 counters->ifOutDiscards = stats.tx_dropped;
216 counters->ifOutErrors = stats.tx_errors;
217 counters->ifPromiscuousMode = 0;
218
219 SFLADD_ELEMENT(cs, &elem);
220 sfl_poller_writeCountersSample(poller, cs);
221}
222
223/* Obtains an address to use for the local sFlow agent and stores it into
224 * '*agent_addr'. Returns true if successful, false on failure.
225 *
226 * The sFlow agent address should be a local IP address that is persistent and
227 * reachable over the network, if possible. The IP address associated with
228 * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP
733adf2a
LG
229 * address used to talk to the controller. If the agent device is not
230 * specified then it is figured out by taking a look at the routing table based
231 * on 'targets'. */
72b06300 232static bool
733adf2a
LG
233sflow_choose_agent_address(const char *agent_device,
234 const struct sset *targets,
235 const char *control_ip,
72b06300
BP
236 SFLAddress *agent_addr)
237{
733adf2a 238 const char *target;
72b06300
BP
239 struct in_addr in4;
240
241 memset(agent_addr, 0, sizeof *agent_addr);
242 agent_addr->type = SFLADDRESSTYPE_IP_V4;
243
244 if (agent_device) {
733adf2a
LG
245 if (!netdev_get_in4_by_name(agent_device, &in4)) {
246 goto success;
247 }
248 }
249
250 SSET_FOR_EACH (target, targets) {
251 struct sockaddr_in sin;
252 char name[IFNAMSIZ];
253
254 if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sin)
255 && route_table_get_name(sin.sin_addr.s_addr, name)
256 && !netdev_get_in4_by_name(name, &in4)) {
257 goto success;
72b06300
BP
258 }
259 }
260
261 if (control_ip && !lookup_ip(control_ip, &in4)) {
262 goto success;
263 }
264
265 VLOG_ERR("could not determine IP address for sFlow agent");
266 return false;
267
268success:
6506f45c 269 agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr;
72b06300
BP
270 return true;
271}
272
273void
bae473fe 274dpif_sflow_clear(struct dpif_sflow *ds)
72b06300 275{
bae473fe
JP
276 if (ds->sflow_agent) {
277 sfl_agent_release(ds->sflow_agent);
278 ds->sflow_agent = NULL;
72b06300 279 }
bae473fe
JP
280 collectors_destroy(ds->collectors);
281 ds->collectors = NULL;
282 ofproto_sflow_options_destroy(ds->options);
283 ds->options = NULL;
72b06300 284
72b06300 285 /* Turn off sampling to save CPU cycles. */
6ff686f2 286 ds->probability = 0;
72b06300
BP
287}
288
289bool
bae473fe 290dpif_sflow_is_enabled(const struct dpif_sflow *ds)
72b06300 291{
bae473fe 292 return ds->collectors != NULL;
72b06300
BP
293}
294
bae473fe
JP
295struct dpif_sflow *
296dpif_sflow_create(struct dpif *dpif)
72b06300 297{
bae473fe 298 struct dpif_sflow *ds;
72b06300 299
bae473fe
JP
300 ds = xcalloc(1, sizeof *ds);
301 ds->dpif = dpif;
302 ds->next_tick = time_now() + 1;
303 hmap_init(&ds->ports);
6ff686f2 304 ds->probability = 0;
733adf2a
LG
305 route_table_register();
306
bae473fe 307 return ds;
72b06300
BP
308}
309
6ff686f2
PS
310/* 32-bit fraction of packets to sample with. A value of 0 samples no packets,
311 * a value of %UINT32_MAX samples all packets and intermediate values sample
312 * intermediate fractions of packets. */
313uint32_t
314dpif_sflow_get_probability(const struct dpif_sflow *ds)
315{
316 return ds->probability;
317}
318
72b06300 319void
bae473fe 320dpif_sflow_destroy(struct dpif_sflow *ds)
72b06300 321{
bae473fe
JP
322 if (ds) {
323 struct dpif_sflow_port *dsp, *next;
32d9dc11 324
733adf2a 325 route_table_unregister();
bae473fe
JP
326 dpif_sflow_clear(ds);
327 HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) {
328 dpif_sflow_del_port__(ds, dsp);
32d9dc11 329 }
bae473fe
JP
330 hmap_destroy(&ds->ports);
331 free(ds);
72b06300
BP
332 }
333}
334
335static void
392c7182 336dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
72b06300 337{
bae473fe 338 SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds,
72b06300 339 sflow_agent_get_counters);
bae473fe 340 sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval);
72b06300 341 sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
392c7182
PS
342 sfl_poller_set_bridgePort(poller,
343 ofp_port_to_odp_port(dsp->ofport->ofp_port));
72b06300
BP
344}
345
56fd8edf 346static void
bae473fe 347dpif_sflow_add_sampler(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
56fd8edf 348{
bae473fe
JP
349 SFLSampler *sampler = sfl_agent_addSampler(ds->sflow_agent, &dsp->dsi);
350 sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate);
351 sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len);
56fd8edf
BP
352 sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX);
353}
354
72b06300 355void
392c7182 356dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport)
72b06300 357{
bae473fe 358 struct dpif_sflow_port *dsp;
392c7182 359 uint16_t odp_port = ofp_port_to_odp_port(ofport->ofp_port);
72b06300 360 uint32_t ifindex;
72b06300 361
bae473fe 362 dpif_sflow_del_port(ds, odp_port);
72b06300 363
72b06300 364 /* Add to table of ports. */
bae473fe 365 dsp = xmalloc(sizeof *dsp);
392c7182 366 ifindex = netdev_get_ifindex(ofport->netdev);
72b06300 367 if (ifindex <= 0) {
bae473fe 368 ifindex = (ds->sflow_agent->subId << 16) + odp_port;
72b06300 369 }
392c7182 370 dsp->ofport = ofport;
bae473fe 371 SFL_DS_SET(dsp->dsi, 0, ifindex, 0);
bae473fe 372 hmap_insert(&ds->ports, &dsp->hmap_node, hash_int(odp_port, 0));
72b06300 373
5586445e 374 /* Add poller and sampler. */
bae473fe 375 if (ds->sflow_agent) {
392c7182 376 dpif_sflow_add_poller(ds, dsp);
bae473fe 377 dpif_sflow_add_sampler(ds, dsp);
72b06300
BP
378 }
379}
380
0cc96e48 381static void
bae473fe 382dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp)
0cc96e48 383{
bae473fe
JP
384 if (ds->sflow_agent) {
385 sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi);
386 sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi);
0cc96e48 387 }
bae473fe
JP
388 hmap_remove(&ds->ports, &dsp->hmap_node);
389 free(dsp);
0cc96e48
BP
390}
391
72b06300 392void
bae473fe 393dpif_sflow_del_port(struct dpif_sflow *ds, uint16_t odp_port)
72b06300 394{
bae473fe
JP
395 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
396 if (dsp) {
397 dpif_sflow_del_port__(ds, dsp);
72b06300
BP
398 }
399}
400
401void
bae473fe
JP
402dpif_sflow_set_options(struct dpif_sflow *ds,
403 const struct ofproto_sflow_options *options)
72b06300 404{
bae473fe 405 struct dpif_sflow_port *dsp;
72b06300 406 bool options_changed;
72b06300 407 SFLReceiver *receiver;
72b06300
BP
408 SFLAddress agentIP;
409 time_t now;
72b06300 410
81e2083f 411 if (sset_is_empty(&options->targets) || !options->sampling_rate) {
a68813c3
BP
412 /* No point in doing any work if there are no targets or nothing to
413 * sample. */
bae473fe 414 dpif_sflow_clear(ds);
a68813c3
BP
415 return;
416 }
417
bae473fe
JP
418 options_changed = (!ds->options
419 || !ofproto_sflow_options_equal(options, ds->options));
72b06300
BP
420
421 /* Configure collectors if options have changed or if we're shortchanged in
422 * collectors (which indicates that opening one or more of the configured
423 * collectors failed, so that we should retry). */
424 if (options_changed
bae473fe
JP
425 || collectors_count(ds->collectors) < sset_count(&options->targets)) {
426 collectors_destroy(ds->collectors);
02ef592c 427 collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT,
bae473fe
JP
428 &ds->collectors);
429 if (ds->collectors == NULL) {
a68813c3
BP
430 VLOG_WARN_RL(&rl, "no collectors could be initialized, "
431 "sFlow disabled");
bae473fe 432 dpif_sflow_clear(ds);
72b06300
BP
433 return;
434 }
435 }
436
733adf2a
LG
437 /* Choose agent IP address and agent device (if not yet setup) */
438 if (!sflow_choose_agent_address(options->agent_device,
439 &options->targets,
440 options->control_ip, &agentIP)) {
441 dpif_sflow_clear(ds);
442 return;
443 }
444
72b06300
BP
445 /* Avoid reconfiguring if options didn't change. */
446 if (!options_changed) {
447 return;
448 }
bae473fe
JP
449 ofproto_sflow_options_destroy(ds->options);
450 ds->options = ofproto_sflow_options_clone(options);
72b06300 451
72b06300
BP
452 /* Create agent. */
453 VLOG_INFO("creating sFlow agent %d", options->sub_id);
bae473fe
JP
454 if (ds->sflow_agent) {
455 sfl_agent_release(ds->sflow_agent);
72b06300 456 }
bae473fe 457 ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent);
c73814a3 458 now = time_wall();
bae473fe 459 sfl_agent_init(ds->sflow_agent,
72b06300
BP
460 &agentIP,
461 options->sub_id,
462 now, /* Boot time. */
463 now, /* Current time. */
bae473fe 464 ds, /* Pointer supplied to callbacks. */
72b06300
BP
465 sflow_agent_alloc_cb,
466 sflow_agent_free_cb,
467 sflow_agent_error_cb,
468 sflow_agent_send_packet_cb);
469
bae473fe 470 receiver = sfl_agent_addReceiver(ds->sflow_agent);
e53df206 471 sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow");
72b06300
BP
472 sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
473
72b06300 474 /* Set the sampling_rate down in the datapath. */
6ff686f2 475 ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate);
72b06300 476
56fd8edf 477 /* Add samplers and pollers for the currently known ports. */
bae473fe 478 HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) {
392c7182 479 dpif_sflow_add_poller(ds, dsp);
bae473fe 480 dpif_sflow_add_sampler(ds, dsp);
72b06300
BP
481 }
482}
483
6ff686f2 484int
bae473fe
JP
485dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds,
486 uint16_t odp_port)
c1e98da1 487{
bae473fe
JP
488 struct dpif_sflow_port *dsp = dpif_sflow_find_port(ds, odp_port);
489 return dsp ? SFL_DS_INDEX(dsp->dsi) : 0;
c1e98da1
BP
490}
491
72b06300 492void
6ff686f2
PS
493dpif_sflow_received(struct dpif_sflow *ds, struct ofpbuf *packet,
494 const struct flow *flow,
1673e0e4 495 const union user_action_cookie *cookie)
72b06300
BP
496{
497 SFL_FLOW_SAMPLE_TYPE fs;
498 SFLFlow_sample_element hdrElem;
499 SFLSampled_header *header;
500 SFLFlow_sample_element switchElem;
56fd8edf 501 SFLSampler *sampler;
6ff686f2
PS
502 struct dpif_sflow_port *in_dsp;
503 struct netdev_stats stats;
1673e0e4 504 ovs_be16 vlan_tci;
6ff686f2 505 int error;
72b06300
BP
506
507 /* Build a flow sample */
508 memset(&fs, 0, sizeof fs);
6ff686f2
PS
509
510 in_dsp = dpif_sflow_find_port(ds, ofp_port_to_odp_port(flow->in_port));
511 if (!in_dsp) {
512 return;
513 }
514 fs.input = SFL_DS_INDEX(in_dsp->dsi);
515
392c7182 516 error = ofproto_port_get_stats(in_dsp->ofport, &stats);
6ff686f2
PS
517 if (error) {
518 VLOG_WARN_RL(&rl, "netdev get-stats error %s", strerror(error));
519 return;
520 }
521 fs.sample_pool = stats.rx_packets;
72b06300 522
56fd8edf
BP
523 /* We are going to give it to the sampler that represents this input port.
524 * By implementing "ingress-only" sampling like this we ensure that we
525 * never have to offer the same sample to more than one sampler. */
bae473fe 526 sampler = sfl_agent_getSamplerByIfIndex(ds->sflow_agent, fs.input);
56fd8edf
BP
527 if (!sampler) {
528 VLOG_WARN_RL(&rl, "no sampler for input ifIndex (%"PRIu32")",
529 fs.input);
530 return;
531 }
532
72b06300
BP
533 /* Sampled header. */
534 memset(&hdrElem, 0, sizeof hdrElem);
535 hdrElem.tag = SFLFLOW_HEADER;
536 header = &hdrElem.flowType.header;
537 header->header_protocol = SFLHEADER_ETHERNET_ISO8023;
c62caaa3
NM
538 /* The frame_length should include the Ethernet FCS (4 bytes),
539 but it has already been stripped, so we need to add 4 here. */
6ff686f2 540 header->frame_length = packet->size + 4;
c62caaa3
NM
541 /* Ethernet FCS stripped off. */
542 header->stripped = 4;
6ff686f2 543 header->header_length = MIN(packet->size,
856081f6 544 sampler->sFlowFsMaximumHeaderSize);
6ff686f2 545 header->header_bytes = packet->data;
72b06300
BP
546
547 /* Add extended switch element. */
548 memset(&switchElem, 0, sizeof(switchElem));
549 switchElem.tag = SFLFLOW_EX_SWITCH;
856081f6
BP
550 switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci);
551 switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci);
6ff686f2
PS
552
553 /* Retrieve data from user_action_cookie. */
1673e0e4
BP
554 vlan_tci = cookie->sflow.vlan_tci;
555 switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci);
556 switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci);
46b47a41 557
1673e0e4 558 fs.output = cookie->sflow.output;
72b06300
BP
559
560 /* Submit the flow sample to be encoded into the next datagram. */
561 SFLADD_ELEMENT(&fs, &hdrElem);
562 SFLADD_ELEMENT(&fs, &switchElem);
563 sfl_sampler_writeFlowSample(sampler, &fs);
564}
565
72b06300 566void
bae473fe 567dpif_sflow_run(struct dpif_sflow *ds)
72b06300 568{
bae473fe 569 if (dpif_sflow_is_enabled(ds)) {
72b06300 570 time_t now = time_now();
733adf2a 571 route_table_run();
bae473fe
JP
572 if (now >= ds->next_tick) {
573 sfl_agent_tick(ds->sflow_agent, time_wall());
574 ds->next_tick = now + 1;
72b06300
BP
575 }
576 }
577}
578
579void
bae473fe 580dpif_sflow_wait(struct dpif_sflow *ds)
72b06300 581{
bae473fe
JP
582 if (dpif_sflow_is_enabled(ds)) {
583 poll_timer_wait_until(ds->next_tick * 1000LL);
72b06300
BP
584 }
585}