]>
Commit | Line | Data |
---|---|---|
abe529af BP |
1 | /* |
2 | * Copyright (c) 2009, 2010, 2011 Nicira Networks. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
5bee6e26 | 19 | #include "ofproto/ofproto-provider.h" |
abe529af BP |
20 | |
21 | #include <errno.h> | |
22 | ||
23 | #include "autopath.h" | |
24 | #include "bond.h" | |
daff3353 | 25 | #include "bundle.h" |
abe529af BP |
26 | #include "byte-order.h" |
27 | #include "connmgr.h" | |
28 | #include "coverage.h" | |
29 | #include "cfm.h" | |
30 | #include "dpif.h" | |
31 | #include "dynamic-string.h" | |
32 | #include "fail-open.h" | |
33 | #include "hmapx.h" | |
34 | #include "lacp.h" | |
75a75043 | 35 | #include "learn.h" |
abe529af BP |
36 | #include "mac-learning.h" |
37 | #include "multipath.h" | |
38 | #include "netdev.h" | |
39 | #include "netlink.h" | |
40 | #include "nx-match.h" | |
41 | #include "odp-util.h" | |
42 | #include "ofp-util.h" | |
43 | #include "ofpbuf.h" | |
44 | #include "ofp-print.h" | |
bae473fe | 45 | #include "ofproto-dpif-sflow.h" |
abe529af BP |
46 | #include "poll-loop.h" |
47 | #include "timer.h" | |
6c1491fb | 48 | #include "unaligned.h" |
abe529af BP |
49 | #include "unixctl.h" |
50 | #include "vlan-bitmap.h" | |
51 | #include "vlog.h" | |
52 | ||
53 | VLOG_DEFINE_THIS_MODULE(ofproto_dpif); | |
54 | ||
55 | COVERAGE_DEFINE(ofproto_dpif_ctlr_action); | |
56 | COVERAGE_DEFINE(ofproto_dpif_expired); | |
57 | COVERAGE_DEFINE(ofproto_dpif_no_packet_in); | |
58 | COVERAGE_DEFINE(ofproto_dpif_xlate); | |
59 | COVERAGE_DEFINE(facet_changed_rule); | |
60 | COVERAGE_DEFINE(facet_invalidated); | |
61 | COVERAGE_DEFINE(facet_revalidate); | |
62 | COVERAGE_DEFINE(facet_unexpected); | |
63 | ||
29901626 | 64 | /* Maximum depth of flow table recursion (due to resubmit actions) in a |
abe529af | 65 | * flow translation. */ |
642a5c05 | 66 | #define MAX_RESUBMIT_RECURSION 32 |
abe529af | 67 | |
9cdaaebe BP |
68 | /* Number of implemented OpenFlow tables. */ |
69 | enum { N_TABLES = 255 }; | |
70 | BUILD_ASSERT_DECL(N_TABLES >= 1 && N_TABLES <= 255); | |
71 | ||
abe529af BP |
72 | struct ofport_dpif; |
73 | struct ofproto_dpif; | |
74 | ||
75 | struct rule_dpif { | |
76 | struct rule up; | |
77 | ||
78 | long long int used; /* Time last used; time created if not used. */ | |
79 | ||
80 | /* These statistics: | |
81 | * | |
82 | * - Do include packets and bytes from facets that have been deleted or | |
83 | * whose own statistics have been folded into the rule. | |
84 | * | |
85 | * - Do include packets and bytes sent "by hand" that were accounted to | |
86 | * the rule without any facet being involved (this is a rare corner | |
87 | * case in rule_execute()). | |
88 | * | |
89 | * - Do not include packet or bytes that can be obtained from any facet's | |
90 | * packet_count or byte_count member or that can be obtained from the | |
91 | * datapath by, e.g., dpif_flow_get() for any facet. | |
92 | */ | |
93 | uint64_t packet_count; /* Number of packets received. */ | |
94 | uint64_t byte_count; /* Number of bytes received. */ | |
95 | ||
54a9cbc9 BP |
96 | tag_type tag; /* Caches rule_calculate_tag() result. */ |
97 | ||
abe529af BP |
98 | struct list facets; /* List of "struct facet"s. */ |
99 | }; | |
100 | ||
101 | static struct rule_dpif *rule_dpif_cast(const struct rule *rule) | |
102 | { | |
103 | return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL; | |
104 | } | |
105 | ||
29901626 BP |
106 | static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *, |
107 | const struct flow *, uint8_t table); | |
abe529af BP |
108 | |
109 | #define MAX_MIRRORS 32 | |
110 | typedef uint32_t mirror_mask_t; | |
111 | #define MIRROR_MASK_C(X) UINT32_C(X) | |
112 | BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS); | |
113 | struct ofmirror { | |
114 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
115 | size_t idx; /* In ofproto's "mirrors" array. */ | |
116 | void *aux; /* Key supplied by ofproto's client. */ | |
117 | char *name; /* Identifier for log messages. */ | |
118 | ||
119 | /* Selection criteria. */ | |
120 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
121 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
122 | unsigned long *vlans; /* Bitmap of chosen VLANs, NULL selects all. */ | |
123 | ||
9ba15e2a | 124 | /* Output (exactly one of out == NULL and out_vlan == -1 is true). */ |
abe529af BP |
125 | struct ofbundle *out; /* Output port or NULL. */ |
126 | int out_vlan; /* Output VLAN or -1. */ | |
9ba15e2a | 127 | mirror_mask_t dup_mirrors; /* Bitmap of mirrors with the same output. */ |
abe529af BP |
128 | }; |
129 | ||
130 | static void mirror_destroy(struct ofmirror *); | |
131 | ||
132 | /* A group of one or more OpenFlow ports. */ | |
133 | #define OFBUNDLE_FLOOD ((struct ofbundle *) 1) | |
134 | struct ofbundle { | |
135 | struct ofproto_dpif *ofproto; /* Owning ofproto. */ | |
136 | struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */ | |
137 | void *aux; /* Key supplied by ofproto's client. */ | |
138 | char *name; /* Identifier for log messages. */ | |
139 | ||
140 | /* Configuration. */ | |
141 | struct list ports; /* Contains "struct ofport"s. */ | |
ecac4ebf | 142 | enum port_vlan_mode vlan_mode; /* VLAN mode */ |
abe529af BP |
143 | int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */ |
144 | unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. | |
145 | * NULL if all VLANs are trunked. */ | |
146 | struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */ | |
147 | struct bond *bond; /* Nonnull iff more than one port. */ | |
5e9ceccd | 148 | bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */ |
abe529af BP |
149 | |
150 | /* Status. */ | |
151 | bool floodable; /* True if no port has OFPPC_NO_FLOOD set. */ | |
152 | ||
153 | /* Port mirroring info. */ | |
154 | mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */ | |
155 | mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */ | |
156 | mirror_mask_t mirror_out; /* Mirrors that output to this bundle. */ | |
157 | }; | |
158 | ||
159 | static void bundle_remove(struct ofport *); | |
7bde8dd8 | 160 | static void bundle_update(struct ofbundle *); |
abe529af BP |
161 | static void bundle_destroy(struct ofbundle *); |
162 | static void bundle_del_port(struct ofport_dpif *); | |
163 | static void bundle_run(struct ofbundle *); | |
164 | static void bundle_wait(struct ofbundle *); | |
165 | ||
21f7563c JP |
166 | static void stp_run(struct ofproto_dpif *ofproto); |
167 | static void stp_wait(struct ofproto_dpif *ofproto); | |
168 | ||
5da5ec37 BP |
169 | static bool ofbundle_includes_vlan(const struct ofbundle *, uint16_t vlan); |
170 | ||
abe529af BP |
171 | struct action_xlate_ctx { |
172 | /* action_xlate_ctx_init() initializes these members. */ | |
173 | ||
174 | /* The ofproto. */ | |
175 | struct ofproto_dpif *ofproto; | |
176 | ||
177 | /* Flow to which the OpenFlow actions apply. xlate_actions() will modify | |
178 | * this flow when actions change header fields. */ | |
179 | struct flow flow; | |
180 | ||
181 | /* The packet corresponding to 'flow', or a null pointer if we are | |
182 | * revalidating without a packet to refer to. */ | |
183 | const struct ofpbuf *packet; | |
184 | ||
75a75043 BP |
185 | /* Should OFPP_NORMAL MAC learning and NXAST_LEARN actions execute? We |
186 | * want to execute them if we are actually processing a packet, or if we | |
187 | * are accounting for packets that the datapath has processed, but not if | |
188 | * we are just revalidating. */ | |
189 | bool may_learn; | |
190 | ||
abe529af BP |
191 | /* If nonnull, called just before executing a resubmit action. |
192 | * | |
193 | * This is normally null so the client has to set it manually after | |
194 | * calling action_xlate_ctx_init(). */ | |
195 | void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *); | |
196 | ||
abe529af BP |
197 | /* xlate_actions() initializes and uses these members. The client might want |
198 | * to look at them after it returns. */ | |
199 | ||
200 | struct ofpbuf *odp_actions; /* Datapath actions. */ | |
75a75043 | 201 | tag_type tags; /* Tags associated with actions. */ |
abe529af BP |
202 | bool may_set_up_flow; /* True ordinarily; false if the actions must |
203 | * be reassessed for every packet. */ | |
75a75043 BP |
204 | bool has_learn; /* Actions include NXAST_LEARN? */ |
205 | bool has_normal; /* Actions output to OFPP_NORMAL? */ | |
abe529af BP |
206 | uint16_t nf_output_iface; /* Output interface index for NetFlow. */ |
207 | ||
208 | /* xlate_actions() initializes and uses these members, but the client has no | |
209 | * reason to look at them. */ | |
210 | ||
211 | int recurse; /* Recursion level, via xlate_table_action. */ | |
b3e9b2ed | 212 | struct flow base_flow; /* Flow at the last commit. */ |
abff858b | 213 | uint32_t original_priority; /* Priority when packet arrived. */ |
29901626 | 214 | uint8_t table_id; /* OpenFlow table ID where flow was found. */ |
6ff686f2 PS |
215 | uint32_t sflow_n_outputs; /* Number of output ports. */ |
216 | uint16_t sflow_odp_port; /* Output port for composing sFlow action. */ | |
217 | uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */ | |
848e8809 | 218 | bool exit; /* No further actions should be processed. */ |
abe529af BP |
219 | }; |
220 | ||
221 | static void action_xlate_ctx_init(struct action_xlate_ctx *, | |
222 | struct ofproto_dpif *, const struct flow *, | |
223 | const struct ofpbuf *); | |
224 | static struct ofpbuf *xlate_actions(struct action_xlate_ctx *, | |
225 | const union ofp_action *in, size_t n_in); | |
226 | ||
227 | /* An exact-match instantiation of an OpenFlow flow. */ | |
228 | struct facet { | |
229 | long long int used; /* Time last used; time created if not used. */ | |
230 | ||
231 | /* These statistics: | |
232 | * | |
233 | * - Do include packets and bytes sent "by hand", e.g. with | |
234 | * dpif_execute(). | |
235 | * | |
236 | * - Do include packets and bytes that were obtained from the datapath | |
907a4c5e | 237 | * when its statistics were reset (e.g. dpif_flow_put() with |
abe529af | 238 | * DPIF_FP_ZERO_STATS). |
abe529af BP |
239 | */ |
240 | uint64_t packet_count; /* Number of packets received. */ | |
241 | uint64_t byte_count; /* Number of bytes received. */ | |
242 | ||
243 | uint64_t dp_packet_count; /* Last known packet count in the datapath. */ | |
244 | uint64_t dp_byte_count; /* Last known byte count in the datapath. */ | |
245 | ||
246 | uint64_t rs_packet_count; /* Packets pushed to resubmit children. */ | |
247 | uint64_t rs_byte_count; /* Bytes pushed to resubmit children. */ | |
248 | long long int rs_used; /* Used time pushed to resubmit children. */ | |
249 | ||
907a4c5e | 250 | uint64_t accounted_bytes; /* Bytes processed by facet_account(). */ |
abe529af BP |
251 | |
252 | struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */ | |
253 | struct list list_node; /* In owning rule's 'facets' list. */ | |
254 | struct rule_dpif *rule; /* Owning rule. */ | |
255 | struct flow flow; /* Exact-match flow. */ | |
256 | bool installed; /* Installed in datapath? */ | |
257 | bool may_install; /* True ordinarily; false if actions must | |
258 | * be reassessed for every packet. */ | |
75a75043 BP |
259 | bool has_learn; /* Actions include NXAST_LEARN? */ |
260 | bool has_normal; /* Actions output to OFPP_NORMAL? */ | |
abe529af BP |
261 | size_t actions_len; /* Number of bytes in actions[]. */ |
262 | struct nlattr *actions; /* Datapath actions. */ | |
263 | tag_type tags; /* Tags. */ | |
264 | struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */ | |
265 | }; | |
266 | ||
f3827897 | 267 | static struct facet *facet_create(struct rule_dpif *, const struct flow *); |
abe529af BP |
268 | static void facet_remove(struct ofproto_dpif *, struct facet *); |
269 | static void facet_free(struct facet *); | |
270 | ||
271 | static struct facet *facet_find(struct ofproto_dpif *, const struct flow *); | |
272 | static struct facet *facet_lookup_valid(struct ofproto_dpif *, | |
273 | const struct flow *); | |
274 | static bool facet_revalidate(struct ofproto_dpif *, struct facet *); | |
275 | ||
3d9e05f8 BP |
276 | static bool execute_controller_action(struct ofproto_dpif *, |
277 | const struct flow *, | |
278 | const struct nlattr *odp_actions, | |
279 | size_t actions_len, | |
280 | struct ofpbuf *packet); | |
abe529af BP |
281 | static void facet_execute(struct ofproto_dpif *, struct facet *, |
282 | struct ofpbuf *packet); | |
283 | ||
284 | static int facet_put__(struct ofproto_dpif *, struct facet *, | |
285 | const struct nlattr *actions, size_t actions_len, | |
286 | struct dpif_flow_stats *); | |
287 | static void facet_install(struct ofproto_dpif *, struct facet *, | |
288 | bool zero_stats); | |
289 | static void facet_uninstall(struct ofproto_dpif *, struct facet *); | |
290 | static void facet_flush_stats(struct ofproto_dpif *, struct facet *); | |
291 | ||
292 | static void facet_make_actions(struct ofproto_dpif *, struct facet *, | |
293 | const struct ofpbuf *packet); | |
294 | static void facet_update_time(struct ofproto_dpif *, struct facet *, | |
295 | long long int used); | |
296 | static void facet_update_stats(struct ofproto_dpif *, struct facet *, | |
297 | const struct dpif_flow_stats *); | |
bbb5d219 | 298 | static void facet_reset_counters(struct facet *); |
3a88e544 | 299 | static void facet_reset_dp_stats(struct facet *, struct dpif_flow_stats *); |
abe529af | 300 | static void facet_push_stats(struct facet *); |
55af77bb | 301 | static void facet_account(struct ofproto_dpif *, struct facet *); |
abe529af BP |
302 | |
303 | static bool facet_is_controller_flow(struct facet *); | |
304 | ||
305 | static void flow_push_stats(const struct rule_dpif *, | |
306 | struct flow *, uint64_t packets, uint64_t bytes, | |
307 | long long int used); | |
308 | ||
54a9cbc9 BP |
309 | static uint32_t rule_calculate_tag(const struct flow *, |
310 | const struct flow_wildcards *, | |
311 | uint32_t basis); | |
312 | static void rule_invalidate(const struct rule_dpif *); | |
313 | ||
abe529af BP |
314 | struct ofport_dpif { |
315 | struct ofport up; | |
316 | ||
317 | uint32_t odp_port; | |
318 | struct ofbundle *bundle; /* Bundle that contains this port, if any. */ | |
319 | struct list bundle_node; /* In struct ofbundle's "ports" list. */ | |
320 | struct cfm *cfm; /* Connectivity Fault Management, if any. */ | |
321 | tag_type tag; /* Tag associated with this port. */ | |
00794817 | 322 | uint32_t bond_stable_id; /* stable_id to use as bond slave, or 0. */ |
015e08bc | 323 | bool may_enable; /* May be enabled in bonds. */ |
21f7563c JP |
324 | |
325 | struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */ | |
326 | enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */ | |
327 | long long int stp_state_entered; | |
8b36f51e EJ |
328 | |
329 | struct hmap priorities; /* Map of attached 'priority_to_dscp's. */ | |
330 | }; | |
331 | ||
332 | /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from | |
333 | * 'priority' (the datapath's term for QoS queue) to the dscp bits which all | |
334 | * traffic egressing the 'ofport' with that priority should be marked with. */ | |
335 | struct priority_to_dscp { | |
336 | struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */ | |
337 | uint32_t priority; /* Priority of this queue (see struct flow). */ | |
338 | ||
339 | uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */ | |
abe529af BP |
340 | }; |
341 | ||
342 | static struct ofport_dpif * | |
343 | ofport_dpif_cast(const struct ofport *ofport) | |
344 | { | |
345 | assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class); | |
346 | return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL; | |
347 | } | |
348 | ||
349 | static void port_run(struct ofport_dpif *); | |
350 | static void port_wait(struct ofport_dpif *); | |
a5610457 | 351 | static int set_cfm(struct ofport *, const struct cfm_settings *); |
8b36f51e | 352 | static void ofport_clear_priorities(struct ofport_dpif *); |
abe529af | 353 | |
7ee20df1 BP |
354 | struct dpif_completion { |
355 | struct list list_node; | |
356 | struct ofoperation *op; | |
357 | }; | |
358 | ||
54a9cbc9 BP |
359 | /* Extra information about a classifier table. |
360 | * Currently used just for optimized flow revalidation. */ | |
361 | struct table_dpif { | |
362 | /* If either of these is nonnull, then this table has a form that allows | |
363 | * flows to be tagged to avoid revalidating most flows for the most common | |
364 | * kinds of flow table changes. */ | |
365 | struct cls_table *catchall_table; /* Table that wildcards all fields. */ | |
366 | struct cls_table *other_table; /* Table with any other wildcard set. */ | |
367 | uint32_t basis; /* Keeps each table's tags separate. */ | |
368 | }; | |
369 | ||
abe529af BP |
370 | struct ofproto_dpif { |
371 | struct ofproto up; | |
372 | struct dpif *dpif; | |
373 | int max_ports; | |
374 | ||
6c1491fb BP |
375 | /* Statistics. */ |
376 | uint64_t n_matches; | |
377 | ||
abe529af BP |
378 | /* Bridging. */ |
379 | struct netflow *netflow; | |
bae473fe | 380 | struct dpif_sflow *sflow; |
abe529af BP |
381 | struct hmap bundles; /* Contains "struct ofbundle"s. */ |
382 | struct mac_learning *ml; | |
383 | struct ofmirror *mirrors[MAX_MIRRORS]; | |
384 | bool has_bonded_bundles; | |
385 | ||
386 | /* Expiration. */ | |
387 | struct timer next_expiration; | |
388 | ||
389 | /* Facets. */ | |
390 | struct hmap facets; | |
54a9cbc9 BP |
391 | |
392 | /* Revalidation. */ | |
393 | struct table_dpif tables[N_TABLES]; | |
abe529af BP |
394 | bool need_revalidate; |
395 | struct tag_set revalidate_set; | |
7ee20df1 BP |
396 | |
397 | /* Support for debugging async flow mods. */ | |
398 | struct list completions; | |
daff3353 EJ |
399 | |
400 | bool has_bundle_action; /* True when the first bundle action appears. */ | |
21f7563c JP |
401 | |
402 | /* Spanning tree. */ | |
403 | struct stp *stp; | |
404 | long long int stp_last_tick; | |
abe529af BP |
405 | }; |
406 | ||
7ee20df1 BP |
407 | /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only |
408 | * for debugging the asynchronous flow_mod implementation.) */ | |
409 | static bool clogged; | |
410 | ||
abe529af BP |
411 | static void ofproto_dpif_unixctl_init(void); |
412 | ||
413 | static struct ofproto_dpif * | |
414 | ofproto_dpif_cast(const struct ofproto *ofproto) | |
415 | { | |
416 | assert(ofproto->ofproto_class == &ofproto_dpif_class); | |
417 | return CONTAINER_OF(ofproto, struct ofproto_dpif, up); | |
418 | } | |
419 | ||
420 | static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *, | |
421 | uint16_t ofp_port); | |
422 | static struct ofport_dpif *get_odp_port(struct ofproto_dpif *, | |
423 | uint32_t odp_port); | |
424 | ||
425 | /* Packet processing. */ | |
426 | static void update_learning_table(struct ofproto_dpif *, | |
427 | const struct flow *, int vlan, | |
428 | struct ofbundle *); | |
501f8d1f BP |
429 | /* Upcalls. */ |
430 | #define FLOW_MISS_MAX_BATCH 50 | |
395e68ce | 431 | |
abe529af | 432 | static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *); |
501f8d1f BP |
433 | static void handle_miss_upcalls(struct ofproto_dpif *, |
434 | struct dpif_upcall *, size_t n); | |
abe529af BP |
435 | |
436 | /* Flow expiration. */ | |
437 | static int expire(struct ofproto_dpif *); | |
438 | ||
439 | /* Utilities. */ | |
b2fda3ef | 440 | static int send_packet(struct ofproto_dpif *, uint32_t odp_port, |
abe529af | 441 | const struct ofpbuf *packet); |
6ff686f2 PS |
442 | static size_t |
443 | compose_sflow_action(const struct ofproto_dpif *, struct ofpbuf *odp_actions, | |
444 | const struct flow *, uint32_t odp_port); | |
abe529af BP |
445 | /* Global variables. */ |
446 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
447 | \f | |
448 | /* Factory functions. */ | |
449 | ||
450 | static void | |
451 | enumerate_types(struct sset *types) | |
452 | { | |
453 | dp_enumerate_types(types); | |
454 | } | |
455 | ||
456 | static int | |
457 | enumerate_names(const char *type, struct sset *names) | |
458 | { | |
459 | return dp_enumerate_names(type, names); | |
460 | } | |
461 | ||
462 | static int | |
463 | del(const char *type, const char *name) | |
464 | { | |
465 | struct dpif *dpif; | |
466 | int error; | |
467 | ||
468 | error = dpif_open(name, type, &dpif); | |
469 | if (!error) { | |
470 | error = dpif_delete(dpif); | |
471 | dpif_close(dpif); | |
472 | } | |
473 | return error; | |
474 | } | |
475 | \f | |
476 | /* Basic life-cycle. */ | |
477 | ||
478 | static struct ofproto * | |
479 | alloc(void) | |
480 | { | |
481 | struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto); | |
482 | return &ofproto->up; | |
483 | } | |
484 | ||
485 | static void | |
486 | dealloc(struct ofproto *ofproto_) | |
487 | { | |
488 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
489 | free(ofproto); | |
490 | } | |
491 | ||
492 | static int | |
073e2a6f | 493 | construct(struct ofproto *ofproto_, int *n_tablesp) |
abe529af BP |
494 | { |
495 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
496 | const char *name = ofproto->up.name; | |
497 | int error; | |
498 | int i; | |
499 | ||
500 | error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif); | |
501 | if (error) { | |
502 | VLOG_ERR("failed to open datapath %s: %s", name, strerror(error)); | |
503 | return error; | |
504 | } | |
505 | ||
506 | ofproto->max_ports = dpif_get_max_ports(ofproto->dpif); | |
6c1491fb | 507 | ofproto->n_matches = 0; |
abe529af | 508 | |
be8194bb JG |
509 | dpif_flow_flush(ofproto->dpif); |
510 | dpif_recv_purge(ofproto->dpif); | |
511 | ||
abe529af BP |
512 | error = dpif_recv_set_mask(ofproto->dpif, |
513 | ((1u << DPIF_UC_MISS) | | |
6ff686f2 | 514 | (1u << DPIF_UC_ACTION))); |
abe529af BP |
515 | if (error) { |
516 | VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error)); | |
517 | dpif_close(ofproto->dpif); | |
518 | return error; | |
519 | } | |
abe529af BP |
520 | |
521 | ofproto->netflow = NULL; | |
522 | ofproto->sflow = NULL; | |
21f7563c | 523 | ofproto->stp = NULL; |
abe529af BP |
524 | hmap_init(&ofproto->bundles); |
525 | ofproto->ml = mac_learning_create(); | |
526 | for (i = 0; i < MAX_MIRRORS; i++) { | |
527 | ofproto->mirrors[i] = NULL; | |
528 | } | |
529 | ofproto->has_bonded_bundles = false; | |
530 | ||
531 | timer_set_duration(&ofproto->next_expiration, 1000); | |
532 | ||
533 | hmap_init(&ofproto->facets); | |
54a9cbc9 BP |
534 | |
535 | for (i = 0; i < N_TABLES; i++) { | |
536 | struct table_dpif *table = &ofproto->tables[i]; | |
537 | ||
538 | table->catchall_table = NULL; | |
539 | table->other_table = NULL; | |
540 | table->basis = random_uint32(); | |
541 | } | |
abe529af BP |
542 | ofproto->need_revalidate = false; |
543 | tag_set_init(&ofproto->revalidate_set); | |
544 | ||
7ee20df1 BP |
545 | list_init(&ofproto->completions); |
546 | ||
abe529af BP |
547 | ofproto_dpif_unixctl_init(); |
548 | ||
daff3353 EJ |
549 | ofproto->has_bundle_action = false; |
550 | ||
9cdaaebe | 551 | *n_tablesp = N_TABLES; |
abe529af BP |
552 | return 0; |
553 | } | |
554 | ||
7ee20df1 BP |
555 | static void |
556 | complete_operations(struct ofproto_dpif *ofproto) | |
557 | { | |
558 | struct dpif_completion *c, *next; | |
559 | ||
560 | LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) { | |
561 | ofoperation_complete(c->op, 0); | |
562 | list_remove(&c->list_node); | |
563 | free(c); | |
564 | } | |
565 | } | |
566 | ||
abe529af BP |
567 | static void |
568 | destruct(struct ofproto *ofproto_) | |
569 | { | |
570 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
7ee20df1 | 571 | struct rule_dpif *rule, *next_rule; |
0697b5c3 | 572 | struct classifier *table; |
abe529af BP |
573 | int i; |
574 | ||
7ee20df1 BP |
575 | complete_operations(ofproto); |
576 | ||
0697b5c3 BP |
577 | OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) { |
578 | struct cls_cursor cursor; | |
579 | ||
580 | cls_cursor_init(&cursor, table, NULL); | |
581 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) { | |
582 | ofproto_rule_destroy(&rule->up); | |
583 | } | |
7ee20df1 BP |
584 | } |
585 | ||
abe529af BP |
586 | for (i = 0; i < MAX_MIRRORS; i++) { |
587 | mirror_destroy(ofproto->mirrors[i]); | |
588 | } | |
589 | ||
590 | netflow_destroy(ofproto->netflow); | |
bae473fe | 591 | dpif_sflow_destroy(ofproto->sflow); |
abe529af BP |
592 | hmap_destroy(&ofproto->bundles); |
593 | mac_learning_destroy(ofproto->ml); | |
594 | ||
595 | hmap_destroy(&ofproto->facets); | |
596 | ||
597 | dpif_close(ofproto->dpif); | |
598 | } | |
599 | ||
600 | static int | |
601 | run(struct ofproto *ofproto_) | |
602 | { | |
603 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
501f8d1f | 604 | struct dpif_upcall misses[FLOW_MISS_MAX_BATCH]; |
abe529af BP |
605 | struct ofport_dpif *ofport; |
606 | struct ofbundle *bundle; | |
501f8d1f | 607 | size_t n_misses; |
abe529af BP |
608 | int i; |
609 | ||
7ee20df1 BP |
610 | if (!clogged) { |
611 | complete_operations(ofproto); | |
612 | } | |
abe529af BP |
613 | dpif_run(ofproto->dpif); |
614 | ||
501f8d1f BP |
615 | n_misses = 0; |
616 | for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) { | |
617 | struct dpif_upcall *upcall = &misses[n_misses]; | |
abe529af BP |
618 | int error; |
619 | ||
501f8d1f | 620 | error = dpif_recv(ofproto->dpif, upcall); |
abe529af | 621 | if (error) { |
501f8d1f | 622 | if (error == ENODEV && n_misses == 0) { |
abe529af BP |
623 | return error; |
624 | } | |
625 | break; | |
626 | } | |
627 | ||
501f8d1f BP |
628 | if (upcall->type == DPIF_UC_MISS) { |
629 | /* Handle it later. */ | |
630 | n_misses++; | |
631 | } else { | |
632 | handle_upcall(ofproto, upcall); | |
633 | } | |
abe529af BP |
634 | } |
635 | ||
501f8d1f BP |
636 | handle_miss_upcalls(ofproto, misses, n_misses); |
637 | ||
abe529af BP |
638 | if (timer_expired(&ofproto->next_expiration)) { |
639 | int delay = expire(ofproto); | |
640 | timer_set_duration(&ofproto->next_expiration, delay); | |
641 | } | |
642 | ||
643 | if (ofproto->netflow) { | |
644 | netflow_run(ofproto->netflow); | |
645 | } | |
646 | if (ofproto->sflow) { | |
bae473fe | 647 | dpif_sflow_run(ofproto->sflow); |
abe529af BP |
648 | } |
649 | ||
650 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
651 | port_run(ofport); | |
652 | } | |
653 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
654 | bundle_run(bundle); | |
655 | } | |
656 | ||
21f7563c | 657 | stp_run(ofproto); |
1c313b88 BP |
658 | mac_learning_run(ofproto->ml, &ofproto->revalidate_set); |
659 | ||
abe529af BP |
660 | /* Now revalidate if there's anything to do. */ |
661 | if (ofproto->need_revalidate | |
662 | || !tag_set_is_empty(&ofproto->revalidate_set)) { | |
663 | struct tag_set revalidate_set = ofproto->revalidate_set; | |
664 | bool revalidate_all = ofproto->need_revalidate; | |
665 | struct facet *facet, *next; | |
666 | ||
667 | /* Clear the revalidation flags. */ | |
668 | tag_set_init(&ofproto->revalidate_set); | |
669 | ofproto->need_revalidate = false; | |
670 | ||
671 | HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) { | |
672 | if (revalidate_all | |
673 | || tag_set_intersects(&revalidate_set, facet->tags)) { | |
674 | facet_revalidate(ofproto, facet); | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
682 | static void | |
683 | wait(struct ofproto *ofproto_) | |
684 | { | |
685 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
686 | struct ofport_dpif *ofport; | |
687 | struct ofbundle *bundle; | |
688 | ||
7ee20df1 BP |
689 | if (!clogged && !list_is_empty(&ofproto->completions)) { |
690 | poll_immediate_wake(); | |
691 | } | |
692 | ||
abe529af BP |
693 | dpif_wait(ofproto->dpif); |
694 | dpif_recv_wait(ofproto->dpif); | |
695 | if (ofproto->sflow) { | |
bae473fe | 696 | dpif_sflow_wait(ofproto->sflow); |
abe529af BP |
697 | } |
698 | if (!tag_set_is_empty(&ofproto->revalidate_set)) { | |
699 | poll_immediate_wake(); | |
700 | } | |
701 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { | |
702 | port_wait(ofport); | |
703 | } | |
704 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
705 | bundle_wait(bundle); | |
706 | } | |
1c313b88 | 707 | mac_learning_wait(ofproto->ml); |
21f7563c | 708 | stp_wait(ofproto); |
abe529af BP |
709 | if (ofproto->need_revalidate) { |
710 | /* Shouldn't happen, but if it does just go around again. */ | |
711 | VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()"); | |
712 | poll_immediate_wake(); | |
713 | } else { | |
714 | timer_wait(&ofproto->next_expiration); | |
715 | } | |
716 | } | |
717 | ||
718 | static void | |
719 | flush(struct ofproto *ofproto_) | |
720 | { | |
721 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
722 | struct facet *facet, *next_facet; | |
723 | ||
724 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
725 | /* Mark the facet as not installed so that facet_remove() doesn't | |
726 | * bother trying to uninstall it. There is no point in uninstalling it | |
727 | * individually since we are about to blow away all the facets with | |
728 | * dpif_flow_flush(). */ | |
729 | facet->installed = false; | |
730 | facet->dp_packet_count = 0; | |
731 | facet->dp_byte_count = 0; | |
732 | facet_remove(ofproto, facet); | |
733 | } | |
734 | dpif_flow_flush(ofproto->dpif); | |
735 | } | |
736 | ||
6c1491fb BP |
737 | static void |
738 | get_features(struct ofproto *ofproto_ OVS_UNUSED, | |
739 | bool *arp_match_ip, uint32_t *actions) | |
740 | { | |
741 | *arp_match_ip = true; | |
742 | *actions = ((1u << OFPAT_OUTPUT) | | |
743 | (1u << OFPAT_SET_VLAN_VID) | | |
744 | (1u << OFPAT_SET_VLAN_PCP) | | |
745 | (1u << OFPAT_STRIP_VLAN) | | |
746 | (1u << OFPAT_SET_DL_SRC) | | |
747 | (1u << OFPAT_SET_DL_DST) | | |
748 | (1u << OFPAT_SET_NW_SRC) | | |
749 | (1u << OFPAT_SET_NW_DST) | | |
750 | (1u << OFPAT_SET_NW_TOS) | | |
751 | (1u << OFPAT_SET_TP_SRC) | | |
752 | (1u << OFPAT_SET_TP_DST) | | |
753 | (1u << OFPAT_ENQUEUE)); | |
754 | } | |
755 | ||
756 | static void | |
757 | get_tables(struct ofproto *ofproto_, struct ofp_table_stats *ots) | |
758 | { | |
759 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
a8d9304d | 760 | struct dpif_dp_stats s; |
6c1491fb BP |
761 | |
762 | strcpy(ots->name, "classifier"); | |
763 | ||
764 | dpif_get_dp_stats(ofproto->dpif, &s); | |
765 | put_32aligned_be64(&ots->lookup_count, htonll(s.n_hit + s.n_missed)); | |
766 | put_32aligned_be64(&ots->matched_count, | |
767 | htonll(s.n_hit + ofproto->n_matches)); | |
768 | } | |
769 | ||
abe529af BP |
770 | static int |
771 | set_netflow(struct ofproto *ofproto_, | |
772 | const struct netflow_options *netflow_options) | |
773 | { | |
774 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
775 | ||
776 | if (netflow_options) { | |
777 | if (!ofproto->netflow) { | |
778 | ofproto->netflow = netflow_create(); | |
779 | } | |
780 | return netflow_set_options(ofproto->netflow, netflow_options); | |
781 | } else { | |
782 | netflow_destroy(ofproto->netflow); | |
783 | ofproto->netflow = NULL; | |
784 | return 0; | |
785 | } | |
786 | } | |
787 | ||
788 | static struct ofport * | |
789 | port_alloc(void) | |
790 | { | |
791 | struct ofport_dpif *port = xmalloc(sizeof *port); | |
792 | return &port->up; | |
793 | } | |
794 | ||
795 | static void | |
796 | port_dealloc(struct ofport *port_) | |
797 | { | |
798 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
799 | free(port); | |
800 | } | |
801 | ||
802 | static int | |
803 | port_construct(struct ofport *port_) | |
804 | { | |
805 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
806 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
807 | ||
f11c28c4 | 808 | ofproto->need_revalidate = true; |
abe529af BP |
809 | port->odp_port = ofp_port_to_odp_port(port->up.ofp_port); |
810 | port->bundle = NULL; | |
811 | port->cfm = NULL; | |
812 | port->tag = tag_create_random(); | |
d5ffa7f2 | 813 | port->may_enable = true; |
21f7563c JP |
814 | port->stp_port = NULL; |
815 | port->stp_state = STP_DISABLED; | |
8b36f51e | 816 | hmap_init(&port->priorities); |
abe529af BP |
817 | |
818 | if (ofproto->sflow) { | |
bae473fe JP |
819 | dpif_sflow_add_port(ofproto->sflow, port->odp_port, |
820 | netdev_get_name(port->up.netdev)); | |
abe529af BP |
821 | } |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
826 | static void | |
827 | port_destruct(struct ofport *port_) | |
828 | { | |
829 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
830 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
831 | ||
f11c28c4 | 832 | ofproto->need_revalidate = true; |
abe529af | 833 | bundle_remove(port_); |
a5610457 | 834 | set_cfm(port_, NULL); |
abe529af | 835 | if (ofproto->sflow) { |
bae473fe | 836 | dpif_sflow_del_port(ofproto->sflow, port->odp_port); |
abe529af | 837 | } |
8b36f51e EJ |
838 | |
839 | ofport_clear_priorities(port); | |
840 | hmap_destroy(&port->priorities); | |
abe529af BP |
841 | } |
842 | ||
843 | static void | |
844 | port_modified(struct ofport *port_) | |
845 | { | |
846 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
847 | ||
848 | if (port->bundle && port->bundle->bond) { | |
849 | bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev); | |
850 | } | |
851 | } | |
852 | ||
853 | static void | |
854 | port_reconfigured(struct ofport *port_, ovs_be32 old_config) | |
855 | { | |
856 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
857 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto); | |
858 | ovs_be32 changed = old_config ^ port->up.opp.config; | |
859 | ||
860 | if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP | | |
861 | OFPPC_NO_FWD | OFPPC_NO_FLOOD)) { | |
862 | ofproto->need_revalidate = true; | |
7bde8dd8 JP |
863 | |
864 | if (changed & htonl(OFPPC_NO_FLOOD) && port->bundle) { | |
865 | bundle_update(port->bundle); | |
866 | } | |
abe529af BP |
867 | } |
868 | } | |
869 | ||
870 | static int | |
871 | set_sflow(struct ofproto *ofproto_, | |
872 | const struct ofproto_sflow_options *sflow_options) | |
873 | { | |
874 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
bae473fe | 875 | struct dpif_sflow *ds = ofproto->sflow; |
6ff686f2 | 876 | |
abe529af | 877 | if (sflow_options) { |
bae473fe | 878 | if (!ds) { |
abe529af BP |
879 | struct ofport_dpif *ofport; |
880 | ||
bae473fe | 881 | ds = ofproto->sflow = dpif_sflow_create(ofproto->dpif); |
abe529af | 882 | HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) { |
bae473fe JP |
883 | dpif_sflow_add_port(ds, ofport->odp_port, |
884 | netdev_get_name(ofport->up.netdev)); | |
abe529af | 885 | } |
6ff686f2 | 886 | ofproto->need_revalidate = true; |
abe529af | 887 | } |
bae473fe | 888 | dpif_sflow_set_options(ds, sflow_options); |
abe529af | 889 | } else { |
6ff686f2 PS |
890 | if (ds) { |
891 | dpif_sflow_destroy(ds); | |
892 | ofproto->need_revalidate = true; | |
893 | ofproto->sflow = NULL; | |
894 | } | |
abe529af BP |
895 | } |
896 | return 0; | |
897 | } | |
898 | ||
899 | static int | |
a5610457 | 900 | set_cfm(struct ofport *ofport_, const struct cfm_settings *s) |
abe529af BP |
901 | { |
902 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
903 | int error; | |
904 | ||
a5610457 | 905 | if (!s) { |
abe529af BP |
906 | error = 0; |
907 | } else { | |
908 | if (!ofport->cfm) { | |
8c977421 EJ |
909 | struct ofproto_dpif *ofproto; |
910 | ||
911 | ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
912 | ofproto->need_revalidate = true; | |
6f629657 | 913 | ofport->cfm = cfm_create(netdev_get_name(ofport->up.netdev)); |
abe529af BP |
914 | } |
915 | ||
a5610457 | 916 | if (cfm_configure(ofport->cfm, s)) { |
abe529af BP |
917 | return 0; |
918 | } | |
919 | ||
920 | error = EINVAL; | |
921 | } | |
922 | cfm_destroy(ofport->cfm); | |
923 | ofport->cfm = NULL; | |
924 | return error; | |
925 | } | |
926 | ||
927 | static int | |
a5610457 | 928 | get_cfm_fault(const struct ofport *ofport_) |
abe529af BP |
929 | { |
930 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
a5610457 EJ |
931 | |
932 | return ofport->cfm ? cfm_get_fault(ofport->cfm) : -1; | |
abe529af | 933 | } |
1de11730 EJ |
934 | |
935 | static int | |
936 | get_cfm_remote_mpids(const struct ofport *ofport_, const uint64_t **rmps, | |
937 | size_t *n_rmps) | |
938 | { | |
939 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
940 | ||
941 | if (ofport->cfm) { | |
942 | cfm_get_remote_mpids(ofport->cfm, rmps, n_rmps); | |
943 | return 0; | |
944 | } else { | |
945 | return -1; | |
946 | } | |
947 | } | |
abe529af | 948 | \f |
21f7563c JP |
949 | /* Spanning Tree. */ |
950 | ||
951 | static void | |
952 | send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_) | |
953 | { | |
954 | struct ofproto_dpif *ofproto = ofproto_; | |
955 | struct stp_port *sp = stp_get_port(ofproto->stp, port_num); | |
956 | struct ofport_dpif *ofport; | |
957 | ||
958 | ofport = stp_port_get_aux(sp); | |
959 | if (!ofport) { | |
960 | VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d", | |
961 | ofproto->up.name, port_num); | |
962 | } else { | |
963 | struct eth_header *eth = pkt->l2; | |
964 | ||
965 | netdev_get_etheraddr(ofport->up.netdev, eth->eth_src); | |
966 | if (eth_addr_is_zero(eth->eth_src)) { | |
967 | VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d " | |
968 | "with unknown MAC", ofproto->up.name, port_num); | |
969 | } else { | |
ea131871 JG |
970 | send_packet(ofproto_dpif_cast(ofport->up.ofproto), |
971 | ofport->odp_port, pkt); | |
21f7563c JP |
972 | } |
973 | } | |
974 | ofpbuf_delete(pkt); | |
975 | } | |
976 | ||
977 | /* Configures STP on 'ofproto_' using the settings defined in 's'. */ | |
978 | static int | |
979 | set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s) | |
980 | { | |
981 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
982 | ||
983 | /* Only revalidate flows if the configuration changed. */ | |
984 | if (!s != !ofproto->stp) { | |
985 | ofproto->need_revalidate = true; | |
986 | } | |
987 | ||
988 | if (s) { | |
989 | if (!ofproto->stp) { | |
990 | ofproto->stp = stp_create(ofproto_->name, s->system_id, | |
991 | send_bpdu_cb, ofproto); | |
992 | ofproto->stp_last_tick = time_msec(); | |
993 | } | |
994 | ||
995 | stp_set_bridge_id(ofproto->stp, s->system_id); | |
996 | stp_set_bridge_priority(ofproto->stp, s->priority); | |
997 | stp_set_hello_time(ofproto->stp, s->hello_time); | |
998 | stp_set_max_age(ofproto->stp, s->max_age); | |
999 | stp_set_forward_delay(ofproto->stp, s->fwd_delay); | |
1000 | } else { | |
1001 | stp_destroy(ofproto->stp); | |
1002 | ofproto->stp = NULL; | |
1003 | } | |
1004 | ||
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | static int | |
1009 | get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s) | |
1010 | { | |
1011 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1012 | ||
1013 | if (ofproto->stp) { | |
1014 | s->enabled = true; | |
1015 | s->bridge_id = stp_get_bridge_id(ofproto->stp); | |
1016 | s->designated_root = stp_get_designated_root(ofproto->stp); | |
1017 | s->root_path_cost = stp_get_root_path_cost(ofproto->stp); | |
1018 | } else { | |
1019 | s->enabled = false; | |
1020 | } | |
1021 | ||
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | static void | |
1026 | update_stp_port_state(struct ofport_dpif *ofport) | |
1027 | { | |
1028 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
1029 | enum stp_state state; | |
1030 | ||
1031 | /* Figure out new state. */ | |
1032 | state = ofport->stp_port ? stp_port_get_state(ofport->stp_port) | |
1033 | : STP_DISABLED; | |
1034 | ||
1035 | /* Update state. */ | |
1036 | if (ofport->stp_state != state) { | |
1037 | ovs_be32 of_state; | |
1038 | bool fwd_change; | |
1039 | ||
1040 | VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s", | |
1041 | netdev_get_name(ofport->up.netdev), | |
1042 | stp_state_name(ofport->stp_state), | |
1043 | stp_state_name(state)); | |
1044 | if (stp_learn_in_state(ofport->stp_state) | |
1045 | != stp_learn_in_state(state)) { | |
1046 | /* xxx Learning action flows should also be flushed. */ | |
1047 | mac_learning_flush(ofproto->ml); | |
1048 | } | |
1049 | fwd_change = stp_forward_in_state(ofport->stp_state) | |
1050 | != stp_forward_in_state(state); | |
1051 | ||
1052 | ofproto->need_revalidate = true; | |
1053 | ofport->stp_state = state; | |
1054 | ofport->stp_state_entered = time_msec(); | |
1055 | ||
b308140a | 1056 | if (fwd_change && ofport->bundle) { |
21f7563c JP |
1057 | bundle_update(ofport->bundle); |
1058 | } | |
1059 | ||
1060 | /* Update the STP state bits in the OpenFlow port description. */ | |
1061 | of_state = (ofport->up.opp.state & htonl(~OFPPS_STP_MASK)) | |
1062 | | htonl(state == STP_LISTENING ? OFPPS_STP_LISTEN | |
1063 | : state == STP_LEARNING ? OFPPS_STP_LEARN | |
1064 | : state == STP_FORWARDING ? OFPPS_STP_FORWARD | |
1065 | : state == STP_BLOCKING ? OFPPS_STP_BLOCK | |
1066 | : 0); | |
1067 | ofproto_port_set_state(&ofport->up, of_state); | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | /* Configures STP on 'ofport_' using the settings defined in 's'. The | |
1072 | * caller is responsible for assigning STP port numbers and ensuring | |
1073 | * there are no duplicates. */ | |
1074 | static int | |
1075 | set_stp_port(struct ofport *ofport_, | |
1076 | const struct ofproto_port_stp_settings *s) | |
1077 | { | |
1078 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
1079 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
1080 | struct stp_port *sp = ofport->stp_port; | |
1081 | ||
1082 | if (!s || !s->enable) { | |
1083 | if (sp) { | |
1084 | ofport->stp_port = NULL; | |
1085 | stp_port_disable(sp); | |
ecd12731 | 1086 | update_stp_port_state(ofport); |
21f7563c JP |
1087 | } |
1088 | return 0; | |
1089 | } else if (sp && stp_port_no(sp) != s->port_num | |
1090 | && ofport == stp_port_get_aux(sp)) { | |
1091 | /* The port-id changed, so disable the old one if it's not | |
1092 | * already in use by another port. */ | |
1093 | stp_port_disable(sp); | |
1094 | } | |
1095 | ||
1096 | sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num); | |
1097 | stp_port_enable(sp); | |
1098 | ||
1099 | stp_port_set_aux(sp, ofport); | |
1100 | stp_port_set_priority(sp, s->priority); | |
1101 | stp_port_set_path_cost(sp, s->path_cost); | |
1102 | ||
1103 | update_stp_port_state(ofport); | |
1104 | ||
1105 | return 0; | |
1106 | } | |
1107 | ||
1108 | static int | |
1109 | get_stp_port_status(struct ofport *ofport_, | |
1110 | struct ofproto_port_stp_status *s) | |
1111 | { | |
1112 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
1113 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
1114 | struct stp_port *sp = ofport->stp_port; | |
1115 | ||
1116 | if (!ofproto->stp || !sp) { | |
1117 | s->enabled = false; | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | s->enabled = true; | |
1122 | s->port_id = stp_port_get_id(sp); | |
1123 | s->state = stp_port_get_state(sp); | |
1124 | s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000; | |
1125 | s->role = stp_port_get_role(sp); | |
80740385 | 1126 | stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count); |
21f7563c JP |
1127 | |
1128 | return 0; | |
1129 | } | |
1130 | ||
1131 | static void | |
1132 | stp_run(struct ofproto_dpif *ofproto) | |
1133 | { | |
1134 | if (ofproto->stp) { | |
1135 | long long int now = time_msec(); | |
1136 | long long int elapsed = now - ofproto->stp_last_tick; | |
1137 | struct stp_port *sp; | |
1138 | ||
1139 | if (elapsed > 0) { | |
1140 | stp_tick(ofproto->stp, MIN(INT_MAX, elapsed)); | |
1141 | ofproto->stp_last_tick = now; | |
1142 | } | |
1143 | while (stp_get_changed_port(ofproto->stp, &sp)) { | |
1144 | struct ofport_dpif *ofport = stp_port_get_aux(sp); | |
1145 | ||
1146 | if (ofport) { | |
1147 | update_stp_port_state(ofport); | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | static void | |
1154 | stp_wait(struct ofproto_dpif *ofproto) | |
1155 | { | |
1156 | if (ofproto->stp) { | |
1157 | poll_timer_wait(1000); | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | /* Returns true if STP should process 'flow'. */ | |
1162 | static bool | |
1163 | stp_should_process_flow(const struct flow *flow) | |
1164 | { | |
1165 | return eth_addr_equals(flow->dl_dst, eth_addr_stp); | |
1166 | } | |
1167 | ||
1168 | static void | |
1169 | stp_process_packet(const struct ofport_dpif *ofport, | |
1170 | const struct ofpbuf *packet) | |
1171 | { | |
1172 | struct ofpbuf payload = *packet; | |
1173 | struct eth_header *eth = payload.data; | |
1174 | struct stp_port *sp = ofport->stp_port; | |
1175 | ||
1176 | /* Sink packets on ports that have STP disabled when the bridge has | |
1177 | * STP enabled. */ | |
1178 | if (!sp || stp_port_get_state(sp) == STP_DISABLED) { | |
1179 | return; | |
1180 | } | |
1181 | ||
1182 | /* Trim off padding on payload. */ | |
c573540b BP |
1183 | if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) { |
1184 | payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN; | |
21f7563c JP |
1185 | } |
1186 | ||
1187 | if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) { | |
1188 | stp_received_bpdu(sp, payload.data, payload.size); | |
1189 | } | |
1190 | } | |
1191 | \f | |
8b36f51e EJ |
1192 | static struct priority_to_dscp * |
1193 | get_priority(const struct ofport_dpif *ofport, uint32_t priority) | |
1194 | { | |
1195 | struct priority_to_dscp *pdscp; | |
1196 | uint32_t hash; | |
1197 | ||
1198 | hash = hash_int(priority, 0); | |
1199 | HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) { | |
1200 | if (pdscp->priority == priority) { | |
1201 | return pdscp; | |
1202 | } | |
1203 | } | |
1204 | return NULL; | |
1205 | } | |
1206 | ||
1207 | static void | |
1208 | ofport_clear_priorities(struct ofport_dpif *ofport) | |
1209 | { | |
1210 | struct priority_to_dscp *pdscp, *next; | |
1211 | ||
1212 | HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) { | |
1213 | hmap_remove(&ofport->priorities, &pdscp->hmap_node); | |
1214 | free(pdscp); | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | static int | |
1219 | set_queues(struct ofport *ofport_, | |
1220 | const struct ofproto_port_queue *qdscp_list, | |
1221 | size_t n_qdscp) | |
1222 | { | |
1223 | struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
1224 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
1225 | struct hmap new = HMAP_INITIALIZER(&new); | |
1226 | size_t i; | |
1227 | ||
1228 | for (i = 0; i < n_qdscp; i++) { | |
1229 | struct priority_to_dscp *pdscp; | |
1230 | uint32_t priority; | |
1231 | uint8_t dscp; | |
1232 | ||
1233 | dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK; | |
1234 | if (dpif_queue_to_priority(ofproto->dpif, qdscp_list[i].queue, | |
1235 | &priority)) { | |
1236 | continue; | |
1237 | } | |
1238 | ||
1239 | pdscp = get_priority(ofport, priority); | |
1240 | if (pdscp) { | |
1241 | hmap_remove(&ofport->priorities, &pdscp->hmap_node); | |
1242 | } else { | |
1243 | pdscp = xmalloc(sizeof *pdscp); | |
1244 | pdscp->priority = priority; | |
1245 | pdscp->dscp = dscp; | |
1246 | ofproto->need_revalidate = true; | |
1247 | } | |
1248 | ||
1249 | if (pdscp->dscp != dscp) { | |
1250 | pdscp->dscp = dscp; | |
1251 | ofproto->need_revalidate = true; | |
1252 | } | |
1253 | ||
1254 | hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0)); | |
1255 | } | |
1256 | ||
1257 | if (!hmap_is_empty(&ofport->priorities)) { | |
1258 | ofport_clear_priorities(ofport); | |
1259 | ofproto->need_revalidate = true; | |
1260 | } | |
1261 | ||
1262 | hmap_swap(&new, &ofport->priorities); | |
1263 | hmap_destroy(&new); | |
1264 | ||
1265 | return 0; | |
1266 | } | |
1267 | \f | |
abe529af BP |
1268 | /* Bundles. */ |
1269 | ||
1270 | /* Expires all MAC learning entries associated with 'port' and forces ofproto | |
1271 | * to revalidate every flow. */ | |
1272 | static void | |
1273 | bundle_flush_macs(struct ofbundle *bundle) | |
1274 | { | |
1275 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
1276 | struct mac_learning *ml = ofproto->ml; | |
1277 | struct mac_entry *mac, *next_mac; | |
1278 | ||
1279 | ofproto->need_revalidate = true; | |
1280 | LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) { | |
1281 | if (mac->port.p == bundle) { | |
1282 | mac_learning_expire(ml, mac); | |
1283 | } | |
1284 | } | |
1285 | } | |
1286 | ||
1287 | static struct ofbundle * | |
1288 | bundle_lookup(const struct ofproto_dpif *ofproto, void *aux) | |
1289 | { | |
1290 | struct ofbundle *bundle; | |
1291 | ||
1292 | HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0), | |
1293 | &ofproto->bundles) { | |
1294 | if (bundle->aux == aux) { | |
1295 | return bundle; | |
1296 | } | |
1297 | } | |
1298 | return NULL; | |
1299 | } | |
1300 | ||
1301 | /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the | |
1302 | * ones that are found to 'bundles'. */ | |
1303 | static void | |
1304 | bundle_lookup_multiple(struct ofproto_dpif *ofproto, | |
1305 | void **auxes, size_t n_auxes, | |
1306 | struct hmapx *bundles) | |
1307 | { | |
1308 | size_t i; | |
1309 | ||
1310 | hmapx_init(bundles); | |
1311 | for (i = 0; i < n_auxes; i++) { | |
1312 | struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]); | |
1313 | if (bundle) { | |
1314 | hmapx_add(bundles, bundle); | |
1315 | } | |
1316 | } | |
1317 | } | |
1318 | ||
7bde8dd8 JP |
1319 | static void |
1320 | bundle_update(struct ofbundle *bundle) | |
1321 | { | |
1322 | struct ofport_dpif *port; | |
1323 | ||
1324 | bundle->floodable = true; | |
1325 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
5e48dc2b | 1326 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { |
7bde8dd8 JP |
1327 | bundle->floodable = false; |
1328 | break; | |
1329 | } | |
1330 | } | |
1331 | } | |
1332 | ||
abe529af BP |
1333 | static void |
1334 | bundle_del_port(struct ofport_dpif *port) | |
1335 | { | |
1336 | struct ofbundle *bundle = port->bundle; | |
1337 | ||
6f77f4ae BP |
1338 | bundle->ofproto->need_revalidate = true; |
1339 | ||
abe529af BP |
1340 | list_remove(&port->bundle_node); |
1341 | port->bundle = NULL; | |
1342 | ||
1343 | if (bundle->lacp) { | |
1344 | lacp_slave_unregister(bundle->lacp, port); | |
1345 | } | |
1346 | if (bundle->bond) { | |
1347 | bond_slave_unregister(bundle->bond, port); | |
1348 | } | |
1349 | ||
7bde8dd8 | 1350 | bundle_update(bundle); |
abe529af BP |
1351 | } |
1352 | ||
1353 | static bool | |
1354 | bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port, | |
00794817 BP |
1355 | struct lacp_slave_settings *lacp, |
1356 | uint32_t bond_stable_id) | |
abe529af BP |
1357 | { |
1358 | struct ofport_dpif *port; | |
1359 | ||
1360 | port = get_ofp_port(bundle->ofproto, ofp_port); | |
1361 | if (!port) { | |
1362 | return false; | |
1363 | } | |
1364 | ||
1365 | if (port->bundle != bundle) { | |
6f77f4ae | 1366 | bundle->ofproto->need_revalidate = true; |
abe529af BP |
1367 | if (port->bundle) { |
1368 | bundle_del_port(port); | |
1369 | } | |
1370 | ||
1371 | port->bundle = bundle; | |
1372 | list_push_back(&bundle->ports, &port->bundle_node); | |
5e48dc2b | 1373 | if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) { |
abe529af BP |
1374 | bundle->floodable = false; |
1375 | } | |
1376 | } | |
1377 | if (lacp) { | |
4a86aece | 1378 | port->bundle->ofproto->need_revalidate = true; |
abe529af BP |
1379 | lacp_slave_register(bundle->lacp, port, lacp); |
1380 | } | |
1381 | ||
00794817 BP |
1382 | port->bond_stable_id = bond_stable_id; |
1383 | ||
abe529af BP |
1384 | return true; |
1385 | } | |
1386 | ||
1387 | static void | |
1388 | bundle_destroy(struct ofbundle *bundle) | |
1389 | { | |
1390 | struct ofproto_dpif *ofproto; | |
1391 | struct ofport_dpif *port, *next_port; | |
1392 | int i; | |
1393 | ||
1394 | if (!bundle) { | |
1395 | return; | |
1396 | } | |
1397 | ||
1398 | ofproto = bundle->ofproto; | |
1399 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1400 | struct ofmirror *m = ofproto->mirrors[i]; | |
1401 | if (m) { | |
1402 | if (m->out == bundle) { | |
1403 | mirror_destroy(m); | |
1404 | } else if (hmapx_find_and_delete(&m->srcs, bundle) | |
1405 | || hmapx_find_and_delete(&m->dsts, bundle)) { | |
1406 | ofproto->need_revalidate = true; | |
1407 | } | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
1412 | bundle_del_port(port); | |
1413 | } | |
1414 | ||
1415 | bundle_flush_macs(bundle); | |
1416 | hmap_remove(&ofproto->bundles, &bundle->hmap_node); | |
1417 | free(bundle->name); | |
1418 | free(bundle->trunks); | |
1419 | lacp_destroy(bundle->lacp); | |
1420 | bond_destroy(bundle->bond); | |
1421 | free(bundle); | |
1422 | } | |
1423 | ||
1424 | static int | |
1425 | bundle_set(struct ofproto *ofproto_, void *aux, | |
1426 | const struct ofproto_bundle_settings *s) | |
1427 | { | |
1428 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1429 | bool need_flush = false; | |
abe529af BP |
1430 | struct ofport_dpif *port; |
1431 | struct ofbundle *bundle; | |
ecac4ebf BP |
1432 | unsigned long *trunks; |
1433 | int vlan; | |
abe529af BP |
1434 | size_t i; |
1435 | bool ok; | |
1436 | ||
1437 | if (!s) { | |
1438 | bundle_destroy(bundle_lookup(ofproto, aux)); | |
1439 | return 0; | |
1440 | } | |
1441 | ||
1442 | assert(s->n_slaves == 1 || s->bond != NULL); | |
1443 | assert((s->lacp != NULL) == (s->lacp_slaves != NULL)); | |
1444 | ||
1445 | bundle = bundle_lookup(ofproto, aux); | |
1446 | if (!bundle) { | |
1447 | bundle = xmalloc(sizeof *bundle); | |
1448 | ||
1449 | bundle->ofproto = ofproto; | |
1450 | hmap_insert(&ofproto->bundles, &bundle->hmap_node, | |
1451 | hash_pointer(aux, 0)); | |
1452 | bundle->aux = aux; | |
1453 | bundle->name = NULL; | |
1454 | ||
1455 | list_init(&bundle->ports); | |
ecac4ebf | 1456 | bundle->vlan_mode = PORT_VLAN_TRUNK; |
abe529af BP |
1457 | bundle->vlan = -1; |
1458 | bundle->trunks = NULL; | |
5e9ceccd | 1459 | bundle->use_priority_tags = s->use_priority_tags; |
abe529af BP |
1460 | bundle->lacp = NULL; |
1461 | bundle->bond = NULL; | |
1462 | ||
1463 | bundle->floodable = true; | |
1464 | ||
1465 | bundle->src_mirrors = 0; | |
1466 | bundle->dst_mirrors = 0; | |
1467 | bundle->mirror_out = 0; | |
1468 | } | |
1469 | ||
1470 | if (!bundle->name || strcmp(s->name, bundle->name)) { | |
1471 | free(bundle->name); | |
1472 | bundle->name = xstrdup(s->name); | |
1473 | } | |
1474 | ||
1475 | /* LACP. */ | |
1476 | if (s->lacp) { | |
1477 | if (!bundle->lacp) { | |
8c977421 | 1478 | ofproto->need_revalidate = true; |
abe529af BP |
1479 | bundle->lacp = lacp_create(); |
1480 | } | |
1481 | lacp_configure(bundle->lacp, s->lacp); | |
1482 | } else { | |
1483 | lacp_destroy(bundle->lacp); | |
1484 | bundle->lacp = NULL; | |
1485 | } | |
1486 | ||
1487 | /* Update set of ports. */ | |
1488 | ok = true; | |
1489 | for (i = 0; i < s->n_slaves; i++) { | |
1490 | if (!bundle_add_port(bundle, s->slaves[i], | |
00794817 BP |
1491 | s->lacp ? &s->lacp_slaves[i] : NULL, |
1492 | s->bond_stable_ids ? s->bond_stable_ids[i] : 0)) { | |
abe529af BP |
1493 | ok = false; |
1494 | } | |
1495 | } | |
1496 | if (!ok || list_size(&bundle->ports) != s->n_slaves) { | |
1497 | struct ofport_dpif *next_port; | |
1498 | ||
1499 | LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) { | |
1500 | for (i = 0; i < s->n_slaves; i++) { | |
56c769ab | 1501 | if (s->slaves[i] == port->up.ofp_port) { |
abe529af BP |
1502 | goto found; |
1503 | } | |
1504 | } | |
1505 | ||
1506 | bundle_del_port(port); | |
1507 | found: ; | |
1508 | } | |
1509 | } | |
1510 | assert(list_size(&bundle->ports) <= s->n_slaves); | |
1511 | ||
1512 | if (list_is_empty(&bundle->ports)) { | |
1513 | bundle_destroy(bundle); | |
1514 | return EINVAL; | |
1515 | } | |
1516 | ||
ecac4ebf | 1517 | /* Set VLAN tagging mode */ |
5e9ceccd BP |
1518 | if (s->vlan_mode != bundle->vlan_mode |
1519 | || s->use_priority_tags != bundle->use_priority_tags) { | |
ecac4ebf | 1520 | bundle->vlan_mode = s->vlan_mode; |
5e9ceccd | 1521 | bundle->use_priority_tags = s->use_priority_tags; |
ecac4ebf BP |
1522 | need_flush = true; |
1523 | } | |
1524 | ||
abe529af | 1525 | /* Set VLAN tag. */ |
ecac4ebf BP |
1526 | vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1 |
1527 | : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan | |
1528 | : 0); | |
1529 | if (vlan != bundle->vlan) { | |
1530 | bundle->vlan = vlan; | |
abe529af BP |
1531 | need_flush = true; |
1532 | } | |
1533 | ||
1534 | /* Get trunked VLANs. */ | |
ecac4ebf BP |
1535 | switch (s->vlan_mode) { |
1536 | case PORT_VLAN_ACCESS: | |
1537 | trunks = NULL; | |
1538 | break; | |
1539 | ||
1540 | case PORT_VLAN_TRUNK: | |
1541 | trunks = (unsigned long *) s->trunks; | |
1542 | break; | |
1543 | ||
1544 | case PORT_VLAN_NATIVE_UNTAGGED: | |
1545 | case PORT_VLAN_NATIVE_TAGGED: | |
1546 | if (vlan != 0 && (!s->trunks | |
1547 | || !bitmap_is_set(s->trunks, vlan) | |
1548 | || bitmap_is_set(s->trunks, 0))) { | |
1549 | /* Force trunking the native VLAN and prohibit trunking VLAN 0. */ | |
1550 | if (s->trunks) { | |
1551 | trunks = bitmap_clone(s->trunks, 4096); | |
1552 | } else { | |
1553 | trunks = bitmap_allocate1(4096); | |
1554 | } | |
1555 | bitmap_set1(trunks, vlan); | |
1556 | bitmap_set0(trunks, 0); | |
1557 | } else { | |
1558 | trunks = (unsigned long *) s->trunks; | |
1559 | } | |
1560 | break; | |
1561 | ||
1562 | default: | |
1563 | NOT_REACHED(); | |
1564 | } | |
abe529af BP |
1565 | if (!vlan_bitmap_equal(trunks, bundle->trunks)) { |
1566 | free(bundle->trunks); | |
ecac4ebf BP |
1567 | if (trunks == s->trunks) { |
1568 | bundle->trunks = vlan_bitmap_clone(trunks); | |
1569 | } else { | |
1570 | bundle->trunks = trunks; | |
1571 | trunks = NULL; | |
1572 | } | |
abe529af BP |
1573 | need_flush = true; |
1574 | } | |
ecac4ebf BP |
1575 | if (trunks != s->trunks) { |
1576 | free(trunks); | |
1577 | } | |
abe529af BP |
1578 | |
1579 | /* Bonding. */ | |
1580 | if (!list_is_short(&bundle->ports)) { | |
1581 | bundle->ofproto->has_bonded_bundles = true; | |
1582 | if (bundle->bond) { | |
1583 | if (bond_reconfigure(bundle->bond, s->bond)) { | |
1584 | ofproto->need_revalidate = true; | |
1585 | } | |
1586 | } else { | |
1587 | bundle->bond = bond_create(s->bond); | |
6f77f4ae | 1588 | ofproto->need_revalidate = true; |
abe529af BP |
1589 | } |
1590 | ||
1591 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
00794817 | 1592 | bond_slave_register(bundle->bond, port, port->bond_stable_id, |
abe529af BP |
1593 | port->up.netdev); |
1594 | } | |
1595 | } else { | |
1596 | bond_destroy(bundle->bond); | |
1597 | bundle->bond = NULL; | |
1598 | } | |
1599 | ||
1600 | /* If we changed something that would affect MAC learning, un-learn | |
1601 | * everything on this port and force flow revalidation. */ | |
1602 | if (need_flush) { | |
1603 | bundle_flush_macs(bundle); | |
1604 | } | |
1605 | ||
1606 | return 0; | |
1607 | } | |
1608 | ||
1609 | static void | |
1610 | bundle_remove(struct ofport *port_) | |
1611 | { | |
1612 | struct ofport_dpif *port = ofport_dpif_cast(port_); | |
1613 | struct ofbundle *bundle = port->bundle; | |
1614 | ||
1615 | if (bundle) { | |
1616 | bundle_del_port(port); | |
1617 | if (list_is_empty(&bundle->ports)) { | |
1618 | bundle_destroy(bundle); | |
1619 | } else if (list_is_short(&bundle->ports)) { | |
1620 | bond_destroy(bundle->bond); | |
1621 | bundle->bond = NULL; | |
1622 | } | |
1623 | } | |
1624 | } | |
1625 | ||
1626 | static void | |
5f877369 | 1627 | send_pdu_cb(void *port_, const void *pdu, size_t pdu_size) |
abe529af BP |
1628 | { |
1629 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10); | |
1630 | struct ofport_dpif *port = port_; | |
1631 | uint8_t ea[ETH_ADDR_LEN]; | |
1632 | int error; | |
1633 | ||
1634 | error = netdev_get_etheraddr(port->up.netdev, ea); | |
1635 | if (!error) { | |
abe529af | 1636 | struct ofpbuf packet; |
5f877369 | 1637 | void *packet_pdu; |
abe529af BP |
1638 | |
1639 | ofpbuf_init(&packet, 0); | |
1640 | packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP, | |
5f877369 EJ |
1641 | pdu_size); |
1642 | memcpy(packet_pdu, pdu, pdu_size); | |
1643 | ||
ea131871 JG |
1644 | send_packet(ofproto_dpif_cast(port->up.ofproto), port->odp_port, |
1645 | &packet); | |
abe529af BP |
1646 | ofpbuf_uninit(&packet); |
1647 | } else { | |
1648 | VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface " | |
1649 | "%s (%s)", port->bundle->name, | |
1650 | netdev_get_name(port->up.netdev), strerror(error)); | |
1651 | } | |
1652 | } | |
1653 | ||
1654 | static void | |
1655 | bundle_send_learning_packets(struct ofbundle *bundle) | |
1656 | { | |
1657 | struct ofproto_dpif *ofproto = bundle->ofproto; | |
1658 | int error, n_packets, n_errors; | |
1659 | struct mac_entry *e; | |
1660 | ||
1661 | error = n_packets = n_errors = 0; | |
1662 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
1663 | if (e->port.p != bundle) { | |
ea131871 JG |
1664 | struct ofpbuf *learning_packet; |
1665 | struct ofport_dpif *port; | |
1666 | int ret; | |
1667 | ||
1668 | learning_packet = bond_compose_learning_packet(bundle->bond, e->mac, | |
1669 | e->vlan, | |
1670 | (void **)&port); | |
1671 | ret = send_packet(ofproto_dpif_cast(port->up.ofproto), | |
1672 | port->odp_port, learning_packet); | |
1673 | ofpbuf_delete(learning_packet); | |
abe529af BP |
1674 | if (ret) { |
1675 | error = ret; | |
1676 | n_errors++; | |
1677 | } | |
1678 | n_packets++; | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | if (n_errors) { | |
1683 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1684 | VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning " | |
1685 | "packets, last error was: %s", | |
1686 | bundle->name, n_errors, n_packets, strerror(error)); | |
1687 | } else { | |
1688 | VLOG_DBG("bond %s: sent %d gratuitous learning packets", | |
1689 | bundle->name, n_packets); | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | static void | |
1694 | bundle_run(struct ofbundle *bundle) | |
1695 | { | |
1696 | if (bundle->lacp) { | |
1697 | lacp_run(bundle->lacp, send_pdu_cb); | |
1698 | } | |
1699 | if (bundle->bond) { | |
1700 | struct ofport_dpif *port; | |
1701 | ||
1702 | LIST_FOR_EACH (port, bundle_node, &bundle->ports) { | |
015e08bc | 1703 | bond_slave_set_may_enable(bundle->bond, port, port->may_enable); |
abe529af BP |
1704 | } |
1705 | ||
1706 | bond_run(bundle->bond, &bundle->ofproto->revalidate_set, | |
1707 | lacp_negotiated(bundle->lacp)); | |
1708 | if (bond_should_send_learning_packets(bundle->bond)) { | |
1709 | bundle_send_learning_packets(bundle); | |
1710 | } | |
1711 | } | |
1712 | } | |
1713 | ||
1714 | static void | |
1715 | bundle_wait(struct ofbundle *bundle) | |
1716 | { | |
1717 | if (bundle->lacp) { | |
1718 | lacp_wait(bundle->lacp); | |
1719 | } | |
1720 | if (bundle->bond) { | |
1721 | bond_wait(bundle->bond); | |
1722 | } | |
1723 | } | |
1724 | \f | |
1725 | /* Mirrors. */ | |
1726 | ||
1727 | static int | |
1728 | mirror_scan(struct ofproto_dpif *ofproto) | |
1729 | { | |
1730 | int idx; | |
1731 | ||
1732 | for (idx = 0; idx < MAX_MIRRORS; idx++) { | |
1733 | if (!ofproto->mirrors[idx]) { | |
1734 | return idx; | |
1735 | } | |
1736 | } | |
1737 | return -1; | |
1738 | } | |
1739 | ||
1740 | static struct ofmirror * | |
1741 | mirror_lookup(struct ofproto_dpif *ofproto, void *aux) | |
1742 | { | |
1743 | int i; | |
1744 | ||
1745 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1746 | struct ofmirror *mirror = ofproto->mirrors[i]; | |
1747 | if (mirror && mirror->aux == aux) { | |
1748 | return mirror; | |
1749 | } | |
1750 | } | |
1751 | ||
1752 | return NULL; | |
1753 | } | |
1754 | ||
9ba15e2a BP |
1755 | /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */ |
1756 | static void | |
1757 | mirror_update_dups(struct ofproto_dpif *ofproto) | |
1758 | { | |
1759 | int i; | |
1760 | ||
1761 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1762 | struct ofmirror *m = ofproto->mirrors[i]; | |
1763 | ||
1764 | if (m) { | |
1765 | m->dup_mirrors = MIRROR_MASK_C(1) << i; | |
1766 | } | |
1767 | } | |
1768 | ||
1769 | for (i = 0; i < MAX_MIRRORS; i++) { | |
1770 | struct ofmirror *m1 = ofproto->mirrors[i]; | |
1771 | int j; | |
1772 | ||
1773 | if (!m1) { | |
1774 | continue; | |
1775 | } | |
1776 | ||
1777 | for (j = i + 1; j < MAX_MIRRORS; j++) { | |
1778 | struct ofmirror *m2 = ofproto->mirrors[j]; | |
1779 | ||
edb0540b | 1780 | if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) { |
9ba15e2a BP |
1781 | m1->dup_mirrors |= MIRROR_MASK_C(1) << j; |
1782 | m2->dup_mirrors |= m1->dup_mirrors; | |
1783 | } | |
1784 | } | |
1785 | } | |
1786 | } | |
1787 | ||
abe529af BP |
1788 | static int |
1789 | mirror_set(struct ofproto *ofproto_, void *aux, | |
1790 | const struct ofproto_mirror_settings *s) | |
1791 | { | |
1792 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1793 | mirror_mask_t mirror_bit; | |
1794 | struct ofbundle *bundle; | |
1795 | struct ofmirror *mirror; | |
1796 | struct ofbundle *out; | |
1797 | struct hmapx srcs; /* Contains "struct ofbundle *"s. */ | |
1798 | struct hmapx dsts; /* Contains "struct ofbundle *"s. */ | |
1799 | int out_vlan; | |
1800 | ||
1801 | mirror = mirror_lookup(ofproto, aux); | |
1802 | if (!s) { | |
1803 | mirror_destroy(mirror); | |
1804 | return 0; | |
1805 | } | |
1806 | if (!mirror) { | |
1807 | int idx; | |
1808 | ||
1809 | idx = mirror_scan(ofproto); | |
1810 | if (idx < 0) { | |
1811 | VLOG_WARN("bridge %s: maximum of %d port mirrors reached, " | |
1812 | "cannot create %s", | |
1813 | ofproto->up.name, MAX_MIRRORS, s->name); | |
1814 | return EFBIG; | |
1815 | } | |
1816 | ||
1817 | mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror); | |
1818 | mirror->ofproto = ofproto; | |
1819 | mirror->idx = idx; | |
8b28d864 | 1820 | mirror->aux = aux; |
abe529af BP |
1821 | mirror->out_vlan = -1; |
1822 | mirror->name = NULL; | |
1823 | } | |
1824 | ||
1825 | if (!mirror->name || strcmp(s->name, mirror->name)) { | |
1826 | free(mirror->name); | |
1827 | mirror->name = xstrdup(s->name); | |
1828 | } | |
1829 | ||
1830 | /* Get the new configuration. */ | |
1831 | if (s->out_bundle) { | |
1832 | out = bundle_lookup(ofproto, s->out_bundle); | |
1833 | if (!out) { | |
1834 | mirror_destroy(mirror); | |
1835 | return EINVAL; | |
1836 | } | |
1837 | out_vlan = -1; | |
1838 | } else { | |
1839 | out = NULL; | |
1840 | out_vlan = s->out_vlan; | |
1841 | } | |
1842 | bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs); | |
1843 | bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts); | |
1844 | ||
1845 | /* If the configuration has not changed, do nothing. */ | |
1846 | if (hmapx_equals(&srcs, &mirror->srcs) | |
1847 | && hmapx_equals(&dsts, &mirror->dsts) | |
1848 | && vlan_bitmap_equal(mirror->vlans, s->src_vlans) | |
1849 | && mirror->out == out | |
1850 | && mirror->out_vlan == out_vlan) | |
1851 | { | |
1852 | hmapx_destroy(&srcs); | |
1853 | hmapx_destroy(&dsts); | |
1854 | return 0; | |
1855 | } | |
1856 | ||
1857 | hmapx_swap(&srcs, &mirror->srcs); | |
1858 | hmapx_destroy(&srcs); | |
1859 | ||
1860 | hmapx_swap(&dsts, &mirror->dsts); | |
1861 | hmapx_destroy(&dsts); | |
1862 | ||
1863 | free(mirror->vlans); | |
1864 | mirror->vlans = vlan_bitmap_clone(s->src_vlans); | |
1865 | ||
1866 | mirror->out = out; | |
1867 | mirror->out_vlan = out_vlan; | |
1868 | ||
1869 | /* Update bundles. */ | |
1870 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1871 | HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) { | |
1872 | if (hmapx_contains(&mirror->srcs, bundle)) { | |
1873 | bundle->src_mirrors |= mirror_bit; | |
1874 | } else { | |
1875 | bundle->src_mirrors &= ~mirror_bit; | |
1876 | } | |
1877 | ||
1878 | if (hmapx_contains(&mirror->dsts, bundle)) { | |
1879 | bundle->dst_mirrors |= mirror_bit; | |
1880 | } else { | |
1881 | bundle->dst_mirrors &= ~mirror_bit; | |
1882 | } | |
1883 | ||
1884 | if (mirror->out == bundle) { | |
1885 | bundle->mirror_out |= mirror_bit; | |
1886 | } else { | |
1887 | bundle->mirror_out &= ~mirror_bit; | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | ofproto->need_revalidate = true; | |
1892 | mac_learning_flush(ofproto->ml); | |
9ba15e2a | 1893 | mirror_update_dups(ofproto); |
abe529af BP |
1894 | |
1895 | return 0; | |
1896 | } | |
1897 | ||
1898 | static void | |
1899 | mirror_destroy(struct ofmirror *mirror) | |
1900 | { | |
1901 | struct ofproto_dpif *ofproto; | |
1902 | mirror_mask_t mirror_bit; | |
1903 | struct ofbundle *bundle; | |
1904 | ||
1905 | if (!mirror) { | |
1906 | return; | |
1907 | } | |
1908 | ||
1909 | ofproto = mirror->ofproto; | |
1910 | ofproto->need_revalidate = true; | |
1911 | mac_learning_flush(ofproto->ml); | |
1912 | ||
1913 | mirror_bit = MIRROR_MASK_C(1) << mirror->idx; | |
1914 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
1915 | bundle->src_mirrors &= ~mirror_bit; | |
1916 | bundle->dst_mirrors &= ~mirror_bit; | |
1917 | bundle->mirror_out &= ~mirror_bit; | |
1918 | } | |
1919 | ||
1920 | hmapx_destroy(&mirror->srcs); | |
1921 | hmapx_destroy(&mirror->dsts); | |
1922 | free(mirror->vlans); | |
1923 | ||
1924 | ofproto->mirrors[mirror->idx] = NULL; | |
1925 | free(mirror->name); | |
1926 | free(mirror); | |
9ba15e2a BP |
1927 | |
1928 | mirror_update_dups(ofproto); | |
abe529af BP |
1929 | } |
1930 | ||
1931 | static int | |
1932 | set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans) | |
1933 | { | |
1934 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1935 | if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) { | |
1936 | ofproto->need_revalidate = true; | |
1937 | mac_learning_flush(ofproto->ml); | |
1938 | } | |
1939 | return 0; | |
1940 | } | |
1941 | ||
1942 | static bool | |
b4affc74 | 1943 | is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux) |
abe529af BP |
1944 | { |
1945 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1946 | struct ofbundle *bundle = bundle_lookup(ofproto, aux); | |
1947 | return bundle && bundle->mirror_out != 0; | |
1948 | } | |
8402c74b SS |
1949 | |
1950 | static void | |
b53055f4 | 1951 | forward_bpdu_changed(struct ofproto *ofproto_) |
8402c74b SS |
1952 | { |
1953 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
1954 | /* Revalidate cached flows whenever forward_bpdu option changes. */ | |
1955 | ofproto->need_revalidate = true; | |
1956 | } | |
abe529af BP |
1957 | \f |
1958 | /* Ports. */ | |
1959 | ||
1960 | static struct ofport_dpif * | |
1961 | get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port) | |
1962 | { | |
7df6a8bd BP |
1963 | struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port); |
1964 | return ofport ? ofport_dpif_cast(ofport) : NULL; | |
abe529af BP |
1965 | } |
1966 | ||
1967 | static struct ofport_dpif * | |
1968 | get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port) | |
1969 | { | |
1970 | return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port)); | |
1971 | } | |
1972 | ||
1973 | static void | |
1974 | ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port, | |
1975 | struct dpif_port *dpif_port) | |
1976 | { | |
1977 | ofproto_port->name = dpif_port->name; | |
1978 | ofproto_port->type = dpif_port->type; | |
1979 | ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no); | |
1980 | } | |
1981 | ||
1982 | static void | |
1983 | port_run(struct ofport_dpif *ofport) | |
1984 | { | |
015e08bc EJ |
1985 | bool enable = netdev_get_carrier(ofport->up.netdev); |
1986 | ||
abe529af BP |
1987 | if (ofport->cfm) { |
1988 | cfm_run(ofport->cfm); | |
1989 | ||
1990 | if (cfm_should_send_ccm(ofport->cfm)) { | |
1991 | struct ofpbuf packet; | |
abe529af BP |
1992 | |
1993 | ofpbuf_init(&packet, 0); | |
c0a2e71d | 1994 | cfm_compose_ccm(ofport->cfm, &packet, ofport->up.opp.hw_addr); |
abe529af | 1995 | send_packet(ofproto_dpif_cast(ofport->up.ofproto), |
b2fda3ef | 1996 | ofport->odp_port, &packet); |
abe529af BP |
1997 | ofpbuf_uninit(&packet); |
1998 | } | |
015e08bc | 1999 | |
86dc6501 EJ |
2000 | enable = enable && !cfm_get_fault(ofport->cfm) |
2001 | && cfm_get_opup(ofport->cfm); | |
abe529af | 2002 | } |
015e08bc EJ |
2003 | |
2004 | if (ofport->bundle) { | |
2005 | enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport); | |
2006 | } | |
2007 | ||
daff3353 EJ |
2008 | if (ofport->may_enable != enable) { |
2009 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto); | |
2010 | ||
2011 | if (ofproto->has_bundle_action) { | |
2012 | ofproto->need_revalidate = true; | |
2013 | } | |
2014 | } | |
2015 | ||
015e08bc | 2016 | ofport->may_enable = enable; |
abe529af BP |
2017 | } |
2018 | ||
2019 | static void | |
2020 | port_wait(struct ofport_dpif *ofport) | |
2021 | { | |
2022 | if (ofport->cfm) { | |
2023 | cfm_wait(ofport->cfm); | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | static int | |
2028 | port_query_by_name(const struct ofproto *ofproto_, const char *devname, | |
2029 | struct ofproto_port *ofproto_port) | |
2030 | { | |
2031 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2032 | struct dpif_port dpif_port; | |
2033 | int error; | |
2034 | ||
2035 | error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port); | |
2036 | if (!error) { | |
2037 | ofproto_port_from_dpif_port(ofproto_port, &dpif_port); | |
2038 | } | |
2039 | return error; | |
2040 | } | |
2041 | ||
2042 | static int | |
2043 | port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp) | |
2044 | { | |
2045 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2046 | uint16_t odp_port; | |
2047 | int error; | |
2048 | ||
2049 | error = dpif_port_add(ofproto->dpif, netdev, &odp_port); | |
2050 | if (!error) { | |
2051 | *ofp_portp = odp_port_to_ofp_port(odp_port); | |
2052 | } | |
2053 | return error; | |
2054 | } | |
2055 | ||
2056 | static int | |
2057 | port_del(struct ofproto *ofproto_, uint16_t ofp_port) | |
2058 | { | |
2059 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2060 | int error; | |
2061 | ||
2062 | error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port)); | |
2063 | if (!error) { | |
2064 | struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port); | |
2065 | if (ofport) { | |
2066 | /* The caller is going to close ofport->up.netdev. If this is a | |
2067 | * bonded port, then the bond is using that netdev, so remove it | |
2068 | * from the bond. The client will need to reconfigure everything | |
2069 | * after deleting ports, so then the slave will get re-added. */ | |
2070 | bundle_remove(&ofport->up); | |
2071 | } | |
2072 | } | |
2073 | return error; | |
2074 | } | |
2075 | ||
2076 | struct port_dump_state { | |
2077 | struct dpif_port_dump dump; | |
2078 | bool done; | |
2079 | }; | |
2080 | ||
2081 | static int | |
2082 | port_dump_start(const struct ofproto *ofproto_, void **statep) | |
2083 | { | |
2084 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2085 | struct port_dump_state *state; | |
2086 | ||
2087 | *statep = state = xmalloc(sizeof *state); | |
2088 | dpif_port_dump_start(&state->dump, ofproto->dpif); | |
2089 | state->done = false; | |
2090 | return 0; | |
2091 | } | |
2092 | ||
2093 | static int | |
2094 | port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_, | |
2095 | struct ofproto_port *port) | |
2096 | { | |
2097 | struct port_dump_state *state = state_; | |
2098 | struct dpif_port dpif_port; | |
2099 | ||
2100 | if (dpif_port_dump_next(&state->dump, &dpif_port)) { | |
2101 | ofproto_port_from_dpif_port(port, &dpif_port); | |
2102 | return 0; | |
2103 | } else { | |
2104 | int error = dpif_port_dump_done(&state->dump); | |
2105 | state->done = true; | |
2106 | return error ? error : EOF; | |
2107 | } | |
2108 | } | |
2109 | ||
2110 | static int | |
2111 | port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_) | |
2112 | { | |
2113 | struct port_dump_state *state = state_; | |
2114 | ||
2115 | if (!state->done) { | |
2116 | dpif_port_dump_done(&state->dump); | |
2117 | } | |
2118 | free(state); | |
2119 | return 0; | |
2120 | } | |
2121 | ||
2122 | static int | |
2123 | port_poll(const struct ofproto *ofproto_, char **devnamep) | |
2124 | { | |
2125 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2126 | return dpif_port_poll(ofproto->dpif, devnamep); | |
2127 | } | |
2128 | ||
2129 | static void | |
2130 | port_poll_wait(const struct ofproto *ofproto_) | |
2131 | { | |
2132 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
2133 | dpif_port_poll_wait(ofproto->dpif); | |
2134 | } | |
2135 | ||
2136 | static int | |
2137 | port_is_lacp_current(const struct ofport *ofport_) | |
2138 | { | |
2139 | const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_); | |
2140 | return (ofport->bundle && ofport->bundle->lacp | |
2141 | ? lacp_slave_is_current(ofport->bundle->lacp, ofport) | |
2142 | : -1); | |
2143 | } | |
2144 | \f | |
2145 | /* Upcall handling. */ | |
2146 | ||
501f8d1f BP |
2147 | /* Flow miss batching. |
2148 | * | |
2149 | * Some dpifs implement operations faster when you hand them off in a batch. | |
2150 | * To allow batching, "struct flow_miss" queues the dpif-related work needed | |
2151 | * for a given flow. Each "struct flow_miss" corresponds to sending one or | |
2152 | * more packets, plus possibly installing the flow in the dpif. | |
2153 | * | |
2154 | * So far we only batch the operations that affect flow setup time the most. | |
2155 | * It's possible to batch more than that, but the benefit might be minimal. */ | |
2156 | struct flow_miss { | |
2157 | struct hmap_node hmap_node; | |
2158 | struct flow flow; | |
2159 | const struct nlattr *key; | |
2160 | size_t key_len; | |
2161 | struct list packets; | |
2162 | }; | |
2163 | ||
2164 | struct flow_miss_op { | |
2165 | union dpif_op dpif_op; | |
2166 | struct facet *facet; | |
2167 | }; | |
2168 | ||
62cd7072 BP |
2169 | /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each |
2170 | * OpenFlow controller as necessary according to their individual | |
2171 | * configurations. | |
2172 | * | |
2173 | * If 'clone' is true, the caller retains ownership of 'packet'. Otherwise, | |
2174 | * ownership is transferred to this function. */ | |
2175 | static void | |
2176 | send_packet_in_miss(struct ofproto_dpif *ofproto, struct ofpbuf *packet, | |
2177 | const struct flow *flow, bool clone) | |
2178 | { | |
2179 | struct ofputil_packet_in pin; | |
2180 | ||
2181 | pin.packet = packet; | |
2182 | pin.in_port = flow->in_port; | |
2183 | pin.reason = OFPR_NO_MATCH; | |
2184 | pin.buffer_id = 0; /* not yet known */ | |
2185 | pin.send_len = 0; /* not used for flow table misses */ | |
2186 | connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow, | |
2187 | clone ? NULL : packet); | |
2188 | } | |
2189 | ||
2190 | /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_ACTION to each | |
2191 | * OpenFlow controller as necessary according to their individual | |
2192 | * configurations. | |
2193 | * | |
2194 | * 'send_len' should be the number of bytes of 'packet' to send to the | |
2195 | * controller, as specified in the action that caused the packet to be sent. | |
abe529af BP |
2196 | * |
2197 | * If 'clone' is true, the caller retains ownership of 'upcall->packet'. | |
2198 | * Otherwise, ownership is transferred to this function. */ | |
2199 | static void | |
62cd7072 BP |
2200 | send_packet_in_action(struct ofproto_dpif *ofproto, struct ofpbuf *packet, |
2201 | uint64_t userdata, const struct flow *flow, bool clone) | |
abe529af BP |
2202 | { |
2203 | struct ofputil_packet_in pin; | |
6ff686f2 | 2204 | struct user_action_cookie cookie; |
abe529af | 2205 | |
62cd7072 BP |
2206 | memcpy(&cookie, &userdata, sizeof(cookie)); |
2207 | ||
2208 | pin.packet = packet; | |
abe529af | 2209 | pin.in_port = flow->in_port; |
62cd7072 | 2210 | pin.reason = OFPR_ACTION; |
abe529af | 2211 | pin.buffer_id = 0; /* not yet known */ |
6ff686f2 | 2212 | pin.send_len = cookie.data; |
78bd1cd0 | 2213 | connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow, |
62cd7072 | 2214 | clone ? NULL : packet); |
abe529af BP |
2215 | } |
2216 | ||
2217 | static bool | |
2218 | process_special(struct ofproto_dpif *ofproto, const struct flow *flow, | |
2219 | const struct ofpbuf *packet) | |
2220 | { | |
b6e001b6 EJ |
2221 | struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port); |
2222 | ||
2223 | if (!ofport) { | |
2224 | return false; | |
2225 | } | |
2226 | ||
ef9819b5 | 2227 | if (ofport->cfm && cfm_should_process_flow(ofport->cfm, flow)) { |
b6e001b6 | 2228 | if (packet) { |
abe529af BP |
2229 | cfm_process_heartbeat(ofport->cfm, packet); |
2230 | } | |
2231 | return true; | |
b6e001b6 EJ |
2232 | } else if (ofport->bundle && ofport->bundle->lacp |
2233 | && flow->dl_type == htons(ETH_TYPE_LACP)) { | |
2234 | if (packet) { | |
2235 | lacp_process_packet(ofport->bundle->lacp, ofport, packet); | |
abe529af | 2236 | } |
da37ebac | 2237 | return true; |
21f7563c JP |
2238 | } else if (ofproto->stp && stp_should_process_flow(flow)) { |
2239 | if (packet) { | |
2240 | stp_process_packet(ofport, packet); | |
2241 | } | |
2242 | return true; | |
abe529af BP |
2243 | } |
2244 | return false; | |
2245 | } | |
2246 | ||
501f8d1f BP |
2247 | static struct flow_miss * |
2248 | flow_miss_create(struct hmap *todo, const struct flow *flow, | |
2249 | const struct nlattr *key, size_t key_len) | |
abe529af | 2250 | { |
501f8d1f BP |
2251 | uint32_t hash = flow_hash(flow, 0); |
2252 | struct flow_miss *miss; | |
abe529af | 2253 | |
501f8d1f BP |
2254 | HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) { |
2255 | if (flow_equal(&miss->flow, flow)) { | |
2256 | return miss; | |
2257 | } | |
2258 | } | |
abe529af | 2259 | |
501f8d1f BP |
2260 | miss = xmalloc(sizeof *miss); |
2261 | hmap_insert(todo, &miss->hmap_node, hash); | |
2262 | miss->flow = *flow; | |
2263 | miss->key = key; | |
2264 | miss->key_len = key_len; | |
2265 | list_init(&miss->packets); | |
2266 | return miss; | |
2267 | } | |
abe529af | 2268 | |
501f8d1f BP |
2269 | static void |
2270 | handle_flow_miss(struct ofproto_dpif *ofproto, struct flow_miss *miss, | |
2271 | struct flow_miss_op *ops, size_t *n_ops) | |
2272 | { | |
2273 | const struct flow *flow = &miss->flow; | |
2274 | struct ofpbuf *packet, *next_packet; | |
2275 | struct facet *facet; | |
abe529af | 2276 | |
501f8d1f | 2277 | facet = facet_lookup_valid(ofproto, flow); |
abe529af | 2278 | if (!facet) { |
501f8d1f BP |
2279 | struct rule_dpif *rule; |
2280 | ||
2281 | rule = rule_dpif_lookup(ofproto, flow, 0); | |
abe529af BP |
2282 | if (!rule) { |
2283 | /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */ | |
501f8d1f | 2284 | struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port); |
abe529af BP |
2285 | if (port) { |
2286 | if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) { | |
2287 | COVERAGE_INC(ofproto_dpif_no_packet_in); | |
2288 | /* XXX install 'drop' flow entry */ | |
abe529af BP |
2289 | return; |
2290 | } | |
2291 | } else { | |
2292 | VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, | |
501f8d1f BP |
2293 | flow->in_port); |
2294 | } | |
2295 | ||
2296 | LIST_FOR_EACH_SAFE (packet, next_packet, list_node, | |
2297 | &miss->packets) { | |
2298 | list_remove(&packet->list_node); | |
2299 | send_packet_in_miss(ofproto, packet, flow, false); | |
abe529af BP |
2300 | } |
2301 | ||
abe529af BP |
2302 | return; |
2303 | } | |
2304 | ||
501f8d1f | 2305 | facet = facet_create(rule, flow); |
abe529af BP |
2306 | } |
2307 | ||
501f8d1f BP |
2308 | LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) { |
2309 | list_remove(&packet->list_node); | |
2310 | ofproto->n_matches++; | |
2311 | ||
2312 | if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) { | |
2313 | /* | |
2314 | * Extra-special case for fail-open mode. | |
2315 | * | |
2316 | * We are in fail-open mode and the packet matched the fail-open | |
2317 | * rule, but we are connected to a controller too. We should send | |
2318 | * the packet up to the controller in the hope that it will try to | |
2319 | * set up a flow and thereby allow us to exit fail-open. | |
2320 | * | |
2321 | * See the top-level comment in fail-open.c for more information. | |
2322 | */ | |
2323 | send_packet_in_miss(ofproto, packet, flow, true); | |
2324 | } | |
2325 | ||
2326 | if (!facet->may_install) { | |
2327 | facet_make_actions(ofproto, facet, packet); | |
2328 | } | |
2329 | if (!execute_controller_action(ofproto, &facet->flow, | |
2330 | facet->actions, facet->actions_len, | |
2331 | packet)) { | |
2332 | struct flow_miss_op *op = &ops[(*n_ops)++]; | |
2333 | struct dpif_execute *execute = &op->dpif_op.execute; | |
2334 | ||
2335 | op->facet = facet; | |
2336 | execute->type = DPIF_OP_EXECUTE; | |
2337 | execute->key = miss->key; | |
2338 | execute->key_len = miss->key_len; | |
2339 | execute->actions | |
2340 | = (facet->may_install | |
2341 | ? facet->actions | |
2342 | : xmemdup(facet->actions, facet->actions_len)); | |
2343 | execute->actions_len = facet->actions_len; | |
2344 | execute->packet = packet; | |
2345 | } | |
2346 | } | |
2347 | ||
2348 | if (facet->may_install) { | |
2349 | struct flow_miss_op *op = &ops[(*n_ops)++]; | |
2350 | struct dpif_flow_put *put = &op->dpif_op.flow_put; | |
2351 | ||
2352 | op->facet = facet; | |
2353 | put->type = DPIF_OP_FLOW_PUT; | |
2354 | put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY; | |
2355 | put->key = miss->key; | |
2356 | put->key_len = miss->key_len; | |
2357 | put->actions = facet->actions; | |
2358 | put->actions_len = facet->actions_len; | |
2359 | put->stats = NULL; | |
2360 | } | |
2361 | } | |
2362 | ||
2363 | static void | |
2364 | handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls, | |
2365 | size_t n_upcalls) | |
2366 | { | |
2367 | struct dpif_upcall *upcall; | |
2368 | struct flow_miss *miss, *next_miss; | |
2369 | struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2]; | |
2370 | union dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2]; | |
2371 | struct hmap todo; | |
2372 | size_t n_ops; | |
2373 | size_t i; | |
2374 | ||
2375 | if (!n_upcalls) { | |
2376 | return; | |
2377 | } | |
2378 | ||
2379 | /* Construct the to-do list. | |
2380 | * | |
2381 | * This just amounts to extracting the flow from each packet and sticking | |
2382 | * the packets that have the same flow in the same "flow_miss" structure so | |
2383 | * that we can process them together. */ | |
2384 | hmap_init(&todo); | |
2385 | for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) { | |
2386 | struct flow_miss *miss; | |
2387 | struct flow flow; | |
2388 | ||
2389 | /* Obtain in_port and tun_id, at least, then set 'flow''s header | |
2390 | * pointers. */ | |
2391 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
abff858b PS |
2392 | flow_extract(upcall->packet, flow.priority, flow.tun_id, |
2393 | flow.in_port, &flow); | |
501f8d1f | 2394 | |
21f7563c | 2395 | /* Handle 802.1ag, LACP, and STP specially. */ |
501f8d1f BP |
2396 | if (process_special(ofproto, &flow, upcall->packet)) { |
2397 | ofpbuf_delete(upcall->packet); | |
2398 | ofproto->n_matches++; | |
2399 | continue; | |
2400 | } | |
2401 | ||
2402 | /* Add other packets to a to-do list. */ | |
2403 | miss = flow_miss_create(&todo, &flow, upcall->key, upcall->key_len); | |
2404 | list_push_back(&miss->packets, &upcall->packet->list_node); | |
2405 | } | |
2406 | ||
2407 | /* Process each element in the to-do list, constructing the set of | |
2408 | * operations to batch. */ | |
2409 | n_ops = 0; | |
2410 | HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &todo) { | |
2411 | handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops); | |
2412 | ofpbuf_list_delete(&miss->packets); | |
2413 | hmap_remove(&todo, &miss->hmap_node); | |
2414 | free(miss); | |
abe529af | 2415 | } |
501f8d1f BP |
2416 | assert(n_ops <= ARRAY_SIZE(flow_miss_ops)); |
2417 | hmap_destroy(&todo); | |
2418 | ||
2419 | /* Execute batch. */ | |
2420 | for (i = 0; i < n_ops; i++) { | |
2421 | dpif_ops[i] = &flow_miss_ops[i].dpif_op; | |
2422 | } | |
2423 | dpif_operate(ofproto->dpif, dpif_ops, n_ops); | |
2424 | ||
2425 | /* Free memory and update facets. */ | |
2426 | for (i = 0; i < n_ops; i++) { | |
2427 | struct flow_miss_op *op = &flow_miss_ops[i]; | |
2428 | struct dpif_execute *execute; | |
2429 | struct dpif_flow_put *put; | |
2430 | ||
2431 | switch (op->dpif_op.type) { | |
2432 | case DPIF_OP_EXECUTE: | |
2433 | execute = &op->dpif_op.execute; | |
2434 | if (op->facet->actions != execute->actions) { | |
2435 | free((struct nlattr *) execute->actions); | |
2436 | } | |
2437 | ofpbuf_delete((struct ofpbuf *) execute->packet); | |
2438 | break; | |
abe529af | 2439 | |
501f8d1f BP |
2440 | case DPIF_OP_FLOW_PUT: |
2441 | put = &op->dpif_op.flow_put; | |
2442 | if (!put->error) { | |
2443 | op->facet->installed = true; | |
2444 | } | |
2445 | break; | |
2446 | } | |
2447 | } | |
abe529af BP |
2448 | } |
2449 | ||
2450 | static void | |
6ff686f2 PS |
2451 | handle_userspace_upcall(struct ofproto_dpif *ofproto, |
2452 | struct dpif_upcall *upcall) | |
abe529af BP |
2453 | { |
2454 | struct flow flow; | |
6ff686f2 | 2455 | struct user_action_cookie cookie; |
abe529af | 2456 | |
6ff686f2 | 2457 | memcpy(&cookie, &upcall->userdata, sizeof(cookie)); |
abe529af | 2458 | |
6ff686f2 | 2459 | if (cookie.type == USER_ACTION_COOKIE_SFLOW) { |
abe529af BP |
2460 | if (ofproto->sflow) { |
2461 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
6ff686f2 | 2462 | dpif_sflow_received(ofproto->sflow, upcall->packet, &flow, &cookie); |
abe529af BP |
2463 | } |
2464 | ofpbuf_delete(upcall->packet); | |
6ff686f2 PS |
2465 | |
2466 | } else if (cookie.type == USER_ACTION_COOKIE_CONTROLLER) { | |
2467 | COVERAGE_INC(ofproto_dpif_ctlr_action); | |
2468 | odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow); | |
62cd7072 BP |
2469 | send_packet_in_action(ofproto, upcall->packet, upcall->userdata, |
2470 | &flow, false); | |
6ff686f2 PS |
2471 | } else { |
2472 | VLOG_WARN_RL(&rl, "invalid user cookie : 0x%"PRIx64, upcall->userdata); | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | static void | |
2477 | handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall) | |
2478 | { | |
2479 | switch (upcall->type) { | |
2480 | case DPIF_UC_ACTION: | |
2481 | handle_userspace_upcall(ofproto, upcall); | |
abe529af BP |
2482 | break; |
2483 | ||
2484 | case DPIF_UC_MISS: | |
501f8d1f BP |
2485 | /* The caller handles these. */ |
2486 | NOT_REACHED(); | |
abe529af BP |
2487 | |
2488 | case DPIF_N_UC_TYPES: | |
2489 | default: | |
2490 | VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type); | |
2491 | break; | |
2492 | } | |
2493 | } | |
2494 | \f | |
2495 | /* Flow expiration. */ | |
2496 | ||
2497 | static int facet_max_idle(const struct ofproto_dpif *); | |
2498 | static void update_stats(struct ofproto_dpif *); | |
2499 | static void rule_expire(struct rule_dpif *); | |
2500 | static void expire_facets(struct ofproto_dpif *, int dp_max_idle); | |
2501 | ||
2502 | /* This function is called periodically by run(). Its job is to collect | |
2503 | * updates for the flows that have been installed into the datapath, most | |
2504 | * importantly when they last were used, and then use that information to | |
2505 | * expire flows that have not been used recently. | |
2506 | * | |
2507 | * Returns the number of milliseconds after which it should be called again. */ | |
2508 | static int | |
2509 | expire(struct ofproto_dpif *ofproto) | |
2510 | { | |
2511 | struct rule_dpif *rule, *next_rule; | |
0697b5c3 | 2512 | struct classifier *table; |
abe529af BP |
2513 | int dp_max_idle; |
2514 | ||
2515 | /* Update stats for each flow in the datapath. */ | |
2516 | update_stats(ofproto); | |
2517 | ||
2518 | /* Expire facets that have been idle too long. */ | |
2519 | dp_max_idle = facet_max_idle(ofproto); | |
2520 | expire_facets(ofproto, dp_max_idle); | |
2521 | ||
2522 | /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */ | |
0697b5c3 BP |
2523 | OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) { |
2524 | struct cls_cursor cursor; | |
2525 | ||
2526 | cls_cursor_init(&cursor, table, NULL); | |
2527 | CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) { | |
2528 | rule_expire(rule); | |
2529 | } | |
abe529af BP |
2530 | } |
2531 | ||
2532 | /* All outstanding data in existing flows has been accounted, so it's a | |
2533 | * good time to do bond rebalancing. */ | |
2534 | if (ofproto->has_bonded_bundles) { | |
2535 | struct ofbundle *bundle; | |
2536 | ||
2537 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
2538 | if (bundle->bond) { | |
2539 | bond_rebalance(bundle->bond, &ofproto->revalidate_set); | |
2540 | } | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | return MIN(dp_max_idle, 1000); | |
2545 | } | |
2546 | ||
2547 | /* Update 'packet_count', 'byte_count', and 'used' members of installed facets. | |
2548 | * | |
2549 | * This function also pushes statistics updates to rules which each facet | |
2550 | * resubmits into. Generally these statistics will be accurate. However, if a | |
2551 | * facet changes the rule it resubmits into at some time in between | |
2552 | * update_stats() runs, it is possible that statistics accrued to the | |
2553 | * old rule will be incorrectly attributed to the new rule. This could be | |
2554 | * avoided by calling update_stats() whenever rules are created or | |
2555 | * deleted. However, the performance impact of making so many calls to the | |
2556 | * datapath do not justify the benefit of having perfectly accurate statistics. | |
2557 | */ | |
2558 | static void | |
2559 | update_stats(struct ofproto_dpif *p) | |
2560 | { | |
2561 | const struct dpif_flow_stats *stats; | |
2562 | struct dpif_flow_dump dump; | |
2563 | const struct nlattr *key; | |
2564 | size_t key_len; | |
2565 | ||
2566 | dpif_flow_dump_start(&dump, p->dpif); | |
2567 | while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) { | |
2568 | struct facet *facet; | |
2569 | struct flow flow; | |
2570 | ||
2571 | if (odp_flow_key_to_flow(key, key_len, &flow)) { | |
2572 | struct ds s; | |
2573 | ||
2574 | ds_init(&s); | |
2575 | odp_flow_key_format(key, key_len, &s); | |
df2c07f4 | 2576 | VLOG_WARN_RL(&rl, "failed to convert datapath flow key to flow: %s", |
abe529af BP |
2577 | ds_cstr(&s)); |
2578 | ds_destroy(&s); | |
2579 | ||
2580 | continue; | |
2581 | } | |
2582 | facet = facet_find(p, &flow); | |
2583 | ||
2584 | if (facet && facet->installed) { | |
2585 | ||
2586 | if (stats->n_packets >= facet->dp_packet_count) { | |
2587 | uint64_t extra = stats->n_packets - facet->dp_packet_count; | |
2588 | facet->packet_count += extra; | |
2589 | } else { | |
2590 | VLOG_WARN_RL(&rl, "unexpected packet count from the datapath"); | |
2591 | } | |
2592 | ||
2593 | if (stats->n_bytes >= facet->dp_byte_count) { | |
2594 | facet->byte_count += stats->n_bytes - facet->dp_byte_count; | |
2595 | } else { | |
2596 | VLOG_WARN_RL(&rl, "unexpected byte count from datapath"); | |
2597 | } | |
2598 | ||
2599 | facet->dp_packet_count = stats->n_packets; | |
2600 | facet->dp_byte_count = stats->n_bytes; | |
2601 | ||
2602 | facet_update_time(p, facet, stats->used); | |
55af77bb | 2603 | facet_account(p, facet); |
abe529af BP |
2604 | facet_push_stats(facet); |
2605 | } else { | |
2606 | /* There's a flow in the datapath that we know nothing about. | |
2607 | * Delete it. */ | |
2608 | COVERAGE_INC(facet_unexpected); | |
2609 | dpif_flow_del(p->dpif, key, key_len, NULL); | |
2610 | } | |
2611 | } | |
2612 | dpif_flow_dump_done(&dump); | |
2613 | } | |
2614 | ||
2615 | /* Calculates and returns the number of milliseconds of idle time after which | |
2616 | * facets should expire from the datapath and we should fold their statistics | |
2617 | * into their parent rules in userspace. */ | |
2618 | static int | |
2619 | facet_max_idle(const struct ofproto_dpif *ofproto) | |
2620 | { | |
2621 | /* | |
2622 | * Idle time histogram. | |
2623 | * | |
2624 | * Most of the time a switch has a relatively small number of facets. When | |
2625 | * this is the case we might as well keep statistics for all of them in | |
2626 | * userspace and to cache them in the kernel datapath for performance as | |
2627 | * well. | |
2628 | * | |
2629 | * As the number of facets increases, the memory required to maintain | |
2630 | * statistics about them in userspace and in the kernel becomes | |
2631 | * significant. However, with a large number of facets it is likely that | |
2632 | * only a few of them are "heavy hitters" that consume a large amount of | |
2633 | * bandwidth. At this point, only heavy hitters are worth caching in the | |
2634 | * kernel and maintaining in userspaces; other facets we can discard. | |
2635 | * | |
2636 | * The technique used to compute the idle time is to build a histogram with | |
2637 | * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each facet | |
2638 | * that is installed in the kernel gets dropped in the appropriate bucket. | |
2639 | * After the histogram has been built, we compute the cutoff so that only | |
084f5290 SH |
2640 | * the most-recently-used 1% of facets (but at least |
2641 | * ofproto->up.flow_eviction_threshold flows) are kept cached. At least | |
2642 | * the most-recently-used bucket of facets is kept, so actually an | |
2643 | * arbitrary number of facets can be kept in any given expiration run | |
2644 | * (though the next run will delete most of those unless they receive | |
2645 | * additional data). | |
abe529af BP |
2646 | * |
2647 | * This requires a second pass through the facets, in addition to the pass | |
2648 | * made by update_stats(), because the former function never looks | |
2649 | * at uninstallable facets. | |
2650 | */ | |
2651 | enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) }; | |
2652 | enum { N_BUCKETS = 5000 / BUCKET_WIDTH }; | |
2653 | int buckets[N_BUCKETS] = { 0 }; | |
f11c1ef4 | 2654 | int total, subtotal, bucket; |
abe529af | 2655 | struct facet *facet; |
abe529af BP |
2656 | long long int now; |
2657 | int i; | |
2658 | ||
2659 | total = hmap_count(&ofproto->facets); | |
084f5290 | 2660 | if (total <= ofproto->up.flow_eviction_threshold) { |
abe529af BP |
2661 | return N_BUCKETS * BUCKET_WIDTH; |
2662 | } | |
2663 | ||
2664 | /* Build histogram. */ | |
2665 | now = time_msec(); | |
2666 | HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) { | |
2667 | long long int idle = now - facet->used; | |
2668 | int bucket = (idle <= 0 ? 0 | |
2669 | : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1 | |
2670 | : (unsigned int) idle / BUCKET_WIDTH); | |
2671 | buckets[bucket]++; | |
2672 | } | |
2673 | ||
2674 | /* Find the first bucket whose flows should be expired. */ | |
f11c1ef4 SH |
2675 | subtotal = bucket = 0; |
2676 | do { | |
2677 | subtotal += buckets[bucket++]; | |
084f5290 SH |
2678 | } while (bucket < N_BUCKETS && |
2679 | subtotal < MAX(ofproto->up.flow_eviction_threshold, total / 100)); | |
abe529af BP |
2680 | |
2681 | if (VLOG_IS_DBG_ENABLED()) { | |
2682 | struct ds s; | |
2683 | ||
2684 | ds_init(&s); | |
2685 | ds_put_cstr(&s, "keep"); | |
2686 | for (i = 0; i < N_BUCKETS; i++) { | |
2687 | if (i == bucket) { | |
2688 | ds_put_cstr(&s, ", drop"); | |
2689 | } | |
2690 | if (buckets[i]) { | |
2691 | ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]); | |
2692 | } | |
2693 | } | |
2694 | VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s)); | |
2695 | ds_destroy(&s); | |
2696 | } | |
2697 | ||
2698 | return bucket * BUCKET_WIDTH; | |
2699 | } | |
2700 | ||
2701 | static void | |
2702 | facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet) | |
2703 | { | |
2704 | if (ofproto->netflow && !facet_is_controller_flow(facet) && | |
2705 | netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) { | |
2706 | struct ofexpired expired; | |
2707 | ||
2708 | if (facet->installed) { | |
2709 | struct dpif_flow_stats stats; | |
2710 | ||
2711 | facet_put__(ofproto, facet, facet->actions, facet->actions_len, | |
2712 | &stats); | |
2713 | facet_update_stats(ofproto, facet, &stats); | |
2714 | } | |
2715 | ||
2716 | expired.flow = facet->flow; | |
2717 | expired.packet_count = facet->packet_count; | |
2718 | expired.byte_count = facet->byte_count; | |
2719 | expired.used = facet->used; | |
2720 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
2721 | } | |
2722 | } | |
2723 | ||
2724 | static void | |
2725 | expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle) | |
2726 | { | |
2727 | long long int cutoff = time_msec() - dp_max_idle; | |
2728 | struct facet *facet, *next_facet; | |
2729 | ||
2730 | HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) { | |
2731 | facet_active_timeout(ofproto, facet); | |
2732 | if (facet->used < cutoff) { | |
2733 | facet_remove(ofproto, facet); | |
2734 | } | |
2735 | } | |
2736 | } | |
2737 | ||
2738 | /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules, | |
2739 | * then delete it entirely. */ | |
2740 | static void | |
2741 | rule_expire(struct rule_dpif *rule) | |
2742 | { | |
2743 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2744 | struct facet *facet, *next_facet; | |
2745 | long long int now; | |
2746 | uint8_t reason; | |
2747 | ||
2748 | /* Has 'rule' expired? */ | |
2749 | now = time_msec(); | |
2750 | if (rule->up.hard_timeout | |
308881af | 2751 | && now > rule->up.modified + rule->up.hard_timeout * 1000) { |
abe529af BP |
2752 | reason = OFPRR_HARD_TIMEOUT; |
2753 | } else if (rule->up.idle_timeout && list_is_empty(&rule->facets) | |
2754 | && now > rule->used + rule->up.idle_timeout * 1000) { | |
2755 | reason = OFPRR_IDLE_TIMEOUT; | |
2756 | } else { | |
2757 | return; | |
2758 | } | |
2759 | ||
2760 | COVERAGE_INC(ofproto_dpif_expired); | |
2761 | ||
2762 | /* Update stats. (This is a no-op if the rule expired due to an idle | |
2763 | * timeout, because that only happens when the rule has no facets left.) */ | |
2764 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { | |
2765 | facet_remove(ofproto, facet); | |
2766 | } | |
2767 | ||
2768 | /* Get rid of the rule. */ | |
2769 | ofproto_rule_expire(&rule->up, reason); | |
2770 | } | |
2771 | \f | |
2772 | /* Facets. */ | |
2773 | ||
f3827897 | 2774 | /* Creates and returns a new facet owned by 'rule', given a 'flow'. |
abe529af BP |
2775 | * |
2776 | * The caller must already have determined that no facet with an identical | |
2777 | * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in | |
f3827897 BP |
2778 | * the ofproto's classifier table. |
2779 | * | |
2780 | * The facet will initially have no ODP actions. The caller should fix that | |
2781 | * by calling facet_make_actions(). */ | |
abe529af | 2782 | static struct facet * |
f3827897 | 2783 | facet_create(struct rule_dpif *rule, const struct flow *flow) |
abe529af BP |
2784 | { |
2785 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
2786 | struct facet *facet; | |
2787 | ||
2788 | facet = xzalloc(sizeof *facet); | |
2789 | facet->used = time_msec(); | |
2790 | hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0)); | |
2791 | list_push_back(&rule->facets, &facet->list_node); | |
2792 | facet->rule = rule; | |
2793 | facet->flow = *flow; | |
2794 | netflow_flow_init(&facet->nf_flow); | |
2795 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used); | |
2796 | ||
abe529af BP |
2797 | return facet; |
2798 | } | |
2799 | ||
2800 | static void | |
2801 | facet_free(struct facet *facet) | |
2802 | { | |
2803 | free(facet->actions); | |
2804 | free(facet); | |
2805 | } | |
2806 | ||
abe529af | 2807 | static bool |
3d9e05f8 BP |
2808 | execute_controller_action(struct ofproto_dpif *ofproto, |
2809 | const struct flow *flow, | |
2810 | const struct nlattr *odp_actions, size_t actions_len, | |
2811 | struct ofpbuf *packet) | |
2812 | { | |
2813 | if (actions_len | |
2814 | && odp_actions->nla_type == OVS_ACTION_ATTR_USERSPACE | |
2815 | && NLA_ALIGN(odp_actions->nla_len) == actions_len) { | |
62cd7072 BP |
2816 | /* As an optimization, avoid a round-trip from userspace to kernel to |
2817 | * userspace. This also avoids possibly filling up kernel packet | |
2818 | * buffers along the way. | |
2819 | * | |
2820 | * This optimization will not accidentally catch sFlow | |
2821 | * OVS_ACTION_ATTR_USERSPACE actions, since those are encapsulated | |
2822 | * inside OVS_ACTION_ATTR_SAMPLE. */ | |
2823 | const struct nlattr *nla; | |
2824 | ||
2825 | nla = nl_attr_find_nested(odp_actions, OVS_USERSPACE_ATTR_USERDATA); | |
2826 | send_packet_in_action(ofproto, packet, nl_attr_get_u64(nla), flow, | |
2827 | false); | |
2828 | return true; | |
3d9e05f8 BP |
2829 | } else { |
2830 | return false; | |
2831 | } | |
2832 | } | |
2833 | ||
2834 | /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on | |
2835 | * 'packet', which arrived on 'in_port'. | |
2836 | * | |
2837 | * Takes ownership of 'packet'. */ | |
2838 | static bool | |
2839 | execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow, | |
2840 | const struct nlattr *odp_actions, size_t actions_len, | |
2841 | struct ofpbuf *packet) | |
2842 | { | |
2843 | struct odputil_keybuf keybuf; | |
2844 | struct ofpbuf key; | |
2845 | int error; | |
2846 | ||
2847 | if (execute_controller_action(ofproto, flow, odp_actions, actions_len, | |
2848 | packet)) { | |
2849 | return true; | |
6ff686f2 | 2850 | } |
abe529af | 2851 | |
6ff686f2 PS |
2852 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); |
2853 | odp_flow_key_from_flow(&key, flow); | |
80e5eed9 | 2854 | |
6ff686f2 PS |
2855 | error = dpif_execute(ofproto->dpif, key.data, key.size, |
2856 | odp_actions, actions_len, packet); | |
80e5eed9 | 2857 | |
6ff686f2 PS |
2858 | ofpbuf_delete(packet); |
2859 | return !error; | |
abe529af BP |
2860 | } |
2861 | ||
2862 | /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s | |
2863 | * statistics appropriately. 'packet' must have at least sizeof(struct | |
2864 | * ofp_packet_in) bytes of headroom. | |
2865 | * | |
2866 | * For correct results, 'packet' must actually be in 'facet''s flow; that is, | |
2867 | * applying flow_extract() to 'packet' would yield the same flow as | |
2868 | * 'facet->flow'. | |
2869 | * | |
df2c07f4 JP |
2870 | * 'facet' must have accurately composed datapath actions; that is, it must |
2871 | * not be in need of revalidation. | |
abe529af BP |
2872 | * |
2873 | * Takes ownership of 'packet'. */ | |
2874 | static void | |
2875 | facet_execute(struct ofproto_dpif *ofproto, struct facet *facet, | |
2876 | struct ofpbuf *packet) | |
2877 | { | |
2878 | struct dpif_flow_stats stats; | |
2879 | ||
2880 | assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in)); | |
2881 | ||
572b7068 | 2882 | dpif_flow_stats_extract(&facet->flow, packet, &stats); |
abe529af BP |
2883 | stats.used = time_msec(); |
2884 | if (execute_odp_actions(ofproto, &facet->flow, | |
2885 | facet->actions, facet->actions_len, packet)) { | |
2886 | facet_update_stats(ofproto, facet, &stats); | |
2887 | } | |
2888 | } | |
2889 | ||
2890 | /* Remove 'facet' from 'ofproto' and free up the associated memory: | |
2891 | * | |
2892 | * - If 'facet' was installed in the datapath, uninstalls it and updates its | |
2893 | * rule's statistics, via facet_uninstall(). | |
2894 | * | |
2895 | * - Removes 'facet' from its rule and from ofproto->facets. | |
2896 | */ | |
2897 | static void | |
2898 | facet_remove(struct ofproto_dpif *ofproto, struct facet *facet) | |
2899 | { | |
2900 | facet_uninstall(ofproto, facet); | |
2901 | facet_flush_stats(ofproto, facet); | |
2902 | hmap_remove(&ofproto->facets, &facet->hmap_node); | |
2903 | list_remove(&facet->list_node); | |
2904 | facet_free(facet); | |
2905 | } | |
2906 | ||
df2c07f4 | 2907 | /* Composes the datapath actions for 'facet' based on its rule's actions. */ |
abe529af BP |
2908 | static void |
2909 | facet_make_actions(struct ofproto_dpif *p, struct facet *facet, | |
2910 | const struct ofpbuf *packet) | |
2911 | { | |
2912 | const struct rule_dpif *rule = facet->rule; | |
2913 | struct ofpbuf *odp_actions; | |
2914 | struct action_xlate_ctx ctx; | |
2915 | ||
2916 | action_xlate_ctx_init(&ctx, p, &facet->flow, packet); | |
2917 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
2918 | facet->tags = ctx.tags; | |
2919 | facet->may_install = ctx.may_set_up_flow; | |
75a75043 BP |
2920 | facet->has_learn = ctx.has_learn; |
2921 | facet->has_normal = ctx.has_normal; | |
abe529af BP |
2922 | facet->nf_flow.output_iface = ctx.nf_output_iface; |
2923 | ||
2924 | if (facet->actions_len != odp_actions->size | |
2925 | || memcmp(facet->actions, odp_actions->data, odp_actions->size)) { | |
2926 | free(facet->actions); | |
2927 | facet->actions_len = odp_actions->size; | |
2928 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
2929 | } | |
2930 | ||
2931 | ofpbuf_delete(odp_actions); | |
2932 | } | |
2933 | ||
3a88e544 BP |
2934 | /* Updates 'facet''s flow in the datapath setting its actions to 'actions_len' |
2935 | * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters | |
2936 | * in the datapath will be zeroed and 'stats' will be updated with traffic new | |
2937 | * since 'facet' was last updated. | |
2938 | * | |
2939 | * Returns 0 if successful, otherwise a positive errno value.*/ | |
abe529af BP |
2940 | static int |
2941 | facet_put__(struct ofproto_dpif *ofproto, struct facet *facet, | |
2942 | const struct nlattr *actions, size_t actions_len, | |
2943 | struct dpif_flow_stats *stats) | |
2944 | { | |
2945 | struct odputil_keybuf keybuf; | |
2946 | enum dpif_flow_put_flags flags; | |
2947 | struct ofpbuf key; | |
3a88e544 | 2948 | int ret; |
abe529af BP |
2949 | |
2950 | flags = DPIF_FP_CREATE | DPIF_FP_MODIFY; | |
2951 | if (stats) { | |
2952 | flags |= DPIF_FP_ZERO_STATS; | |
abe529af BP |
2953 | } |
2954 | ||
2955 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
2956 | odp_flow_key_from_flow(&key, &facet->flow); | |
2957 | ||
3a88e544 BP |
2958 | ret = dpif_flow_put(ofproto->dpif, flags, key.data, key.size, |
2959 | actions, actions_len, stats); | |
2960 | ||
2961 | if (stats) { | |
2962 | facet_reset_dp_stats(facet, stats); | |
2963 | } | |
2964 | ||
2965 | return ret; | |
abe529af BP |
2966 | } |
2967 | ||
2968 | /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath. If | |
2969 | * 'zero_stats' is true, clears any existing statistics from the datapath for | |
2970 | * 'facet'. */ | |
2971 | static void | |
2972 | facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats) | |
2973 | { | |
2974 | struct dpif_flow_stats stats; | |
2975 | ||
2976 | if (facet->may_install | |
2977 | && !facet_put__(p, facet, facet->actions, facet->actions_len, | |
2978 | zero_stats ? &stats : NULL)) { | |
2979 | facet->installed = true; | |
2980 | } | |
2981 | } | |
2982 | ||
2983 | static void | |
55af77bb | 2984 | facet_account(struct ofproto_dpif *ofproto, struct facet *facet) |
abe529af | 2985 | { |
55af77bb | 2986 | uint64_t n_bytes; |
abe529af | 2987 | const struct nlattr *a; |
abe529af | 2988 | unsigned int left; |
d78be13b | 2989 | ovs_be16 vlan_tci; |
abe529af | 2990 | |
55af77bb | 2991 | if (facet->byte_count <= facet->accounted_bytes) { |
abe529af BP |
2992 | return; |
2993 | } | |
55af77bb EJ |
2994 | n_bytes = facet->byte_count - facet->accounted_bytes; |
2995 | facet->accounted_bytes = facet->byte_count; | |
abe529af | 2996 | |
75a75043 | 2997 | /* Feed information from the active flows back into the learning table to |
abe529af BP |
2998 | * ensure that table is always in sync with what is actually flowing |
2999 | * through the datapath. */ | |
75a75043 BP |
3000 | if (facet->has_learn || facet->has_normal) { |
3001 | struct action_xlate_ctx ctx; | |
abe529af | 3002 | |
75a75043 BP |
3003 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); |
3004 | ctx.may_learn = true; | |
3005 | ofpbuf_delete(xlate_actions(&ctx, facet->rule->up.actions, | |
3006 | facet->rule->up.n_actions)); | |
3007 | } | |
abe529af | 3008 | |
75a75043 | 3009 | if (!facet->has_normal || !ofproto->has_bonded_bundles) { |
abe529af BP |
3010 | return; |
3011 | } | |
d78be13b BP |
3012 | |
3013 | /* This loop feeds byte counters to bond_account() for rebalancing to use | |
3014 | * as a basis. We also need to track the actual VLAN on which the packet | |
3015 | * is going to be sent to ensure that it matches the one passed to | |
3016 | * bond_choose_output_slave(). (Otherwise, we will account to the wrong | |
3017 | * hash bucket.) */ | |
3018 | vlan_tci = facet->flow.vlan_tci; | |
abe529af | 3019 | NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) { |
fea393b1 | 3020 | const struct ovs_action_push_vlan *vlan; |
d78be13b | 3021 | struct ofport_dpif *port; |
abe529af | 3022 | |
d78be13b | 3023 | switch (nl_attr_type(a)) { |
df2c07f4 | 3024 | case OVS_ACTION_ATTR_OUTPUT: |
abe529af BP |
3025 | port = get_odp_port(ofproto, nl_attr_get_u32(a)); |
3026 | if (port && port->bundle && port->bundle->bond) { | |
d78be13b | 3027 | bond_account(port->bundle->bond, &facet->flow, |
dc155bff | 3028 | vlan_tci_to_vid(vlan_tci), n_bytes); |
abe529af | 3029 | } |
d78be13b BP |
3030 | break; |
3031 | ||
fea393b1 BP |
3032 | case OVS_ACTION_ATTR_POP_VLAN: |
3033 | vlan_tci = htons(0); | |
d78be13b BP |
3034 | break; |
3035 | ||
fea393b1 BP |
3036 | case OVS_ACTION_ATTR_PUSH_VLAN: |
3037 | vlan = nl_attr_get(a); | |
3038 | vlan_tci = vlan->vlan_tci; | |
d78be13b | 3039 | break; |
abe529af BP |
3040 | } |
3041 | } | |
3042 | } | |
3043 | ||
3044 | /* If 'rule' is installed in the datapath, uninstalls it. */ | |
3045 | static void | |
3046 | facet_uninstall(struct ofproto_dpif *p, struct facet *facet) | |
3047 | { | |
3048 | if (facet->installed) { | |
3049 | struct odputil_keybuf keybuf; | |
3050 | struct dpif_flow_stats stats; | |
3051 | struct ofpbuf key; | |
3a88e544 | 3052 | int error; |
abe529af BP |
3053 | |
3054 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
3055 | odp_flow_key_from_flow(&key, &facet->flow); | |
3056 | ||
3a88e544 BP |
3057 | error = dpif_flow_del(p->dpif, key.data, key.size, &stats); |
3058 | facet_reset_dp_stats(facet, &stats); | |
3059 | if (!error) { | |
abe529af BP |
3060 | facet_update_stats(p, facet, &stats); |
3061 | } | |
3062 | facet->installed = false; | |
abe529af BP |
3063 | } else { |
3064 | assert(facet->dp_packet_count == 0); | |
3065 | assert(facet->dp_byte_count == 0); | |
3066 | } | |
3067 | } | |
3068 | ||
3069 | /* Returns true if the only action for 'facet' is to send to the controller. | |
3070 | * (We don't report NetFlow expiration messages for such facets because they | |
3071 | * are just part of the control logic for the network, not real traffic). */ | |
3072 | static bool | |
3073 | facet_is_controller_flow(struct facet *facet) | |
3074 | { | |
3075 | return (facet | |
3076 | && facet->rule->up.n_actions == 1 | |
3077 | && action_outputs_to_port(&facet->rule->up.actions[0], | |
3078 | htons(OFPP_CONTROLLER))); | |
3079 | } | |
3080 | ||
3a88e544 BP |
3081 | /* Resets 'facet''s datapath statistics counters. This should be called when |
3082 | * 'facet''s statistics are cleared in the datapath. If 'stats' is non-null, | |
3083 | * it should contain the statistics returned by dpif when 'facet' was reset in | |
3084 | * the datapath. 'stats' will be modified to only included statistics new | |
3085 | * since 'facet' was last updated. */ | |
3086 | static void | |
3087 | facet_reset_dp_stats(struct facet *facet, struct dpif_flow_stats *stats) | |
3088 | { | |
3089 | if (stats && facet->dp_packet_count <= stats->n_packets | |
3090 | && facet->dp_byte_count <= stats->n_bytes) { | |
3091 | stats->n_packets -= facet->dp_packet_count; | |
3092 | stats->n_bytes -= facet->dp_byte_count; | |
3093 | } | |
3094 | ||
3095 | facet->dp_packet_count = 0; | |
3096 | facet->dp_byte_count = 0; | |
3097 | } | |
3098 | ||
abe529af BP |
3099 | /* Folds all of 'facet''s statistics into its rule. Also updates the |
3100 | * accounting ofhook and emits a NetFlow expiration if appropriate. All of | |
3101 | * 'facet''s statistics in the datapath should have been zeroed and folded into | |
3102 | * its packet and byte counts before this function is called. */ | |
3103 | static void | |
3104 | facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet) | |
3105 | { | |
3106 | assert(!facet->dp_byte_count); | |
3107 | assert(!facet->dp_packet_count); | |
3108 | ||
3109 | facet_push_stats(facet); | |
55af77bb | 3110 | facet_account(ofproto, facet); |
abe529af BP |
3111 | |
3112 | if (ofproto->netflow && !facet_is_controller_flow(facet)) { | |
3113 | struct ofexpired expired; | |
3114 | expired.flow = facet->flow; | |
3115 | expired.packet_count = facet->packet_count; | |
3116 | expired.byte_count = facet->byte_count; | |
3117 | expired.used = facet->used; | |
3118 | netflow_expire(ofproto->netflow, &facet->nf_flow, &expired); | |
3119 | } | |
3120 | ||
3121 | facet->rule->packet_count += facet->packet_count; | |
3122 | facet->rule->byte_count += facet->byte_count; | |
3123 | ||
3124 | /* Reset counters to prevent double counting if 'facet' ever gets | |
3125 | * reinstalled. */ | |
bbb5d219 | 3126 | facet_reset_counters(facet); |
abe529af BP |
3127 | |
3128 | netflow_flow_clear(&facet->nf_flow); | |
3129 | } | |
3130 | ||
3131 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
3132 | * Returns it if found, otherwise a null pointer. | |
3133 | * | |
3134 | * The returned facet might need revalidation; use facet_lookup_valid() | |
3135 | * instead if that is important. */ | |
3136 | static struct facet * | |
3137 | facet_find(struct ofproto_dpif *ofproto, const struct flow *flow) | |
3138 | { | |
3139 | struct facet *facet; | |
3140 | ||
3141 | HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0), | |
3142 | &ofproto->facets) { | |
3143 | if (flow_equal(flow, &facet->flow)) { | |
3144 | return facet; | |
3145 | } | |
3146 | } | |
3147 | ||
3148 | return NULL; | |
3149 | } | |
3150 | ||
3151 | /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'. | |
3152 | * Returns it if found, otherwise a null pointer. | |
3153 | * | |
3154 | * The returned facet is guaranteed to be valid. */ | |
3155 | static struct facet * | |
3156 | facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow) | |
3157 | { | |
3158 | struct facet *facet = facet_find(ofproto, flow); | |
3159 | ||
3160 | /* The facet we found might not be valid, since we could be in need of | |
3161 | * revalidation. If it is not valid, don't return it. */ | |
3162 | if (facet | |
0e4b3771 BP |
3163 | && (ofproto->need_revalidate |
3164 | || tag_set_intersects(&ofproto->revalidate_set, facet->tags)) | |
abe529af BP |
3165 | && !facet_revalidate(ofproto, facet)) { |
3166 | COVERAGE_INC(facet_invalidated); | |
3167 | return NULL; | |
3168 | } | |
3169 | ||
3170 | return facet; | |
3171 | } | |
3172 | ||
3173 | /* Re-searches 'ofproto''s classifier for a rule matching 'facet': | |
3174 | * | |
3175 | * - If the rule found is different from 'facet''s current rule, moves | |
3176 | * 'facet' to the new rule and recompiles its actions. | |
3177 | * | |
3178 | * - If the rule found is the same as 'facet''s current rule, leaves 'facet' | |
3179 | * where it is and recompiles its actions anyway. | |
3180 | * | |
3181 | * - If there is none, destroys 'facet'. | |
3182 | * | |
3183 | * Returns true if 'facet' still exists, false if it has been destroyed. */ | |
3184 | static bool | |
3185 | facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet) | |
3186 | { | |
3187 | struct action_xlate_ctx ctx; | |
3188 | struct ofpbuf *odp_actions; | |
3189 | struct rule_dpif *new_rule; | |
3190 | bool actions_changed; | |
3191 | ||
3192 | COVERAGE_INC(facet_revalidate); | |
3193 | ||
3194 | /* Determine the new rule. */ | |
29901626 | 3195 | new_rule = rule_dpif_lookup(ofproto, &facet->flow, 0); |
abe529af BP |
3196 | if (!new_rule) { |
3197 | /* No new rule, so delete the facet. */ | |
3198 | facet_remove(ofproto, facet); | |
3199 | return false; | |
3200 | } | |
3201 | ||
df2c07f4 | 3202 | /* Calculate new datapath actions. |
abe529af BP |
3203 | * |
3204 | * We do not modify any 'facet' state yet, because we might need to, e.g., | |
3205 | * emit a NetFlow expiration and, if so, we need to have the old state | |
3206 | * around to properly compose it. */ | |
3207 | action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL); | |
3208 | odp_actions = xlate_actions(&ctx, | |
3209 | new_rule->up.actions, new_rule->up.n_actions); | |
3210 | actions_changed = (facet->actions_len != odp_actions->size | |
3211 | || memcmp(facet->actions, odp_actions->data, | |
3212 | facet->actions_len)); | |
3213 | ||
df2c07f4 JP |
3214 | /* If the datapath actions changed or the installability changed, |
3215 | * then we need to talk to the datapath. */ | |
abe529af BP |
3216 | if (actions_changed || ctx.may_set_up_flow != facet->installed) { |
3217 | if (ctx.may_set_up_flow) { | |
3218 | struct dpif_flow_stats stats; | |
3219 | ||
3220 | facet_put__(ofproto, facet, | |
3221 | odp_actions->data, odp_actions->size, &stats); | |
3222 | facet_update_stats(ofproto, facet, &stats); | |
3223 | } else { | |
3224 | facet_uninstall(ofproto, facet); | |
3225 | } | |
3226 | ||
3227 | /* The datapath flow is gone or has zeroed stats, so push stats out of | |
3228 | * 'facet' into 'rule'. */ | |
3229 | facet_flush_stats(ofproto, facet); | |
3230 | } | |
3231 | ||
3232 | /* Update 'facet' now that we've taken care of all the old state. */ | |
3233 | facet->tags = ctx.tags; | |
3234 | facet->nf_flow.output_iface = ctx.nf_output_iface; | |
3235 | facet->may_install = ctx.may_set_up_flow; | |
75a75043 BP |
3236 | facet->has_learn = ctx.has_learn; |
3237 | facet->has_normal = ctx.has_normal; | |
abe529af BP |
3238 | if (actions_changed) { |
3239 | free(facet->actions); | |
3240 | facet->actions_len = odp_actions->size; | |
3241 | facet->actions = xmemdup(odp_actions->data, odp_actions->size); | |
3242 | } | |
3243 | if (facet->rule != new_rule) { | |
3244 | COVERAGE_INC(facet_changed_rule); | |
3245 | list_remove(&facet->list_node); | |
3246 | list_push_back(&new_rule->facets, &facet->list_node); | |
3247 | facet->rule = new_rule; | |
3248 | facet->used = new_rule->up.created; | |
3249 | facet->rs_used = facet->used; | |
3250 | } | |
3251 | ||
3252 | ofpbuf_delete(odp_actions); | |
3253 | ||
3254 | return true; | |
3255 | } | |
3256 | ||
3257 | /* Updates 'facet''s used time. Caller is responsible for calling | |
3258 | * facet_push_stats() to update the flows which 'facet' resubmits into. */ | |
3259 | static void | |
3260 | facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet, | |
3261 | long long int used) | |
3262 | { | |
3263 | if (used > facet->used) { | |
3264 | facet->used = used; | |
3265 | if (used > facet->rule->used) { | |
3266 | facet->rule->used = used; | |
3267 | } | |
3268 | netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used); | |
3269 | } | |
3270 | } | |
3271 | ||
3272 | /* Folds the statistics from 'stats' into the counters in 'facet'. | |
3273 | * | |
3274 | * Because of the meaning of a facet's counters, it only makes sense to do this | |
3275 | * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a | |
3276 | * packet that was sent by hand or if it represents statistics that have been | |
3277 | * cleared out of the datapath. */ | |
3278 | static void | |
3279 | facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet, | |
3280 | const struct dpif_flow_stats *stats) | |
3281 | { | |
3282 | if (stats->n_packets || stats->used > facet->used) { | |
3283 | facet_update_time(ofproto, facet, stats->used); | |
3284 | facet->packet_count += stats->n_packets; | |
3285 | facet->byte_count += stats->n_bytes; | |
3286 | facet_push_stats(facet); | |
3287 | netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags); | |
3288 | } | |
3289 | } | |
3290 | ||
bbb5d219 EJ |
3291 | static void |
3292 | facet_reset_counters(struct facet *facet) | |
3293 | { | |
3294 | facet->packet_count = 0; | |
3295 | facet->byte_count = 0; | |
3296 | facet->rs_packet_count = 0; | |
3297 | facet->rs_byte_count = 0; | |
3298 | facet->accounted_bytes = 0; | |
3299 | } | |
3300 | ||
abe529af BP |
3301 | static void |
3302 | facet_push_stats(struct facet *facet) | |
3303 | { | |
3304 | uint64_t rs_packets, rs_bytes; | |
3305 | ||
3306 | assert(facet->packet_count >= facet->rs_packet_count); | |
3307 | assert(facet->byte_count >= facet->rs_byte_count); | |
3308 | assert(facet->used >= facet->rs_used); | |
3309 | ||
3310 | rs_packets = facet->packet_count - facet->rs_packet_count; | |
3311 | rs_bytes = facet->byte_count - facet->rs_byte_count; | |
3312 | ||
3313 | if (rs_packets || rs_bytes || facet->used > facet->rs_used) { | |
3314 | facet->rs_packet_count = facet->packet_count; | |
3315 | facet->rs_byte_count = facet->byte_count; | |
3316 | facet->rs_used = facet->used; | |
3317 | ||
3318 | flow_push_stats(facet->rule, &facet->flow, | |
3319 | rs_packets, rs_bytes, facet->used); | |
3320 | } | |
3321 | } | |
3322 | ||
3323 | struct ofproto_push { | |
3324 | struct action_xlate_ctx ctx; | |
3325 | uint64_t packets; | |
3326 | uint64_t bytes; | |
3327 | long long int used; | |
3328 | }; | |
3329 | ||
3330 | static void | |
3331 | push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
3332 | { | |
3333 | struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx); | |
3334 | ||
3335 | if (rule) { | |
3336 | rule->packet_count += push->packets; | |
3337 | rule->byte_count += push->bytes; | |
3338 | rule->used = MAX(push->used, rule->used); | |
3339 | } | |
3340 | } | |
3341 | ||
3342 | /* Pushes flow statistics to the rules which 'flow' resubmits into given | |
3343 | * 'rule''s actions. */ | |
3344 | static void | |
3345 | flow_push_stats(const struct rule_dpif *rule, | |
3346 | struct flow *flow, uint64_t packets, uint64_t bytes, | |
3347 | long long int used) | |
3348 | { | |
3349 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
3350 | struct ofproto_push push; | |
3351 | ||
3352 | push.packets = packets; | |
3353 | push.bytes = bytes; | |
3354 | push.used = used; | |
3355 | ||
3356 | action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL); | |
3357 | push.ctx.resubmit_hook = push_resubmit; | |
3358 | ofpbuf_delete(xlate_actions(&push.ctx, | |
3359 | rule->up.actions, rule->up.n_actions)); | |
3360 | } | |
3361 | \f | |
3362 | /* Rules. */ | |
3363 | ||
3364 | static struct rule_dpif * | |
29901626 BP |
3365 | rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow, |
3366 | uint8_t table_id) | |
abe529af | 3367 | { |
7257b535 BP |
3368 | struct cls_rule *cls_rule; |
3369 | struct classifier *cls; | |
3370 | ||
9cdaaebe BP |
3371 | if (table_id >= N_TABLES) { |
3372 | return NULL; | |
3373 | } | |
3374 | ||
7257b535 | 3375 | cls = &ofproto->up.tables[table_id]; |
eadef313 | 3376 | if (flow->nw_frag & FLOW_NW_FRAG_ANY |
7257b535 BP |
3377 | && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) { |
3378 | /* For OFPC_NORMAL frag_handling, we must pretend that transport ports | |
3379 | * are unavailable. */ | |
3380 | struct flow ofpc_normal_flow = *flow; | |
3381 | ofpc_normal_flow.tp_src = htons(0); | |
3382 | ofpc_normal_flow.tp_dst = htons(0); | |
3383 | cls_rule = classifier_lookup(cls, &ofpc_normal_flow); | |
3384 | } else { | |
3385 | cls_rule = classifier_lookup(cls, flow); | |
3386 | } | |
3387 | return rule_dpif_cast(rule_from_cls_rule(cls_rule)); | |
abe529af BP |
3388 | } |
3389 | ||
7ee20df1 BP |
3390 | static void |
3391 | complete_operation(struct rule_dpif *rule) | |
3392 | { | |
3393 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
3394 | ||
54a9cbc9 | 3395 | rule_invalidate(rule); |
7ee20df1 BP |
3396 | if (clogged) { |
3397 | struct dpif_completion *c = xmalloc(sizeof *c); | |
3398 | c->op = rule->up.pending; | |
3399 | list_push_back(&ofproto->completions, &c->list_node); | |
3400 | } else { | |
3401 | ofoperation_complete(rule->up.pending, 0); | |
3402 | } | |
3403 | } | |
3404 | ||
abe529af BP |
3405 | static struct rule * |
3406 | rule_alloc(void) | |
3407 | { | |
3408 | struct rule_dpif *rule = xmalloc(sizeof *rule); | |
3409 | return &rule->up; | |
3410 | } | |
3411 | ||
3412 | static void | |
3413 | rule_dealloc(struct rule *rule_) | |
3414 | { | |
3415 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3416 | free(rule); | |
3417 | } | |
3418 | ||
3419 | static int | |
3420 | rule_construct(struct rule *rule_) | |
3421 | { | |
3422 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3423 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
7ee20df1 | 3424 | struct rule_dpif *victim; |
54a9cbc9 | 3425 | uint8_t table_id; |
5bf0e941 BP |
3426 | int error; |
3427 | ||
3428 | error = validate_actions(rule->up.actions, rule->up.n_actions, | |
3429 | &rule->up.cr.flow, ofproto->max_ports); | |
3430 | if (error) { | |
3431 | return error; | |
3432 | } | |
abe529af BP |
3433 | |
3434 | rule->used = rule->up.created; | |
3435 | rule->packet_count = 0; | |
3436 | rule->byte_count = 0; | |
abe529af | 3437 | |
7ee20df1 BP |
3438 | victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending)); |
3439 | if (victim && !list_is_empty(&victim->facets)) { | |
3440 | struct facet *facet; | |
3441 | ||
3442 | rule->facets = victim->facets; | |
3443 | list_moved(&rule->facets); | |
3444 | LIST_FOR_EACH (facet, list_node, &rule->facets) { | |
bbb5d219 EJ |
3445 | /* XXX: We're only clearing our local counters here. It's possible |
3446 | * that quite a few packets are unaccounted for in the datapath | |
3447 | * statistics. These will be accounted to the new rule instead of | |
3448 | * cleared as required. This could be fixed by clearing out the | |
3449 | * datapath statistics for this facet, but currently it doesn't | |
3450 | * seem worth it. */ | |
3451 | facet_reset_counters(facet); | |
7ee20df1 BP |
3452 | facet->rule = rule; |
3453 | } | |
3454 | } else { | |
3455 | /* Must avoid list_moved() in this case. */ | |
3456 | list_init(&rule->facets); | |
3457 | } | |
abe529af | 3458 | |
54a9cbc9 BP |
3459 | table_id = rule->up.table_id; |
3460 | rule->tag = (victim ? victim->tag | |
3461 | : table_id == 0 ? 0 | |
3462 | : rule_calculate_tag(&rule->up.cr.flow, &rule->up.cr.wc, | |
3463 | ofproto->tables[table_id].basis)); | |
3464 | ||
7ee20df1 | 3465 | complete_operation(rule); |
abe529af BP |
3466 | return 0; |
3467 | } | |
3468 | ||
3469 | static void | |
3470 | rule_destruct(struct rule *rule_) | |
3471 | { | |
3472 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3473 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
3474 | struct facet *facet, *next_facet; | |
3475 | ||
abe529af BP |
3476 | LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) { |
3477 | facet_revalidate(ofproto, facet); | |
3478 | } | |
7ee20df1 BP |
3479 | |
3480 | complete_operation(rule); | |
abe529af BP |
3481 | } |
3482 | ||
3483 | static void | |
3484 | rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes) | |
3485 | { | |
3486 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3487 | struct facet *facet; | |
3488 | ||
3489 | /* Start from historical data for 'rule' itself that are no longer tracked | |
3490 | * in facets. This counts, for example, facets that have expired. */ | |
3491 | *packets = rule->packet_count; | |
3492 | *bytes = rule->byte_count; | |
3493 | ||
3494 | /* Add any statistics that are tracked by facets. This includes | |
3495 | * statistical data recently updated by ofproto_update_stats() as well as | |
3496 | * stats for packets that were executed "by hand" via dpif_execute(). */ | |
3497 | LIST_FOR_EACH (facet, list_node, &rule->facets) { | |
3498 | *packets += facet->packet_count; | |
3499 | *bytes += facet->byte_count; | |
3500 | } | |
3501 | } | |
3502 | ||
5bf0e941 | 3503 | static int |
abe529af BP |
3504 | rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet) |
3505 | { | |
3506 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3507 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
3508 | struct action_xlate_ctx ctx; | |
3509 | struct ofpbuf *odp_actions; | |
3510 | struct facet *facet; | |
3511 | size_t size; | |
3512 | ||
3513 | /* First look for a related facet. If we find one, account it to that. */ | |
3514 | facet = facet_lookup_valid(ofproto, flow); | |
3515 | if (facet && facet->rule == rule) { | |
eea109bb BP |
3516 | if (!facet->may_install) { |
3517 | facet_make_actions(ofproto, facet, packet); | |
3518 | } | |
abe529af | 3519 | facet_execute(ofproto, facet, packet); |
5bf0e941 | 3520 | return 0; |
abe529af BP |
3521 | } |
3522 | ||
3523 | /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then | |
3524 | * create a new facet for it and use that. */ | |
29901626 | 3525 | if (rule_dpif_lookup(ofproto, flow, 0) == rule) { |
f3827897 BP |
3526 | facet = facet_create(rule, flow); |
3527 | facet_make_actions(ofproto, facet, packet); | |
abe529af BP |
3528 | facet_execute(ofproto, facet, packet); |
3529 | facet_install(ofproto, facet, true); | |
5bf0e941 | 3530 | return 0; |
abe529af BP |
3531 | } |
3532 | ||
3533 | /* We can't account anything to a facet. If we were to try, then that | |
3534 | * facet would have a non-matching rule, busting our invariants. */ | |
3535 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
3536 | odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions); | |
3537 | size = packet->size; | |
3538 | if (execute_odp_actions(ofproto, flow, odp_actions->data, | |
3539 | odp_actions->size, packet)) { | |
3540 | rule->used = time_msec(); | |
3541 | rule->packet_count++; | |
3542 | rule->byte_count += size; | |
3543 | flow_push_stats(rule, flow, 1, size, rule->used); | |
3544 | } | |
3545 | ofpbuf_delete(odp_actions); | |
5bf0e941 BP |
3546 | |
3547 | return 0; | |
abe529af BP |
3548 | } |
3549 | ||
7ee20df1 BP |
3550 | static void |
3551 | rule_modify_actions(struct rule *rule_) | |
abe529af BP |
3552 | { |
3553 | struct rule_dpif *rule = rule_dpif_cast(rule_); | |
3554 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
3555 | int error; | |
3556 | ||
7ee20df1 BP |
3557 | error = validate_actions(rule->up.actions, rule->up.n_actions, |
3558 | &rule->up.cr.flow, ofproto->max_ports); | |
3559 | if (error) { | |
3560 | ofoperation_complete(rule->up.pending, error); | |
3561 | return; | |
abe529af | 3562 | } |
7ee20df1 BP |
3563 | |
3564 | complete_operation(rule); | |
abe529af BP |
3565 | } |
3566 | \f | |
b47e2a82 | 3567 | /* Sends 'packet' out of port 'odp_port' within 'ofproto'. |
abe529af BP |
3568 | * Returns 0 if successful, otherwise a positive errno value. */ |
3569 | static int | |
b2fda3ef | 3570 | send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port, |
abe529af BP |
3571 | const struct ofpbuf *packet) |
3572 | { | |
80e5eed9 BP |
3573 | struct ofpbuf key, odp_actions; |
3574 | struct odputil_keybuf keybuf; | |
3575 | struct flow flow; | |
abe529af BP |
3576 | int error; |
3577 | ||
abff858b | 3578 | flow_extract((struct ofpbuf *) packet, 0, 0, 0, &flow); |
80e5eed9 BP |
3579 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); |
3580 | odp_flow_key_from_flow(&key, &flow); | |
3581 | ||
abe529af | 3582 | ofpbuf_init(&odp_actions, 32); |
6ff686f2 PS |
3583 | compose_sflow_action(ofproto, &odp_actions, &flow, odp_port); |
3584 | ||
df2c07f4 | 3585 | nl_msg_put_u32(&odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port); |
80e5eed9 BP |
3586 | error = dpif_execute(ofproto->dpif, |
3587 | key.data, key.size, | |
3588 | odp_actions.data, odp_actions.size, | |
abe529af BP |
3589 | packet); |
3590 | ofpbuf_uninit(&odp_actions); | |
3591 | ||
3592 | if (error) { | |
3593 | VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)", | |
3594 | ofproto->up.name, odp_port, strerror(error)); | |
3595 | } | |
3596 | return error; | |
3597 | } | |
3598 | \f | |
df2c07f4 | 3599 | /* OpenFlow to datapath action translation. */ |
abe529af BP |
3600 | |
3601 | static void do_xlate_actions(const union ofp_action *in, size_t n_in, | |
3602 | struct action_xlate_ctx *ctx); | |
4cd78906 | 3603 | static void xlate_normal(struct action_xlate_ctx *); |
abe529af | 3604 | |
98403001 BP |
3605 | static size_t |
3606 | put_userspace_action(const struct ofproto_dpif *ofproto, | |
3607 | struct ofpbuf *odp_actions, | |
3608 | const struct flow *flow, | |
3609 | const struct user_action_cookie *cookie) | |
3610 | { | |
98403001 BP |
3611 | uint32_t pid; |
3612 | ||
3613 | pid = dpif_port_get_pid(ofproto->dpif, | |
3614 | ofp_port_to_odp_port(flow->in_port)); | |
3615 | ||
39db78a0 | 3616 | return odp_put_userspace_action(pid, cookie, odp_actions); |
98403001 BP |
3617 | } |
3618 | ||
6ff686f2 PS |
3619 | /* Compose SAMPLE action for sFlow. */ |
3620 | static size_t | |
3621 | compose_sflow_action(const struct ofproto_dpif *ofproto, | |
3622 | struct ofpbuf *odp_actions, | |
3623 | const struct flow *flow, | |
3624 | uint32_t odp_port) | |
3625 | { | |
3626 | uint32_t port_ifindex; | |
3627 | uint32_t probability; | |
98403001 | 3628 | struct user_action_cookie cookie; |
6ff686f2 | 3629 | size_t sample_offset, actions_offset; |
98403001 | 3630 | int cookie_offset, n_output; |
6ff686f2 PS |
3631 | |
3632 | if (!ofproto->sflow || flow->in_port == OFPP_NONE) { | |
3633 | return 0; | |
3634 | } | |
3635 | ||
3636 | if (odp_port == OVSP_NONE) { | |
3637 | port_ifindex = 0; | |
3638 | n_output = 0; | |
3639 | } else { | |
3640 | port_ifindex = dpif_sflow_odp_port_to_ifindex(ofproto->sflow, odp_port); | |
3641 | n_output = 1; | |
3642 | } | |
3643 | ||
3644 | sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE); | |
3645 | ||
3646 | /* Number of packets out of UINT_MAX to sample. */ | |
3647 | probability = dpif_sflow_get_probability(ofproto->sflow); | |
3648 | nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability); | |
3649 | ||
3650 | actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS); | |
3651 | ||
98403001 BP |
3652 | cookie.type = USER_ACTION_COOKIE_SFLOW; |
3653 | cookie.data = port_ifindex; | |
3654 | cookie.n_output = n_output; | |
3655 | cookie.vlan_tci = 0; | |
3656 | cookie_offset = put_userspace_action(ofproto, odp_actions, flow, &cookie); | |
6ff686f2 PS |
3657 | |
3658 | nl_msg_end_nested(odp_actions, actions_offset); | |
3659 | nl_msg_end_nested(odp_actions, sample_offset); | |
98403001 | 3660 | return cookie_offset; |
6ff686f2 PS |
3661 | } |
3662 | ||
3663 | /* SAMPLE action must be first action in any given list of actions. | |
3664 | * At this point we do not have all information required to build it. So try to | |
3665 | * build sample action as complete as possible. */ | |
3666 | static void | |
3667 | add_sflow_action(struct action_xlate_ctx *ctx) | |
3668 | { | |
3669 | ctx->user_cookie_offset = compose_sflow_action(ctx->ofproto, | |
3670 | ctx->odp_actions, | |
3671 | &ctx->flow, OVSP_NONE); | |
3672 | ctx->sflow_odp_port = 0; | |
3673 | ctx->sflow_n_outputs = 0; | |
3674 | } | |
3675 | ||
3676 | /* Fix SAMPLE action according to data collected while composing ODP actions. | |
3677 | * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested | |
3678 | * USERSPACE action's user-cookie which is required for sflow. */ | |
3679 | static void | |
3680 | fix_sflow_action(struct action_xlate_ctx *ctx) | |
3681 | { | |
3682 | const struct flow *base = &ctx->base_flow; | |
3683 | struct user_action_cookie *cookie; | |
3684 | ||
3685 | if (!ctx->user_cookie_offset) { | |
3686 | return; | |
3687 | } | |
3688 | ||
3689 | cookie = ofpbuf_at(ctx->odp_actions, ctx->user_cookie_offset, | |
3690 | sizeof(*cookie)); | |
3691 | assert(cookie != NULL); | |
3692 | assert(cookie->type == USER_ACTION_COOKIE_SFLOW); | |
3693 | ||
3694 | if (ctx->sflow_n_outputs) { | |
3695 | cookie->data = dpif_sflow_odp_port_to_ifindex(ctx->ofproto->sflow, | |
3696 | ctx->sflow_odp_port); | |
3697 | } | |
3698 | if (ctx->sflow_n_outputs >= 255) { | |
3699 | cookie->n_output = 255; | |
3700 | } else { | |
3701 | cookie->n_output = ctx->sflow_n_outputs; | |
3702 | } | |
3703 | cookie->vlan_tci = base->vlan_tci; | |
3704 | } | |
3705 | ||
823518f1 | 3706 | static void |
fea393b1 BP |
3707 | commit_set_action(struct ofpbuf *odp_actions, enum ovs_key_attr key_type, |
3708 | const void *key, size_t key_size) | |
823518f1 | 3709 | { |
fea393b1 | 3710 | size_t offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SET); |
4edb9ae9 PS |
3711 | nl_msg_put_unspec(odp_actions, key_type, key, key_size); |
3712 | nl_msg_end_nested(odp_actions, offset); | |
3713 | } | |
3714 | ||
3715 | static void | |
3716 | commit_set_tun_id_action(const struct flow *flow, struct flow *base, | |
3717 | struct ofpbuf *odp_actions) | |
3718 | { | |
3719 | if (base->tun_id == flow->tun_id) { | |
3720 | return; | |
823518f1 | 3721 | } |
4edb9ae9 PS |
3722 | base->tun_id = flow->tun_id; |
3723 | ||
fea393b1 BP |
3724 | commit_set_action(odp_actions, OVS_KEY_ATTR_TUN_ID, |
3725 | &base->tun_id, sizeof(base->tun_id)); | |
823518f1 BP |
3726 | } |
3727 | ||
b3e9b2ed | 3728 | static void |
4edb9ae9 PS |
3729 | commit_set_ether_addr_action(const struct flow *flow, struct flow *base, |
3730 | struct ofpbuf *odp_actions) | |
3731 | { | |
3732 | struct ovs_key_ethernet eth_key; | |
3733 | ||
3734 | if (eth_addr_equals(base->dl_src, flow->dl_src) && | |
3735 | eth_addr_equals(base->dl_dst, flow->dl_dst)) { | |
3736 | return; | |
3737 | } | |
3738 | ||
3739 | memcpy(base->dl_src, flow->dl_src, ETH_ADDR_LEN); | |
3740 | memcpy(base->dl_dst, flow->dl_dst, ETH_ADDR_LEN); | |
3741 | ||
3742 | memcpy(eth_key.eth_src, base->dl_src, ETH_ADDR_LEN); | |
3743 | memcpy(eth_key.eth_dst, base->dl_dst, ETH_ADDR_LEN); | |
3744 | ||
fea393b1 BP |
3745 | commit_set_action(odp_actions, OVS_KEY_ATTR_ETHERNET, |
3746 | ð_key, sizeof(eth_key)); | |
4edb9ae9 PS |
3747 | } |
3748 | ||
3749 | static void | |
81b1afb1 EJ |
3750 | commit_vlan_action(const struct flow *flow, struct flow *base, |
3751 | struct ofpbuf *odp_actions) | |
b3e9b2ed | 3752 | { |
81b1afb1 | 3753 | if (base->vlan_tci == flow->vlan_tci) { |
4edb9ae9 | 3754 | return; |
b3e9b2ed EJ |
3755 | } |
3756 | ||
4edb9ae9 | 3757 | if (base->vlan_tci & htons(VLAN_CFI)) { |
81b1afb1 | 3758 | nl_msg_put_flag(odp_actions, OVS_ACTION_ATTR_POP_VLAN); |
b3e9b2ed EJ |
3759 | } |
3760 | ||
81b1afb1 | 3761 | if (flow->vlan_tci & htons(VLAN_CFI)) { |
fea393b1 | 3762 | struct ovs_action_push_vlan vlan; |
4edb9ae9 | 3763 | |
fea393b1 | 3764 | vlan.vlan_tpid = htons(ETH_TYPE_VLAN); |
81b1afb1 EJ |
3765 | vlan.vlan_tci = flow->vlan_tci; |
3766 | nl_msg_put_unspec(odp_actions, OVS_ACTION_ATTR_PUSH_VLAN, | |
fea393b1 | 3767 | &vlan, sizeof vlan); |
b3e9b2ed | 3768 | } |
81b1afb1 | 3769 | base->vlan_tci = flow->vlan_tci; |
4edb9ae9 | 3770 | } |
b3e9b2ed | 3771 | |
4edb9ae9 PS |
3772 | static void |
3773 | commit_set_nw_action(const struct flow *flow, struct flow *base, | |
3774 | struct ofpbuf *odp_actions) | |
3775 | { | |
3776 | struct ovs_key_ipv4 ipv4_key; | |
3777 | ||
3778 | if (base->dl_type != htons(ETH_TYPE_IP) || | |
3779 | !base->nw_src || !base->nw_dst) { | |
3780 | return; | |
3781 | } | |
3782 | ||
3783 | if (base->nw_src == flow->nw_src && | |
3784 | base->nw_dst == flow->nw_dst && | |
eadef313 | 3785 | base->nw_tos == flow->nw_tos && |
a61680c6 | 3786 | base->nw_ttl == flow->nw_ttl && |
eadef313 | 3787 | base->nw_frag == flow->nw_frag) { |
4edb9ae9 | 3788 | return; |
150a9f15 BP |
3789 | } |
3790 | ||
4edb9ae9 PS |
3791 | ipv4_key.ipv4_src = base->nw_src = flow->nw_src; |
3792 | ipv4_key.ipv4_dst = base->nw_dst = flow->nw_dst; | |
be51a446 EJ |
3793 | ipv4_key.ipv4_tos = base->nw_tos = flow->nw_tos; |
3794 | ipv4_key.ipv4_ttl = base->nw_ttl = flow->nw_ttl; | |
4edb9ae9 | 3795 | ipv4_key.ipv4_proto = base->nw_proto; |
eadef313 JP |
3796 | ipv4_key.ipv4_frag = (base->nw_frag == 0 ? OVS_FRAG_TYPE_NONE |
3797 | : base->nw_frag == FLOW_NW_FRAG_ANY | |
3798 | ? OVS_FRAG_TYPE_FIRST : OVS_FRAG_TYPE_LATER); | |
4edb9ae9 | 3799 | |
fea393b1 BP |
3800 | commit_set_action(odp_actions, OVS_KEY_ATTR_IPV4, |
3801 | &ipv4_key, sizeof(ipv4_key)); | |
4edb9ae9 PS |
3802 | } |
3803 | ||
3804 | static void | |
3805 | commit_set_port_action(const struct flow *flow, struct flow *base, | |
3806 | struct ofpbuf *odp_actions) | |
3807 | { | |
3808 | if (!base->tp_src || !base->tp_dst) { | |
3809 | return; | |
b3e9b2ed EJ |
3810 | } |
3811 | ||
4edb9ae9 PS |
3812 | if (base->tp_src == flow->tp_src && |
3813 | base->tp_dst == flow->tp_dst) { | |
3814 | return; | |
b3e9b2ed EJ |
3815 | } |
3816 | ||
4edb9ae9 PS |
3817 | if (flow->nw_proto == IPPROTO_TCP) { |
3818 | struct ovs_key_tcp port_key; | |
3819 | ||
3820 | port_key.tcp_src = base->tp_src = flow->tp_src; | |
3821 | port_key.tcp_dst = base->tp_dst = flow->tp_dst; | |
3822 | ||
fea393b1 BP |
3823 | commit_set_action(odp_actions, OVS_KEY_ATTR_TCP, |
3824 | &port_key, sizeof(port_key)); | |
4edb9ae9 PS |
3825 | |
3826 | } else if (flow->nw_proto == IPPROTO_UDP) { | |
3827 | struct ovs_key_udp port_key; | |
3828 | ||
3829 | port_key.udp_src = base->tp_src = flow->tp_src; | |
3830 | port_key.udp_dst = base->tp_dst = flow->tp_dst; | |
3831 | ||
fea393b1 BP |
3832 | commit_set_action(odp_actions, OVS_KEY_ATTR_UDP, |
3833 | &port_key, sizeof(port_key)); | |
b3e9b2ed | 3834 | } |
4edb9ae9 | 3835 | } |
b3e9b2ed | 3836 | |
4edb9ae9 | 3837 | static void |
abff858b PS |
3838 | commit_set_priority_action(const struct flow *flow, struct flow *base, |
3839 | struct ofpbuf *odp_actions) | |
4edb9ae9 | 3840 | { |
abff858b | 3841 | if (base->priority == flow->priority) { |
4edb9ae9 | 3842 | return; |
b3e9b2ed | 3843 | } |
abff858b | 3844 | base->priority = flow->priority; |
b3e9b2ed | 3845 | |
fea393b1 BP |
3846 | commit_set_action(odp_actions, OVS_KEY_ATTR_PRIORITY, |
3847 | &base->priority, sizeof(base->priority)); | |
4edb9ae9 PS |
3848 | } |
3849 | ||
3850 | static void | |
3851 | commit_odp_actions(struct action_xlate_ctx *ctx) | |
3852 | { | |
3853 | const struct flow *flow = &ctx->flow; | |
3854 | struct flow *base = &ctx->base_flow; | |
3855 | struct ofpbuf *odp_actions = ctx->odp_actions; | |
3856 | ||
3857 | commit_set_tun_id_action(flow, base, odp_actions); | |
3858 | commit_set_ether_addr_action(flow, base, odp_actions); | |
81b1afb1 | 3859 | commit_vlan_action(flow, base, odp_actions); |
4edb9ae9 PS |
3860 | commit_set_nw_action(flow, base, odp_actions); |
3861 | commit_set_port_action(flow, base, odp_actions); | |
abff858b | 3862 | commit_set_priority_action(flow, base, odp_actions); |
b3e9b2ed EJ |
3863 | } |
3864 | ||
6ff686f2 | 3865 | static void |
81b1afb1 EJ |
3866 | compose_output_action__(struct action_xlate_ctx *ctx, uint16_t ofp_port, |
3867 | bool check_stp) | |
6ff686f2 | 3868 | { |
d59906fb | 3869 | const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port); |
5e48dc2b | 3870 | uint16_t odp_port = ofp_port_to_odp_port(ofp_port); |
8b36f51e | 3871 | uint8_t flow_nw_tos = ctx->flow.nw_tos; |
d59906fb | 3872 | |
81b1afb1 | 3873 | if (ofport) { |
8b36f51e EJ |
3874 | struct priority_to_dscp *pdscp; |
3875 | ||
81b1afb1 EJ |
3876 | if (ofport->up.opp.config & htonl(OFPPC_NO_FWD) |
3877 | || (check_stp && !stp_forward_in_state(ofport->stp_state))) { | |
3878 | return; | |
3879 | } | |
8b36f51e EJ |
3880 | |
3881 | pdscp = get_priority(ofport, ctx->flow.priority); | |
3882 | if (pdscp) { | |
3883 | ctx->flow.nw_tos &= ~IP_DSCP_MASK; | |
3884 | ctx->flow.nw_tos |= pdscp->dscp; | |
3885 | } | |
81b1afb1 EJ |
3886 | } else { |
3887 | /* We may not have an ofport record for this port, but it doesn't hurt | |
3888 | * to allow forwarding to it anyhow. Maybe such a port will appear | |
3889 | * later and we're pre-populating the flow table. */ | |
d59906fb EJ |
3890 | } |
3891 | ||
81b1afb1 | 3892 | commit_odp_actions(ctx); |
6ff686f2 PS |
3893 | nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_OUTPUT, odp_port); |
3894 | ctx->sflow_odp_port = odp_port; | |
3895 | ctx->sflow_n_outputs++; | |
81b1afb1 | 3896 | ctx->nf_output_iface = ofp_port; |
8b36f51e | 3897 | ctx->flow.nw_tos = flow_nw_tos; |
6ff686f2 PS |
3898 | } |
3899 | ||
abe529af | 3900 | static void |
5e48dc2b | 3901 | compose_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port) |
abe529af | 3902 | { |
81b1afb1 | 3903 | compose_output_action__(ctx, ofp_port, true); |
abe529af BP |
3904 | } |
3905 | ||
3906 | static void | |
29901626 BP |
3907 | xlate_table_action(struct action_xlate_ctx *ctx, |
3908 | uint16_t in_port, uint8_t table_id) | |
abe529af BP |
3909 | { |
3910 | if (ctx->recurse < MAX_RESUBMIT_RECURSION) { | |
54a9cbc9 | 3911 | struct ofproto_dpif *ofproto = ctx->ofproto; |
abe529af BP |
3912 | struct rule_dpif *rule; |
3913 | uint16_t old_in_port; | |
29901626 BP |
3914 | uint8_t old_table_id; |
3915 | ||
3916 | old_table_id = ctx->table_id; | |
3917 | ctx->table_id = table_id; | |
abe529af | 3918 | |
54a9cbc9 | 3919 | /* Look up a flow with 'in_port' as the input port. */ |
abe529af BP |
3920 | old_in_port = ctx->flow.in_port; |
3921 | ctx->flow.in_port = in_port; | |
54a9cbc9 BP |
3922 | rule = rule_dpif_lookup(ofproto, &ctx->flow, table_id); |
3923 | ||
3924 | /* Tag the flow. */ | |
3925 | if (table_id > 0 && table_id < N_TABLES) { | |
3926 | struct table_dpif *table = &ofproto->tables[table_id]; | |
3927 | if (table->other_table) { | |
3928 | ctx->tags |= (rule | |
3929 | ? rule->tag | |
3930 | : rule_calculate_tag(&ctx->flow, | |
3931 | &table->other_table->wc, | |
3932 | table->basis)); | |
3933 | } | |
3934 | } | |
3935 | ||
3936 | /* Restore the original input port. Otherwise OFPP_NORMAL and | |
3937 | * OFPP_IN_PORT will have surprising behavior. */ | |
abe529af BP |
3938 | ctx->flow.in_port = old_in_port; |
3939 | ||
3940 | if (ctx->resubmit_hook) { | |
3941 | ctx->resubmit_hook(ctx, rule); | |
3942 | } | |
3943 | ||
3944 | if (rule) { | |
3945 | ctx->recurse++; | |
3946 | do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx); | |
3947 | ctx->recurse--; | |
3948 | } | |
29901626 BP |
3949 | |
3950 | ctx->table_id = old_table_id; | |
abe529af BP |
3951 | } else { |
3952 | static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
3953 | ||
29901626 | 3954 | VLOG_ERR_RL(&recurse_rl, "resubmit actions recursed over %d times", |
abe529af BP |
3955 | MAX_RESUBMIT_RECURSION); |
3956 | } | |
3957 | } | |
3958 | ||
29901626 BP |
3959 | static void |
3960 | xlate_resubmit_table(struct action_xlate_ctx *ctx, | |
3961 | const struct nx_action_resubmit *nar) | |
3962 | { | |
3963 | uint16_t in_port; | |
3964 | uint8_t table_id; | |
3965 | ||
3966 | in_port = (nar->in_port == htons(OFPP_IN_PORT) | |
3967 | ? ctx->flow.in_port | |
3968 | : ntohs(nar->in_port)); | |
3969 | table_id = nar->table == 255 ? ctx->table_id : nar->table; | |
3970 | ||
3971 | xlate_table_action(ctx, in_port, table_id); | |
3972 | } | |
3973 | ||
abe529af | 3974 | static void |
d59906fb | 3975 | flood_packets(struct action_xlate_ctx *ctx, bool all) |
abe529af BP |
3976 | { |
3977 | struct ofport_dpif *ofport; | |
3978 | ||
b3e9b2ed EJ |
3979 | commit_odp_actions(ctx); |
3980 | HMAP_FOR_EACH (ofport, up.hmap_node, &ctx->ofproto->up.ports) { | |
abe529af | 3981 | uint16_t ofp_port = ofport->up.ofp_port; |
d59906fb EJ |
3982 | |
3983 | if (ofp_port == ctx->flow.in_port) { | |
3984 | continue; | |
3985 | } | |
3986 | ||
5e48dc2b | 3987 | if (all) { |
81b1afb1 | 3988 | compose_output_action__(ctx, ofp_port, false); |
5e48dc2b EJ |
3989 | } else if (!(ofport->up.opp.config & htonl(OFPPC_NO_FLOOD))) { |
3990 | compose_output_action(ctx, ofp_port); | |
abe529af BP |
3991 | } |
3992 | } | |
b3e9b2ed EJ |
3993 | |
3994 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
abe529af BP |
3995 | } |
3996 | ||
6ff686f2 | 3997 | static void |
98403001 | 3998 | compose_controller_action(struct action_xlate_ctx *ctx, int len) |
6ff686f2 PS |
3999 | { |
4000 | struct user_action_cookie cookie; | |
4001 | ||
81b1afb1 | 4002 | commit_odp_actions(ctx); |
6ff686f2 PS |
4003 | cookie.type = USER_ACTION_COOKIE_CONTROLLER; |
4004 | cookie.data = len; | |
4005 | cookie.n_output = 0; | |
4006 | cookie.vlan_tci = 0; | |
98403001 | 4007 | put_userspace_action(ctx->ofproto, ctx->odp_actions, &ctx->flow, &cookie); |
6ff686f2 PS |
4008 | } |
4009 | ||
abe529af BP |
4010 | static void |
4011 | xlate_output_action__(struct action_xlate_ctx *ctx, | |
4012 | uint16_t port, uint16_t max_len) | |
4013 | { | |
4014 | uint16_t prev_nf_output_iface = ctx->nf_output_iface; | |
4015 | ||
4016 | ctx->nf_output_iface = NF_OUT_DROP; | |
4017 | ||
4018 | switch (port) { | |
4019 | case OFPP_IN_PORT: | |
81b1afb1 | 4020 | compose_output_action(ctx, ctx->flow.in_port); |
abe529af BP |
4021 | break; |
4022 | case OFPP_TABLE: | |
29901626 | 4023 | xlate_table_action(ctx, ctx->flow.in_port, ctx->table_id); |
abe529af BP |
4024 | break; |
4025 | case OFPP_NORMAL: | |
4026 | xlate_normal(ctx); | |
4027 | break; | |
4028 | case OFPP_FLOOD: | |
d59906fb | 4029 | flood_packets(ctx, false); |
abe529af BP |
4030 | break; |
4031 | case OFPP_ALL: | |
d59906fb | 4032 | flood_packets(ctx, true); |
abe529af BP |
4033 | break; |
4034 | case OFPP_CONTROLLER: | |
98403001 | 4035 | compose_controller_action(ctx, max_len); |
abe529af BP |
4036 | break; |
4037 | case OFPP_LOCAL: | |
81b1afb1 | 4038 | compose_output_action(ctx, OFPP_LOCAL); |
abe529af | 4039 | break; |
e81d2933 EJ |
4040 | case OFPP_NONE: |
4041 | break; | |
abe529af BP |
4042 | default: |
4043 | if (port != ctx->flow.in_port) { | |
81b1afb1 | 4044 | compose_output_action(ctx, port); |
abe529af BP |
4045 | } |
4046 | break; | |
4047 | } | |
4048 | ||
4049 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
4050 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
4051 | } else if (ctx->nf_output_iface == NF_OUT_DROP) { | |
4052 | ctx->nf_output_iface = prev_nf_output_iface; | |
4053 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
4054 | ctx->nf_output_iface != NF_OUT_FLOOD) { | |
4055 | ctx->nf_output_iface = NF_OUT_MULTI; | |
4056 | } | |
4057 | } | |
4058 | ||
f694937d EJ |
4059 | static void |
4060 | xlate_output_reg_action(struct action_xlate_ctx *ctx, | |
4061 | const struct nx_action_output_reg *naor) | |
4062 | { | |
4063 | uint64_t ofp_port; | |
4064 | ||
4065 | ofp_port = nxm_read_field_bits(naor->src, naor->ofs_nbits, &ctx->flow); | |
4066 | ||
4067 | if (ofp_port <= UINT16_MAX) { | |
4068 | xlate_output_action__(ctx, ofp_port, ntohs(naor->max_len)); | |
4069 | } | |
4070 | } | |
4071 | ||
abe529af BP |
4072 | static void |
4073 | xlate_output_action(struct action_xlate_ctx *ctx, | |
4074 | const struct ofp_action_output *oao) | |
4075 | { | |
4076 | xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len)); | |
4077 | } | |
4078 | ||
abe529af BP |
4079 | static void |
4080 | xlate_enqueue_action(struct action_xlate_ctx *ctx, | |
4081 | const struct ofp_action_enqueue *oae) | |
4082 | { | |
e479e41e | 4083 | uint16_t ofp_port; |
abff858b | 4084 | uint32_t flow_priority, priority; |
abe529af BP |
4085 | int error; |
4086 | ||
4087 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id), | |
4088 | &priority); | |
4089 | if (error) { | |
4090 | /* Fall back to ordinary output action. */ | |
4091 | xlate_output_action__(ctx, ntohs(oae->port), 0); | |
4092 | return; | |
4093 | } | |
4094 | ||
df2c07f4 | 4095 | /* Figure out datapath output port. */ |
abe529af BP |
4096 | ofp_port = ntohs(oae->port); |
4097 | if (ofp_port == OFPP_IN_PORT) { | |
4098 | ofp_port = ctx->flow.in_port; | |
8ba855c1 BP |
4099 | } else if (ofp_port == ctx->flow.in_port) { |
4100 | return; | |
abe529af | 4101 | } |
abe529af | 4102 | |
df2c07f4 | 4103 | /* Add datapath actions. */ |
abff858b PS |
4104 | flow_priority = ctx->flow.priority; |
4105 | ctx->flow.priority = priority; | |
81b1afb1 | 4106 | compose_output_action(ctx, ofp_port); |
abff858b | 4107 | ctx->flow.priority = flow_priority; |
abe529af BP |
4108 | |
4109 | /* Update NetFlow output port. */ | |
4110 | if (ctx->nf_output_iface == NF_OUT_DROP) { | |
4b23aebf | 4111 | ctx->nf_output_iface = ofp_port; |
abe529af BP |
4112 | } else if (ctx->nf_output_iface != NF_OUT_FLOOD) { |
4113 | ctx->nf_output_iface = NF_OUT_MULTI; | |
4114 | } | |
4115 | } | |
4116 | ||
4117 | static void | |
4118 | xlate_set_queue_action(struct action_xlate_ctx *ctx, | |
4119 | const struct nx_action_set_queue *nasq) | |
4120 | { | |
4121 | uint32_t priority; | |
4122 | int error; | |
4123 | ||
4124 | error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id), | |
4125 | &priority); | |
4126 | if (error) { | |
4127 | /* Couldn't translate queue to a priority, so ignore. A warning | |
4128 | * has already been logged. */ | |
4129 | return; | |
4130 | } | |
4131 | ||
abff858b | 4132 | ctx->flow.priority = priority; |
abe529af BP |
4133 | } |
4134 | ||
4135 | struct xlate_reg_state { | |
4136 | ovs_be16 vlan_tci; | |
4137 | ovs_be64 tun_id; | |
4138 | }; | |
4139 | ||
abe529af BP |
4140 | static void |
4141 | xlate_autopath(struct action_xlate_ctx *ctx, | |
4142 | const struct nx_action_autopath *naa) | |
4143 | { | |
4144 | uint16_t ofp_port = ntohl(naa->id); | |
4145 | struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port); | |
4146 | ||
4147 | if (!port || !port->bundle) { | |
4148 | ofp_port = OFPP_NONE; | |
4149 | } else if (port->bundle->bond) { | |
4150 | /* Autopath does not support VLAN hashing. */ | |
4151 | struct ofport_dpif *slave = bond_choose_output_slave( | |
dc155bff | 4152 | port->bundle->bond, &ctx->flow, 0, &ctx->tags); |
abe529af BP |
4153 | if (slave) { |
4154 | ofp_port = slave->up.ofp_port; | |
4155 | } | |
4156 | } | |
4157 | autopath_execute(naa, &ctx->flow, ofp_port); | |
4158 | } | |
4159 | ||
daff3353 EJ |
4160 | static bool |
4161 | slave_enabled_cb(uint16_t ofp_port, void *ofproto_) | |
4162 | { | |
4163 | struct ofproto_dpif *ofproto = ofproto_; | |
4164 | struct ofport_dpif *port; | |
4165 | ||
4166 | switch (ofp_port) { | |
4167 | case OFPP_IN_PORT: | |
4168 | case OFPP_TABLE: | |
4169 | case OFPP_NORMAL: | |
4170 | case OFPP_FLOOD: | |
4171 | case OFPP_ALL: | |
439e4d8c | 4172 | case OFPP_NONE: |
daff3353 EJ |
4173 | return true; |
4174 | case OFPP_CONTROLLER: /* Not supported by the bundle action. */ | |
4175 | return false; | |
4176 | default: | |
4177 | port = get_ofp_port(ofproto, ofp_port); | |
4178 | return port ? port->may_enable : false; | |
4179 | } | |
4180 | } | |
4181 | ||
75a75043 BP |
4182 | static void |
4183 | xlate_learn_action(struct action_xlate_ctx *ctx, | |
4184 | const struct nx_action_learn *learn) | |
4185 | { | |
4186 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1); | |
4187 | struct ofputil_flow_mod fm; | |
4188 | int error; | |
4189 | ||
4190 | learn_execute(learn, &ctx->flow, &fm); | |
4191 | ||
4192 | error = ofproto_flow_mod(&ctx->ofproto->up, &fm); | |
4193 | if (error && !VLOG_DROP_WARN(&rl)) { | |
4194 | char *msg = ofputil_error_to_string(error); | |
4195 | VLOG_WARN("learning action failed to modify flow table (%s)", msg); | |
4196 | free(msg); | |
4197 | } | |
4198 | ||
4199 | free(fm.actions); | |
4200 | } | |
4201 | ||
21f7563c JP |
4202 | static bool |
4203 | may_receive(const struct ofport_dpif *port, struct action_xlate_ctx *ctx) | |
4204 | { | |
4205 | if (port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp) | |
4206 | ? htonl(OFPPC_NO_RECV_STP) | |
4207 | : htonl(OFPPC_NO_RECV))) { | |
4208 | return false; | |
4209 | } | |
4210 | ||
4211 | /* Only drop packets here if both forwarding and learning are | |
4212 | * disabled. If just learning is enabled, we need to have | |
4213 | * OFPP_NORMAL and the learning action have a look at the packet | |
4214 | * before we can drop it. */ | |
4215 | if (!stp_forward_in_state(port->stp_state) | |
4216 | && !stp_learn_in_state(port->stp_state)) { | |
4217 | return false; | |
4218 | } | |
4219 | ||
4220 | return true; | |
4221 | } | |
4222 | ||
abe529af BP |
4223 | static void |
4224 | do_xlate_actions(const union ofp_action *in, size_t n_in, | |
4225 | struct action_xlate_ctx *ctx) | |
4226 | { | |
4227 | const struct ofport_dpif *port; | |
abe529af | 4228 | const union ofp_action *ia; |
b4b8c781 | 4229 | size_t left; |
abe529af BP |
4230 | |
4231 | port = get_ofp_port(ctx->ofproto, ctx->flow.in_port); | |
21f7563c | 4232 | if (port && !may_receive(port, ctx)) { |
abe529af BP |
4233 | /* Drop this flow. */ |
4234 | return; | |
4235 | } | |
4236 | ||
b4b8c781 | 4237 | OFPUTIL_ACTION_FOR_EACH_UNSAFE (ia, left, in, n_in) { |
abe529af | 4238 | const struct ofp_action_dl_addr *oada; |
38f2e360 BP |
4239 | const struct nx_action_resubmit *nar; |
4240 | const struct nx_action_set_tunnel *nast; | |
4241 | const struct nx_action_set_queue *nasq; | |
4242 | const struct nx_action_multipath *nam; | |
4243 | const struct nx_action_autopath *naa; | |
daff3353 | 4244 | const struct nx_action_bundle *nab; |
f694937d | 4245 | const struct nx_action_output_reg *naor; |
38f2e360 BP |
4246 | enum ofputil_action_code code; |
4247 | ovs_be64 tun_id; | |
4248 | ||
848e8809 EJ |
4249 | if (ctx->exit) { |
4250 | break; | |
4251 | } | |
4252 | ||
38f2e360 BP |
4253 | code = ofputil_decode_action_unsafe(ia); |
4254 | switch (code) { | |
4255 | case OFPUTIL_OFPAT_OUTPUT: | |
abe529af BP |
4256 | xlate_output_action(ctx, &ia->output); |
4257 | break; | |
4258 | ||
38f2e360 | 4259 | case OFPUTIL_OFPAT_SET_VLAN_VID: |
abe529af BP |
4260 | ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK); |
4261 | ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI); | |
abe529af BP |
4262 | break; |
4263 | ||
38f2e360 | 4264 | case OFPUTIL_OFPAT_SET_VLAN_PCP: |
abe529af BP |
4265 | ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK); |
4266 | ctx->flow.vlan_tci |= htons( | |
4267 | (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI); | |
abe529af BP |
4268 | break; |
4269 | ||
38f2e360 | 4270 | case OFPUTIL_OFPAT_STRIP_VLAN: |
abe529af | 4271 | ctx->flow.vlan_tci = htons(0); |
abe529af BP |
4272 | break; |
4273 | ||
38f2e360 | 4274 | case OFPUTIL_OFPAT_SET_DL_SRC: |
abe529af | 4275 | oada = ((struct ofp_action_dl_addr *) ia); |
abe529af BP |
4276 | memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN); |
4277 | break; | |
4278 | ||
38f2e360 | 4279 | case OFPUTIL_OFPAT_SET_DL_DST: |
abe529af | 4280 | oada = ((struct ofp_action_dl_addr *) ia); |
abe529af BP |
4281 | memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN); |
4282 | break; | |
4283 | ||
38f2e360 | 4284 | case OFPUTIL_OFPAT_SET_NW_SRC: |
abe529af BP |
4285 | ctx->flow.nw_src = ia->nw_addr.nw_addr; |
4286 | break; | |
4287 | ||
38f2e360 | 4288 | case OFPUTIL_OFPAT_SET_NW_DST: |
abe529af BP |
4289 | ctx->flow.nw_dst = ia->nw_addr.nw_addr; |
4290 | break; | |
4291 | ||
38f2e360 | 4292 | case OFPUTIL_OFPAT_SET_NW_TOS: |
eadef313 JP |
4293 | ctx->flow.nw_tos &= ~IP_DSCP_MASK; |
4294 | ctx->flow.nw_tos |= ia->nw_tos.nw_tos & IP_DSCP_MASK; | |
abe529af BP |
4295 | break; |
4296 | ||
38f2e360 | 4297 | case OFPUTIL_OFPAT_SET_TP_SRC: |
abe529af BP |
4298 | ctx->flow.tp_src = ia->tp_port.tp_port; |
4299 | break; | |
4300 | ||
38f2e360 | 4301 | case OFPUTIL_OFPAT_SET_TP_DST: |
abe529af BP |
4302 | ctx->flow.tp_dst = ia->tp_port.tp_port; |
4303 | break; | |
4304 | ||
38f2e360 BP |
4305 | case OFPUTIL_OFPAT_ENQUEUE: |
4306 | xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia); | |
4307 | break; | |
4308 | ||
4309 | case OFPUTIL_NXAST_RESUBMIT: | |
4310 | nar = (const struct nx_action_resubmit *) ia; | |
29901626 BP |
4311 | xlate_table_action(ctx, ntohs(nar->in_port), ctx->table_id); |
4312 | break; | |
4313 | ||
4314 | case OFPUTIL_NXAST_RESUBMIT_TABLE: | |
4315 | xlate_resubmit_table(ctx, (const struct nx_action_resubmit *) ia); | |
abe529af BP |
4316 | break; |
4317 | ||
38f2e360 BP |
4318 | case OFPUTIL_NXAST_SET_TUNNEL: |
4319 | nast = (const struct nx_action_set_tunnel *) ia; | |
4320 | tun_id = htonll(ntohl(nast->tun_id)); | |
4321 | ctx->flow.tun_id = tun_id; | |
4322 | break; | |
4323 | ||
4324 | case OFPUTIL_NXAST_SET_QUEUE: | |
4325 | nasq = (const struct nx_action_set_queue *) ia; | |
4326 | xlate_set_queue_action(ctx, nasq); | |
4327 | break; | |
4328 | ||
4329 | case OFPUTIL_NXAST_POP_QUEUE: | |
abff858b | 4330 | ctx->flow.priority = ctx->original_priority; |
38f2e360 BP |
4331 | break; |
4332 | ||
4333 | case OFPUTIL_NXAST_REG_MOVE: | |
4334 | nxm_execute_reg_move((const struct nx_action_reg_move *) ia, | |
4335 | &ctx->flow); | |
4336 | break; | |
4337 | ||
4338 | case OFPUTIL_NXAST_REG_LOAD: | |
4339 | nxm_execute_reg_load((const struct nx_action_reg_load *) ia, | |
4340 | &ctx->flow); | |
4341 | break; | |
4342 | ||
4343 | case OFPUTIL_NXAST_NOTE: | |
4344 | /* Nothing to do. */ | |
4345 | break; | |
4346 | ||
4347 | case OFPUTIL_NXAST_SET_TUNNEL64: | |
4348 | tun_id = ((const struct nx_action_set_tunnel64 *) ia)->tun_id; | |
4349 | ctx->flow.tun_id = tun_id; | |
4350 | break; | |
4351 | ||
4352 | case OFPUTIL_NXAST_MULTIPATH: | |
4353 | nam = (const struct nx_action_multipath *) ia; | |
4354 | multipath_execute(nam, &ctx->flow); | |
abe529af BP |
4355 | break; |
4356 | ||
38f2e360 BP |
4357 | case OFPUTIL_NXAST_AUTOPATH: |
4358 | naa = (const struct nx_action_autopath *) ia; | |
4359 | xlate_autopath(ctx, naa); | |
abe529af | 4360 | break; |
daff3353 EJ |
4361 | |
4362 | case OFPUTIL_NXAST_BUNDLE: | |
4363 | ctx->ofproto->has_bundle_action = true; | |
4364 | nab = (const struct nx_action_bundle *) ia; | |
4365 | xlate_output_action__(ctx, bundle_execute(nab, &ctx->flow, | |
4366 | slave_enabled_cb, | |
4367 | ctx->ofproto), 0); | |
4368 | break; | |
a368bb53 EJ |
4369 | |
4370 | case OFPUTIL_NXAST_BUNDLE_LOAD: | |
4371 | ctx->ofproto->has_bundle_action = true; | |
4372 | nab = (const struct nx_action_bundle *) ia; | |
4373 | bundle_execute_load(nab, &ctx->flow, slave_enabled_cb, | |
4374 | ctx->ofproto); | |
4375 | break; | |
f694937d EJ |
4376 | |
4377 | case OFPUTIL_NXAST_OUTPUT_REG: | |
4378 | naor = (const struct nx_action_output_reg *) ia; | |
4379 | xlate_output_reg_action(ctx, naor); | |
4380 | break; | |
75a75043 BP |
4381 | |
4382 | case OFPUTIL_NXAST_LEARN: | |
4383 | ctx->has_learn = true; | |
4384 | if (ctx->may_learn) { | |
4385 | xlate_learn_action(ctx, (const struct nx_action_learn *) ia); | |
4386 | } | |
4387 | break; | |
848e8809 EJ |
4388 | |
4389 | case OFPUTIL_NXAST_EXIT: | |
4390 | ctx->exit = true; | |
4391 | break; | |
abe529af BP |
4392 | } |
4393 | } | |
21f7563c JP |
4394 | |
4395 | /* We've let OFPP_NORMAL and the learning action look at the packet, | |
4396 | * so drop it now if forwarding is disabled. */ | |
4397 | if (port && !stp_forward_in_state(port->stp_state)) { | |
4398 | ofpbuf_clear(ctx->odp_actions); | |
4399 | add_sflow_action(ctx); | |
4400 | } | |
abe529af BP |
4401 | } |
4402 | ||
4403 | static void | |
4404 | action_xlate_ctx_init(struct action_xlate_ctx *ctx, | |
4405 | struct ofproto_dpif *ofproto, const struct flow *flow, | |
4406 | const struct ofpbuf *packet) | |
4407 | { | |
4408 | ctx->ofproto = ofproto; | |
4409 | ctx->flow = *flow; | |
4410 | ctx->packet = packet; | |
75a75043 | 4411 | ctx->may_learn = packet != NULL; |
abe529af | 4412 | ctx->resubmit_hook = NULL; |
abe529af BP |
4413 | } |
4414 | ||
4415 | static struct ofpbuf * | |
4416 | xlate_actions(struct action_xlate_ctx *ctx, | |
4417 | const union ofp_action *in, size_t n_in) | |
4418 | { | |
4419 | COVERAGE_INC(ofproto_dpif_xlate); | |
4420 | ||
4421 | ctx->odp_actions = ofpbuf_new(512); | |
b6848f13 | 4422 | ofpbuf_reserve(ctx->odp_actions, NL_A_U32_SIZE); |
97e42c92 BP |
4423 | ctx->tags = 0; |
4424 | ctx->may_set_up_flow = true; | |
4425 | ctx->has_learn = false; | |
4426 | ctx->has_normal = false; | |
4427 | ctx->nf_output_iface = NF_OUT_DROP; | |
4428 | ctx->recurse = 0; | |
abff858b | 4429 | ctx->original_priority = ctx->flow.priority; |
97e42c92 BP |
4430 | ctx->base_flow = ctx->flow; |
4431 | ctx->base_flow.tun_id = 0; | |
4432 | ctx->table_id = 0; | |
848e8809 | 4433 | ctx->exit = false; |
7257b535 | 4434 | |
eadef313 | 4435 | if (ctx->flow.nw_frag & FLOW_NW_FRAG_ANY) { |
7257b535 BP |
4436 | switch (ctx->ofproto->up.frag_handling) { |
4437 | case OFPC_FRAG_NORMAL: | |
4438 | /* We must pretend that transport ports are unavailable. */ | |
97e42c92 BP |
4439 | ctx->flow.tp_src = ctx->base_flow.tp_src = htons(0); |
4440 | ctx->flow.tp_dst = ctx->base_flow.tp_dst = htons(0); | |
7257b535 BP |
4441 | break; |
4442 | ||
4443 | case OFPC_FRAG_DROP: | |
4444 | return ctx->odp_actions; | |
4445 | ||
4446 | case OFPC_FRAG_REASM: | |
4447 | NOT_REACHED(); | |
4448 | ||
4449 | case OFPC_FRAG_NX_MATCH: | |
4450 | /* Nothing to do. */ | |
4451 | break; | |
4452 | } | |
4453 | } | |
4454 | ||
fc08b7a2 | 4455 | if (process_special(ctx->ofproto, &ctx->flow, ctx->packet)) { |
abe529af | 4456 | ctx->may_set_up_flow = false; |
b6848f13 | 4457 | return ctx->odp_actions; |
abe529af | 4458 | } else { |
6ff686f2 | 4459 | add_sflow_action(ctx); |
abe529af | 4460 | do_xlate_actions(in, n_in, ctx); |
abe529af | 4461 | |
b6848f13 BP |
4462 | if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow, |
4463 | ctx->odp_actions->data, | |
4464 | ctx->odp_actions->size)) { | |
4465 | ctx->may_set_up_flow = false; | |
4466 | if (ctx->packet | |
4467 | && connmgr_msg_in_hook(ctx->ofproto->up.connmgr, &ctx->flow, | |
4468 | ctx->packet)) { | |
5e48dc2b | 4469 | compose_output_action(ctx, OFPP_LOCAL); |
b6848f13 BP |
4470 | } |
4471 | } | |
a7c4eaf6 | 4472 | fix_sflow_action(ctx); |
abe529af BP |
4473 | } |
4474 | ||
4475 | return ctx->odp_actions; | |
4476 | } | |
4477 | \f | |
4478 | /* OFPP_NORMAL implementation. */ | |
4479 | ||
abe529af BP |
4480 | static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *); |
4481 | ||
ecac4ebf BP |
4482 | /* Given 'vid', the VID obtained from the 802.1Q header that was received as |
4483 | * part of a packet (specify 0 if there was no 802.1Q header), and 'in_bundle', | |
4484 | * the bundle on which the packet was received, returns the VLAN to which the | |
4485 | * packet belongs. | |
4486 | * | |
4487 | * Both 'vid' and the return value are in the range 0...4095. */ | |
4488 | static uint16_t | |
4489 | input_vid_to_vlan(const struct ofbundle *in_bundle, uint16_t vid) | |
4490 | { | |
4491 | switch (in_bundle->vlan_mode) { | |
4492 | case PORT_VLAN_ACCESS: | |
4493 | return in_bundle->vlan; | |
4494 | break; | |
4495 | ||
4496 | case PORT_VLAN_TRUNK: | |
4497 | return vid; | |
4498 | ||
4499 | case PORT_VLAN_NATIVE_UNTAGGED: | |
4500 | case PORT_VLAN_NATIVE_TAGGED: | |
4501 | return vid ? vid : in_bundle->vlan; | |
4502 | ||
4503 | default: | |
4504 | NOT_REACHED(); | |
4505 | } | |
4506 | } | |
4507 | ||
5da5ec37 BP |
4508 | /* Checks whether a packet with the given 'vid' may ingress on 'in_bundle'. |
4509 | * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs | |
4510 | * a warning. | |
4511 | * | |
4512 | * 'vid' should be the VID obtained from the 802.1Q header that was received as | |
4513 | * part of a packet (specify 0 if there was no 802.1Q header), in the range | |
4514 | * 0...4095. */ | |
4515 | static bool | |
4516 | input_vid_is_valid(uint16_t vid, struct ofbundle *in_bundle, bool warn) | |
4517 | { | |
4518 | switch (in_bundle->vlan_mode) { | |
4519 | case PORT_VLAN_ACCESS: | |
4520 | if (vid) { | |
4521 | if (warn) { | |
4522 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4523 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" tagged " | |
4524 | "packet received on port %s configured as VLAN " | |
4525 | "%"PRIu16" access port", | |
4526 | in_bundle->ofproto->up.name, vid, | |
4527 | in_bundle->name, in_bundle->vlan); | |
4528 | } | |
4529 | return false; | |
4530 | } | |
4531 | return true; | |
4532 | ||
4533 | case PORT_VLAN_NATIVE_UNTAGGED: | |
4534 | case PORT_VLAN_NATIVE_TAGGED: | |
4535 | if (!vid) { | |
4536 | /* Port must always carry its native VLAN. */ | |
4537 | return true; | |
4538 | } | |
4539 | /* Fall through. */ | |
4540 | case PORT_VLAN_TRUNK: | |
4541 | if (!ofbundle_includes_vlan(in_bundle, vid)) { | |
4542 | if (warn) { | |
4543 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4544 | VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %"PRIu16" packet " | |
4545 | "received on port %s not configured for trunking " | |
4546 | "VLAN %"PRIu16, | |
4547 | in_bundle->ofproto->up.name, vid, | |
4548 | in_bundle->name, vid); | |
4549 | } | |
4550 | return false; | |
4551 | } | |
4552 | return true; | |
4553 | ||
4554 | default: | |
4555 | NOT_REACHED(); | |
4556 | } | |
4557 | ||
4558 | } | |
4559 | ||
ecac4ebf BP |
4560 | /* Given 'vlan', the VLAN that a packet belongs to, and |
4561 | * 'out_bundle', a bundle on which the packet is to be output, returns the VID | |
4562 | * that should be included in the 802.1Q header. (If the return value is 0, | |
4563 | * then the 802.1Q header should only be included in the packet if there is a | |
4564 | * nonzero PCP.) | |
4565 | * | |
4566 | * Both 'vlan' and the return value are in the range 0...4095. */ | |
4567 | static uint16_t | |
4568 | output_vlan_to_vid(const struct ofbundle *out_bundle, uint16_t vlan) | |
4569 | { | |
4570 | switch (out_bundle->vlan_mode) { | |
4571 | case PORT_VLAN_ACCESS: | |
4572 | return 0; | |
4573 | ||
4574 | case PORT_VLAN_TRUNK: | |
4575 | case PORT_VLAN_NATIVE_TAGGED: | |
4576 | return vlan; | |
4577 | ||
4578 | case PORT_VLAN_NATIVE_UNTAGGED: | |
4579 | return vlan == out_bundle->vlan ? 0 : vlan; | |
4580 | ||
4581 | default: | |
4582 | NOT_REACHED(); | |
4583 | } | |
4584 | } | |
4585 | ||
395e68ce BP |
4586 | static void |
4587 | output_normal(struct action_xlate_ctx *ctx, const struct ofbundle *out_bundle, | |
4588 | uint16_t vlan) | |
abe529af | 4589 | { |
395e68ce BP |
4590 | struct ofport_dpif *port; |
4591 | uint16_t vid; | |
81b1afb1 | 4592 | ovs_be16 tci, old_tci; |
ecac4ebf | 4593 | |
395e68ce BP |
4594 | vid = output_vlan_to_vid(out_bundle, vlan); |
4595 | if (!out_bundle->bond) { | |
4596 | port = ofbundle_get_a_port(out_bundle); | |
4597 | } else { | |
4598 | port = bond_choose_output_slave(out_bundle->bond, &ctx->flow, | |
4599 | vid, &ctx->tags); | |
4600 | if (!port) { | |
4601 | /* No slaves enabled, so drop packet. */ | |
4602 | return; | |
4603 | } | |
4604 | } | |
abe529af | 4605 | |
81b1afb1 | 4606 | old_tci = ctx->flow.vlan_tci; |
5e9ceccd BP |
4607 | tci = htons(vid); |
4608 | if (tci || out_bundle->use_priority_tags) { | |
4609 | tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK); | |
4610 | if (tci) { | |
4611 | tci |= htons(VLAN_CFI); | |
4612 | } | |
395e68ce | 4613 | } |
81b1afb1 | 4614 | ctx->flow.vlan_tci = tci; |
395e68ce | 4615 | |
5e48dc2b | 4616 | compose_output_action(ctx, port->up.ofp_port); |
81b1afb1 | 4617 | ctx->flow.vlan_tci = old_tci; |
abe529af BP |
4618 | } |
4619 | ||
4620 | static int | |
4621 | mirror_mask_ffs(mirror_mask_t mask) | |
4622 | { | |
4623 | BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask)); | |
4624 | return ffs(mask); | |
4625 | } | |
4626 | ||
abe529af BP |
4627 | static bool |
4628 | ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
4629 | { | |
ecac4ebf | 4630 | return (bundle->vlan_mode != PORT_VLAN_ACCESS |
fc3d7408 | 4631 | && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan))); |
abe529af BP |
4632 | } |
4633 | ||
4634 | static bool | |
4635 | ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan) | |
4636 | { | |
4637 | return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan); | |
4638 | } | |
4639 | ||
4640 | /* Returns an arbitrary interface within 'bundle'. */ | |
4641 | static struct ofport_dpif * | |
4642 | ofbundle_get_a_port(const struct ofbundle *bundle) | |
4643 | { | |
4644 | return CONTAINER_OF(list_front(&bundle->ports), | |
4645 | struct ofport_dpif, bundle_node); | |
4646 | } | |
4647 | ||
395e68ce | 4648 | static mirror_mask_t |
abe529af BP |
4649 | compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan, |
4650 | const struct ofbundle *in_bundle, | |
395e68ce | 4651 | const struct ofbundle *out_bundle) |
abe529af | 4652 | { |
395e68ce | 4653 | mirror_mask_t dst_mirrors = 0; |
abe529af BP |
4654 | |
4655 | if (out_bundle == OFBUNDLE_FLOOD) { | |
4656 | struct ofbundle *bundle; | |
4657 | ||
4658 | HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) { | |
4659 | if (bundle != in_bundle | |
4660 | && ofbundle_includes_vlan(bundle, vlan) | |
4661 | && bundle->floodable | |
395e68ce BP |
4662 | && !bundle->mirror_out) { |
4663 | output_normal(ctx, bundle, vlan); | |
4664 | dst_mirrors |= bundle->dst_mirrors; | |
abe529af BP |
4665 | } |
4666 | } | |
4667 | ctx->nf_output_iface = NF_OUT_FLOOD; | |
395e68ce BP |
4668 | } else if (out_bundle) { |
4669 | output_normal(ctx, out_bundle, vlan); | |
4670 | dst_mirrors = out_bundle->dst_mirrors; | |
abe529af | 4671 | } |
395e68ce BP |
4672 | |
4673 | return dst_mirrors; | |
abe529af BP |
4674 | } |
4675 | ||
4676 | static bool | |
4677 | vlan_is_mirrored(const struct ofmirror *m, int vlan) | |
4678 | { | |
fc3d7408 | 4679 | return !m->vlans || bitmap_is_set(m->vlans, vlan); |
abe529af BP |
4680 | } |
4681 | ||
07817dfe BP |
4682 | /* Returns true if a packet with Ethernet destination MAC 'dst' may be mirrored |
4683 | * to a VLAN. In general most packets may be mirrored but we want to drop | |
4684 | * protocols that may confuse switches. */ | |
4685 | static bool | |
4686 | eth_dst_may_rspan(const uint8_t dst[ETH_ADDR_LEN]) | |
4687 | { | |
4688 | /* If you change this function's behavior, please update corresponding | |
4689 | * documentation in vswitch.xml at the same time. */ | |
4690 | if (dst[0] != 0x01) { | |
4691 | /* All the currently banned MACs happen to start with 01 currently, so | |
4692 | * this is a quick way to eliminate most of the good ones. */ | |
4693 | } else { | |
4694 | if (eth_addr_is_reserved(dst)) { | |
4695 | /* Drop STP, IEEE pause frames, and other reserved protocols | |
4696 | * (01-80-c2-00-00-0x). */ | |
4697 | return false; | |
4698 | } | |
4699 | ||
4700 | if (dst[0] == 0x01 && dst[1] == 0x00 && dst[2] == 0x0c) { | |
4701 | /* Cisco OUI. */ | |
4702 | if ((dst[3] & 0xfe) == 0xcc && | |
4703 | (dst[4] & 0xfe) == 0xcc && | |
4704 | (dst[5] & 0xfe) == 0xcc) { | |
4705 | /* Drop the following protocols plus others following the same | |
4706 | pattern: | |
4707 | ||
4708 | CDP, VTP, DTP, PAgP (01-00-0c-cc-cc-cc) | |
4709 | Spanning Tree PVSTP+ (01-00-0c-cc-cc-cd) | |
4710 | STP Uplink Fast (01-00-0c-cd-cd-cd) */ | |
4711 | return false; | |
4712 | } | |
4713 | ||
4714 | if (!(dst[3] | dst[4] | dst[5])) { | |
4715 | /* Drop Inter Switch Link packets (01-00-0c-00-00-00). */ | |
4716 | return false; | |
4717 | } | |
4718 | } | |
4719 | } | |
4720 | return true; | |
4721 | } | |
4722 | ||
abe529af | 4723 | static void |
395e68ce BP |
4724 | output_mirrors(struct action_xlate_ctx *ctx, |
4725 | uint16_t vlan, const struct ofbundle *in_bundle, | |
4726 | mirror_mask_t dst_mirrors) | |
abe529af BP |
4727 | { |
4728 | struct ofproto_dpif *ofproto = ctx->ofproto; | |
4729 | mirror_mask_t mirrors; | |
abe529af | 4730 | |
395e68ce | 4731 | mirrors = in_bundle->src_mirrors | dst_mirrors; |
abe529af BP |
4732 | if (!mirrors) { |
4733 | return; | |
4734 | } | |
4735 | ||
9ba15e2a BP |
4736 | while (mirrors) { |
4737 | struct ofmirror *m; | |
9ba15e2a BP |
4738 | |
4739 | m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1]; | |
4740 | ||
4741 | if (!vlan_is_mirrored(m, vlan)) { | |
4742 | mirrors &= mirrors - 1; | |
4743 | continue; | |
4744 | } | |
4745 | ||
4746 | mirrors &= ~m->dup_mirrors; | |
4747 | if (m->out) { | |
395e68ce | 4748 | output_normal(ctx, m->out, vlan); |
9ba15e2a BP |
4749 | } else if (eth_dst_may_rspan(ctx->flow.dl_dst) |
4750 | && vlan != m->out_vlan) { | |
4751 | struct ofbundle *bundle; | |
4752 | ||
4753 | HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) { | |
4754 | if (ofbundle_includes_vlan(bundle, m->out_vlan) | |
395e68ce BP |
4755 | && !bundle->mirror_out) { |
4756 | output_normal(ctx, bundle, m->out_vlan); | |
abe529af BP |
4757 | } |
4758 | } | |
4759 | } | |
abe529af BP |
4760 | } |
4761 | } | |
4762 | ||
abe529af BP |
4763 | /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after |
4764 | * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to | |
4765 | * indicate this; newer upstream kernels use gratuitous ARP requests. */ | |
4766 | static bool | |
4767 | is_gratuitous_arp(const struct flow *flow) | |
4768 | { | |
4769 | return (flow->dl_type == htons(ETH_TYPE_ARP) | |
4770 | && eth_addr_is_broadcast(flow->dl_dst) | |
4771 | && (flow->nw_proto == ARP_OP_REPLY | |
4772 | || (flow->nw_proto == ARP_OP_REQUEST | |
4773 | && flow->nw_src == flow->nw_dst))); | |
4774 | } | |
4775 | ||
4776 | static void | |
4777 | update_learning_table(struct ofproto_dpif *ofproto, | |
4778 | const struct flow *flow, int vlan, | |
4779 | struct ofbundle *in_bundle) | |
4780 | { | |
4781 | struct mac_entry *mac; | |
4782 | ||
4783 | if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) { | |
4784 | return; | |
4785 | } | |
4786 | ||
4787 | mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan); | |
4788 | if (is_gratuitous_arp(flow)) { | |
4789 | /* We don't want to learn from gratuitous ARP packets that are | |
4790 | * reflected back over bond slaves so we lock the learning table. */ | |
4791 | if (!in_bundle->bond) { | |
4792 | mac_entry_set_grat_arp_lock(mac); | |
4793 | } else if (mac_entry_is_grat_arp_locked(mac)) { | |
4794 | return; | |
4795 | } | |
4796 | } | |
4797 | ||
4798 | if (mac_entry_is_new(mac) || mac->port.p != in_bundle) { | |
4799 | /* The log messages here could actually be useful in debugging, | |
4800 | * so keep the rate limit relatively high. */ | |
4801 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
4802 | VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is " | |
4803 | "on port %s in VLAN %d", | |
4804 | ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src), | |
4805 | in_bundle->name, vlan); | |
4806 | ||
4807 | mac->port.p = in_bundle; | |
4808 | tag_set_add(&ofproto->revalidate_set, | |
4809 | mac_learning_changed(ofproto->ml, mac)); | |
4810 | } | |
4811 | } | |
4812 | ||
395e68ce BP |
4813 | static struct ofport_dpif * |
4814 | lookup_input_bundle(struct ofproto_dpif *ofproto, uint16_t in_port, bool warn) | |
4815 | { | |
4816 | struct ofport_dpif *ofport; | |
4817 | ||
4818 | /* Find the port and bundle for the received packet. */ | |
4819 | ofport = get_ofp_port(ofproto, in_port); | |
4820 | if (ofport && ofport->bundle) { | |
4821 | return ofport; | |
4822 | } | |
4823 | ||
4824 | /* Odd. A few possible reasons here: | |
4825 | * | |
4826 | * - We deleted a port but there are still a few packets queued up | |
4827 | * from it. | |
4828 | * | |
4829 | * - Someone externally added a port (e.g. "ovs-dpctl add-if") that | |
4830 | * we don't know about. | |
4831 | * | |
4832 | * - The ofproto client didn't configure the port as part of a bundle. | |
4833 | */ | |
4834 | if (warn) { | |
4835 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4836 | ||
4837 | VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown " | |
4838 | "port %"PRIu16, ofproto->up.name, in_port); | |
4839 | } | |
4840 | return NULL; | |
4841 | } | |
4842 | ||
5da5ec37 | 4843 | /* Determines whether packets in 'flow' within 'ofproto' should be forwarded or |
abe529af BP |
4844 | * dropped. Returns true if they may be forwarded, false if they should be |
4845 | * dropped. | |
4846 | * | |
395e68ce BP |
4847 | * 'in_port' must be the ofport_dpif that corresponds to flow->in_port. |
4848 | * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull). | |
abe529af | 4849 | * |
395e68ce BP |
4850 | * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as |
4851 | * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as | |
4852 | * checked by input_vid_is_valid(). | |
abe529af BP |
4853 | * |
4854 | * May also add tags to '*tags', although the current implementation only does | |
4855 | * so in one special case. | |
4856 | */ | |
4857 | static bool | |
4858 | is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow, | |
395e68ce | 4859 | struct ofport_dpif *in_port, uint16_t vlan, tag_type *tags) |
abe529af | 4860 | { |
395e68ce | 4861 | struct ofbundle *in_bundle = in_port->bundle; |
abe529af | 4862 | |
395e68ce BP |
4863 | /* Drop frames for reserved multicast addresses |
4864 | * only if forward_bpdu option is absent. */ | |
21f7563c | 4865 | if (eth_addr_is_reserved(flow->dl_dst) && !ofproto->up.forward_bpdu) { |
abe529af BP |
4866 | return false; |
4867 | } | |
4868 | ||
abe529af BP |
4869 | if (in_bundle->bond) { |
4870 | struct mac_entry *mac; | |
4871 | ||
4872 | switch (bond_check_admissibility(in_bundle->bond, in_port, | |
4873 | flow->dl_dst, tags)) { | |
4874 | case BV_ACCEPT: | |
4875 | break; | |
4876 | ||
4877 | case BV_DROP: | |
4878 | return false; | |
4879 | ||
4880 | case BV_DROP_IF_MOVED: | |
4881 | mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL); | |
4882 | if (mac && mac->port.p != in_bundle && | |
4883 | (!is_gratuitous_arp(flow) | |
4884 | || mac_entry_is_grat_arp_locked(mac))) { | |
4885 | return false; | |
4886 | } | |
4887 | break; | |
4888 | } | |
4889 | } | |
4890 | ||
4891 | return true; | |
4892 | } | |
4893 | ||
4cd78906 | 4894 | static void |
abe529af BP |
4895 | xlate_normal(struct action_xlate_ctx *ctx) |
4896 | { | |
395e68ce BP |
4897 | mirror_mask_t dst_mirrors = 0; |
4898 | struct ofport_dpif *in_port; | |
abe529af BP |
4899 | struct ofbundle *in_bundle; |
4900 | struct ofbundle *out_bundle; | |
4901 | struct mac_entry *mac; | |
395e68ce BP |
4902 | uint16_t vlan; |
4903 | uint16_t vid; | |
abe529af | 4904 | |
75a75043 BP |
4905 | ctx->has_normal = true; |
4906 | ||
395e68ce BP |
4907 | /* Obtain in_port from ctx->flow.in_port. |
4908 | * | |
4909 | * lookup_input_bundle() also ensures that in_port belongs to a bundle. */ | |
4910 | in_port = lookup_input_bundle(ctx->ofproto, ctx->flow.in_port, | |
4911 | ctx->packet != NULL); | |
4912 | if (!in_port) { | |
4913 | return; | |
4914 | } | |
4915 | in_bundle = in_port->bundle; | |
4916 | ||
4917 | /* Drop malformed frames. */ | |
4918 | if (ctx->flow.dl_type == htons(ETH_TYPE_VLAN) && | |
4919 | !(ctx->flow.vlan_tci & htons(VLAN_CFI))) { | |
4920 | if (ctx->packet != NULL) { | |
4921 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4922 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial " | |
4923 | "VLAN tag received on port %s", | |
4924 | ctx->ofproto->up.name, in_bundle->name); | |
4925 | } | |
4926 | return; | |
4927 | } | |
4928 | ||
4929 | /* Drop frames on bundles reserved for mirroring. */ | |
4930 | if (in_bundle->mirror_out) { | |
4931 | if (ctx->packet != NULL) { | |
4932 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
4933 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port " | |
4934 | "%s, which is reserved exclusively for mirroring", | |
4935 | ctx->ofproto->up.name, in_bundle->name); | |
4936 | } | |
4937 | return; | |
4938 | } | |
4939 | ||
4940 | /* Check VLAN. */ | |
4941 | vid = vlan_tci_to_vid(ctx->flow.vlan_tci); | |
4942 | if (!input_vid_is_valid(vid, in_bundle, ctx->packet != NULL)) { | |
4943 | return; | |
4944 | } | |
4945 | vlan = input_vid_to_vlan(in_bundle, vid); | |
4946 | ||
4947 | /* Check other admissibility requirements. */ | |
4948 | if (!is_admissible(ctx->ofproto, &ctx->flow, in_port, vlan, &ctx->tags)) { | |
4949 | output_mirrors(ctx, vlan, in_bundle, 0); | |
4950 | return; | |
abe529af BP |
4951 | } |
4952 | ||
75a75043 BP |
4953 | /* Learn source MAC. */ |
4954 | if (ctx->may_learn) { | |
abe529af BP |
4955 | update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle); |
4956 | } | |
4957 | ||
4958 | /* Determine output bundle. */ | |
4959 | mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan, | |
4960 | &ctx->tags); | |
4961 | if (mac) { | |
4962 | out_bundle = mac->port.p; | |
4963 | } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) { | |
4964 | /* If we are revalidating but don't have a learning entry then eject | |
4965 | * the flow. Installing a flow that floods packets opens up a window | |
4966 | * of time where we could learn from a packet reflected on a bond and | |
4967 | * blackhole packets before the learning table is updated to reflect | |
4968 | * the correct port. */ | |
4cd78906 BP |
4969 | ctx->may_set_up_flow = false; |
4970 | return; | |
abe529af BP |
4971 | } else { |
4972 | out_bundle = OFBUNDLE_FLOOD; | |
4973 | } | |
4974 | ||
4975 | /* Don't send packets out their input bundles. */ | |
395e68ce BP |
4976 | if (in_bundle != out_bundle) { |
4977 | dst_mirrors = compose_dsts(ctx, vlan, in_bundle, out_bundle); | |
abe529af | 4978 | } |
395e68ce | 4979 | output_mirrors(ctx, vlan, in_bundle, dst_mirrors); |
abe529af BP |
4980 | } |
4981 | \f | |
54a9cbc9 BP |
4982 | /* Optimized flow revalidation. |
4983 | * | |
4984 | * It's a difficult problem, in general, to tell which facets need to have | |
4985 | * their actions recalculated whenever the OpenFlow flow table changes. We | |
4986 | * don't try to solve that general problem: for most kinds of OpenFlow flow | |
4987 | * table changes, we recalculate the actions for every facet. This is | |
4988 | * relatively expensive, but it's good enough if the OpenFlow flow table | |
4989 | * doesn't change very often. | |
4990 | * | |
4991 | * However, we can expect one particular kind of OpenFlow flow table change to | |
4992 | * happen frequently: changes caused by MAC learning. To avoid wasting a lot | |
4993 | * of CPU on revalidating every facet whenever MAC learning modifies the flow | |
4994 | * table, we add a special case that applies to flow tables in which every rule | |
4995 | * has the same form (that is, the same wildcards), except that the table is | |
4996 | * also allowed to have a single "catch-all" flow that matches all packets. We | |
4997 | * optimize this case by tagging all of the facets that resubmit into the table | |
4998 | * and invalidating the same tag whenever a flow changes in that table. The | |
4999 | * end result is that we revalidate just the facets that need it (and sometimes | |
5000 | * a few more, but not all of the facets or even all of the facets that | |
5001 | * resubmit to the table modified by MAC learning). */ | |
5002 | ||
5003 | /* Calculates the tag to use for 'flow' and wildcards 'wc' when it is inserted | |
5004 | * into an OpenFlow table with the given 'basis'. */ | |
5005 | static uint32_t | |
5006 | rule_calculate_tag(const struct flow *flow, const struct flow_wildcards *wc, | |
5007 | uint32_t secret) | |
5008 | { | |
5009 | if (flow_wildcards_is_catchall(wc)) { | |
5010 | return 0; | |
5011 | } else { | |
5012 | struct flow tag_flow = *flow; | |
5013 | flow_zero_wildcards(&tag_flow, wc); | |
5014 | return tag_create_deterministic(flow_hash(&tag_flow, secret)); | |
5015 | } | |
5016 | } | |
5017 | ||
5018 | /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the | |
5019 | * taggability of that table. | |
5020 | * | |
5021 | * This function must be called after *each* change to a flow table. If you | |
5022 | * skip calling it on some changes then the pointer comparisons at the end can | |
5023 | * be invalid if you get unlucky. For example, if a flow removal causes a | |
5024 | * cls_table to be destroyed and then a flow insertion causes a cls_table with | |
5025 | * different wildcards to be created with the same address, then this function | |
5026 | * will incorrectly skip revalidation. */ | |
5027 | static void | |
5028 | table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id) | |
5029 | { | |
5030 | struct table_dpif *table = &ofproto->tables[table_id]; | |
5031 | const struct classifier *cls = &ofproto->up.tables[table_id]; | |
5032 | struct cls_table *catchall, *other; | |
5033 | struct cls_table *t; | |
5034 | ||
5035 | catchall = other = NULL; | |
5036 | ||
5037 | switch (hmap_count(&cls->tables)) { | |
5038 | case 0: | |
5039 | /* We could tag this OpenFlow table but it would make the logic a | |
5040 | * little harder and it's a corner case that doesn't seem worth it | |
5041 | * yet. */ | |
5042 | break; | |
5043 | ||
5044 | case 1: | |
5045 | case 2: | |
5046 | HMAP_FOR_EACH (t, hmap_node, &cls->tables) { | |
5047 | if (cls_table_is_catchall(t)) { | |
5048 | catchall = t; | |
5049 | } else if (!other) { | |
5050 | other = t; | |
5051 | } else { | |
5052 | /* Indicate that we can't tag this by setting both tables to | |
5053 | * NULL. (We know that 'catchall' is already NULL.) */ | |
5054 | other = NULL; | |
5055 | } | |
5056 | } | |
5057 | break; | |
5058 | ||
5059 | default: | |
5060 | /* Can't tag this table. */ | |
5061 | break; | |
5062 | } | |
5063 | ||
5064 | if (table->catchall_table != catchall || table->other_table != other) { | |
5065 | table->catchall_table = catchall; | |
5066 | table->other_table = other; | |
5067 | ofproto->need_revalidate = true; | |
5068 | } | |
5069 | } | |
5070 | ||
5071 | /* Given 'rule' that has changed in some way (either it is a rule being | |
5072 | * inserted, a rule being deleted, or a rule whose actions are being | |
5073 | * modified), marks facets for revalidation to ensure that packets will be | |
5074 | * forwarded correctly according to the new state of the flow table. | |
5075 | * | |
5076 | * This function must be called after *each* change to a flow table. See | |
5077 | * the comment on table_update_taggable() for more information. */ | |
5078 | static void | |
5079 | rule_invalidate(const struct rule_dpif *rule) | |
5080 | { | |
5081 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto); | |
5082 | ||
5083 | table_update_taggable(ofproto, rule->up.table_id); | |
5084 | ||
5085 | if (!ofproto->need_revalidate) { | |
5086 | struct table_dpif *table = &ofproto->tables[rule->up.table_id]; | |
5087 | ||
5088 | if (table->other_table && rule->tag) { | |
5089 | tag_set_add(&ofproto->revalidate_set, rule->tag); | |
5090 | } else { | |
5091 | ofproto->need_revalidate = true; | |
5092 | } | |
5093 | } | |
5094 | } | |
5095 | \f | |
abe529af | 5096 | static bool |
7257b535 BP |
5097 | set_frag_handling(struct ofproto *ofproto_, |
5098 | enum ofp_config_flags frag_handling) | |
abe529af BP |
5099 | { |
5100 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
abe529af | 5101 | |
7257b535 BP |
5102 | if (frag_handling != OFPC_FRAG_REASM) { |
5103 | ofproto->need_revalidate = true; | |
5104 | return true; | |
5105 | } else { | |
5106 | return false; | |
5107 | } | |
abe529af BP |
5108 | } |
5109 | ||
5110 | static int | |
5111 | packet_out(struct ofproto *ofproto_, struct ofpbuf *packet, | |
5112 | const struct flow *flow, | |
5113 | const union ofp_action *ofp_actions, size_t n_ofp_actions) | |
5114 | { | |
5115 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
5116 | int error; | |
5117 | ||
e1154f71 BP |
5118 | if (flow->in_port >= ofproto->max_ports && flow->in_port < OFPP_MAX) { |
5119 | return ofp_mkerr_nicira(OFPET_BAD_REQUEST, NXBRC_BAD_IN_PORT); | |
5120 | } | |
5121 | ||
abe529af BP |
5122 | error = validate_actions(ofp_actions, n_ofp_actions, flow, |
5123 | ofproto->max_ports); | |
5124 | if (!error) { | |
80e5eed9 | 5125 | struct odputil_keybuf keybuf; |
abe529af BP |
5126 | struct action_xlate_ctx ctx; |
5127 | struct ofpbuf *odp_actions; | |
80e5eed9 BP |
5128 | struct ofpbuf key; |
5129 | ||
5130 | ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); | |
5131 | odp_flow_key_from_flow(&key, flow); | |
abe529af BP |
5132 | |
5133 | action_xlate_ctx_init(&ctx, ofproto, flow, packet); | |
5134 | odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions); | |
80e5eed9 BP |
5135 | dpif_execute(ofproto->dpif, key.data, key.size, |
5136 | odp_actions->data, odp_actions->size, packet); | |
abe529af BP |
5137 | ofpbuf_delete(odp_actions); |
5138 | } | |
5139 | return error; | |
5140 | } | |
5141 | ||
5142 | static void | |
5143 | get_netflow_ids(const struct ofproto *ofproto_, | |
5144 | uint8_t *engine_type, uint8_t *engine_id) | |
5145 | { | |
5146 | struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_); | |
5147 | ||
5148 | dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id); | |
5149 | } | |
5150 | \f | |
5151 | static struct ofproto_dpif * | |
5152 | ofproto_dpif_lookup(const char *name) | |
5153 | { | |
5154 | struct ofproto *ofproto = ofproto_lookup(name); | |
5155 | return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class | |
5156 | ? ofproto_dpif_cast(ofproto) | |
5157 | : NULL); | |
5158 | } | |
5159 | ||
f0a3aa2e AA |
5160 | static void |
5161 | ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, | |
5162 | const char *args, void *aux OVS_UNUSED) | |
5163 | { | |
5164 | const struct ofproto_dpif *ofproto; | |
5165 | ||
5166 | ofproto = ofproto_dpif_lookup(args); | |
5167 | if (!ofproto) { | |
5168 | unixctl_command_reply(conn, 501, "no such bridge"); | |
5169 | return; | |
5170 | } | |
5171 | mac_learning_flush(ofproto->ml); | |
5172 | ||
5173 | unixctl_command_reply(conn, 200, "table successfully flushed"); | |
5174 | } | |
5175 | ||
abe529af BP |
5176 | static void |
5177 | ofproto_unixctl_fdb_show(struct unixctl_conn *conn, | |
5178 | const char *args, void *aux OVS_UNUSED) | |
5179 | { | |
5180 | struct ds ds = DS_EMPTY_INITIALIZER; | |
5181 | const struct ofproto_dpif *ofproto; | |
5182 | const struct mac_entry *e; | |
5183 | ||
5184 | ofproto = ofproto_dpif_lookup(args); | |
5185 | if (!ofproto) { | |
5186 | unixctl_command_reply(conn, 501, "no such bridge"); | |
5187 | return; | |
5188 | } | |
5189 | ||
5190 | ds_put_cstr(&ds, " port VLAN MAC Age\n"); | |
5191 | LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) { | |
5192 | struct ofbundle *bundle = e->port.p; | |
5193 | ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n", | |
5194 | ofbundle_get_a_port(bundle)->odp_port, | |
5195 | e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e)); | |
5196 | } | |
5197 | unixctl_command_reply(conn, 200, ds_cstr(&ds)); | |
5198 | ds_destroy(&ds); | |
5199 | } | |
5200 | ||
5201 | struct ofproto_trace { | |
5202 | struct action_xlate_ctx ctx; | |
5203 | struct flow flow; | |
5204 | struct ds *result; | |
5205 | }; | |
5206 | ||
5207 | static void | |
29901626 BP |
5208 | trace_format_rule(struct ds *result, uint8_t table_id, int level, |
5209 | const struct rule_dpif *rule) | |
abe529af BP |
5210 | { |
5211 | ds_put_char_multiple(result, '\t', level); | |
5212 | if (!rule) { | |
5213 | ds_put_cstr(result, "No match\n"); | |
5214 | return; | |
5215 | } | |
5216 | ||
29901626 BP |
5217 | ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ", |
5218 | table_id, ntohll(rule->up.flow_cookie)); | |
79feb7df | 5219 | cls_rule_format(&rule->up.cr, result); |
abe529af BP |
5220 | ds_put_char(result, '\n'); |
5221 | ||
5222 | ds_put_char_multiple(result, '\t', level); | |
5223 | ds_put_cstr(result, "OpenFlow "); | |
79feb7df | 5224 | ofp_print_actions(result, rule->up.actions, rule->up.n_actions); |
abe529af BP |
5225 | ds_put_char(result, '\n'); |
5226 | } | |
5227 | ||
5228 | static void | |
5229 | trace_format_flow(struct ds *result, int level, const char *title, | |
5230 | struct ofproto_trace *trace) | |
5231 | { | |
5232 | ds_put_char_multiple(result, '\t', level); | |
5233 | ds_put_format(result, "%s: ", title); | |
5234 | if (flow_equal(&trace->ctx.flow, &trace->flow)) { | |
5235 | ds_put_cstr(result, "unchanged"); | |
5236 | } else { | |
5237 | flow_format(result, &trace->ctx.flow); | |
5238 | trace->flow = trace->ctx.flow; | |
5239 | } | |
5240 | ds_put_char(result, '\n'); | |
5241 | } | |
5242 | ||
eb9e1c26 EJ |
5243 | static void |
5244 | trace_format_regs(struct ds *result, int level, const char *title, | |
5245 | struct ofproto_trace *trace) | |
5246 | { | |
5247 | size_t i; | |
5248 | ||
5249 | ds_put_char_multiple(result, '\t', level); | |
5250 | ds_put_format(result, "%s:", title); | |
5251 | for (i = 0; i < FLOW_N_REGS; i++) { | |
5252 | ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]); | |
5253 | } | |
5254 | ds_put_char(result, '\n'); | |
5255 | } | |
5256 | ||
abe529af BP |
5257 | static void |
5258 | trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule) | |
5259 | { | |
5260 | struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx); | |
5261 | struct ds *result = trace->result; | |
5262 | ||
5263 | ds_put_char(result, '\n'); | |
5264 | trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace); | |
eb9e1c26 | 5265 | trace_format_regs(result, ctx->recurse + 1, "Resubmitted regs", trace); |
29901626 | 5266 | trace_format_rule(result, ctx->table_id, ctx->recurse + 1, rule); |
abe529af BP |
5267 | } |
5268 | ||
5269 | static void | |
5270 | ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_, | |
5271 | void *aux OVS_UNUSED) | |
5272 | { | |
abff858b | 5273 | char *dpname, *arg1, *arg2, *arg3, *arg4; |
abe529af BP |
5274 | char *args = xstrdup(args_); |
5275 | char *save_ptr = NULL; | |
5276 | struct ofproto_dpif *ofproto; | |
876b0e1c BP |
5277 | struct ofpbuf odp_key; |
5278 | struct ofpbuf *packet; | |
abe529af BP |
5279 | struct rule_dpif *rule; |
5280 | struct ds result; | |
5281 | struct flow flow; | |
abe529af BP |
5282 | char *s; |
5283 | ||
876b0e1c BP |
5284 | packet = NULL; |
5285 | ofpbuf_init(&odp_key, 0); | |
abe529af BP |
5286 | ds_init(&result); |
5287 | ||
5288 | dpname = strtok_r(args, " ", &save_ptr); | |
876b0e1c BP |
5289 | arg1 = strtok_r(NULL, " ", &save_ptr); |
5290 | arg2 = strtok_r(NULL, " ", &save_ptr); | |
abff858b PS |
5291 | arg3 = strtok_r(NULL, " ", &save_ptr); |
5292 | arg4 = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */ | |
8b3b8dd1 BP |
5293 | if (dpname && arg1 && (!arg2 || !strcmp(arg2, "-generate")) && !arg3) { |
5294 | /* ofproto/trace dpname flow [-generate] */ | |
876b0e1c BP |
5295 | int error; |
5296 | ||
df2c07f4 | 5297 | /* Convert string to datapath key. */ |
876b0e1c | 5298 | ofpbuf_init(&odp_key, 0); |
b2a60db8 | 5299 | error = odp_flow_key_from_string(arg1, NULL, &odp_key); |
876b0e1c BP |
5300 | if (error) { |
5301 | unixctl_command_reply(conn, 501, "Bad flow syntax"); | |
5302 | goto exit; | |
5303 | } | |
5304 | ||
5305 | /* Convert odp_key to flow. */ | |
5306 | error = odp_flow_key_to_flow(odp_key.data, odp_key.size, &flow); | |
5307 | if (error) { | |
5308 | unixctl_command_reply(conn, 501, "Invalid flow"); | |
5309 | goto exit; | |
5310 | } | |
8b3b8dd1 BP |
5311 | |
5312 | /* Generate a packet, if requested. */ | |
5313 | if (arg2) { | |
5314 | packet = ofpbuf_new(0); | |
5315 | flow_compose(packet, &flow); | |
5316 | } | |
abff858b PS |
5317 | } else if (dpname && arg1 && arg2 && arg3 && arg4) { |
5318 | /* ofproto/trace dpname priority tun_id in_port packet */ | |
876b0e1c BP |
5319 | uint16_t in_port; |
5320 | ovs_be64 tun_id; | |
abff858b | 5321 | uint32_t priority; |
876b0e1c | 5322 | |
abff858b PS |
5323 | priority = atoi(arg1); |
5324 | tun_id = htonll(strtoull(arg2, NULL, 0)); | |
5325 | in_port = ofp_port_to_odp_port(atoi(arg3)); | |
876b0e1c BP |
5326 | |
5327 | packet = ofpbuf_new(strlen(args) / 2); | |
abff858b PS |
5328 | arg4 = ofpbuf_put_hex(packet, arg4, NULL); |
5329 | arg4 += strspn(arg4, " "); | |
5330 | if (*arg4 != '\0') { | |
876b0e1c BP |
5331 | unixctl_command_reply(conn, 501, "Trailing garbage in command"); |
5332 | goto exit; | |
5333 | } | |
5334 | if (packet->size < ETH_HEADER_LEN) { | |
5335 | unixctl_command_reply(conn, 501, | |
5336 | "Packet data too short for Ethernet"); | |
5337 | goto exit; | |
5338 | } | |
5339 | ||
5340 | ds_put_cstr(&result, "Packet: "); | |
5341 | s = ofp_packet_to_string(packet->data, packet->size, packet->size); | |
5342 | ds_put_cstr(&result, s); | |
5343 | free(s); | |
5344 | ||
abff858b | 5345 | flow_extract(packet, priority, tun_id, in_port, &flow); |
876b0e1c | 5346 | } else { |
abe529af BP |
5347 | unixctl_command_reply(conn, 501, "Bad command syntax"); |
5348 | goto exit; | |
5349 | } | |
5350 | ||
5351 | ofproto = ofproto_dpif_lookup(dpname); | |
5352 | if (!ofproto) { | |
5353 | unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list " | |
5354 | "for help)"); | |
5355 | goto exit; | |
5356 | } | |
5357 | ||
abe529af BP |
5358 | ds_put_cstr(&result, "Flow: "); |
5359 | flow_format(&result, &flow); | |
5360 | ds_put_char(&result, '\n'); | |
5361 | ||
29901626 BP |
5362 | rule = rule_dpif_lookup(ofproto, &flow, 0); |
5363 | trace_format_rule(&result, 0, 0, rule); | |
abe529af BP |
5364 | if (rule) { |
5365 | struct ofproto_trace trace; | |
5366 | struct ofpbuf *odp_actions; | |
5367 | ||
5368 | trace.result = &result; | |
5369 | trace.flow = flow; | |
876b0e1c | 5370 | action_xlate_ctx_init(&trace.ctx, ofproto, &flow, packet); |
abe529af BP |
5371 | trace.ctx.resubmit_hook = trace_resubmit; |
5372 | odp_actions = xlate_actions(&trace.ctx, | |
5373 | rule->up.actions, rule->up.n_actions); | |
5374 | ||
5375 | ds_put_char(&result, '\n'); | |
5376 | trace_format_flow(&result, 0, "Final flow", &trace); | |
5377 | ds_put_cstr(&result, "Datapath actions: "); | |
5378 | format_odp_actions(&result, odp_actions->data, odp_actions->size); | |
5379 | ofpbuf_delete(odp_actions); | |
876b0e1c BP |
5380 | |
5381 | if (!trace.ctx.may_set_up_flow) { | |
5382 | if (packet) { | |
5383 | ds_put_cstr(&result, "\nThis flow is not cachable."); | |
5384 | } else { | |
5385 | ds_put_cstr(&result, "\nThe datapath actions are incomplete--" | |
5386 | "for complete actions, please supply a packet."); | |
5387 | } | |
5388 | } | |
abe529af BP |
5389 | } |
5390 | ||
5391 | unixctl_command_reply(conn, 200, ds_cstr(&result)); | |
5392 | ||
5393 | exit: | |
5394 | ds_destroy(&result); | |
876b0e1c BP |
5395 | ofpbuf_delete(packet); |
5396 | ofpbuf_uninit(&odp_key); | |
abe529af BP |
5397 | free(args); |
5398 | } | |
5399 | ||
7ee20df1 BP |
5400 | static void |
5401 | ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, | |
5402 | const char *args_ OVS_UNUSED, void *aux OVS_UNUSED) | |
5403 | { | |
5404 | clogged = true; | |
5405 | unixctl_command_reply(conn, 200, NULL); | |
5406 | } | |
5407 | ||
5408 | static void | |
5409 | ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, | |
5410 | const char *args_ OVS_UNUSED, void *aux OVS_UNUSED) | |
5411 | { | |
5412 | clogged = false; | |
5413 | unixctl_command_reply(conn, 200, NULL); | |
5414 | } | |
5415 | ||
abe529af BP |
5416 | static void |
5417 | ofproto_dpif_unixctl_init(void) | |
5418 | { | |
5419 | static bool registered; | |
5420 | if (registered) { | |
5421 | return; | |
5422 | } | |
5423 | registered = true; | |
5424 | ||
7ff2009a JP |
5425 | unixctl_command_register("ofproto/trace", |
5426 | "bridge {tun_id in_port packet | odp_flow [-generate]}", | |
5427 | ofproto_unixctl_trace, NULL); | |
f0a3aa2e AA |
5428 | unixctl_command_register("fdb/flush", "bridge", ofproto_unixctl_fdb_flush, |
5429 | NULL); | |
7ff2009a | 5430 | unixctl_command_register("fdb/show", "bridge", ofproto_unixctl_fdb_show, |
e29749d5 | 5431 | NULL); |
7ff2009a JP |
5432 | unixctl_command_register("ofproto/clog", "", ofproto_dpif_clog, NULL); |
5433 | unixctl_command_register("ofproto/unclog", "", ofproto_dpif_unclog, NULL); | |
abe529af BP |
5434 | } |
5435 | \f | |
5436 | const struct ofproto_class ofproto_dpif_class = { | |
5437 | enumerate_types, | |
5438 | enumerate_names, | |
5439 | del, | |
5440 | alloc, | |
5441 | construct, | |
5442 | destruct, | |
5443 | dealloc, | |
5444 | run, | |
5445 | wait, | |
5446 | flush, | |
6c1491fb BP |
5447 | get_features, |
5448 | get_tables, | |
abe529af BP |
5449 | port_alloc, |
5450 | port_construct, | |
5451 | port_destruct, | |
5452 | port_dealloc, | |
5453 | port_modified, | |
5454 | port_reconfigured, | |
5455 | port_query_by_name, | |
5456 | port_add, | |
5457 | port_del, | |
5458 | port_dump_start, | |
5459 | port_dump_next, | |
5460 | port_dump_done, | |
5461 | port_poll, | |
5462 | port_poll_wait, | |
5463 | port_is_lacp_current, | |
0ab6decf | 5464 | NULL, /* rule_choose_table */ |
abe529af BP |
5465 | rule_alloc, |
5466 | rule_construct, | |
5467 | rule_destruct, | |
5468 | rule_dealloc, | |
abe529af BP |
5469 | rule_get_stats, |
5470 | rule_execute, | |
5471 | rule_modify_actions, | |
7257b535 | 5472 | set_frag_handling, |
abe529af BP |
5473 | packet_out, |
5474 | set_netflow, | |
5475 | get_netflow_ids, | |
5476 | set_sflow, | |
5477 | set_cfm, | |
a5610457 | 5478 | get_cfm_fault, |
1de11730 | 5479 | get_cfm_remote_mpids, |
21f7563c JP |
5480 | set_stp, |
5481 | get_stp_status, | |
5482 | set_stp_port, | |
5483 | get_stp_port_status, | |
8b36f51e | 5484 | set_queues, |
abe529af BP |
5485 | bundle_set, |
5486 | bundle_remove, | |
5487 | mirror_set, | |
5488 | set_flood_vlans, | |
5489 | is_mirror_output_bundle, | |
8402c74b | 5490 | forward_bpdu_changed, |
abe529af | 5491 | }; |