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