]>
Commit | Line | Data |
---|---|---|
72b06300 | 1 | /* |
2225c0b9 | 2 | * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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> |
3d2912f2 | 21 | #include <sys/resource.h> |
f6eb6b20 | 22 | #include <sys/socket.h> |
733adf2a | 23 | #include <net/if.h> |
72b06300 BP |
24 | #include <stdlib.h> |
25 | #include "collectors.h" | |
72b06300 | 26 | #include "compiler.h" |
bae473fe | 27 | #include "dpif.h" |
0cc96e48 | 28 | #include "hash.h" |
ee89ea7b | 29 | #include "openvswitch/hmap.h" |
72b06300 | 30 | #include "netdev.h" |
cdee00fd | 31 | #include "netlink.h" |
64c96779 | 32 | #include "openvswitch/ofpbuf.h" |
72b06300 | 33 | #include "ofproto.h" |
26233bb4 | 34 | #include "packets.h" |
72b06300 | 35 | #include "poll-loop.h" |
d9b4ebc5 | 36 | #include "ovs-router.h" |
733adf2a | 37 | #include "route-table.h" |
72b06300 BP |
38 | #include "sflow_api.h" |
39 | #include "socket-util.h" | |
40 | #include "timeval.h" | |
e6211adc | 41 | #include "openvswitch/vlog.h" |
975a704c | 42 | #include "lib/odp-util.h" |
7321bda3 | 43 | #include "lib/unaligned.h" |
392c7182 | 44 | #include "ofproto-provider.h" |
50b9699f | 45 | #include "lacp.h" |
72b06300 | 46 | |
d98e6007 | 47 | VLOG_DEFINE_THIS_MODULE(sflow); |
5136ce49 | 48 | |
34ae6d76 EJ |
49 | static struct ovs_mutex mutex; |
50 | ||
3d2912f2 NM |
51 | /* This global var is used to determine which sFlow |
52 | sub-agent should send the datapath counters. */ | |
53 | #define SFLOW_GC_SUBID_UNCLAIMED (uint32_t)-1 | |
54 | static uint32_t sflow_global_counters_subid = SFLOW_GC_SUBID_UNCLAIMED; | |
55 | ||
7321bda3 NM |
56 | /* |
57 | * The enum dpif_sflow_tunnel_type is to declare the types supported | |
58 | */ | |
59 | enum dpif_sflow_tunnel_type { | |
60 | DPIF_SFLOW_TUNNEL_UNKNOWN = 0, | |
61 | DPIF_SFLOW_TUNNEL_VXLAN, | |
62 | DPIF_SFLOW_TUNNEL_GRE, | |
7321bda3 | 63 | DPIF_SFLOW_TUNNEL_LISP, |
7321bda3 NM |
64 | DPIF_SFLOW_TUNNEL_GENEVE |
65 | }; | |
66 | ||
bae473fe JP |
67 | struct dpif_sflow_port { |
68 | struct hmap_node hmap_node; /* In struct dpif_sflow's "ports" hmap. */ | |
72b06300 | 69 | SFLDataSource_instance dsi; /* sFlow library's notion of port number. */ |
392c7182 | 70 | struct ofport *ofport; /* To retrive port stats. */ |
4e022ec0 | 71 | odp_port_t odp_port; |
7321bda3 | 72 | enum dpif_sflow_tunnel_type tunnel_type; |
72b06300 BP |
73 | }; |
74 | ||
bae473fe | 75 | struct dpif_sflow { |
72b06300 BP |
76 | struct collectors *collectors; |
77 | SFLAgent *sflow_agent; | |
78 | struct ofproto_sflow_options *options; | |
72b06300 BP |
79 | time_t next_tick; |
80 | size_t n_flood, n_all; | |
bae473fe | 81 | struct hmap ports; /* Contains "struct dpif_sflow_port"s. */ |
6ff686f2 | 82 | uint32_t probability; |
37bec3d3 | 83 | struct ovs_refcount ref_cnt; |
72b06300 BP |
84 | }; |
85 | ||
bae473fe JP |
86 | static void dpif_sflow_del_port__(struct dpif_sflow *, |
87 | struct dpif_sflow_port *); | |
0cc96e48 | 88 | |
72b06300 BP |
89 | #define RECEIVER_INDEX 1 |
90 | ||
91 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
92 | ||
93 | static bool | |
94 | ofproto_sflow_options_equal(const struct ofproto_sflow_options *a, | |
bae473fe | 95 | const struct ofproto_sflow_options *b) |
72b06300 | 96 | { |
81e2083f | 97 | return (sset_equals(&a->targets, &b->targets) |
72b06300 BP |
98 | && a->sampling_rate == b->sampling_rate |
99 | && a->polling_interval == b->polling_interval | |
100 | && a->header_len == b->header_len | |
101 | && a->sub_id == b->sub_id | |
fa49ca80 BP |
102 | && nullable_string_is_equal(a->agent_device, b->agent_device) |
103 | && nullable_string_is_equal(a->control_ip, b->control_ip)); | |
72b06300 BP |
104 | } |
105 | ||
106 | static struct ofproto_sflow_options * | |
107 | ofproto_sflow_options_clone(const struct ofproto_sflow_options *old) | |
108 | { | |
109 | struct ofproto_sflow_options *new = xmemdup(old, sizeof *old); | |
81e2083f | 110 | sset_clone(&new->targets, &old->targets); |
2225c0b9 BP |
111 | new->agent_device = nullable_xstrdup(old->agent_device); |
112 | new->control_ip = nullable_xstrdup(old->control_ip); | |
72b06300 BP |
113 | return new; |
114 | } | |
115 | ||
116 | static void | |
117 | ofproto_sflow_options_destroy(struct ofproto_sflow_options *options) | |
118 | { | |
119 | if (options) { | |
81e2083f | 120 | sset_destroy(&options->targets); |
72b06300 BP |
121 | free(options->agent_device); |
122 | free(options->control_ip); | |
123 | free(options); | |
124 | } | |
125 | } | |
126 | ||
127 | /* sFlow library callback to allocate memory. */ | |
128 | static void * | |
67a4917b BP |
129 | sflow_agent_alloc_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
130 | size_t bytes) | |
72b06300 | 131 | { |
37551f8b | 132 | return xzalloc(bytes); |
72b06300 BP |
133 | } |
134 | ||
135 | /* sFlow library callback to free memory. */ | |
136 | static int | |
67a4917b BP |
137 | sflow_agent_free_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
138 | void *obj) | |
72b06300 BP |
139 | { |
140 | free(obj); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* sFlow library callback to report error. */ | |
145 | static void | |
67a4917b BP |
146 | sflow_agent_error_cb(void *magic OVS_UNUSED, SFLAgent *agent OVS_UNUSED, |
147 | char *msg) | |
72b06300 BP |
148 | { |
149 | VLOG_WARN("sFlow agent error: %s", msg); | |
150 | } | |
151 | ||
152 | /* sFlow library callback to send datagram. */ | |
153 | static void | |
bae473fe | 154 | sflow_agent_send_packet_cb(void *ds_, SFLAgent *agent OVS_UNUSED, |
67a4917b | 155 | SFLReceiver *receiver OVS_UNUSED, u_char *pkt, |
72b06300 BP |
156 | uint32_t pktLen) |
157 | { | |
bae473fe JP |
158 | struct dpif_sflow *ds = ds_; |
159 | collectors_send(ds->collectors, pkt, pktLen); | |
72b06300 BP |
160 | } |
161 | ||
bae473fe | 162 | static struct dpif_sflow_port * |
4e022ec0 | 163 | dpif_sflow_find_port(const struct dpif_sflow *ds, odp_port_t odp_port) |
344e21d4 | 164 | OVS_REQUIRES(mutex) |
0cc96e48 | 165 | { |
bae473fe | 166 | struct dpif_sflow_port *dsp; |
0cc96e48 | 167 | |
f9c0c3ec EJ |
168 | HMAP_FOR_EACH_IN_BUCKET (dsp, hmap_node, hash_odp_port(odp_port), |
169 | &ds->ports) { | |
e1b1d06a | 170 | if (dsp->odp_port == odp_port) { |
bae473fe | 171 | return dsp; |
0cc96e48 BP |
172 | } |
173 | } | |
174 | return NULL; | |
175 | } | |
176 | ||
3d2912f2 NM |
177 | /* Call to get the datapath stats. Modeled after the dpctl utility. |
178 | * | |
179 | * It might be more efficient for this module to be given a handle it can use | |
180 | * to get these stats more efficiently, but this is only going to be called | |
181 | * once every 20-30 seconds. Return number of datapaths found (normally expect | |
182 | * 1). */ | |
183 | static int | |
184 | sflow_get_dp_stats(struct dpif_sflow *ds OVS_UNUSED, | |
185 | struct dpif_dp_stats *dp_totals) | |
186 | { | |
187 | struct sset types; | |
188 | const char *type; | |
189 | int count = 0; | |
190 | ||
191 | memset(dp_totals, 0, sizeof *dp_totals); | |
192 | sset_init(&types); | |
193 | dp_enumerate_types(&types); | |
194 | SSET_FOR_EACH (type, &types) { | |
195 | struct sset names; | |
196 | const char *name; | |
197 | sset_init(&names); | |
198 | if (dp_enumerate_names(type, &names) == 0) { | |
199 | SSET_FOR_EACH (name, &names) { | |
200 | struct dpif *dpif; | |
201 | if (dpif_open(name, type, &dpif) == 0) { | |
202 | struct dpif_dp_stats dp_stats; | |
203 | if (dpif_get_dp_stats(dpif, &dp_stats) == 0) { | |
204 | count++; | |
205 | dp_totals->n_hit += dp_stats.n_hit; | |
206 | dp_totals->n_missed += dp_stats.n_missed; | |
207 | dp_totals->n_lost += dp_stats.n_lost; | |
208 | dp_totals->n_flows += dp_stats.n_flows; | |
209 | dp_totals->n_mask_hit += dp_stats.n_mask_hit; | |
210 | dp_totals->n_masks += dp_stats.n_masks; | |
211 | } | |
212 | dpif_close(dpif); | |
213 | } | |
214 | } | |
215 | sset_destroy(&names); | |
216 | } | |
217 | } | |
218 | sset_destroy(&types); | |
219 | return count; | |
220 | } | |
221 | ||
222 | /* If there are multiple bridges defined then we need some | |
223 | minimal artibration to decide which one should send the | |
224 | global counters. This function allows each sub-agent to | |
225 | ask if he should do it or not. */ | |
226 | static bool | |
227 | sflow_global_counters_subid_test(uint32_t subid) | |
228 | OVS_REQUIRES(mutex) | |
229 | { | |
230 | if (sflow_global_counters_subid == SFLOW_GC_SUBID_UNCLAIMED) { | |
231 | /* The role is up for grabs. */ | |
232 | sflow_global_counters_subid = subid; | |
233 | } | |
234 | return (sflow_global_counters_subid == subid); | |
235 | } | |
236 | ||
237 | static void | |
238 | sflow_global_counters_subid_clear(uint32_t subid) | |
239 | OVS_REQUIRES(mutex) | |
240 | { | |
241 | if (sflow_global_counters_subid == subid) { | |
242 | /* The sub-agent that was sending global counters | |
243 | is going away, so reset to allow another | |
244 | to take over. */ | |
245 | sflow_global_counters_subid = SFLOW_GC_SUBID_UNCLAIMED; | |
246 | } | |
247 | } | |
248 | ||
249 | static void | |
250 | sflow_agent_get_global_counters(void *ds_, SFLPoller *poller, | |
251 | SFL_COUNTERS_SAMPLE_TYPE *cs) | |
252 | OVS_REQUIRES(mutex) | |
253 | { | |
254 | struct dpif_sflow *ds = ds_; | |
255 | SFLCounters_sample_element dp_elem, res_elem; | |
256 | struct dpif_dp_stats dp_totals; | |
257 | struct rusage usage; | |
258 | ||
259 | if (!sflow_global_counters_subid_test(poller->agent->subId)) { | |
260 | /* Another sub-agent is currently responsible for this. */ | |
261 | return; | |
262 | } | |
263 | ||
264 | /* datapath stats */ | |
265 | if (sflow_get_dp_stats(ds, &dp_totals)) { | |
266 | dp_elem.tag = SFLCOUNTERS_OVSDP; | |
267 | dp_elem.counterBlock.ovsdp.n_hit = dp_totals.n_hit; | |
268 | dp_elem.counterBlock.ovsdp.n_missed = dp_totals.n_missed; | |
269 | dp_elem.counterBlock.ovsdp.n_lost = dp_totals.n_lost; | |
270 | dp_elem.counterBlock.ovsdp.n_mask_hit = dp_totals.n_mask_hit; | |
271 | dp_elem.counterBlock.ovsdp.n_flows = dp_totals.n_flows; | |
272 | dp_elem.counterBlock.ovsdp.n_masks = dp_totals.n_masks; | |
273 | SFLADD_ELEMENT(cs, &dp_elem); | |
274 | } | |
275 | ||
276 | /* resource usage */ | |
277 | getrusage(RUSAGE_SELF, &usage); | |
278 | res_elem.tag = SFLCOUNTERS_APP_RESOURCES; | |
279 | res_elem.counterBlock.appResources.user_time | |
280 | = timeval_to_msec(&usage.ru_utime); | |
281 | res_elem.counterBlock.appResources.system_time | |
282 | = timeval_to_msec(&usage.ru_stime); | |
283 | res_elem.counterBlock.appResources.mem_used = (usage.ru_maxrss * 1024); | |
284 | SFL_UNDEF_GAUGE(res_elem.counterBlock.appResources.mem_max); | |
285 | SFL_UNDEF_GAUGE(res_elem.counterBlock.appResources.fd_open); | |
286 | SFL_UNDEF_GAUGE(res_elem.counterBlock.appResources.fd_max); | |
287 | SFL_UNDEF_GAUGE(res_elem.counterBlock.appResources.conn_open); | |
288 | SFL_UNDEF_GAUGE(res_elem.counterBlock.appResources.conn_max); | |
289 | ||
290 | SFLADD_ELEMENT(cs, &res_elem); | |
291 | sfl_poller_writeCountersSample(poller, cs); | |
292 | } | |
293 | ||
72b06300 | 294 | static void |
bae473fe | 295 | sflow_agent_get_counters(void *ds_, SFLPoller *poller, |
72b06300 | 296 | SFL_COUNTERS_SAMPLE_TYPE *cs) |
344e21d4 | 297 | OVS_REQUIRES(mutex) |
72b06300 | 298 | { |
bae473fe | 299 | struct dpif_sflow *ds = ds_; |
50b9699f | 300 | SFLCounters_sample_element elem, lacp_elem, of_elem, name_elem; |
934386dd | 301 | SFLCounters_sample_element eth_elem; |
6c038611 | 302 | enum netdev_features current; |
bae473fe | 303 | struct dpif_sflow_port *dsp; |
72b06300 | 304 | SFLIf_counters *counters; |
934386dd | 305 | SFLEthernet_counters* eth_counters; |
72b06300 BP |
306 | struct netdev_stats stats; |
307 | enum netdev_flags flags; | |
50b9699f NM |
308 | struct lacp_slave_stats lacp_stats; |
309 | const char *ifName; | |
72b06300 | 310 | |
4e022ec0 | 311 | dsp = dpif_sflow_find_port(ds, u32_to_odp(poller->bridgePort)); |
bae473fe | 312 | if (!dsp) { |
72b06300 BP |
313 | return; |
314 | } | |
315 | ||
316 | elem.tag = SFLCOUNTERS_GENERIC; | |
317 | counters = &elem.counterBlock.generic; | |
318 | counters->ifIndex = SFL_DS_INDEX(poller->dsi); | |
319 | counters->ifType = 6; | |
392c7182 | 320 | if (!netdev_get_features(dsp->ofport->netdev, ¤t, NULL, NULL, NULL)) { |
bae473fe JP |
321 | /* The values of ifDirection come from MAU MIB (RFC 2668): 0 = unknown, |
322 | 1 = full-duplex, 2 = half-duplex, 3 = in, 4=out */ | |
d02a5f8e | 323 | counters->ifSpeed = netdev_features_to_bps(current, 0); |
72b06300 BP |
324 | counters->ifDirection = (netdev_features_is_full_duplex(current) |
325 | ? 1 : 2); | |
326 | } else { | |
327 | counters->ifSpeed = 100000000; | |
b6dab095 | 328 | counters->ifDirection = 0; |
72b06300 | 329 | } |
392c7182 | 330 | if (!netdev_get_flags(dsp->ofport->netdev, &flags) && flags & NETDEV_UP) { |
72b06300 | 331 | counters->ifStatus = 1; /* ifAdminStatus up. */ |
392c7182 | 332 | if (netdev_get_carrier(dsp->ofport->netdev)) { |
72b06300 BP |
333 | counters->ifStatus |= 2; /* ifOperStatus us. */ |
334 | } | |
335 | } else { | |
336 | counters->ifStatus = 0; /* Down. */ | |
337 | } | |
338 | ||
339 | /* XXX | |
340 | 1. Is the multicast counter filled in? | |
341 | 2. Does the multicast counter include broadcasts? | |
342 | 3. Does the rx_packets counter include multicasts/broadcasts? | |
343 | */ | |
392c7182 | 344 | ofproto_port_get_stats(dsp->ofport, &stats); |
72b06300 BP |
345 | counters->ifInOctets = stats.rx_bytes; |
346 | counters->ifInUcastPkts = stats.rx_packets; | |
347 | counters->ifInMulticastPkts = stats.multicast; | |
934386dd | 348 | counters->ifInBroadcastPkts = stats.rx_broadcast_packets; |
72b06300 BP |
349 | counters->ifInDiscards = stats.rx_dropped; |
350 | counters->ifInErrors = stats.rx_errors; | |
351 | counters->ifInUnknownProtos = -1; | |
352 | counters->ifOutOctets = stats.tx_bytes; | |
353 | counters->ifOutUcastPkts = stats.tx_packets; | |
934386dd RW |
354 | counters->ifOutMulticastPkts = stats.tx_multicast_packets; |
355 | counters->ifOutBroadcastPkts = stats.tx_broadcast_packets; | |
72b06300 BP |
356 | counters->ifOutDiscards = stats.tx_dropped; |
357 | counters->ifOutErrors = stats.tx_errors; | |
358 | counters->ifPromiscuousMode = 0; | |
359 | ||
360 | SFLADD_ELEMENT(cs, &elem); | |
50b9699f NM |
361 | |
362 | /* Include LACP counters and identifiers if this port is part of a LAG. */ | |
363 | if (ofproto_port_get_lacp_stats(dsp->ofport, &lacp_stats) == 0) { | |
364 | memset(&lacp_elem, 0, sizeof lacp_elem); | |
365 | lacp_elem.tag = SFLCOUNTERS_LACP; | |
74ff3298 JR |
366 | lacp_elem.counterBlock.lacp.actorSystemID = |
367 | lacp_stats.dot3adAggPortActorSystemID; | |
368 | lacp_elem.counterBlock.lacp.partnerSystemID = | |
369 | lacp_stats.dot3adAggPortPartnerOperSystemID; | |
50b9699f NM |
370 | lacp_elem.counterBlock.lacp.attachedAggID = |
371 | lacp_stats.dot3adAggPortAttachedAggID; | |
372 | lacp_elem.counterBlock.lacp.portState.v.actorAdmin = | |
373 | lacp_stats.dot3adAggPortActorAdminState; | |
374 | lacp_elem.counterBlock.lacp.portState.v.actorOper = | |
375 | lacp_stats.dot3adAggPortActorOperState; | |
376 | lacp_elem.counterBlock.lacp.portState.v.partnerAdmin = | |
377 | lacp_stats.dot3adAggPortPartnerAdminState; | |
378 | lacp_elem.counterBlock.lacp.portState.v.partnerOper = | |
379 | lacp_stats.dot3adAggPortPartnerOperState; | |
380 | lacp_elem.counterBlock.lacp.LACPDUsRx = | |
381 | lacp_stats.dot3adAggPortStatsLACPDUsRx; | |
382 | SFL_UNDEF_COUNTER(lacp_elem.counterBlock.lacp.markerPDUsRx); | |
383 | SFL_UNDEF_COUNTER(lacp_elem.counterBlock.lacp.markerResponsePDUsRx); | |
384 | SFL_UNDEF_COUNTER(lacp_elem.counterBlock.lacp.unknownRx); | |
385 | lacp_elem.counterBlock.lacp.illegalRx = | |
386 | lacp_stats.dot3adAggPortStatsIllegalRx; | |
387 | lacp_elem.counterBlock.lacp.LACPDUsTx = | |
388 | lacp_stats.dot3adAggPortStatsLACPDUsTx; | |
389 | SFL_UNDEF_COUNTER(lacp_elem.counterBlock.lacp.markerPDUsTx); | |
390 | SFL_UNDEF_COUNTER(lacp_elem.counterBlock.lacp.markerResponsePDUsTx); | |
391 | SFLADD_ELEMENT(cs, &lacp_elem); | |
392 | } | |
393 | ||
394 | /* Include Port name. */ | |
395 | if ((ifName = netdev_get_name(dsp->ofport->netdev)) != NULL) { | |
396 | memset(&name_elem, 0, sizeof name_elem); | |
397 | name_elem.tag = SFLCOUNTERS_PORTNAME; | |
398 | name_elem.counterBlock.portName.portName.str = (char *)ifName; | |
399 | name_elem.counterBlock.portName.portName.len = strlen(ifName); | |
400 | SFLADD_ELEMENT(cs, &name_elem); | |
401 | } | |
402 | ||
403 | /* Include OpenFlow DPID and openflow port number. */ | |
404 | memset(&of_elem, 0, sizeof of_elem); | |
405 | of_elem.tag = SFLCOUNTERS_OPENFLOWPORT; | |
406 | of_elem.counterBlock.ofPort.datapath_id = | |
407 | ofproto_get_datapath_id(dsp->ofport->ofproto); | |
408 | of_elem.counterBlock.ofPort.port_no = | |
409 | (OVS_FORCE uint32_t)dsp->ofport->ofp_port; | |
410 | SFLADD_ELEMENT(cs, &of_elem); | |
411 | ||
934386dd RW |
412 | /* Include ethernet counters */ |
413 | memset(ð_elem, 0, sizeof eth_elem); | |
414 | eth_elem.tag = SFLCOUNTERS_ETHERNET; | |
415 | eth_counters = ð_elem.counterBlock.ethernet; | |
416 | eth_counters->dot3StatsAlignmentErrors = stats.rx_frame_errors; | |
417 | eth_counters->dot3StatsFCSErrors = stats.rx_crc_errors; | |
418 | eth_counters->dot3StatsFrameTooLongs = stats.rx_oversize_errors; | |
419 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsSingleCollisionFrames); | |
420 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsMultipleCollisionFrames); | |
421 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsSQETestErrors); | |
422 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsDeferredTransmissions); | |
423 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsLateCollisions); | |
424 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsExcessiveCollisions); | |
425 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsInternalMacTransmitErrors); | |
426 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsCarrierSenseErrors); | |
427 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsInternalMacReceiveErrors); | |
428 | SFL_UNDEF_COUNTER(eth_counters->dot3StatsSymbolErrors); | |
429 | SFLADD_ELEMENT(cs, ð_elem); | |
430 | ||
72b06300 BP |
431 | sfl_poller_writeCountersSample(poller, cs); |
432 | } | |
433 | ||
434 | /* Obtains an address to use for the local sFlow agent and stores it into | |
435 | * '*agent_addr'. Returns true if successful, false on failure. | |
436 | * | |
437 | * The sFlow agent address should be a local IP address that is persistent and | |
438 | * reachable over the network, if possible. The IP address associated with | |
439 | * 'agent_device' is used if it has one, and otherwise 'control_ip', the IP | |
733adf2a LG |
440 | * address used to talk to the controller. If the agent device is not |
441 | * specified then it is figured out by taking a look at the routing table based | |
442 | * on 'targets'. */ | |
72b06300 | 443 | static bool |
733adf2a LG |
444 | sflow_choose_agent_address(const char *agent_device, |
445 | const struct sset *targets, | |
446 | const char *control_ip, | |
72b06300 BP |
447 | SFLAddress *agent_addr) |
448 | { | |
733adf2a | 449 | const char *target; |
72b06300 BP |
450 | struct in_addr in4; |
451 | ||
452 | memset(agent_addr, 0, sizeof *agent_addr); | |
453 | agent_addr->type = SFLADDRESSTYPE_IP_V4; | |
454 | ||
455 | if (agent_device) { | |
733adf2a LG |
456 | if (!netdev_get_in4_by_name(agent_device, &in4)) { |
457 | goto success; | |
458 | } | |
459 | } | |
460 | ||
461 | SSET_FOR_EACH (target, targets) { | |
02334943 JR |
462 | union { |
463 | struct sockaddr_storage ss; | |
464 | struct sockaddr_in sin; | |
465 | } sa; | |
733adf2a LG |
466 | char name[IFNAMSIZ]; |
467 | ||
02334943 JR |
468 | if (inet_parse_active(target, SFL_DEFAULT_COLLECTOR_PORT, &sa.ss) |
469 | && sa.ss.ss_family == AF_INET) { | |
ec6c5379 | 470 | struct in6_addr addr6, src, gw; |
d9b4ebc5 | 471 | |
ec6c5379 | 472 | in6_addr_set_mapped_ipv4(&addr6, sa.sin.sin_addr.s_addr); |
ed52ca57 PS |
473 | /* sFlow only supports target in default routing table with |
474 | * packet mark zero. | |
475 | */ | |
476 | if (ovs_router_lookup(0, &addr6, name, &src, &gw)) { | |
ec6c5379 PS |
477 | |
478 | in4.s_addr = in6_addr_get_mapped_ipv4(&src); | |
e731d71b AS |
479 | goto success; |
480 | } | |
72b06300 BP |
481 | } |
482 | } | |
483 | ||
484 | if (control_ip && !lookup_ip(control_ip, &in4)) { | |
485 | goto success; | |
486 | } | |
487 | ||
488 | VLOG_ERR("could not determine IP address for sFlow agent"); | |
489 | return false; | |
490 | ||
491 | success: | |
6506f45c | 492 | agent_addr->address.ip_v4.addr = (OVS_FORCE uint32_t) in4.s_addr; |
72b06300 BP |
493 | return true; |
494 | } | |
495 | ||
34ae6d76 | 496 | static void |
bd3950dd | 497 | dpif_sflow_clear__(struct dpif_sflow *ds) OVS_REQUIRES(mutex) |
72b06300 | 498 | { |
bae473fe | 499 | if (ds->sflow_agent) { |
3d2912f2 | 500 | sflow_global_counters_subid_clear(ds->sflow_agent->subId); |
bae473fe | 501 | sfl_agent_release(ds->sflow_agent); |
0891637f | 502 | free(ds->sflow_agent); |
bae473fe | 503 | ds->sflow_agent = NULL; |
72b06300 | 504 | } |
bae473fe JP |
505 | collectors_destroy(ds->collectors); |
506 | ds->collectors = NULL; | |
507 | ofproto_sflow_options_destroy(ds->options); | |
508 | ds->options = NULL; | |
72b06300 | 509 | |
72b06300 | 510 | /* Turn off sampling to save CPU cycles. */ |
6ff686f2 | 511 | ds->probability = 0; |
72b06300 BP |
512 | } |
513 | ||
34ae6d76 EJ |
514 | void |
515 | dpif_sflow_clear(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) | |
516 | { | |
517 | ovs_mutex_lock(&mutex); | |
518 | dpif_sflow_clear__(ds); | |
519 | ovs_mutex_unlock(&mutex); | |
520 | } | |
521 | ||
72b06300 | 522 | bool |
34ae6d76 | 523 | dpif_sflow_is_enabled(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 524 | { |
34ae6d76 EJ |
525 | bool enabled; |
526 | ||
527 | ovs_mutex_lock(&mutex); | |
528 | enabled = ds->collectors != NULL; | |
529 | ovs_mutex_unlock(&mutex); | |
530 | return enabled; | |
72b06300 BP |
531 | } |
532 | ||
bae473fe | 533 | struct dpif_sflow * |
4213f19d | 534 | dpif_sflow_create(void) |
72b06300 | 535 | { |
34ae6d76 | 536 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
bae473fe | 537 | struct dpif_sflow *ds; |
72b06300 | 538 | |
34ae6d76 | 539 | if (ovsthread_once_start(&once)) { |
834d6caf | 540 | ovs_mutex_init_recursive(&mutex); |
34ae6d76 EJ |
541 | ovsthread_once_done(&once); |
542 | } | |
543 | ||
bae473fe | 544 | ds = xcalloc(1, sizeof *ds); |
bae473fe JP |
545 | ds->next_tick = time_now() + 1; |
546 | hmap_init(&ds->ports); | |
6ff686f2 | 547 | ds->probability = 0; |
37bec3d3 | 548 | ovs_refcount_init(&ds->ref_cnt); |
733adf2a | 549 | |
bae473fe | 550 | return ds; |
72b06300 BP |
551 | } |
552 | ||
9723bcce EJ |
553 | struct dpif_sflow * |
554 | dpif_sflow_ref(const struct dpif_sflow *ds_) | |
555 | { | |
556 | struct dpif_sflow *ds = CONST_CAST(struct dpif_sflow *, ds_); | |
557 | if (ds) { | |
37bec3d3 | 558 | ovs_refcount_ref(&ds->ref_cnt); |
9723bcce EJ |
559 | } |
560 | return ds; | |
561 | } | |
562 | ||
6ff686f2 PS |
563 | /* 32-bit fraction of packets to sample with. A value of 0 samples no packets, |
564 | * a value of %UINT32_MAX samples all packets and intermediate values sample | |
565 | * intermediate fractions of packets. */ | |
566 | uint32_t | |
34ae6d76 | 567 | dpif_sflow_get_probability(const struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
6ff686f2 | 568 | { |
34ae6d76 EJ |
569 | uint32_t probability; |
570 | ovs_mutex_lock(&mutex); | |
571 | probability = ds->probability; | |
572 | ovs_mutex_unlock(&mutex); | |
573 | return probability; | |
6ff686f2 PS |
574 | } |
575 | ||
72b06300 | 576 | void |
34ae6d76 | 577 | dpif_sflow_unref(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 578 | { |
24f83812 | 579 | if (ds && ovs_refcount_unref_relaxed(&ds->ref_cnt) == 1) { |
bae473fe | 580 | struct dpif_sflow_port *dsp, *next; |
32d9dc11 | 581 | |
bae473fe JP |
582 | dpif_sflow_clear(ds); |
583 | HMAP_FOR_EACH_SAFE (dsp, next, hmap_node, &ds->ports) { | |
584 | dpif_sflow_del_port__(ds, dsp); | |
32d9dc11 | 585 | } |
bae473fe JP |
586 | hmap_destroy(&ds->ports); |
587 | free(ds); | |
72b06300 BP |
588 | } |
589 | } | |
590 | ||
591 | static void | |
392c7182 | 592 | dpif_sflow_add_poller(struct dpif_sflow *ds, struct dpif_sflow_port *dsp) |
bd3950dd | 593 | OVS_REQUIRES(mutex) |
72b06300 | 594 | { |
bae473fe | 595 | SFLPoller *poller = sfl_agent_addPoller(ds->sflow_agent, &dsp->dsi, ds, |
72b06300 | 596 | sflow_agent_get_counters); |
bae473fe | 597 | sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval); |
72b06300 | 598 | sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX); |
4e022ec0 | 599 | sfl_poller_set_bridgePort(poller, odp_to_u32(dsp->odp_port)); |
72b06300 BP |
600 | } |
601 | ||
7321bda3 NM |
602 | static enum dpif_sflow_tunnel_type |
603 | dpif_sflow_tunnel_type(struct ofport *ofport) { | |
604 | const char *type = netdev_get_type(ofport->netdev); | |
605 | if (type) { | |
606 | if (strcmp(type, "gre") == 0) { | |
607 | return DPIF_SFLOW_TUNNEL_GRE; | |
7321bda3 NM |
608 | } else if (strcmp(type, "vxlan") == 0) { |
609 | return DPIF_SFLOW_TUNNEL_VXLAN; | |
610 | } else if (strcmp(type, "lisp") == 0) { | |
611 | return DPIF_SFLOW_TUNNEL_LISP; | |
612 | } else if (strcmp(type, "geneve") == 0) { | |
613 | return DPIF_SFLOW_TUNNEL_GENEVE; | |
614 | } | |
615 | } | |
616 | return DPIF_SFLOW_TUNNEL_UNKNOWN; | |
617 | } | |
618 | ||
619 | static uint8_t | |
620 | dpif_sflow_tunnel_proto(enum dpif_sflow_tunnel_type tunnel_type) | |
621 | { | |
622 | /* Default to 0 (IPPROTO_IP), meaning "unknown". */ | |
623 | uint8_t ipproto = 0; | |
624 | switch(tunnel_type) { | |
625 | ||
626 | case DPIF_SFLOW_TUNNEL_GRE: | |
7321bda3 NM |
627 | ipproto = IPPROTO_GRE; |
628 | break; | |
629 | ||
7321bda3 NM |
630 | case DPIF_SFLOW_TUNNEL_VXLAN: |
631 | case DPIF_SFLOW_TUNNEL_LISP: | |
632 | case DPIF_SFLOW_TUNNEL_GENEVE: | |
633 | ipproto = IPPROTO_UDP; | |
634 | ||
635 | case DPIF_SFLOW_TUNNEL_UNKNOWN: | |
636 | break; | |
637 | } | |
638 | return ipproto; | |
639 | } | |
640 | ||
72b06300 | 641 | void |
e1b1d06a | 642 | dpif_sflow_add_port(struct dpif_sflow *ds, struct ofport *ofport, |
34ae6d76 | 643 | odp_port_t odp_port) OVS_EXCLUDED(mutex) |
72b06300 | 644 | { |
bae473fe | 645 | struct dpif_sflow_port *dsp; |
743cea45 | 646 | int ifindex; |
7321bda3 | 647 | enum dpif_sflow_tunnel_type tunnel_type; |
72b06300 | 648 | |
34ae6d76 | 649 | ovs_mutex_lock(&mutex); |
bae473fe | 650 | dpif_sflow_del_port(ds, odp_port); |
72b06300 | 651 | |
7321bda3 | 652 | tunnel_type = dpif_sflow_tunnel_type(ofport); |
392c7182 | 653 | ifindex = netdev_get_ifindex(ofport->netdev); |
743cea45 | 654 | |
7321bda3 NM |
655 | if (ifindex <= 0 |
656 | && tunnel_type == DPIF_SFLOW_TUNNEL_UNKNOWN) { | |
657 | /* Not an ifindex port, and not a tunnel port either | |
658 | * so do not add a cross-reference to it here. | |
659 | */ | |
34ae6d76 | 660 | goto out; |
72b06300 | 661 | } |
743cea45 NM |
662 | |
663 | /* Add to table of ports. */ | |
664 | dsp = xmalloc(sizeof *dsp); | |
392c7182 | 665 | dsp->ofport = ofport; |
e1b1d06a | 666 | dsp->odp_port = odp_port; |
7321bda3 | 667 | dsp->tunnel_type = tunnel_type; |
f9c0c3ec | 668 | hmap_insert(&ds->ports, &dsp->hmap_node, hash_odp_port(odp_port)); |
72b06300 | 669 | |
7321bda3 NM |
670 | if (ifindex > 0) { |
671 | /* Add poller for ports that have ifindex. */ | |
672 | SFL_DS_SET(dsp->dsi, SFL_DSCLASS_IFINDEX, ifindex, 0); | |
673 | if (ds->sflow_agent) { | |
674 | dpif_sflow_add_poller(ds, dsp); | |
675 | } | |
676 | } else { | |
677 | /* Record "ifindex unknown" for the others */ | |
678 | SFL_DS_SET(dsp->dsi, SFL_DSCLASS_IFINDEX, 0, 0); | |
72b06300 | 679 | } |
34ae6d76 EJ |
680 | |
681 | out: | |
682 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
683 | } |
684 | ||
0cc96e48 | 685 | static void |
bae473fe | 686 | dpif_sflow_del_port__(struct dpif_sflow *ds, struct dpif_sflow_port *dsp) |
bd3950dd | 687 | OVS_REQUIRES(mutex) |
0cc96e48 | 688 | { |
7321bda3 NM |
689 | if (ds->sflow_agent |
690 | && SFL_DS_INDEX(dsp->dsi)) { | |
691 | sfl_agent_removePoller(ds->sflow_agent, &dsp->dsi); | |
692 | sfl_agent_removeSampler(ds->sflow_agent, &dsp->dsi); | |
0cc96e48 | 693 | } |
bae473fe JP |
694 | hmap_remove(&ds->ports, &dsp->hmap_node); |
695 | free(dsp); | |
0cc96e48 BP |
696 | } |
697 | ||
72b06300 | 698 | void |
4e022ec0 | 699 | dpif_sflow_del_port(struct dpif_sflow *ds, odp_port_t odp_port) |
34ae6d76 | 700 | OVS_EXCLUDED(mutex) |
72b06300 | 701 | { |
34ae6d76 EJ |
702 | struct dpif_sflow_port *dsp; |
703 | ||
704 | ovs_mutex_lock(&mutex); | |
705 | dsp = dpif_sflow_find_port(ds, odp_port); | |
bae473fe JP |
706 | if (dsp) { |
707 | dpif_sflow_del_port__(ds, dsp); | |
72b06300 | 708 | } |
34ae6d76 | 709 | ovs_mutex_unlock(&mutex); |
72b06300 BP |
710 | } |
711 | ||
712 | void | |
bae473fe JP |
713 | dpif_sflow_set_options(struct dpif_sflow *ds, |
714 | const struct ofproto_sflow_options *options) | |
34ae6d76 | 715 | OVS_EXCLUDED(mutex) |
72b06300 | 716 | { |
bae473fe | 717 | struct dpif_sflow_port *dsp; |
72b06300 | 718 | bool options_changed; |
72b06300 | 719 | SFLReceiver *receiver; |
72b06300 BP |
720 | SFLAddress agentIP; |
721 | time_t now; | |
743cea45 NM |
722 | SFLDataSource_instance dsi; |
723 | uint32_t dsIndex; | |
724 | SFLSampler *sampler; | |
3d2912f2 | 725 | SFLPoller *poller; |
72b06300 | 726 | |
34ae6d76 | 727 | ovs_mutex_lock(&mutex); |
81e2083f | 728 | if (sset_is_empty(&options->targets) || !options->sampling_rate) { |
a68813c3 BP |
729 | /* No point in doing any work if there are no targets or nothing to |
730 | * sample. */ | |
34ae6d76 EJ |
731 | dpif_sflow_clear__(ds); |
732 | goto out; | |
a68813c3 BP |
733 | } |
734 | ||
bae473fe JP |
735 | options_changed = (!ds->options |
736 | || !ofproto_sflow_options_equal(options, ds->options)); | |
72b06300 BP |
737 | |
738 | /* Configure collectors if options have changed or if we're shortchanged in | |
739 | * collectors (which indicates that opening one or more of the configured | |
740 | * collectors failed, so that we should retry). */ | |
741 | if (options_changed | |
bae473fe JP |
742 | || collectors_count(ds->collectors) < sset_count(&options->targets)) { |
743 | collectors_destroy(ds->collectors); | |
02ef592c | 744 | collectors_create(&options->targets, SFL_DEFAULT_COLLECTOR_PORT, |
bae473fe JP |
745 | &ds->collectors); |
746 | if (ds->collectors == NULL) { | |
a68813c3 BP |
747 | VLOG_WARN_RL(&rl, "no collectors could be initialized, " |
748 | "sFlow disabled"); | |
34ae6d76 EJ |
749 | dpif_sflow_clear__(ds); |
750 | goto out; | |
72b06300 BP |
751 | } |
752 | } | |
753 | ||
733adf2a LG |
754 | /* Choose agent IP address and agent device (if not yet setup) */ |
755 | if (!sflow_choose_agent_address(options->agent_device, | |
756 | &options->targets, | |
757 | options->control_ip, &agentIP)) { | |
34ae6d76 EJ |
758 | dpif_sflow_clear__(ds); |
759 | goto out; | |
733adf2a LG |
760 | } |
761 | ||
72b06300 BP |
762 | /* Avoid reconfiguring if options didn't change. */ |
763 | if (!options_changed) { | |
34ae6d76 | 764 | goto out; |
72b06300 | 765 | } |
bae473fe JP |
766 | ofproto_sflow_options_destroy(ds->options); |
767 | ds->options = ofproto_sflow_options_clone(options); | |
72b06300 | 768 | |
72b06300 BP |
769 | /* Create agent. */ |
770 | VLOG_INFO("creating sFlow agent %d", options->sub_id); | |
bae473fe | 771 | if (ds->sflow_agent) { |
3d2912f2 | 772 | sflow_global_counters_subid_clear(ds->sflow_agent->subId); |
bae473fe | 773 | sfl_agent_release(ds->sflow_agent); |
72b06300 | 774 | } |
bae473fe | 775 | ds->sflow_agent = xcalloc(1, sizeof *ds->sflow_agent); |
c73814a3 | 776 | now = time_wall(); |
bae473fe | 777 | sfl_agent_init(ds->sflow_agent, |
72b06300 BP |
778 | &agentIP, |
779 | options->sub_id, | |
780 | now, /* Boot time. */ | |
781 | now, /* Current time. */ | |
bae473fe | 782 | ds, /* Pointer supplied to callbacks. */ |
72b06300 BP |
783 | sflow_agent_alloc_cb, |
784 | sflow_agent_free_cb, | |
785 | sflow_agent_error_cb, | |
786 | sflow_agent_send_packet_cb); | |
787 | ||
bae473fe | 788 | receiver = sfl_agent_addReceiver(ds->sflow_agent); |
e53df206 | 789 | sfl_receiver_set_sFlowRcvrOwner(receiver, "Open vSwitch sFlow"); |
72b06300 BP |
790 | sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff); |
791 | ||
72b06300 | 792 | /* Set the sampling_rate down in the datapath. */ |
6ff686f2 | 793 | ds->probability = MAX(1, UINT32_MAX / ds->options->sampling_rate); |
72b06300 | 794 | |
743cea45 NM |
795 | /* Add a single sampler for the bridge. This appears as a PHYSICAL_ENTITY |
796 | because it is associated with the hypervisor, and interacts with the server | |
797 | hardware directly. The sub_id is used to distinguish this sampler from | |
798 | others on other bridges within the same agent. */ | |
799 | dsIndex = 1000 + options->sub_id; | |
800 | SFL_DS_SET(dsi, SFL_DSCLASS_PHYSICAL_ENTITY, dsIndex, 0); | |
801 | sampler = sfl_agent_addSampler(ds->sflow_agent, &dsi); | |
802 | sfl_sampler_set_sFlowFsPacketSamplingRate(sampler, ds->options->sampling_rate); | |
803 | sfl_sampler_set_sFlowFsMaximumHeaderSize(sampler, ds->options->header_len); | |
804 | sfl_sampler_set_sFlowFsReceiver(sampler, RECEIVER_INDEX); | |
805 | ||
3d2912f2 NM |
806 | /* Add a counter poller for the bridge so we can use it to send |
807 | global counters such as datapath cache hit/miss stats. */ | |
808 | poller = sfl_agent_addPoller(ds->sflow_agent, &dsi, ds, | |
809 | sflow_agent_get_global_counters); | |
810 | sfl_poller_set_sFlowCpInterval(poller, ds->options->polling_interval); | |
811 | sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX); | |
812 | ||
743cea45 | 813 | /* Add pollers for the currently known ifindex-ports */ |
bae473fe | 814 | HMAP_FOR_EACH (dsp, hmap_node, &ds->ports) { |
7321bda3 NM |
815 | if (SFL_DS_INDEX(dsp->dsi)) { |
816 | dpif_sflow_add_poller(ds, dsp); | |
817 | } | |
72b06300 | 818 | } |
34ae6d76 EJ |
819 | |
820 | ||
821 | out: | |
822 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
823 | } |
824 | ||
6ff686f2 | 825 | int |
bae473fe | 826 | dpif_sflow_odp_port_to_ifindex(const struct dpif_sflow *ds, |
34ae6d76 | 827 | odp_port_t odp_port) OVS_EXCLUDED(mutex) |
c1e98da1 | 828 | { |
34ae6d76 EJ |
829 | struct dpif_sflow_port *dsp; |
830 | int ret; | |
831 | ||
832 | ovs_mutex_lock(&mutex); | |
833 | dsp = dpif_sflow_find_port(ds, odp_port); | |
834 | ret = dsp ? SFL_DS_INDEX(dsp->dsi) : 0; | |
835 | ovs_mutex_unlock(&mutex); | |
836 | return ret; | |
c1e98da1 BP |
837 | } |
838 | ||
7321bda3 NM |
839 | static void |
840 | dpif_sflow_tunnel_v4(uint8_t tunnel_ipproto, | |
841 | const struct flow_tnl *tunnel, | |
842 | SFLSampled_ipv4 *ipv4) | |
843 | ||
844 | { | |
845 | ipv4->protocol = tunnel_ipproto; | |
846 | ipv4->tos = tunnel->ip_tos; | |
847 | ipv4->src_ip.addr = (OVS_FORCE uint32_t) tunnel->ip_src; | |
848 | ipv4->dst_ip.addr = (OVS_FORCE uint32_t) tunnel->ip_dst; | |
849 | ipv4->src_port = (OVS_FORCE uint16_t) tunnel->tp_src; | |
850 | ipv4->dst_port = (OVS_FORCE uint16_t) tunnel->tp_dst; | |
851 | } | |
852 | ||
853 | static void | |
854 | dpif_sflow_push_mpls_lse(struct dpif_sflow_actions *sflow_actions, | |
855 | ovs_be32 lse) | |
856 | { | |
857 | if (sflow_actions->mpls_stack_depth >= FLOW_MAX_MPLS_LABELS) { | |
858 | sflow_actions->mpls_err = true; | |
859 | return; | |
860 | } | |
861 | ||
862 | /* Record the new lse in host-byte-order. */ | |
863 | /* BOS flag will be fixed later when we send stack to sFlow library. */ | |
864 | sflow_actions->mpls_lse[sflow_actions->mpls_stack_depth++] = ntohl(lse); | |
865 | } | |
866 | ||
867 | static void | |
868 | dpif_sflow_pop_mpls_lse(struct dpif_sflow_actions *sflow_actions) | |
869 | { | |
870 | if (sflow_actions->mpls_stack_depth == 0) { | |
871 | sflow_actions->mpls_err = true; | |
872 | return; | |
873 | } | |
874 | sflow_actions->mpls_stack_depth--; | |
875 | } | |
876 | ||
877 | static void | |
878 | dpif_sflow_set_mpls(struct dpif_sflow_actions *sflow_actions, | |
879 | const struct ovs_key_mpls *mpls_key, int n) | |
880 | { | |
881 | int ii; | |
882 | if (n > FLOW_MAX_MPLS_LABELS) { | |
883 | sflow_actions->mpls_err = true; | |
884 | return; | |
885 | } | |
886 | ||
887 | for (ii = 0; ii < n; ii++) { | |
888 | /* Reverse stack order, and use host-byte-order for each lse. */ | |
889 | sflow_actions->mpls_lse[n - ii - 1] = ntohl(mpls_key[ii].mpls_lse); | |
890 | } | |
891 | sflow_actions->mpls_stack_depth = n; | |
892 | } | |
893 | ||
894 | static void | |
895 | sflow_read_tnl_push_action(const struct nlattr *attr, | |
896 | struct dpif_sflow_actions *sflow_actions) | |
897 | { | |
898 | /* Modeled on lib/odp-util.c: format_odp_tnl_push_header */ | |
899 | const struct ovs_action_push_tnl *data = nl_attr_get(attr); | |
900 | const struct eth_header *eth = (const struct eth_header *) data->header; | |
901 | const struct ip_header *ip | |
902 | = ALIGNED_CAST(const struct ip_header *, eth + 1); | |
903 | ||
904 | sflow_actions->out_port = u32_to_odp(data->out_port); | |
905 | ||
906 | /* Ethernet. */ | |
907 | /* TODO: SFlow does not currently define a MAC-in-MAC | |
908 | * encapsulation structure. We could use an extension | |
909 | * structure to report this. | |
910 | */ | |
911 | ||
912 | /* IPv4 */ | |
913 | /* Cannot assume alignment so just use memcpy. */ | |
914 | sflow_actions->tunnel.ip_src = get_16aligned_be32(&ip->ip_src); | |
915 | sflow_actions->tunnel.ip_dst = get_16aligned_be32(&ip->ip_dst); | |
916 | sflow_actions->tunnel.ip_tos = ip->ip_tos; | |
917 | sflow_actions->tunnel.ip_ttl = ip->ip_ttl; | |
918 | /* The tnl_push action can supply the ip_protocol too. */ | |
919 | sflow_actions->tunnel_ipproto = ip->ip_proto; | |
920 | ||
921 | /* Layer 4 */ | |
922 | if (data->tnl_type == OVS_VPORT_TYPE_VXLAN | |
923 | || data->tnl_type == OVS_VPORT_TYPE_GENEVE) { | |
924 | const struct udp_header *udp = (const struct udp_header *) (ip + 1); | |
925 | sflow_actions->tunnel.tp_src = udp->udp_src; | |
926 | sflow_actions->tunnel.tp_dst = udp->udp_dst; | |
927 | ||
928 | if (data->tnl_type == OVS_VPORT_TYPE_VXLAN) { | |
929 | const struct vxlanhdr *vxh = (const struct vxlanhdr *) (udp + 1); | |
930 | uint64_t tun_id = ntohl(get_16aligned_be32(&vxh->vx_vni)) >> 8; | |
931 | sflow_actions->tunnel.tun_id = htonll(tun_id); | |
932 | } else { | |
933 | const struct genevehdr *gnh = (const struct genevehdr *) (udp + 1); | |
934 | uint64_t tun_id = ntohl(get_16aligned_be32(&gnh->vni)) >> 8; | |
935 | sflow_actions->tunnel.tun_id = htonll(tun_id); | |
936 | } | |
937 | } else if (data->tnl_type == OVS_VPORT_TYPE_GRE) { | |
938 | const void *l4 = ip + 1; | |
939 | const struct gre_base_hdr *greh = (const struct gre_base_hdr *) l4; | |
940 | ovs_16aligned_be32 *options = (ovs_16aligned_be32 *)(greh + 1); | |
941 | if (greh->flags & htons(GRE_CSUM)) { | |
942 | options++; | |
943 | } | |
944 | if (greh->flags & htons(GRE_KEY)) { | |
945 | uint64_t tun_id = ntohl(get_16aligned_be32(options)); | |
946 | sflow_actions->tunnel.tun_id = htonll(tun_id); | |
947 | } | |
948 | } | |
949 | } | |
950 | ||
951 | static void | |
952 | sflow_read_set_action(const struct nlattr *attr, | |
953 | struct dpif_sflow_actions *sflow_actions) | |
954 | { | |
955 | enum ovs_key_attr type = nl_attr_type(attr); | |
956 | switch (type) { | |
957 | case OVS_KEY_ATTR_ENCAP: | |
958 | if (++sflow_actions->encap_depth > 1) { | |
959 | /* Do not handle multi-encap for now. */ | |
960 | sflow_actions->tunnel_err = true; | |
961 | } else { | |
962 | dpif_sflow_read_actions(NULL, | |
963 | nl_attr_get(attr), nl_attr_get_size(attr), | |
964 | sflow_actions); | |
965 | } | |
966 | break; | |
967 | case OVS_KEY_ATTR_PRIORITY: | |
968 | case OVS_KEY_ATTR_SKB_MARK: | |
969 | case OVS_KEY_ATTR_DP_HASH: | |
970 | case OVS_KEY_ATTR_RECIRC_ID: | |
971 | break; | |
972 | ||
973 | case OVS_KEY_ATTR_TUNNEL: { | |
974 | if (++sflow_actions->encap_depth > 1) { | |
975 | /* Do not handle multi-encap for now. */ | |
976 | sflow_actions->tunnel_err = true; | |
977 | } else { | |
8d8ab6c2 | 978 | if (odp_tun_key_from_attr(attr, &sflow_actions->tunnel) |
7321bda3 NM |
979 | == ODP_FIT_ERROR) { |
980 | /* Tunnel parsing error. */ | |
981 | sflow_actions->tunnel_err = true; | |
982 | } | |
983 | } | |
984 | break; | |
985 | } | |
986 | ||
987 | case OVS_KEY_ATTR_IN_PORT: | |
988 | case OVS_KEY_ATTR_ETHERNET: | |
989 | case OVS_KEY_ATTR_VLAN: | |
990 | break; | |
991 | ||
992 | case OVS_KEY_ATTR_MPLS: { | |
993 | const struct ovs_key_mpls *mpls_key = nl_attr_get(attr); | |
994 | size_t size = nl_attr_get_size(attr); | |
995 | dpif_sflow_set_mpls(sflow_actions, mpls_key, size / sizeof *mpls_key); | |
996 | break; | |
997 | } | |
998 | ||
999 | case OVS_KEY_ATTR_ETHERTYPE: | |
1000 | case OVS_KEY_ATTR_IPV4: | |
1001 | if (sflow_actions->encap_depth == 1) { | |
1002 | const struct ovs_key_ipv4 *key = nl_attr_get(attr); | |
1003 | if (key->ipv4_src) { | |
1004 | sflow_actions->tunnel.ip_src = key->ipv4_src; | |
1005 | } | |
1006 | if (key->ipv4_dst) { | |
1007 | sflow_actions->tunnel.ip_dst = key->ipv4_dst; | |
1008 | } | |
1009 | if (key->ipv4_proto) { | |
1010 | sflow_actions->tunnel_ipproto = key->ipv4_proto; | |
1011 | } | |
1012 | if (key->ipv4_tos) { | |
1013 | sflow_actions->tunnel.ip_tos = key->ipv4_tos; | |
1014 | } | |
1015 | if (key->ipv4_ttl) { | |
1016 | sflow_actions->tunnel.ip_tos = key->ipv4_ttl; | |
1017 | } | |
1018 | } | |
1019 | break; | |
1020 | ||
1021 | case OVS_KEY_ATTR_IPV6: | |
1022 | /* TODO: parse IPv6 encap. */ | |
1023 | break; | |
1024 | ||
1025 | /* These have the same structure and format. */ | |
1026 | case OVS_KEY_ATTR_TCP: | |
1027 | case OVS_KEY_ATTR_UDP: | |
1028 | case OVS_KEY_ATTR_SCTP: | |
1029 | if (sflow_actions->encap_depth == 1) { | |
1030 | const struct ovs_key_tcp *key = nl_attr_get(attr); | |
1031 | if (key->tcp_src) { | |
1032 | sflow_actions->tunnel.tp_src = key->tcp_src; | |
1033 | } | |
1034 | if (key->tcp_dst) { | |
1035 | sflow_actions->tunnel.tp_dst = key->tcp_dst; | |
1036 | } | |
1037 | } | |
1038 | break; | |
1039 | ||
1040 | case OVS_KEY_ATTR_TCP_FLAGS: | |
1041 | case OVS_KEY_ATTR_ICMP: | |
1042 | case OVS_KEY_ATTR_ICMPV6: | |
1043 | case OVS_KEY_ATTR_ARP: | |
1044 | case OVS_KEY_ATTR_ND: | |
07659514 JS |
1045 | case OVS_KEY_ATTR_CT_STATE: |
1046 | case OVS_KEY_ATTR_CT_ZONE: | |
8e53fe8c | 1047 | case OVS_KEY_ATTR_CT_MARK: |
9daf2348 | 1048 | case OVS_KEY_ATTR_CT_LABELS: |
c30b4cea JR |
1049 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4: |
1050 | case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6: | |
7321bda3 NM |
1051 | case OVS_KEY_ATTR_UNSPEC: |
1052 | case __OVS_KEY_ATTR_MAX: | |
1053 | default: | |
1054 | break; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | static void | |
1059 | dpif_sflow_capture_input_mpls(const struct flow *flow, | |
1060 | struct dpif_sflow_actions *sflow_actions) | |
1061 | { | |
1062 | if (eth_type_mpls(flow->dl_type)) { | |
1063 | int depth = 0; | |
1064 | int ii; | |
1065 | ovs_be32 lse; | |
1066 | /* Calculate depth by detecting BOS. */ | |
1067 | for (ii = 0; ii < FLOW_MAX_MPLS_LABELS; ii++) { | |
1068 | lse = flow->mpls_lse[ii]; | |
1069 | depth++; | |
1070 | if (lse & htonl(MPLS_BOS_MASK)) { | |
1071 | break; | |
1072 | } | |
1073 | } | |
1074 | /* Capture stack, reversing stack order, and | |
1075 | * using host-byte-order for each lse. BOS flag | |
1076 | * is ignored for now. It is set later when | |
1077 | * the output stack is encoded. | |
1078 | */ | |
1079 | for (ii = 0; ii < depth; ii++) { | |
1080 | lse = flow->mpls_lse[ii]; | |
1081 | sflow_actions->mpls_lse[depth - ii - 1] = ntohl(lse); | |
1082 | } | |
1083 | sflow_actions->mpls_stack_depth = depth; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | void | |
1088 | dpif_sflow_read_actions(const struct flow *flow, | |
1089 | const struct nlattr *actions, size_t actions_len, | |
1090 | struct dpif_sflow_actions *sflow_actions) | |
1091 | { | |
1092 | const struct nlattr *a; | |
1093 | unsigned int left; | |
1094 | ||
1095 | if (actions_len == 0) { | |
1096 | /* Packet dropped.*/ | |
1097 | return; | |
1098 | } | |
1099 | ||
1100 | if (flow != NULL) { | |
1101 | /* Make sure the MPLS output stack | |
1102 | * is seeded with the input stack. | |
1103 | */ | |
1104 | dpif_sflow_capture_input_mpls(flow, sflow_actions); | |
1105 | ||
1106 | /* XXX when 802.1AD(QinQ) is supported then | |
1107 | * we can do the same with VLAN stacks here | |
1108 | */ | |
1109 | } | |
1110 | ||
1111 | NL_ATTR_FOR_EACH (a, left, actions, actions_len) { | |
1112 | enum ovs_action_attr type = nl_attr_type(a); | |
1113 | switch (type) { | |
1114 | case OVS_ACTION_ATTR_OUTPUT: | |
1115 | /* Capture the output port in case we need it | |
1116 | * to get the output tunnel type. | |
1117 | */ | |
8611f9a4 | 1118 | sflow_actions->out_port = nl_attr_get_odp_port(a); |
7321bda3 NM |
1119 | break; |
1120 | ||
1121 | case OVS_ACTION_ATTR_TUNNEL_POP: | |
1122 | /* XXX: Do not handle this for now. It's not clear | |
1123 | * if we should start with encap_depth == 1 when we | |
1124 | * see an input tunnel, or if we should assume | |
1125 | * that the input tunnel was always "popped" if it | |
1126 | * was presented to us decoded in flow->tunnel? | |
1127 | * | |
1128 | * If we do handle this it might look like this, | |
1129 | * as we clear the captured tunnel info and decrement | |
1130 | * the encap_depth: | |
1131 | * | |
1132 | * memset(&sflow_actions->tunnel, 0, sizeof struct flow_tnl); | |
1133 | * sflow_actions->tunnel_ipproto = 0; | |
1134 | * --sflow_actions->encap_depth; | |
1135 | * | |
1136 | * but for now just disable the tunnel annotation: | |
1137 | */ | |
1138 | sflow_actions->tunnel_err = true; | |
1139 | break; | |
1140 | ||
1141 | case OVS_ACTION_ATTR_TUNNEL_PUSH: | |
1142 | /* XXX: This actions appears to come with it's own | |
1143 | * OUTPUT action, so should it be regarded as having | |
1144 | * an implicit "pop" following it too? Put another | |
1145 | * way, would two tnl_push() actions in succession | |
1146 | * result in a packet with two layers of encap? | |
1147 | */ | |
1148 | if (++sflow_actions->encap_depth > 1) { | |
1149 | /* Do not handle multi-encap for now. */ | |
1150 | sflow_actions->tunnel_err = true; | |
1151 | } else { | |
1152 | sflow_read_tnl_push_action(a, sflow_actions); | |
1153 | } | |
1154 | break; | |
1155 | ||
aaca4fe0 | 1156 | case OVS_ACTION_ATTR_TRUNC: |
7321bda3 NM |
1157 | case OVS_ACTION_ATTR_USERSPACE: |
1158 | case OVS_ACTION_ATTR_RECIRC: | |
1159 | case OVS_ACTION_ATTR_HASH: | |
07659514 | 1160 | case OVS_ACTION_ATTR_CT: |
5dddf960 | 1161 | case OVS_ACTION_ATTR_METER: |
7321bda3 NM |
1162 | break; |
1163 | ||
1164 | case OVS_ACTION_ATTR_SET_MASKED: | |
1165 | /* TODO: apply mask. XXX: Are we likely to see this? */ | |
1166 | break; | |
1167 | ||
1168 | case OVS_ACTION_ATTR_SET: | |
1169 | sflow_read_set_action(nl_attr_get(a), sflow_actions); | |
1170 | break; | |
1171 | ||
1172 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
1173 | case OVS_ACTION_ATTR_POP_VLAN: | |
1174 | /* TODO: 802.1AD(QinQ) is not supported by OVS (yet), so do not | |
1175 | * construct a VLAN-stack. The sFlow user-action cookie already | |
1176 | * captures the egress VLAN ID so there is nothing more to do here. | |
1177 | */ | |
1178 | break; | |
1179 | ||
1180 | case OVS_ACTION_ATTR_PUSH_MPLS: { | |
1181 | const struct ovs_action_push_mpls *mpls = nl_attr_get(a); | |
1182 | if (mpls) { | |
1183 | dpif_sflow_push_mpls_lse(sflow_actions, mpls->mpls_lse); | |
1184 | } | |
1185 | break; | |
1186 | } | |
1187 | case OVS_ACTION_ATTR_POP_MPLS: { | |
1188 | dpif_sflow_pop_mpls_lse(sflow_actions); | |
1189 | break; | |
1190 | } | |
88fc5281 JS |
1191 | case OVS_ACTION_ATTR_PUSH_ETH: |
1192 | case OVS_ACTION_ATTR_POP_ETH: | |
1193 | /* TODO: SFlow does not currently define a MAC-in-MAC | |
1194 | * encapsulation structure. We could use an extension | |
1195 | * structure to report this. | |
1196 | */ | |
1197 | break; | |
7321bda3 | 1198 | case OVS_ACTION_ATTR_SAMPLE: |
535e3acf | 1199 | case OVS_ACTION_ATTR_CLONE: |
7321bda3 NM |
1200 | case OVS_ACTION_ATTR_UNSPEC: |
1201 | case __OVS_ACTION_ATTR_MAX: | |
1202 | default: | |
1203 | break; | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | static void | |
1209 | dpif_sflow_encode_mpls_stack(SFLLabelStack *stack, | |
1210 | uint32_t *mpls_lse_buf, | |
1211 | const struct dpif_sflow_actions *sflow_actions) | |
1212 | { | |
1213 | /* Put the MPLS stack back into "packet header" order, | |
1214 | * and make sure the BOS flag is set correctly on the last | |
1215 | * one. Each lse is still in host-byte-order. | |
1216 | */ | |
1217 | int ii; | |
1218 | uint32_t lse; | |
1219 | stack->depth = sflow_actions->mpls_stack_depth; | |
1220 | stack->stack = mpls_lse_buf; | |
1221 | for (ii = 0; ii < stack->depth; ii++) { | |
1222 | lse = sflow_actions->mpls_lse[stack->depth - ii - 1]; | |
1223 | stack->stack[ii] = (lse & ~MPLS_BOS_MASK); | |
1224 | } | |
1225 | stack->stack[stack->depth - 1] |= MPLS_BOS_MASK; | |
1226 | } | |
1227 | ||
1228 | /* Extract the output port count from the user action cookie. | |
1229 | * See http://sflow.org/sflow_version_5.txt "Input/Output port information" | |
1230 | */ | |
1231 | static uint32_t | |
1232 | dpif_sflow_cookie_num_outputs(const union user_action_cookie *cookie) | |
1233 | { | |
1234 | uint32_t format = cookie->sflow.output & 0xC0000000; | |
1235 | uint32_t port_n = cookie->sflow.output & 0x3FFFFFFF; | |
1236 | if (format == 0) { | |
1237 | return port_n ? 1 : 0; | |
1238 | } | |
1239 | else if (format == 0x80000000) { | |
1240 | return port_n; | |
1241 | } | |
1242 | return 0; | |
1243 | } | |
1244 | ||
72b06300 | 1245 | void |
cf62fa4c | 1246 | dpif_sflow_received(struct dpif_sflow *ds, const struct dp_packet *packet, |
7321bda3 NM |
1247 | const struct flow *flow, odp_port_t odp_in_port, |
1248 | const union user_action_cookie *cookie, | |
1249 | const struct dpif_sflow_actions *sflow_actions) | |
34ae6d76 | 1250 | OVS_EXCLUDED(mutex) |
72b06300 BP |
1251 | { |
1252 | SFL_FLOW_SAMPLE_TYPE fs; | |
1253 | SFLFlow_sample_element hdrElem; | |
1254 | SFLSampled_header *header; | |
1255 | SFLFlow_sample_element switchElem; | |
7321bda3 NM |
1256 | uint8_t tnlInProto, tnlOutProto; |
1257 | SFLFlow_sample_element tnlInElem, tnlOutElem; | |
1258 | SFLFlow_sample_element vniInElem, vniOutElem; | |
1259 | SFLFlow_sample_element mplsElem; | |
1260 | uint32_t mpls_lse_buf[FLOW_MAX_MPLS_LABELS]; | |
56fd8edf | 1261 | SFLSampler *sampler; |
6ff686f2 | 1262 | struct dpif_sflow_port *in_dsp; |
7321bda3 | 1263 | struct dpif_sflow_port *out_dsp; |
1673e0e4 | 1264 | ovs_be16 vlan_tci; |
72b06300 | 1265 | |
34ae6d76 | 1266 | ovs_mutex_lock(&mutex); |
743cea45 NM |
1267 | sampler = ds->sflow_agent->samplers; |
1268 | if (!sampler) { | |
34ae6d76 | 1269 | goto out; |
6ff686f2 | 1270 | } |
6ff686f2 | 1271 | |
743cea45 NM |
1272 | /* Build a flow sample. */ |
1273 | memset(&fs, 0, sizeof fs); | |
72b06300 | 1274 | |
743cea45 NM |
1275 | /* Look up the input ifIndex if this port has one. Otherwise just |
1276 | * leave it as 0 (meaning 'unknown') and continue. */ | |
1277 | in_dsp = dpif_sflow_find_port(ds, odp_in_port); | |
1278 | if (in_dsp) { | |
1279 | fs.input = SFL_DS_INDEX(in_dsp->dsi); | |
56fd8edf BP |
1280 | } |
1281 | ||
743cea45 NM |
1282 | /* Make the assumption that the random number generator in the datapath converges |
1283 | * to the configured mean, and just increment the samplePool by the configured | |
1284 | * sampling rate every time. */ | |
1285 | sampler->samplePool += sfl_sampler_get_sFlowFsPacketSamplingRate(sampler); | |
1286 | ||
72b06300 BP |
1287 | /* Sampled header. */ |
1288 | memset(&hdrElem, 0, sizeof hdrElem); | |
1289 | hdrElem.tag = SFLFLOW_HEADER; | |
1290 | header = &hdrElem.flowType.header; | |
1291 | header->header_protocol = SFLHEADER_ETHERNET_ISO8023; | |
c62caaa3 | 1292 | /* The frame_length should include the Ethernet FCS (4 bytes), |
743cea45 | 1293 | * but it has already been stripped, so we need to add 4 here. */ |
cf62fa4c | 1294 | header->frame_length = dp_packet_size(packet) + 4; |
c62caaa3 NM |
1295 | /* Ethernet FCS stripped off. */ |
1296 | header->stripped = 4; | |
cf62fa4c | 1297 | header->header_length = MIN(dp_packet_size(packet), |
856081f6 | 1298 | sampler->sFlowFsMaximumHeaderSize); |
cf62fa4c | 1299 | header->header_bytes = dp_packet_data(packet); |
72b06300 BP |
1300 | |
1301 | /* Add extended switch element. */ | |
1302 | memset(&switchElem, 0, sizeof(switchElem)); | |
1303 | switchElem.tag = SFLFLOW_EX_SWITCH; | |
f0fb825a EG |
1304 | switchElem.flowType.sw.src_vlan = vlan_tci_to_vid(flow->vlans[0].tci); |
1305 | switchElem.flowType.sw.src_priority = vlan_tci_to_pcp(flow->vlans[0].tci); | |
6ff686f2 PS |
1306 | |
1307 | /* Retrieve data from user_action_cookie. */ | |
1673e0e4 BP |
1308 | vlan_tci = cookie->sflow.vlan_tci; |
1309 | switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(vlan_tci); | |
1310 | switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(vlan_tci); | |
46b47a41 | 1311 | |
1673e0e4 | 1312 | fs.output = cookie->sflow.output; |
72b06300 | 1313 | |
7321bda3 NM |
1314 | /* Input tunnel. */ |
1315 | if (flow->tunnel.ip_dst) { | |
1316 | memset(&tnlInElem, 0, sizeof(tnlInElem)); | |
1317 | tnlInElem.tag = SFLFLOW_EX_IPV4_TUNNEL_INGRESS; | |
1898b54f | 1318 | tnlInProto = in_dsp ? dpif_sflow_tunnel_proto(in_dsp->tunnel_type) : 0; |
7321bda3 NM |
1319 | dpif_sflow_tunnel_v4(tnlInProto, |
1320 | &flow->tunnel, | |
1321 | &tnlInElem.flowType.ipv4); | |
1322 | SFLADD_ELEMENT(&fs, &tnlInElem); | |
1323 | if (flow->tunnel.tun_id) { | |
1324 | memset(&vniInElem, 0, sizeof(vniInElem)); | |
1325 | vniInElem.tag = SFLFLOW_EX_VNI_INGRESS; | |
1326 | vniInElem.flowType.tunnel_vni.vni | |
1327 | = ntohll(flow->tunnel.tun_id); | |
1328 | SFLADD_ELEMENT(&fs, &vniInElem); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* Output tunnel. */ | |
1333 | if (sflow_actions | |
1334 | && sflow_actions->encap_depth == 1 | |
1335 | && !sflow_actions->tunnel_err | |
1336 | && dpif_sflow_cookie_num_outputs(cookie) == 1) { | |
1337 | tnlOutProto = sflow_actions->tunnel_ipproto; | |
1338 | if (tnlOutProto == 0) { | |
1339 | /* Try to infer the ip-protocol from the output port. */ | |
1340 | if (sflow_actions->out_port != ODPP_NONE) { | |
1341 | out_dsp = dpif_sflow_find_port(ds, sflow_actions->out_port); | |
1342 | if (out_dsp) { | |
1343 | tnlOutProto = dpif_sflow_tunnel_proto(out_dsp->tunnel_type); | |
1344 | } | |
1345 | } | |
1346 | } | |
1347 | memset(&tnlOutElem, 0, sizeof(tnlOutElem)); | |
1348 | tnlOutElem.tag = SFLFLOW_EX_IPV4_TUNNEL_EGRESS; | |
1349 | dpif_sflow_tunnel_v4(tnlOutProto, | |
1350 | &sflow_actions->tunnel, | |
1351 | &tnlOutElem.flowType.ipv4); | |
1352 | SFLADD_ELEMENT(&fs, &tnlOutElem); | |
1353 | if (sflow_actions->tunnel.tun_id) { | |
1354 | memset(&vniOutElem, 0, sizeof(vniOutElem)); | |
1355 | vniOutElem.tag = SFLFLOW_EX_VNI_EGRESS; | |
1356 | vniOutElem.flowType.tunnel_vni.vni | |
1357 | = ntohll(sflow_actions->tunnel.tun_id); | |
1358 | SFLADD_ELEMENT(&fs, &vniOutElem); | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* MPLS output label stack. */ | |
1363 | if (sflow_actions | |
1364 | && sflow_actions->mpls_stack_depth > 0 | |
1365 | && !sflow_actions->mpls_err | |
1366 | && dpif_sflow_cookie_num_outputs(cookie) == 1) { | |
1367 | memset(&mplsElem, 0, sizeof(mplsElem)); | |
1368 | mplsElem.tag = SFLFLOW_EX_MPLS; | |
1369 | dpif_sflow_encode_mpls_stack(&mplsElem.flowType.mpls.out_stack, | |
1370 | mpls_lse_buf, | |
1371 | sflow_actions); | |
1372 | SFLADD_ELEMENT(&fs, &mplsElem); | |
1373 | } | |
1374 | ||
72b06300 BP |
1375 | /* Submit the flow sample to be encoded into the next datagram. */ |
1376 | SFLADD_ELEMENT(&fs, &hdrElem); | |
1377 | SFLADD_ELEMENT(&fs, &switchElem); | |
1378 | sfl_sampler_writeFlowSample(sampler, &fs); | |
34ae6d76 EJ |
1379 | |
1380 | out: | |
1381 | ovs_mutex_unlock(&mutex); | |
72b06300 BP |
1382 | } |
1383 | ||
72b06300 | 1384 | void |
34ae6d76 | 1385 | dpif_sflow_run(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 1386 | { |
34ae6d76 EJ |
1387 | ovs_mutex_lock(&mutex); |
1388 | if (ds->collectors != NULL) { | |
72b06300 | 1389 | time_t now = time_now(); |
733adf2a | 1390 | route_table_run(); |
bae473fe JP |
1391 | if (now >= ds->next_tick) { |
1392 | sfl_agent_tick(ds->sflow_agent, time_wall()); | |
1393 | ds->next_tick = now + 1; | |
72b06300 BP |
1394 | } |
1395 | } | |
34ae6d76 | 1396 | ovs_mutex_unlock(&mutex); |
72b06300 BP |
1397 | } |
1398 | ||
1399 | void | |
34ae6d76 | 1400 | dpif_sflow_wait(struct dpif_sflow *ds) OVS_EXCLUDED(mutex) |
72b06300 | 1401 | { |
34ae6d76 EJ |
1402 | ovs_mutex_lock(&mutex); |
1403 | if (ds->collectors != NULL) { | |
bae473fe | 1404 | poll_timer_wait_until(ds->next_tick * 1000LL); |
72b06300 | 1405 | } |
34ae6d76 | 1406 | ovs_mutex_unlock(&mutex); |
72b06300 | 1407 | } |