]>
Commit | Line | Data |
---|---|---|
72b06300 | 1 | /* |
10a89ef0 | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 | 43 | VLOG_DEFINE_THIS_MODULE(sflow); |
5136ce49 | 44 | |
34ae6d76 EJ |
45 | static struct ovs_mutex mutex; |
46 | ||
bae473fe JP |
47 | struct dpif_sflow_port { |
48 | struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */ | |
72b06300 | 49 | SFLDataSource_instance dsi; /* sFlow library's notion of port number. */ |
392c7182 | 50 | struct ofport *ofport; /* To retrive port stats. */ |
4e022ec0 | 51 | odp_port_t odp_port; |
72b06300 BP |
52 | }; |
53 | ||
bae473fe | 54 | struct dpif_sflow { |
72b06300 BP |
55 | struct collectors *collectors; |
56 | SFLAgent *sflow_agent; | |
57 | struct ofproto_sflow_options *options; | |
72b06300 BP |
58 | time_t next_tick; |
59 | size_t n_flood, n_all; | |
bae473fe | 60 | struct hmap ports; /* Contains "struct dpif_sflow_port"s. */ |
6ff686f2 | 61 | uint32_t probability; |
34ae6d76 | 62 | atomic_int ref_cnt; |
72b06300 BP |
63 | }; |
64 | ||
bae473fe JP |
65 | static void dpif_sflow_del_port__(struct dpif_sflow *, |
66 | struct dpif_sflow_port *); | |
0cc96e48 | 67 | |
72b06300 BP |
68 | #define RECEIVER_INDEX 1 |
69 | ||
70 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
71 | ||
fa49ca80 BP |
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 | ||
72b06300 BP |
78 | static bool |
79 | ofproto_sflow_options_equal(const struct ofproto_sflow_options *a, | |
bae473fe | 80 | const struct ofproto_sflow_options *b) |
72b06300 | 81 | { |
81e2083f | 82 | return (sset_equals(&a->targets, &b->targets) |
72b06300 BP |
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 | |
fa49ca80 BP |
87 | && nullable_string_is_equal(a->agent_device, b->agent_device) |
88 | && nullable_string_is_equal(a->control_ip, b->control_ip)); | |
72b06300 BP |
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); | |
81e2083f | 95 | sset_clone(&new->targets, &old->targets); |
72b06300 BP |
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) { | |
81e2083f | 105 | sset_destroy(&options->targets); |
72b06300 BP |
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 * | |
67a4917b BP |
114 | sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
115 | size_t bytes) | |
72b06300 BP |
116 | { |
117 | return calloc(1, bytes); | |
118 | } | |
119 | ||
120 | /* sFlow library callback to free memory. */ | |
121 | static int | |
67a4917b BP |
122 | sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
123 | void *obj) | |
72b06300 BP |
124 | { |
125 | free(obj); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* sFlow library callback to report error. */ | |
130 | static void | |
67a4917b BP |
131 | sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
132 | char *msg) | |
72b06300 BP |
133 | { |
134 | VLOG_WARN("sFlow agent error: %s", msg); | |
135 | } | |
136 | ||
137 | /* sFlow library callback to send datagram. */ | |
138 | static void | |
bae473fe | 139 | sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED, |
67a4917b | 140 | SFLReceiver *receiver OVS_UNUSED, u_char *pkt, |
72b06300 BP |
141 | uint32_t pktLen) |
142 | { | |
bae473fe JP |
143 | struct dpif_sflow *ds = ds_; |
144 | collectors_send(ds->collectors, pkt, pktLen); | |
72b06300 BP |
145 | } |
146 | ||
bae473fe | 147 | static struct dpif_sflow_port * |
4e022ec0 | 148 | dpif_sflow_find_port(const struct dpif_sflow *ds, odp_port_t odp_port) |
34ae6d76 | 149 | OVS_REQ_WRLOCK(&mutex) |
0cc96e48 | 150 | { |
bae473fe | 151 | struct dpif_sflow_port *dsp; |
0cc96e48 | 152 | |
f9c0c3ec EJ |
153 | HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node, hash_odp_port(odp_port), |
154 | &ds->ports) { | |
e1b1d06a | 155 | if (dsp->odp_port == odp_port) { |
bae473fe | 156 | return dsp; |
0cc96e48 BP |
157 | } |
158 | } | |
159 | return NULL; | |
160 | } | |
161 | ||
72b06300 | 162 | static void |
bae473fe | 163 | sflow_agent_get_counters(void *ds_, SFLPoller *poller, |
72b06300 | 164 | SFL_COUNTERS_SAMPLE_TYPE *cs) |
34ae6d76 | 165 | OVS_REQ_WRLOCK(&mutex) |
72b06300 | 166 | { |
bae473fe | 167 | struct dpif_sflow *ds = ds_; |
72b06300 | 168 | SFLCounters_sample_element elem; |
6c038611 | 169 | enum netdev_features current; |
bae473fe | 170 | struct dpif_sflow_port *dsp; |
72b06300 BP |
171 | SFLIf_counters *counters; |
172 | struct netdev_stats stats; | |
173 | enum netdev_flags flags; | |
72b06300 | 174 | |
4e022ec0 | 175 | dsp = dpif_sflow_find_port(ds, u32_to_odp(poller->bridgePort)); |
bae473fe | 176 | if (!dsp) { |
72b06300 BP |
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; | |
392c7182 | 184 | if (!netdev_get_features(dsp->ofport->netdev, ¤t, NULL, NULL, NULL)) { |
bae473fe JP |
185 | /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown, |
186 | 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */ | |
d02a5f8e | 187 | counters->ifSpeed = netdev_features_to_bps(current, 0); |
72b06300 BP |
188 | counters->ifDirection = (netdev_features_is_full_duplex(current) |
189 | ? 1 : 2); | |
190 | } else { | |
191 | counters->ifSpeed = 100000000; | |
b6dab095 | 192 | counters->ifDirection = 0; |
72b06300 | 193 | } |
392c7182 | 194 | if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) { |
72b06300 | 195 | counters->ifStatus = 1; /* ifAdminStatus up. */ |
392c7182 | 196 | if (netdev_get_carrier(dsp->ofport->netdev)) { |
72b06300 BP |
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 | */ | |
392c7182 | 208 | ofproto_port_get_stats(dsp->ofport, &stats); |
72b06300 BP |
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 | |
733adf2a LG |
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'. */ | |
72b06300 | 237 | static bool |
733adf2a LG |
238 | sflow_choose_agent_address(const char *agent_device, |
239 | const struct sset *targets, | |
240 | const char *control_ip, | |
72b06300 BP |
241 | SFLAddress *agent_addr) |
242 | { | |
733adf2a | 243 | const char *target; |
72b06300 BP |
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) { | |
733adf2a LG |
250 | if (!netdev_get_in4_by_name(agent_device, &in4)) { |
251 | goto success; | |
252 | } | |
253 | } | |
254 | ||
255 | SSET_FOR_EACH (target, targets) { | |
256 | struct sockaddr_in sin; | |
257 | char name[IFNAMSIZ]; | |
258 | ||
259 | if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sin) | |
260 | && route_table_get_name(sin.sin_addr.s_addr, name) | |
261 | && !netdev_get_in4_by_name(name, &in4)) { | |
262 | goto success; | |
72b06300 BP |
263 | } |
264 | } | |
265 | ||
266 | if (control_ip && !lookup_ip(control_ip, &in4)) { | |
267 | goto success; | |
268 | } | |
269 | ||
270 | VLOG_ERR("could not determine IP address for sFlow agent"); | |
271 | return false; | |
272 | ||
273 | success: | |
6506f45c | 274 | agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr; |
72b06300 BP |
275 | return true; |
276 | } | |
277 | ||
34ae6d76 EJ |
278 | static void |
279 | dpif_sflow_clear__(struct dpif_sflow *ds) OVS_REQ_WRLOCK(mutex) | |
72b06300 | 280 | { |
bae473fe JP |
281 | if (ds->sflow_agent) { |
282 | sfl_agent_release(ds->sflow_agent); | |
283 | ds->sflow_agent = NULL; | |
72b06300 | 284 | } |
bae473fe JP |
285 | collectors_destroy(ds->collectors); |
286 | ds->collectors = NULL; | |
287 | ofproto_sflow_options_destroy(ds->options); | |
288 | ds->options = NULL; | |
72b06300 | 289 | |
72b06300 | 290 | /* Turn off sampling to save CPU cycles. */ |
6ff686f2 | 291 | ds->probability = 0; |
72b06300 BP |
292 | } |
293 | ||
34ae6d76 EJ |
294 | void |
295 | dpif_sflow_clear(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) | |
296 | { | |
297 | ovs_mutex_lock(&mutex); | |
298 | dpif_sflow_clear__(ds); | |
299 | ovs_mutex_unlock(&mutex); | |
300 | } | |
301 | ||
72b06300 | 302 | bool |
34ae6d76 | 303 | dpif_sflow_is_enabled(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 304 | { |
34ae6d76 EJ |
305 | bool enabled; |
306 | ||
307 | ovs_mutex_lock(&mutex); | |
308 | enabled = ds->collectors != NULL; | |
309 | ovs_mutex_unlock(&mutex); | |
310 | return enabled; | |
72b06300 BP |
311 | } |
312 | ||
bae473fe | 313 | struct dpif_sflow * |
4213f19d | 314 | dpif_sflow_create(void) |
72b06300 | 315 | { |
34ae6d76 | 316 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
bae473fe | 317 | struct dpif_sflow *ds; |
72b06300 | 318 | |
34ae6d76 EJ |
319 | if (ovsthread_once_start(&once)) { |
320 | ovs_mutex_init(&mutex, PTHREAD_MUTEX_RECURSIVE); | |
321 | ovsthread_once_done(&once); | |
322 | } | |
323 | ||
bae473fe | 324 | ds = xcalloc(1, sizeof *ds); |
bae473fe JP |
325 | ds->next_tick = time_now() + 1; |
326 | hmap_init(&ds->ports); | |
6ff686f2 | 327 | ds->probability = 0; |
733adf2a | 328 | route_table_register(); |
34ae6d76 | 329 | atomic_init(&ds->ref_cnt, 1); |
733adf2a | 330 | |
bae473fe | 331 | return ds; |
72b06300 BP |
332 | } |
333 | ||
9723bcce EJ |
334 | struct dpif_sflow * |
335 | dpif_sflow_ref(const struct dpif_sflow *ds_) | |
336 | { | |
337 | struct dpif_sflow *ds = CONST_CAST(struct dpif_sflow *, ds_); | |
338 | if (ds) { | |
34ae6d76 EJ |
339 | int orig; |
340 | atomic_add(&ds->ref_cnt, 1, &orig); | |
341 | ovs_assert(orig > 0); | |
9723bcce EJ |
342 | } |
343 | return ds; | |
344 | } | |
345 | ||
6ff686f2 PS |
346 | /* 32-bit fraction of packets to sample with. A value of 0 samples no packets, |
347 | * a value of %UINT32_MAX samples all packets and intermediate values sample | |
348 | * intermediate fractions of packets. */ | |
349 | uint32_t | |
34ae6d76 | 350 | dpif_sflow_get_probability(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
6ff686f2 | 351 | { |
34ae6d76 EJ |
352 | uint32_t probability; |
353 | ovs_mutex_lock(&mutex); | |
354 | probability = ds->probability; | |
355 | ovs_mutex_unlock(&mutex); | |
356 | return probability; | |
6ff686f2 PS |
357 | } |
358 | ||
72b06300 | 359 | void |
34ae6d76 | 360 | dpif_sflow_unref(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 361 | { |
34ae6d76 EJ |
362 | int orig; |
363 | ||
9723bcce EJ |
364 | if (!ds) { |
365 | return; | |
366 | } | |
367 | ||
34ae6d76 EJ |
368 | atomic_sub(&ds->ref_cnt, 1, &orig); |
369 | ovs_assert(orig > 0); | |
370 | if (orig == 1) { | |
bae473fe | 371 | struct dpif_sflow_port *dsp, *next; |
32d9dc11 | 372 | |
733adf2a | 373 | route_table_unregister(); |
bae473fe JP |
374 | dpif_sflow_clear(ds); |
375 | HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) { | |
376 | dpif_sflow_del_port__(ds, dsp); | |
32d9dc11 | 377 | } |
bae473fe JP |
378 | hmap_destroy(&ds->ports); |
379 | free(ds); | |
72b06300 BP |
380 | } |
381 | } | |
382 | ||
383 | static void | |
392c7182 | 384 | dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp) |
34ae6d76 | 385 | OVS_REQ_WRLOCK(mutex) |
72b06300 | 386 | { |
bae473fe | 387 | SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds, |
72b06300 | 388 | sflow_agent_get_counters); |
bae473fe | 389 | sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval); |
72b06300 | 390 | sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX); |
4e022ec0 | 391 | sfl_poller_set_bridgePort(poller, odp_to_u32(dsp->odp_port)); |
72b06300 BP |
392 | } |
393 | ||
394 | void | |
e1b1d06a | 395 | dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport, |
34ae6d76 | 396 | odp_port_t odp_port) OVS_EXCLUDED(mutex) |
72b06300 | 397 | { |
bae473fe | 398 | struct dpif_sflow_port *dsp; |
743cea45 | 399 | int ifindex; |
72b06300 | 400 | |
34ae6d76 | 401 | ovs_mutex_lock(&mutex); |
bae473fe | 402 | dpif_sflow_del_port(ds, odp_port); |
72b06300 | 403 | |
392c7182 | 404 | ifindex = netdev_get_ifindex(ofport->netdev); |
743cea45 | 405 | |
72b06300 | 406 | if (ifindex <= 0) { |
743cea45 | 407 | /* Not an ifindex port, so do not add a cross-reference to it here */ |
34ae6d76 | 408 | goto out; |
72b06300 | 409 | } |
743cea45 NM |
410 | |
411 | /* Add to table of ports. */ | |
412 | dsp = xmalloc(sizeof *dsp); | |
392c7182 | 413 | dsp->ofport = ofport; |
e1b1d06a | 414 | dsp->odp_port = odp_port; |
743cea45 | 415 | SFL_DS_SET(dsp->dsi, SFL_DSCLASS_IFINDEX, ifindex, 0); |
f9c0c3ec | 416 | hmap_insert(&ds->ports, &dsp->hmap_node, hash_odp_port(odp_port)); |
72b06300 | 417 | |
743cea45 | 418 | /* Add poller. */ |
bae473fe | 419 | if (ds->sflow_agent) { |
392c7182 | 420 | dpif_sflow_add_poller(ds, dsp); |
72b06300 | 421 | } |
34ae6d76 EJ |
422 | |
423 | out: | |
424 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
425 | } |
426 | ||
0cc96e48 | 427 | static void |
bae473fe | 428 | dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp) |
34ae6d76 | 429 | OVS_REQ_WRLOCK(mutex) |
0cc96e48 | 430 | { |
bae473fe JP |
431 | if (ds->sflow_agent) { |
432 | sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi); | |
433 | sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi); | |
0cc96e48 | 434 | } |
bae473fe JP |
435 | hmap_remove(&ds->ports, &dsp->hmap_node); |
436 | free(dsp); | |
0cc96e48 BP |
437 | } |
438 | ||
72b06300 | 439 | void |
4e022ec0 | 440 | dpif_sflow_del_port(struct dpif_sflow *ds, odp_port_t odp_port) |
34ae6d76 | 441 | OVS_EXCLUDED(mutex) |
72b06300 | 442 | { |
34ae6d76 EJ |
443 | struct dpif_sflow_port *dsp; |
444 | ||
445 | ovs_mutex_lock(&mutex); | |
446 | dsp = dpif_sflow_find_port(ds, odp_port); | |
bae473fe JP |
447 | if (dsp) { |
448 | dpif_sflow_del_port__(ds, dsp); | |
72b06300 | 449 | } |
34ae6d76 | 450 | ovs_mutex_unlock(&mutex); |
72b06300 BP |
451 | } |
452 | ||
453 | void | |
bae473fe JP |
454 | dpif_sflow_set_options(struct dpif_sflow *ds, |
455 | const struct ofproto_sflow_options *options) | |
34ae6d76 | 456 | OVS_EXCLUDED(mutex) |
72b06300 | 457 | { |
bae473fe | 458 | struct dpif_sflow_port *dsp; |
72b06300 | 459 | bool options_changed; |
72b06300 | 460 | SFLReceiver *receiver; |
72b06300 BP |
461 | SFLAddress agentIP; |
462 | time_t now; | |
743cea45 NM |
463 | SFLDataSource_instance dsi; |
464 | uint32_t dsIndex; | |
465 | SFLSampler *sampler; | |
72b06300 | 466 | |
34ae6d76 | 467 | ovs_mutex_lock(&mutex); |
81e2083f | 468 | if (sset_is_empty(&options->targets) || !options->sampling_rate) { |
a68813c3 BP |
469 | /* No point in doing any work if there are no targets or nothing to |
470 | * sample. */ | |
34ae6d76 EJ |
471 | dpif_sflow_clear__(ds); |
472 | goto out; | |
a68813c3 BP |
473 | } |
474 | ||
bae473fe JP |
475 | options_changed = (!ds->options |
476 | || !ofproto_sflow_options_equal(options, ds->options)); | |
72b06300 BP |
477 | |
478 | /* Configure collectors if options have changed or if we're shortchanged in | |
479 | * collectors (which indicates that opening one or more of the configured | |
480 | * collectors failed, so that we should retry). */ | |
481 | if (options_changed | |
bae473fe JP |
482 | || collectors_count(ds->collectors) < sset_count(&options->targets)) { |
483 | collectors_destroy(ds->collectors); | |
02ef592c | 484 | collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT, |
bae473fe JP |
485 | &ds->collectors); |
486 | if (ds->collectors == NULL) { | |
a68813c3 BP |
487 | VLOG_WARN_RL(&rl, "no collectors could be initialized, " |
488 | "sFlow disabled"); | |
34ae6d76 EJ |
489 | dpif_sflow_clear__(ds); |
490 | goto out; | |
72b06300 BP |
491 | } |
492 | } | |
493 | ||
733adf2a LG |
494 | /* Choose agent IP address and agent device (if not yet setup) */ |
495 | if (!sflow_choose_agent_address(options->agent_device, | |
496 | &options->targets, | |
497 | options->control_ip, &agentIP)) { | |
34ae6d76 EJ |
498 | dpif_sflow_clear__(ds); |
499 | goto out; | |
733adf2a LG |
500 | } |
501 | ||
72b06300 BP |
502 | /* Avoid reconfiguring if options didn't change. */ |
503 | if (!options_changed) { | |
34ae6d76 | 504 | goto out; |
72b06300 | 505 | } |
bae473fe JP |
506 | ofproto_sflow_options_destroy(ds->options); |
507 | ds->options = ofproto_sflow_options_clone(options); | |
72b06300 | 508 | |
72b06300 BP |
509 | /* Create agent. */ |
510 | VLOG_INFO("creating sFlow agent %d", options->sub_id); | |
bae473fe JP |
511 | if (ds->sflow_agent) { |
512 | sfl_agent_release(ds->sflow_agent); | |
72b06300 | 513 | } |
bae473fe | 514 | ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent); |
c73814a3 | 515 | now = time_wall(); |
bae473fe | 516 | sfl_agent_init(ds->sflow_agent, |
72b06300 BP |
517 | &agentIP, |
518 | options->sub_id, | |
519 | now, /* Boot time. */ | |
520 | now, /* Current time. */ | |
bae473fe | 521 | ds, /* Pointer supplied to callbacks. */ |
72b06300 BP |
522 | sflow_agent_alloc_cb, |
523 | sflow_agent_free_cb, | |
524 | sflow_agent_error_cb, | |
525 | sflow_agent_send_packet_cb); | |
526 | ||
bae473fe | 527 | receiver = sfl_agent_addReceiver(ds->sflow_agent); |
e53df206 | 528 | sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow"); |
72b06300 BP |
529 | sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff); |
530 | ||
72b06300 | 531 | /* Set the sampling_rate down in the datapath. */ |
6ff686f2 | 532 | ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate); |
72b06300 | 533 | |
743cea45 NM |
534 | /* Add a single sampler for the bridge. This appears as a PHYSICAL_ENTITY |
535 | because it is associated with the hypervisor, and interacts with the server | |
536 | hardware directly. The sub_id is used to distinguish this sampler from | |
537 | others on other bridges within the same agent. */ | |
538 | dsIndex = 1000 + options->sub_id; | |
539 | SFL_DS_SET(dsi, SFL_DSCLASS_PHYSICAL_ENTITY, dsIndex, 0); | |
540 | sampler = sfl_agent_addSampler(ds->sflow_agent, &dsi); | |
541 | sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate); | |
542 | sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len); | |
543 | sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX); | |
544 | ||
545 | /* Add pollers for the currently known ifindex-ports */ | |
bae473fe | 546 | HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) { |
392c7182 | 547 | dpif_sflow_add_poller(ds, dsp); |
72b06300 | 548 | } |
34ae6d76 EJ |
549 | |
550 | ||
551 | out: | |
552 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
553 | } |
554 | ||
6ff686f2 | 555 | int |
bae473fe | 556 | dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds, |
34ae6d76 | 557 | odp_port_t odp_port) OVS_EXCLUDED(mutex) |
c1e98da1 | 558 | { |
34ae6d76 EJ |
559 | struct dpif_sflow_port *dsp; |
560 | int ret; | |
561 | ||
562 | ovs_mutex_lock(&mutex); | |
563 | dsp = dpif_sflow_find_port(ds, odp_port); | |
564 | ret = dsp ? SFL_DS_INDEX(dsp->dsi) : 0; | |
565 | ovs_mutex_unlock(&mutex); | |
566 | return ret; | |
c1e98da1 BP |
567 | } |
568 | ||
72b06300 | 569 | void |
6ff686f2 | 570 | dpif_sflow_received(struct dpif_sflow *ds, struct ofpbuf *packet, |
4e022ec0 | 571 | const struct flow *flow, odp_port_t odp_in_port, |
1673e0e4 | 572 | const union user_action_cookie *cookie) |
34ae6d76 | 573 | OVS_EXCLUDED(mutex) |
72b06300 BP |
574 | { |
575 | SFL_FLOW_SAMPLE_TYPE fs; | |
576 | SFLFlow_sample_element hdrElem; | |
577 | SFLSampled_header *header; | |
578 | SFLFlow_sample_element switchElem; | |
56fd8edf | 579 | SFLSampler *sampler; |
6ff686f2 | 580 | struct dpif_sflow_port *in_dsp; |
1673e0e4 | 581 | ovs_be16 vlan_tci; |
72b06300 | 582 | |
34ae6d76 | 583 | ovs_mutex_lock(&mutex); |
743cea45 NM |
584 | sampler = ds->sflow_agent->samplers; |
585 | if (!sampler) { | |
34ae6d76 | 586 | goto out; |
6ff686f2 | 587 | } |
6ff686f2 | 588 | |
743cea45 NM |
589 | /* Build a flow sample. */ |
590 | memset(&fs, 0, sizeof fs); | |
72b06300 | 591 | |
743cea45 NM |
592 | /* Look up the input ifIndex if this port has one. Otherwise just |
593 | * leave it as 0 (meaning 'unknown') and continue. */ | |
594 | in_dsp = dpif_sflow_find_port(ds, odp_in_port); | |
595 | if (in_dsp) { | |
596 | fs.input = SFL_DS_INDEX(in_dsp->dsi); | |
56fd8edf BP |
597 | } |
598 | ||
743cea45 NM |
599 | /* Make the assumption that the random number generator in the datapath converges |
600 | * to the configured mean, and just increment the samplePool by the configured | |
601 | * sampling rate every time. */ | |
602 | sampler->samplePool += sfl_sampler_get_sFlowFsPacketSamplingRate(sampler); | |
603 | ||
72b06300 BP |
604 | /* Sampled header. */ |
605 | memset(&hdrElem, 0, sizeof hdrElem); | |
606 | hdrElem.tag = SFLFLOW_HEADER; | |
607 | header = &hdrElem.flowType.header; | |
608 | header->header_protocol = SFLHEADER_ETHERNET_ISO8023; | |
c62caaa3 | 609 | /* The frame_length should include the Ethernet FCS (4 bytes), |
743cea45 | 610 | * but it has already been stripped, so we need to add 4 here. */ |
6ff686f2 | 611 | header->frame_length = packet->size + 4; |
c62caaa3 NM |
612 | /* Ethernet FCS stripped off. */ |
613 | header->stripped = 4; | |
6ff686f2 | 614 | header->header_length = MIN(packet->size, |
856081f6 | 615 | sampler->sFlowFsMaximumHeaderSize); |
6ff686f2 | 616 | header->header_bytes = packet->data; |
72b06300 BP |
617 | |
618 | /* Add extended switch element. */ | |
619 | memset(&switchElem, 0, sizeof(switchElem)); | |
620 | switchElem.tag = SFLFLOW_EX_SWITCH; | |
856081f6 BP |
621 | switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlan_tci); |
622 | switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlan_tci); | |
6ff686f2 PS |
623 | |
624 | /* Retrieve data from user_action_cookie. */ | |
1673e0e4 BP |
625 | vlan_tci = cookie->sflow.vlan_tci; |
626 | switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci); | |
627 | switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci); | |
46b47a41 | 628 | |
1673e0e4 | 629 | fs.output = cookie->sflow.output; |
72b06300 BP |
630 | |
631 | /* Submit the flow sample to be encoded into the next datagram. */ | |
632 | SFLADD_ELEMENT(&fs, &hdrElem); | |
633 | SFLADD_ELEMENT(&fs, &switchElem); | |
634 | sfl_sampler_writeFlowSample(sampler, &fs); | |
34ae6d76 EJ |
635 | |
636 | out: | |
637 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
638 | } |
639 | ||
72b06300 | 640 | void |
34ae6d76 | 641 | dpif_sflow_run(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 642 | { |
34ae6d76 EJ |
643 | ovs_mutex_lock(&mutex); |
644 | if (ds->collectors != NULL) { | |
72b06300 | 645 | time_t now = time_now(); |
733adf2a | 646 | route_table_run(); |
bae473fe JP |
647 | if (now >= ds->next_tick) { |
648 | sfl_agent_tick(ds->sflow_agent, time_wall()); | |
649 | ds->next_tick = now + 1; | |
72b06300 BP |
650 | } |
651 | } | |
34ae6d76 | 652 | ovs_mutex_unlock(&mutex); |
72b06300 BP |
653 | } |
654 | ||
655 | void | |
34ae6d76 | 656 | dpif_sflow_wait(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 657 | { |
34ae6d76 EJ |
658 | ovs_mutex_lock(&mutex); |
659 | if (ds->collectors != NULL) { | |
bae473fe | 660 | poll_timer_wait_until(ds->next_tick * 1000LL); |
72b06300 | 661 | } |
34ae6d76 | 662 | ovs_mutex_unlock(&mutex); |
72b06300 | 663 | } |