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