]>
Commit | Line | Data |
---|---|---|
9583bc14 EJ |
1 | /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. |
2 | * | |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at: | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. */ | |
14 | ||
15 | #include <config.h> | |
16 | ||
17 | #include "ofproto/ofproto-dpif-xlate.h" | |
18 | ||
8449c4d6 EJ |
19 | #include <errno.h> |
20 | ||
db7d4e46 | 21 | #include "bfd.h" |
9583bc14 EJ |
22 | #include "bitmap.h" |
23 | #include "bond.h" | |
24 | #include "bundle.h" | |
25 | #include "byte-order.h" | |
db7d4e46 | 26 | #include "cfm.h" |
9583bc14 EJ |
27 | #include "connmgr.h" |
28 | #include "coverage.h" | |
29 | #include "dpif.h" | |
30 | #include "dynamic-string.h" | |
f7f1ea29 | 31 | #include "in-band.h" |
db7d4e46 | 32 | #include "lacp.h" |
9583bc14 | 33 | #include "learn.h" |
46c88433 | 34 | #include "list.h" |
9583bc14 EJ |
35 | #include "mac-learning.h" |
36 | #include "meta-flow.h" | |
37 | #include "multipath.h" | |
38 | #include "netdev-vport.h" | |
39 | #include "netlink.h" | |
40 | #include "nx-match.h" | |
41 | #include "odp-execute.h" | |
42 | #include "ofp-actions.h" | |
43 | #include "ofproto/ofproto-dpif-ipfix.h" | |
ec7ceaed | 44 | #include "ofproto/ofproto-dpif-mirror.h" |
9583bc14 EJ |
45 | #include "ofproto/ofproto-dpif-sflow.h" |
46 | #include "ofproto/ofproto-dpif.h" | |
6f00e29b | 47 | #include "ofproto/ofproto-provider.h" |
9583bc14 EJ |
48 | #include "tunnel.h" |
49 | #include "vlog.h" | |
50 | ||
46c88433 | 51 | COVERAGE_DEFINE(xlate_actions); |
9583bc14 EJ |
52 | |
53 | VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate); | |
54 | ||
8a553e9a EJ |
55 | /* Maximum depth of flow table recursion (due to resubmit actions) in a |
56 | * flow translation. */ | |
57 | #define MAX_RESUBMIT_RECURSION 64 | |
58 | ||
98b07853 BP |
59 | /* Maximum number of resubmit actions in a flow translation, whether they are |
60 | * recursive or not. */ | |
61 | #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION) | |
62 | ||
dc24a00f EJ |
63 | struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER; |
64 | ||
46c88433 EJ |
65 | struct xbridge { |
66 | struct hmap_node hmap_node; /* Node in global 'xbridges' map. */ | |
67 | struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */ | |
68 | ||
69 | struct list xbundles; /* Owned xbundles. */ | |
70 | struct hmap xports; /* Indexed by ofp_port. */ | |
71 | ||
72 | char *name; /* Name used in log messages. */ | |
89a8a7f0 | 73 | struct dpif *dpif; /* Datapath interface. */ |
46c88433 EJ |
74 | struct mac_learning *ml; /* Mac learning handle. */ |
75 | struct mbridge *mbridge; /* Mirroring. */ | |
76 | struct dpif_sflow *sflow; /* SFlow handle, or null. */ | |
77 | struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */ | |
9d189a50 | 78 | struct stp *stp; /* STP or null if disabled. */ |
46c88433 | 79 | |
ec89fc6f EJ |
80 | /* Special rules installed by ofproto-dpif. */ |
81 | struct rule_dpif *miss_rule; | |
82 | struct rule_dpif *no_packet_in_rule; | |
83 | ||
46c88433 | 84 | enum ofp_config_flags frag; /* Fragmentation handling. */ |
46c88433 EJ |
85 | bool has_netflow; /* Bridge runs netflow? */ |
86 | bool has_in_band; /* Bridge has in band control? */ | |
87 | bool forward_bpdu; /* Bridge forwards STP BPDUs? */ | |
88 | }; | |
89 | ||
90 | struct xbundle { | |
91 | struct hmap_node hmap_node; /* In global 'xbundles' map. */ | |
92 | struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */ | |
93 | ||
94 | struct list list_node; /* In parent 'xbridges' list. */ | |
95 | struct xbridge *xbridge; /* Parent xbridge. */ | |
96 | ||
97 | struct list xports; /* Contains "struct xport"s. */ | |
98 | ||
99 | char *name; /* Name used in log messages. */ | |
100 | struct bond *bond; /* Nonnull iff more than one port. */ | |
101 | struct lacp *lacp; /* LACP handle or null. */ | |
102 | ||
103 | enum port_vlan_mode vlan_mode; /* VLAN mode. */ | |
104 | int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */ | |
105 | unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1. | |
106 | * NULL if all VLANs are trunked. */ | |
107 | bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */ | |
108 | bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */ | |
109 | }; | |
110 | ||
111 | struct xport { | |
112 | struct hmap_node hmap_node; /* Node in global 'xports' map. */ | |
113 | struct ofport_dpif *ofport; /* Key in global 'xports map. */ | |
114 | ||
115 | struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */ | |
116 | ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */ | |
117 | ||
118 | odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */ | |
119 | ||
120 | struct list bundle_node; /* In parent xbundle (if it exists). */ | |
121 | struct xbundle *xbundle; /* Parent xbundle or null. */ | |
122 | ||
123 | struct netdev *netdev; /* 'ofport''s netdev. */ | |
124 | ||
125 | struct xbridge *xbridge; /* Parent bridge. */ | |
126 | struct xport *peer; /* Patch port peer or null. */ | |
127 | ||
128 | enum ofputil_port_config config; /* OpenFlow port configuration. */ | |
92cf817b | 129 | int stp_port_no; /* STP port number or -1 if not in use. */ |
46c88433 | 130 | |
55954f6e EJ |
131 | struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */ |
132 | ||
46c88433 EJ |
133 | bool may_enable; /* May be enabled in bonds. */ |
134 | bool is_tunnel; /* Is a tunnel port. */ | |
135 | ||
136 | struct cfm *cfm; /* CFM handle or null. */ | |
137 | struct bfd *bfd; /* BFD handle or null. */ | |
138 | }; | |
139 | ||
4d0acc70 EJ |
140 | struct xlate_ctx { |
141 | struct xlate_in *xin; | |
142 | struct xlate_out *xout; | |
143 | ||
46c88433 | 144 | const struct xbridge *xbridge; |
4d0acc70 EJ |
145 | |
146 | /* Flow at the last commit. */ | |
147 | struct flow base_flow; | |
148 | ||
149 | /* Tunnel IP destination address as received. This is stored separately | |
150 | * as the base_flow.tunnel is cleared on init to reflect the datapath | |
151 | * behavior. Used to make sure not to send tunneled output to ourselves, | |
152 | * which might lead to an infinite loop. This could happen easily | |
153 | * if a tunnel is marked as 'ip_remote=flow', and the flow does not | |
154 | * actually set the tun_dst field. */ | |
155 | ovs_be32 orig_tunnel_ip_dst; | |
156 | ||
157 | /* Stack for the push and pop actions. Each stack element is of type | |
158 | * "union mf_subvalue". */ | |
159 | union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)]; | |
160 | struct ofpbuf stack; | |
161 | ||
162 | /* The rule that we are currently translating, or NULL. */ | |
163 | struct rule_dpif *rule; | |
164 | ||
b0a17866 SH |
165 | int mpls_depth_delta; /* Delta of the mpls stack depth since |
166 | * actions were last committed. | |
167 | * Must be between -1 and 1 inclusive. */ | |
168 | ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE | |
169 | * prior to an mpls_push so that it may be | |
170 | * used for a subsequent mpls_pop. */ | |
171 | ||
98b07853 BP |
172 | /* Resubmit statistics, via xlate_table_action(). */ |
173 | int recurse; /* Current resubmit nesting depth. */ | |
174 | int resubmits; /* Total number of resubmits. */ | |
175 | ||
4d0acc70 EJ |
176 | uint32_t orig_skb_priority; /* Priority when packet arrived. */ |
177 | uint8_t table_id; /* OpenFlow table ID where flow was found. */ | |
178 | uint32_t sflow_n_outputs; /* Number of output ports. */ | |
4e022ec0 | 179 | odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */ |
4d0acc70 EJ |
180 | uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */ |
181 | bool exit; /* No further actions should be processed. */ | |
182 | }; | |
183 | ||
9583bc14 EJ |
184 | /* A controller may use OFPP_NONE as the ingress port to indicate that |
185 | * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for | |
186 | * when an input bundle is needed for validation (e.g., mirroring or | |
187 | * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have | |
3815d6c2 LS |
188 | * any 'port' structs, so care must be taken when dealing with it. |
189 | * The bundle's name and vlan mode are initialized in lookup_input_bundle() */ | |
190 | static struct xbundle ofpp_none_bundle; | |
9583bc14 | 191 | |
55954f6e EJ |
192 | /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from |
193 | * 'priority' (the datapath's term for QoS queue) to the dscp bits which all | |
194 | * traffic egressing the 'ofport' with that priority should be marked with. */ | |
195 | struct skb_priority_to_dscp { | |
196 | struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */ | |
197 | uint32_t skb_priority; /* Priority of this queue (see struct flow). */ | |
198 | ||
199 | uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */ | |
200 | }; | |
201 | ||
46c88433 EJ |
202 | static struct hmap xbridges = HMAP_INITIALIZER(&xbridges); |
203 | static struct hmap xbundles = HMAP_INITIALIZER(&xbundles); | |
204 | static struct hmap xports = HMAP_INITIALIZER(&xports); | |
205 | ||
206 | static bool may_receive(const struct xport *, struct xlate_ctx *); | |
9583bc14 EJ |
207 | static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len, |
208 | struct xlate_ctx *); | |
91d6cd12 AW |
209 | static void xlate_actions__(struct xlate_in *, struct xlate_out *) |
210 | OVS_REQ_RDLOCK(xlate_rwlock); | |
9583bc14 EJ |
211 | static void xlate_normal(struct xlate_ctx *); |
212 | static void xlate_report(struct xlate_ctx *, const char *); | |
4e022ec0 | 213 | static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port, |
9583bc14 | 214 | uint8_t table_id, bool may_packet_in); |
46c88433 EJ |
215 | static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn); |
216 | static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid); | |
217 | static void output_normal(struct xlate_ctx *, const struct xbundle *, | |
9583bc14 | 218 | uint16_t vlan); |
4e022ec0 | 219 | static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port); |
9583bc14 | 220 | |
46c88433 EJ |
221 | static struct xbridge *xbridge_lookup(const struct ofproto_dpif *); |
222 | static struct xbundle *xbundle_lookup(const struct ofbundle *); | |
5e6af486 | 223 | static struct xport *xport_lookup(const struct ofport_dpif *); |
46c88433 | 224 | static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port); |
55954f6e EJ |
225 | static struct skb_priority_to_dscp *get_skb_priority(const struct xport *, |
226 | uint32_t skb_priority); | |
227 | static void clear_skb_priorities(struct xport *); | |
228 | static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority, | |
229 | uint8_t *dscp); | |
46c88433 EJ |
230 | |
231 | void | |
232 | xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name, | |
ec89fc6f EJ |
233 | struct dpif *dpif, struct rule_dpif *miss_rule, |
234 | struct rule_dpif *no_packet_in_rule, | |
235 | const struct mac_learning *ml, struct stp *stp, | |
236 | const struct mbridge *mbridge, | |
46c88433 EJ |
237 | const struct dpif_sflow *sflow, |
238 | const struct dpif_ipfix *ipfix, enum ofp_config_flags frag, | |
9d189a50 | 239 | bool forward_bpdu, bool has_in_band, bool has_netflow) |
46c88433 EJ |
240 | { |
241 | struct xbridge *xbridge = xbridge_lookup(ofproto); | |
242 | ||
243 | if (!xbridge) { | |
244 | xbridge = xzalloc(sizeof *xbridge); | |
245 | xbridge->ofproto = ofproto; | |
246 | ||
247 | hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0)); | |
248 | hmap_init(&xbridge->xports); | |
249 | list_init(&xbridge->xbundles); | |
250 | } | |
251 | ||
252 | if (xbridge->ml != ml) { | |
253 | mac_learning_unref(xbridge->ml); | |
254 | xbridge->ml = mac_learning_ref(ml); | |
255 | } | |
256 | ||
257 | if (xbridge->mbridge != mbridge) { | |
258 | mbridge_unref(xbridge->mbridge); | |
259 | xbridge->mbridge = mbridge_ref(mbridge); | |
260 | } | |
261 | ||
262 | if (xbridge->sflow != sflow) { | |
263 | dpif_sflow_unref(xbridge->sflow); | |
264 | xbridge->sflow = dpif_sflow_ref(sflow); | |
265 | } | |
266 | ||
267 | if (xbridge->ipfix != ipfix) { | |
268 | dpif_ipfix_unref(xbridge->ipfix); | |
269 | xbridge->ipfix = dpif_ipfix_ref(ipfix); | |
270 | } | |
271 | ||
9d189a50 EJ |
272 | if (xbridge->stp != stp) { |
273 | stp_unref(xbridge->stp); | |
274 | xbridge->stp = stp_ref(stp); | |
275 | } | |
276 | ||
46c88433 EJ |
277 | free(xbridge->name); |
278 | xbridge->name = xstrdup(name); | |
279 | ||
89a8a7f0 | 280 | xbridge->dpif = dpif; |
46c88433 EJ |
281 | xbridge->forward_bpdu = forward_bpdu; |
282 | xbridge->has_in_band = has_in_band; | |
283 | xbridge->has_netflow = has_netflow; | |
46c88433 | 284 | xbridge->frag = frag; |
ec89fc6f EJ |
285 | xbridge->miss_rule = miss_rule; |
286 | xbridge->no_packet_in_rule = no_packet_in_rule; | |
46c88433 EJ |
287 | } |
288 | ||
289 | void | |
290 | xlate_remove_ofproto(struct ofproto_dpif *ofproto) | |
291 | { | |
292 | struct xbridge *xbridge = xbridge_lookup(ofproto); | |
293 | struct xbundle *xbundle, *next_xbundle; | |
294 | struct xport *xport, *next_xport; | |
295 | ||
296 | if (!xbridge) { | |
297 | return; | |
298 | } | |
299 | ||
300 | HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) { | |
301 | xlate_ofport_remove(xport->ofport); | |
302 | } | |
303 | ||
304 | LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) { | |
305 | xlate_bundle_remove(xbundle->ofbundle); | |
306 | } | |
307 | ||
308 | hmap_remove(&xbridges, &xbridge->hmap_node); | |
795cc5c1 EJ |
309 | mac_learning_unref(xbridge->ml); |
310 | mbridge_unref(xbridge->mbridge); | |
311 | dpif_sflow_unref(xbridge->sflow); | |
312 | dpif_ipfix_unref(xbridge->ipfix); | |
313 | stp_unref(xbridge->stp); | |
314 | hmap_destroy(&xbridge->xports); | |
46c88433 EJ |
315 | free(xbridge->name); |
316 | free(xbridge); | |
317 | } | |
318 | ||
319 | void | |
320 | xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle, | |
321 | const char *name, enum port_vlan_mode vlan_mode, int vlan, | |
322 | unsigned long *trunks, bool use_priority_tags, | |
323 | const struct bond *bond, const struct lacp *lacp, | |
324 | bool floodable) | |
325 | { | |
326 | struct xbundle *xbundle = xbundle_lookup(ofbundle); | |
327 | ||
328 | if (!xbundle) { | |
329 | xbundle = xzalloc(sizeof *xbundle); | |
330 | xbundle->ofbundle = ofbundle; | |
331 | xbundle->xbridge = xbridge_lookup(ofproto); | |
332 | ||
333 | hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0)); | |
334 | list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node); | |
335 | list_init(&xbundle->xports); | |
336 | } | |
337 | ||
338 | ovs_assert(xbundle->xbridge); | |
339 | ||
340 | free(xbundle->name); | |
341 | xbundle->name = xstrdup(name); | |
342 | ||
343 | xbundle->vlan_mode = vlan_mode; | |
344 | xbundle->vlan = vlan; | |
345 | xbundle->trunks = trunks; | |
346 | xbundle->use_priority_tags = use_priority_tags; | |
347 | xbundle->floodable = floodable; | |
348 | ||
349 | if (xbundle->bond != bond) { | |
350 | bond_unref(xbundle->bond); | |
351 | xbundle->bond = bond_ref(bond); | |
352 | } | |
353 | ||
354 | if (xbundle->lacp != lacp) { | |
355 | lacp_unref(xbundle->lacp); | |
356 | xbundle->lacp = lacp_ref(lacp); | |
357 | } | |
358 | } | |
359 | ||
360 | void | |
361 | xlate_bundle_remove(struct ofbundle *ofbundle) | |
362 | { | |
363 | struct xbundle *xbundle = xbundle_lookup(ofbundle); | |
364 | struct xport *xport, *next; | |
365 | ||
366 | if (!xbundle) { | |
367 | return; | |
368 | } | |
369 | ||
370 | LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) { | |
371 | list_remove(&xport->bundle_node); | |
372 | xport->xbundle = NULL; | |
373 | } | |
374 | ||
375 | hmap_remove(&xbundles, &xbundle->hmap_node); | |
376 | list_remove(&xbundle->list_node); | |
377 | bond_unref(xbundle->bond); | |
378 | lacp_unref(xbundle->lacp); | |
379 | free(xbundle->name); | |
380 | free(xbundle); | |
381 | } | |
382 | ||
383 | void | |
384 | xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle, | |
385 | struct ofport_dpif *ofport, ofp_port_t ofp_port, | |
386 | odp_port_t odp_port, const struct netdev *netdev, | |
387 | const struct cfm *cfm, const struct bfd *bfd, | |
9d189a50 | 388 | struct ofport_dpif *peer, int stp_port_no, |
55954f6e | 389 | const struct ofproto_port_queue *qdscp_list, size_t n_qdscp, |
9d189a50 EJ |
390 | enum ofputil_port_config config, bool is_tunnel, |
391 | bool may_enable) | |
46c88433 EJ |
392 | { |
393 | struct xport *xport = xport_lookup(ofport); | |
55954f6e | 394 | size_t i; |
46c88433 EJ |
395 | |
396 | if (!xport) { | |
397 | xport = xzalloc(sizeof *xport); | |
398 | xport->ofport = ofport; | |
399 | xport->xbridge = xbridge_lookup(ofproto); | |
400 | xport->ofp_port = ofp_port; | |
401 | ||
55954f6e | 402 | hmap_init(&xport->skb_priorities); |
46c88433 EJ |
403 | hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0)); |
404 | hmap_insert(&xport->xbridge->xports, &xport->ofp_node, | |
405 | hash_ofp_port(xport->ofp_port)); | |
406 | } | |
407 | ||
408 | ovs_assert(xport->ofp_port == ofp_port); | |
409 | ||
410 | xport->config = config; | |
9d189a50 | 411 | xport->stp_port_no = stp_port_no; |
46c88433 EJ |
412 | xport->is_tunnel = is_tunnel; |
413 | xport->may_enable = may_enable; | |
414 | xport->odp_port = odp_port; | |
415 | ||
416 | if (xport->netdev != netdev) { | |
417 | netdev_close(xport->netdev); | |
418 | xport->netdev = netdev_ref(netdev); | |
419 | } | |
420 | ||
421 | if (xport->cfm != cfm) { | |
422 | cfm_unref(xport->cfm); | |
423 | xport->cfm = cfm_ref(cfm); | |
424 | } | |
425 | ||
426 | if (xport->bfd != bfd) { | |
427 | bfd_unref(xport->bfd); | |
428 | xport->bfd = bfd_ref(bfd); | |
429 | } | |
430 | ||
431 | if (xport->peer) { | |
432 | xport->peer->peer = NULL; | |
433 | } | |
5e6af486 | 434 | xport->peer = xport_lookup(peer); |
46c88433 EJ |
435 | if (xport->peer) { |
436 | xport->peer->peer = xport; | |
437 | } | |
438 | ||
439 | if (xport->xbundle) { | |
440 | list_remove(&xport->bundle_node); | |
441 | } | |
5e6af486 | 442 | xport->xbundle = xbundle_lookup(ofbundle); |
46c88433 EJ |
443 | if (xport->xbundle) { |
444 | list_insert(&xport->xbundle->xports, &xport->bundle_node); | |
445 | } | |
55954f6e EJ |
446 | |
447 | clear_skb_priorities(xport); | |
448 | for (i = 0; i < n_qdscp; i++) { | |
449 | struct skb_priority_to_dscp *pdscp; | |
450 | uint32_t skb_priority; | |
451 | ||
89a8a7f0 EJ |
452 | if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue, |
453 | &skb_priority)) { | |
55954f6e EJ |
454 | continue; |
455 | } | |
456 | ||
457 | pdscp = xmalloc(sizeof *pdscp); | |
458 | pdscp->skb_priority = skb_priority; | |
459 | pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK; | |
460 | hmap_insert(&xport->skb_priorities, &pdscp->hmap_node, | |
461 | hash_int(pdscp->skb_priority, 0)); | |
462 | } | |
46c88433 EJ |
463 | } |
464 | ||
465 | void | |
466 | xlate_ofport_remove(struct ofport_dpif *ofport) | |
467 | { | |
468 | struct xport *xport = xport_lookup(ofport); | |
469 | ||
470 | if (!xport) { | |
471 | return; | |
472 | } | |
473 | ||
474 | if (xport->peer) { | |
475 | xport->peer->peer = NULL; | |
476 | xport->peer = NULL; | |
477 | } | |
478 | ||
e621a12d EJ |
479 | if (xport->xbundle) { |
480 | list_remove(&xport->bundle_node); | |
481 | } | |
482 | ||
55954f6e EJ |
483 | clear_skb_priorities(xport); |
484 | hmap_destroy(&xport->skb_priorities); | |
485 | ||
46c88433 EJ |
486 | hmap_remove(&xports, &xport->hmap_node); |
487 | hmap_remove(&xport->xbridge->xports, &xport->ofp_node); | |
488 | ||
489 | netdev_close(xport->netdev); | |
490 | cfm_unref(xport->cfm); | |
491 | bfd_unref(xport->bfd); | |
492 | free(xport); | |
493 | } | |
494 | ||
8449c4d6 EJ |
495 | /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key' |
496 | * respectively), populates 'flow' with the result of odp_flow_key_to_flow(). | |
497 | * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as | |
498 | * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto' | |
499 | * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that | |
500 | * 'packet' ingressed. | |
501 | * | |
502 | * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets | |
503 | * 'flow''s in_port to OFPP_NONE. | |
504 | * | |
505 | * This function does post-processing on data returned from | |
506 | * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest | |
507 | * of the upcall processing logic. In particular, if the extracted in_port is | |
508 | * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets | |
509 | * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes | |
510 | * a VLAN header onto 'packet' (if it is nonnull). | |
511 | * | |
512 | * Similarly, this function also includes some logic to help with tunnels. It | |
513 | * may modify 'flow' as necessary to make the tunneling implementation | |
514 | * transparent to the upcall processing logic. | |
515 | * | |
516 | * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport, | |
517 | * or some other positive errno if there are other problems. */ | |
518 | int | |
519 | xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet, | |
520 | const struct nlattr *key, size_t key_len, | |
521 | struct flow *flow, enum odp_key_fitness *fitnessp, | |
522 | struct ofproto_dpif **ofproto, odp_port_t *odp_in_port) | |
523 | { | |
524 | enum odp_key_fitness fitness; | |
525 | const struct xport *xport; | |
526 | int error = ENODEV; | |
527 | ||
dc24a00f | 528 | ovs_rwlock_rdlock(&xlate_rwlock); |
8449c4d6 EJ |
529 | fitness = odp_flow_key_to_flow(key, key_len, flow); |
530 | if (fitness == ODP_FIT_ERROR) { | |
531 | error = EINVAL; | |
532 | goto exit; | |
533 | } | |
534 | ||
535 | if (odp_in_port) { | |
536 | *odp_in_port = flow->in_port.odp_port; | |
537 | } | |
538 | ||
539 | xport = xport_lookup(tnl_port_should_receive(flow) | |
540 | ? tnl_port_receive(flow) | |
541 | : odp_port_to_ofport(backer, flow->in_port.odp_port)); | |
542 | ||
543 | flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE; | |
544 | if (!xport) { | |
545 | goto exit; | |
546 | } | |
547 | ||
548 | if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) { | |
549 | if (packet) { | |
550 | /* Make the packet resemble the flow, so that it gets sent to | |
551 | * an OpenFlow controller properly, so that it looks correct | |
552 | * for sFlow, and so that flow_extract() will get the correct | |
553 | * vlan_tci if it is called on 'packet'. | |
554 | * | |
555 | * The allocated space inside 'packet' probably also contains | |
556 | * 'key', that is, both 'packet' and 'key' are probably part of | |
557 | * a struct dpif_upcall (see the large comment on that | |
558 | * structure definition), so pushing data on 'packet' is in | |
559 | * general not a good idea since it could overwrite 'key' or | |
560 | * free it as a side effect. However, it's OK in this special | |
561 | * case because we know that 'packet' is inside a Netlink | |
562 | * attribute: pushing 4 bytes will just overwrite the 4-byte | |
563 | * "struct nlattr", which is fine since we don't need that | |
564 | * header anymore. */ | |
565 | eth_push_vlan(packet, flow->vlan_tci); | |
566 | } | |
567 | /* We can't reproduce 'key' from 'flow'. */ | |
568 | fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness; | |
569 | } | |
570 | error = 0; | |
571 | ||
572 | if (ofproto) { | |
573 | *ofproto = xport->xbridge->ofproto; | |
574 | } | |
575 | ||
576 | exit: | |
577 | if (fitnessp) { | |
578 | *fitnessp = fitness; | |
579 | } | |
dc24a00f | 580 | ovs_rwlock_unlock(&xlate_rwlock); |
8449c4d6 EJ |
581 | return error; |
582 | } | |
583 | ||
46c88433 EJ |
584 | static struct xbridge * |
585 | xbridge_lookup(const struct ofproto_dpif *ofproto) | |
586 | { | |
587 | struct xbridge *xbridge; | |
588 | ||
5e6af486 EJ |
589 | if (!ofproto) { |
590 | return NULL; | |
591 | } | |
592 | ||
46c88433 EJ |
593 | HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0), |
594 | &xbridges) { | |
595 | if (xbridge->ofproto == ofproto) { | |
596 | return xbridge; | |
597 | } | |
598 | } | |
599 | return NULL; | |
600 | } | |
601 | ||
602 | static struct xbundle * | |
603 | xbundle_lookup(const struct ofbundle *ofbundle) | |
604 | { | |
605 | struct xbundle *xbundle; | |
606 | ||
5e6af486 EJ |
607 | if (!ofbundle) { |
608 | return NULL; | |
609 | } | |
610 | ||
46c88433 EJ |
611 | HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0), |
612 | &xbundles) { | |
613 | if (xbundle->ofbundle == ofbundle) { | |
614 | return xbundle; | |
615 | } | |
616 | } | |
617 | return NULL; | |
618 | } | |
619 | ||
620 | static struct xport * | |
5e6af486 | 621 | xport_lookup(const struct ofport_dpif *ofport) |
46c88433 EJ |
622 | { |
623 | struct xport *xport; | |
624 | ||
5e6af486 EJ |
625 | if (!ofport) { |
626 | return NULL; | |
627 | } | |
628 | ||
46c88433 EJ |
629 | HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0), |
630 | &xports) { | |
631 | if (xport->ofport == ofport) { | |
632 | return xport; | |
633 | } | |
634 | } | |
635 | return NULL; | |
636 | } | |
637 | ||
40085e56 EJ |
638 | static struct stp_port * |
639 | xport_get_stp_port(const struct xport *xport) | |
640 | { | |
92cf817b | 641 | return xport->xbridge->stp && xport->stp_port_no != -1 |
40085e56 EJ |
642 | ? stp_get_port(xport->xbridge->stp, xport->stp_port_no) |
643 | : NULL; | |
644 | } | |
9d189a50 EJ |
645 | |
646 | static enum stp_state | |
647 | xport_stp_learn_state(const struct xport *xport) | |
648 | { | |
40085e56 EJ |
649 | struct stp_port *sp = xport_get_stp_port(xport); |
650 | return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED); | |
9d189a50 EJ |
651 | } |
652 | ||
653 | static bool | |
654 | xport_stp_forward_state(const struct xport *xport) | |
655 | { | |
40085e56 EJ |
656 | struct stp_port *sp = xport_get_stp_port(xport); |
657 | return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED); | |
9d189a50 EJ |
658 | } |
659 | ||
660 | /* Returns true if STP should process 'flow'. Sets fields in 'wc' that | |
661 | * were used to make the determination.*/ | |
662 | static bool | |
663 | stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc) | |
664 | { | |
665 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); | |
666 | return eth_addr_equals(flow->dl_dst, eth_addr_stp); | |
667 | } | |
668 | ||
669 | static void | |
670 | stp_process_packet(const struct xport *xport, const struct ofpbuf *packet) | |
671 | { | |
40085e56 | 672 | struct stp_port *sp = xport_get_stp_port(xport); |
9d189a50 EJ |
673 | struct ofpbuf payload = *packet; |
674 | struct eth_header *eth = payload.data; | |
9d189a50 EJ |
675 | |
676 | /* Sink packets on ports that have STP disabled when the bridge has | |
677 | * STP enabled. */ | |
678 | if (!sp || stp_port_get_state(sp) == STP_DISABLED) { | |
679 | return; | |
680 | } | |
681 | ||
682 | /* Trim off padding on payload. */ | |
683 | if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) { | |
684 | payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN; | |
685 | } | |
686 | ||
687 | if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) { | |
688 | stp_received_bpdu(sp, payload.data, payload.size); | |
689 | } | |
690 | } | |
691 | ||
46c88433 EJ |
692 | static struct xport * |
693 | get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port) | |
694 | { | |
695 | struct xport *xport; | |
696 | ||
697 | HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port), | |
698 | &xbridge->xports) { | |
699 | if (xport->ofp_port == ofp_port) { | |
700 | return xport; | |
701 | } | |
702 | } | |
703 | return NULL; | |
704 | } | |
705 | ||
706 | static odp_port_t | |
707 | ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port) | |
708 | { | |
709 | const struct xport *xport = get_ofp_port(xbridge, ofp_port); | |
710 | return xport ? xport->odp_port : ODPP_NONE; | |
711 | } | |
712 | ||
9583bc14 | 713 | static bool |
46c88433 | 714 | xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan) |
9583bc14 EJ |
715 | { |
716 | return (bundle->vlan_mode != PORT_VLAN_ACCESS | |
717 | && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan))); | |
718 | } | |
719 | ||
720 | static bool | |
46c88433 EJ |
721 | xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan) |
722 | { | |
723 | return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan); | |
724 | } | |
725 | ||
726 | static mirror_mask_t | |
727 | xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle) | |
728 | { | |
729 | return xbundle != &ofpp_none_bundle | |
730 | ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle) | |
731 | : 0; | |
732 | } | |
733 | ||
734 | static mirror_mask_t | |
735 | xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle) | |
9583bc14 | 736 | { |
46c88433 EJ |
737 | return xbundle != &ofpp_none_bundle |
738 | ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle) | |
739 | : 0; | |
9583bc14 EJ |
740 | } |
741 | ||
46c88433 EJ |
742 | static mirror_mask_t |
743 | xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle) | |
9583bc14 | 744 | { |
46c88433 EJ |
745 | return xbundle != &ofpp_none_bundle |
746 | ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle) | |
747 | : 0; | |
748 | } | |
749 | ||
750 | static struct xbundle * | |
751 | lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port, | |
752 | bool warn, struct xport **in_xportp) | |
753 | { | |
754 | struct xport *xport; | |
9583bc14 EJ |
755 | |
756 | /* Find the port and bundle for the received packet. */ | |
46c88433 EJ |
757 | xport = get_ofp_port(xbridge, in_port); |
758 | if (in_xportp) { | |
759 | *in_xportp = xport; | |
9583bc14 | 760 | } |
46c88433 EJ |
761 | if (xport && xport->xbundle) { |
762 | return xport->xbundle; | |
9583bc14 EJ |
763 | } |
764 | ||
765 | /* Special-case OFPP_NONE, which a controller may use as the ingress | |
766 | * port for traffic that it is sourcing. */ | |
767 | if (in_port == OFPP_NONE) { | |
3815d6c2 LS |
768 | ofpp_none_bundle.name = "OFPP_NONE"; |
769 | ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK; | |
9583bc14 EJ |
770 | return &ofpp_none_bundle; |
771 | } | |
772 | ||
773 | /* Odd. A few possible reasons here: | |
774 | * | |
775 | * - We deleted a port but there are still a few packets queued up | |
776 | * from it. | |
777 | * | |
778 | * - Someone externally added a port (e.g. "ovs-dpctl add-if") that | |
779 | * we don't know about. | |
780 | * | |
781 | * - The ofproto client didn't configure the port as part of a bundle. | |
782 | * This is particularly likely to happen if a packet was received on the | |
783 | * port after it was created, but before the client had a chance to | |
784 | * configure its bundle. | |
785 | */ | |
786 | if (warn) { | |
787 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
788 | ||
789 | VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown " | |
46c88433 | 790 | "port %"PRIu16, xbridge->name, in_port); |
9583bc14 EJ |
791 | } |
792 | return NULL; | |
793 | } | |
794 | ||
795 | static void | |
796 | add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow) | |
797 | { | |
46c88433 | 798 | const struct xbridge *xbridge = ctx->xbridge; |
9583bc14 | 799 | mirror_mask_t mirrors; |
46c88433 | 800 | struct xbundle *in_xbundle; |
9583bc14 EJ |
801 | uint16_t vlan; |
802 | uint16_t vid; | |
cdf5d3a5 EJ |
803 | |
804 | mirrors = ctx->xout->mirrors; | |
805 | ctx->xout->mirrors = 0; | |
9583bc14 | 806 | |
46c88433 EJ |
807 | in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port, |
808 | ctx->xin->packet != NULL, NULL); | |
809 | if (!in_xbundle) { | |
9583bc14 EJ |
810 | return; |
811 | } | |
46c88433 | 812 | mirrors |= xbundle_mirror_src(xbridge, in_xbundle); |
9583bc14 EJ |
813 | |
814 | /* Drop frames on bundles reserved for mirroring. */ | |
46c88433 | 815 | if (xbundle_mirror_out(xbridge, in_xbundle)) { |
9583bc14 EJ |
816 | if (ctx->xin->packet != NULL) { |
817 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
818 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port " | |
819 | "%s, which is reserved exclusively for mirroring", | |
46c88433 | 820 | ctx->xbridge->name, in_xbundle->name); |
9583bc14 | 821 | } |
aaa0fbae | 822 | ofpbuf_clear(&ctx->xout->odp_actions); |
9583bc14 EJ |
823 | return; |
824 | } | |
825 | ||
826 | /* Check VLAN. */ | |
827 | vid = vlan_tci_to_vid(orig_flow->vlan_tci); | |
46c88433 | 828 | if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) { |
9583bc14 EJ |
829 | return; |
830 | } | |
46c88433 | 831 | vlan = input_vid_to_vlan(in_xbundle, vid); |
9583bc14 | 832 | |
9583bc14 EJ |
833 | if (!mirrors) { |
834 | return; | |
835 | } | |
836 | ||
837 | /* Restore the original packet before adding the mirror actions. */ | |
838 | ctx->xin->flow = *orig_flow; | |
839 | ||
840 | while (mirrors) { | |
ec7ceaed EJ |
841 | mirror_mask_t dup_mirrors; |
842 | struct ofbundle *out; | |
843 | unsigned long *vlans; | |
844 | bool vlan_mirrored; | |
845 | bool has_mirror; | |
846 | int out_vlan; | |
847 | ||
46c88433 | 848 | has_mirror = mirror_get(xbridge->mbridge, mirror_mask_ffs(mirrors) - 1, |
ec7ceaed EJ |
849 | &vlans, &dup_mirrors, &out, &out_vlan); |
850 | ovs_assert(has_mirror); | |
851 | ||
852 | if (vlans) { | |
9583bc14 EJ |
853 | ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK); |
854 | } | |
ec7ceaed EJ |
855 | vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan); |
856 | free(vlans); | |
9583bc14 | 857 | |
ec7ceaed | 858 | if (!vlan_mirrored) { |
9583bc14 EJ |
859 | mirrors = zero_rightmost_1bit(mirrors); |
860 | continue; | |
861 | } | |
862 | ||
ec7ceaed EJ |
863 | mirrors &= ~dup_mirrors; |
864 | ctx->xout->mirrors |= dup_mirrors; | |
865 | if (out) { | |
46c88433 EJ |
866 | struct xbundle *out_xbundle = xbundle_lookup(out); |
867 | if (out_xbundle) { | |
868 | output_normal(ctx, out_xbundle, vlan); | |
869 | } | |
ec7ceaed | 870 | } else if (vlan != out_vlan |
9583bc14 | 871 | && !eth_addr_is_reserved(orig_flow->dl_dst)) { |
46c88433 | 872 | struct xbundle *xbundle; |
9583bc14 | 873 | |
46c88433 EJ |
874 | LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) { |
875 | if (xbundle_includes_vlan(xbundle, out_vlan) | |
876 | && !xbundle_mirror_out(xbridge, xbundle)) { | |
877 | output_normal(ctx, xbundle, out_vlan); | |
9583bc14 EJ |
878 | } |
879 | } | |
880 | } | |
881 | } | |
882 | } | |
883 | ||
884 | /* Given 'vid', the VID obtained from the 802.1Q header that was received as | |
46c88433 | 885 | * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle', |
9583bc14 EJ |
886 | * the bundle on which the packet was received, returns the VLAN to which the |
887 | * packet belongs. | |
888 | * | |
889 | * Both 'vid' and the return value are in the range 0...4095. */ | |
890 | static uint16_t | |
46c88433 | 891 | input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid) |
9583bc14 | 892 | { |
46c88433 | 893 | switch (in_xbundle->vlan_mode) { |
9583bc14 | 894 | case PORT_VLAN_ACCESS: |
46c88433 | 895 | return in_xbundle->vlan; |
9583bc14 EJ |
896 | break; |
897 | ||
898 | case PORT_VLAN_TRUNK: | |
899 | return vid; | |
900 | ||
901 | case PORT_VLAN_NATIVE_UNTAGGED: | |
902 | case PORT_VLAN_NATIVE_TAGGED: | |
46c88433 | 903 | return vid ? vid : in_xbundle->vlan; |
9583bc14 EJ |
904 | |
905 | default: | |
906 | NOT_REACHED(); | |
907 | } | |
908 | } | |
909 | ||
46c88433 | 910 | /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'. |
9583bc14 EJ |
911 | * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs |
912 | * a warning. | |
913 | * | |
914 | * 'vid' should be the VID obtained from the 802.1Q header that was received as | |
915 | * part of a packet (specify 0 if there was no 802.1Q header), in the range | |
916 | * 0...4095. */ | |
917 | static bool | |
46c88433 | 918 | input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn) |
9583bc14 EJ |
919 | { |
920 | /* Allow any VID on the OFPP_NONE port. */ | |
46c88433 | 921 | if (in_xbundle == &ofpp_none_bundle) { |
9583bc14 EJ |
922 | return true; |
923 | } | |
924 | ||
46c88433 | 925 | switch (in_xbundle->vlan_mode) { |
9583bc14 EJ |
926 | case PORT_VLAN_ACCESS: |
927 | if (vid) { | |
928 | if (warn) { | |
929 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
46c88433 | 930 | VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged " |
9583bc14 | 931 | "packet received on port %s configured as VLAN " |
46c88433 EJ |
932 | "%"PRIu16" access port", vid, in_xbundle->name, |
933 | in_xbundle->vlan); | |
9583bc14 EJ |
934 | } |
935 | return false; | |
936 | } | |
937 | return true; | |
938 | ||
939 | case PORT_VLAN_NATIVE_UNTAGGED: | |
940 | case PORT_VLAN_NATIVE_TAGGED: | |
941 | if (!vid) { | |
942 | /* Port must always carry its native VLAN. */ | |
943 | return true; | |
944 | } | |
945 | /* Fall through. */ | |
946 | case PORT_VLAN_TRUNK: | |
46c88433 | 947 | if (!xbundle_includes_vlan(in_xbundle, vid)) { |
9583bc14 EJ |
948 | if (warn) { |
949 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
46c88433 | 950 | VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet " |
9583bc14 | 951 | "received on port %s not configured for trunking " |
46c88433 | 952 | "VLAN %"PRIu16, vid, in_xbundle->name, vid); |
9583bc14 EJ |
953 | } |
954 | return false; | |
955 | } | |
956 | return true; | |
957 | ||
958 | default: | |
959 | NOT_REACHED(); | |
960 | } | |
961 | ||
962 | } | |
963 | ||
964 | /* Given 'vlan', the VLAN that a packet belongs to, and | |
46c88433 | 965 | * 'out_xbundle', a bundle on which the packet is to be output, returns the VID |
9583bc14 EJ |
966 | * that should be included in the 802.1Q header. (If the return value is 0, |
967 | * then the 802.1Q header should only be included in the packet if there is a | |
968 | * nonzero PCP.) | |
969 | * | |
970 | * Both 'vlan' and the return value are in the range 0...4095. */ | |
971 | static uint16_t | |
46c88433 | 972 | output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan) |
9583bc14 | 973 | { |
46c88433 | 974 | switch (out_xbundle->vlan_mode) { |
9583bc14 EJ |
975 | case PORT_VLAN_ACCESS: |
976 | return 0; | |
977 | ||
978 | case PORT_VLAN_TRUNK: | |
979 | case PORT_VLAN_NATIVE_TAGGED: | |
980 | return vlan; | |
981 | ||
982 | case PORT_VLAN_NATIVE_UNTAGGED: | |
46c88433 | 983 | return vlan == out_xbundle->vlan ? 0 : vlan; |
9583bc14 EJ |
984 | |
985 | default: | |
986 | NOT_REACHED(); | |
987 | } | |
988 | } | |
989 | ||
990 | static void | |
46c88433 | 991 | output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle, |
9583bc14 EJ |
992 | uint16_t vlan) |
993 | { | |
33bf9176 | 994 | ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci; |
9583bc14 EJ |
995 | uint16_t vid; |
996 | ovs_be16 tci, old_tci; | |
46c88433 | 997 | struct xport *xport; |
9583bc14 | 998 | |
46c88433 EJ |
999 | vid = output_vlan_to_vid(out_xbundle, vlan); |
1000 | if (list_is_empty(&out_xbundle->xports)) { | |
1001 | /* Partially configured bundle with no slaves. Drop the packet. */ | |
1002 | return; | |
1003 | } else if (!out_xbundle->bond) { | |
1004 | xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport, | |
1005 | bundle_node); | |
9583bc14 | 1006 | } else { |
46c88433 EJ |
1007 | struct ofport_dpif *ofport; |
1008 | ||
1009 | ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow, | |
4a1b8f30 | 1010 | &ctx->xout->wc, vid); |
5e6af486 | 1011 | xport = xport_lookup(ofport); |
46c88433 EJ |
1012 | |
1013 | if (!xport) { | |
9583bc14 EJ |
1014 | /* No slaves enabled, so drop packet. */ |
1015 | return; | |
1016 | } | |
1017 | } | |
1018 | ||
33bf9176 | 1019 | old_tci = *flow_tci; |
9583bc14 | 1020 | tci = htons(vid); |
46c88433 | 1021 | if (tci || out_xbundle->use_priority_tags) { |
33bf9176 | 1022 | tci |= *flow_tci & htons(VLAN_PCP_MASK); |
9583bc14 EJ |
1023 | if (tci) { |
1024 | tci |= htons(VLAN_CFI); | |
1025 | } | |
1026 | } | |
33bf9176 | 1027 | *flow_tci = tci; |
9583bc14 | 1028 | |
46c88433 | 1029 | compose_output_action(ctx, xport->ofp_port); |
33bf9176 | 1030 | *flow_tci = old_tci; |
9583bc14 EJ |
1031 | } |
1032 | ||
1033 | /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after | |
1034 | * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to | |
1035 | * indicate this; newer upstream kernels use gratuitous ARP requests. */ | |
1036 | static bool | |
1037 | is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc) | |
1038 | { | |
1039 | if (flow->dl_type != htons(ETH_TYPE_ARP)) { | |
1040 | return false; | |
1041 | } | |
1042 | ||
1043 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); | |
1044 | if (!eth_addr_is_broadcast(flow->dl_dst)) { | |
1045 | return false; | |
1046 | } | |
1047 | ||
1048 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
1049 | if (flow->nw_proto == ARP_OP_REPLY) { | |
1050 | return true; | |
1051 | } else if (flow->nw_proto == ARP_OP_REQUEST) { | |
1052 | memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src); | |
1053 | memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst); | |
1054 | ||
1055 | return flow->nw_src == flow->nw_dst; | |
1056 | } else { | |
1057 | return false; | |
1058 | } | |
1059 | } | |
1060 | ||
ee047520 BP |
1061 | /* Checks whether a MAC learning update is necessary for MAC learning table |
1062 | * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in | |
1063 | * 'vlan'. | |
1064 | * | |
1065 | * Most packets processed through the MAC learning table do not actually | |
1066 | * change it in any way. This function requires only a read lock on the MAC | |
1067 | * learning table, so it is much cheaper in this common case. | |
1068 | * | |
1069 | * Keep the code here synchronized with that in update_learning_table__() | |
1070 | * below. */ | |
1071 | static bool | |
1072 | is_mac_learning_update_needed(const struct mac_learning *ml, | |
1073 | const struct flow *flow, | |
1074 | struct flow_wildcards *wc, | |
1075 | int vlan, struct xbundle *in_xbundle) | |
1076 | OVS_REQ_RDLOCK(ml->rwlock) | |
9583bc14 EJ |
1077 | { |
1078 | struct mac_entry *mac; | |
1079 | ||
ee047520 BP |
1080 | if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) { |
1081 | return false; | |
1082 | } | |
1083 | ||
1084 | mac = mac_learning_lookup(ml, flow->dl_src, vlan); | |
1085 | if (!mac || mac_entry_age(ml, mac)) { | |
1086 | return true; | |
9583bc14 EJ |
1087 | } |
1088 | ||
ee047520 BP |
1089 | if (is_gratuitous_arp(flow, wc)) { |
1090 | /* We don't want to learn from gratuitous ARP packets that are | |
1091 | * reflected back over bond slaves so we lock the learning table. */ | |
1092 | if (!in_xbundle->bond) { | |
1093 | return true; | |
1094 | } else if (mac_entry_is_grat_arp_locked(mac)) { | |
1095 | return false; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | return mac->port.p != in_xbundle->ofbundle; | |
1100 | } | |
1101 | ||
1102 | ||
1103 | /* Updates MAC learning table 'ml' given that a packet matching 'flow' was | |
1104 | * received on 'in_xbundle' in 'vlan'. | |
1105 | * | |
1106 | * This code repeats all the checks in is_mac_learning_update_needed() because | |
1107 | * the lock was released between there and here and thus the MAC learning state | |
1108 | * could have changed. | |
1109 | * | |
1110 | * Keep the code here synchronized with that in is_mac_learning_update_needed() | |
1111 | * above. */ | |
1112 | static void | |
1113 | update_learning_table__(const struct xbridge *xbridge, | |
1114 | const struct flow *flow, struct flow_wildcards *wc, | |
1115 | int vlan, struct xbundle *in_xbundle) | |
1116 | OVS_REQ_WRLOCK(xbridge->ml->rwlock) | |
1117 | { | |
1118 | struct mac_entry *mac; | |
1119 | ||
46c88433 | 1120 | if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) { |
ee047520 | 1121 | return; |
9583bc14 EJ |
1122 | } |
1123 | ||
46c88433 | 1124 | mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan); |
9583bc14 EJ |
1125 | if (is_gratuitous_arp(flow, wc)) { |
1126 | /* We don't want to learn from gratuitous ARP packets that are | |
1127 | * reflected back over bond slaves so we lock the learning table. */ | |
46c88433 | 1128 | if (!in_xbundle->bond) { |
9583bc14 EJ |
1129 | mac_entry_set_grat_arp_lock(mac); |
1130 | } else if (mac_entry_is_grat_arp_locked(mac)) { | |
ee047520 | 1131 | return; |
9583bc14 EJ |
1132 | } |
1133 | } | |
1134 | ||
30618594 | 1135 | if (mac->port.p != in_xbundle->ofbundle) { |
9583bc14 EJ |
1136 | /* The log messages here could actually be useful in debugging, |
1137 | * so keep the rate limit relatively high. */ | |
1138 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300); | |
ee047520 | 1139 | |
9583bc14 EJ |
1140 | VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is " |
1141 | "on port %s in VLAN %d", | |
46c88433 EJ |
1142 | xbridge->name, ETH_ADDR_ARGS(flow->dl_src), |
1143 | in_xbundle->name, vlan); | |
9583bc14 | 1144 | |
46c88433 | 1145 | mac->port.p = in_xbundle->ofbundle; |
30618594 | 1146 | mac_learning_changed(xbridge->ml); |
9583bc14 | 1147 | } |
ee047520 BP |
1148 | } |
1149 | ||
1150 | static void | |
1151 | update_learning_table(const struct xbridge *xbridge, | |
1152 | const struct flow *flow, struct flow_wildcards *wc, | |
1153 | int vlan, struct xbundle *in_xbundle) | |
1154 | { | |
1155 | bool need_update; | |
1156 | ||
1157 | /* Don't learn the OFPP_NONE port. */ | |
1158 | if (in_xbundle == &ofpp_none_bundle) { | |
1159 | return; | |
1160 | } | |
1161 | ||
1162 | /* First try the common case: no change to MAC learning table. */ | |
1163 | ovs_rwlock_rdlock(&xbridge->ml->rwlock); | |
1164 | need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan, | |
1165 | in_xbundle); | |
509c0149 | 1166 | ovs_rwlock_unlock(&xbridge->ml->rwlock); |
ee047520 BP |
1167 | |
1168 | if (need_update) { | |
1169 | /* Slow path: MAC learning table might need an update. */ | |
1170 | ovs_rwlock_wrlock(&xbridge->ml->rwlock); | |
1171 | update_learning_table__(xbridge, flow, wc, vlan, in_xbundle); | |
1172 | ovs_rwlock_unlock(&xbridge->ml->rwlock); | |
1173 | } | |
9583bc14 EJ |
1174 | } |
1175 | ||
46c88433 | 1176 | /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or |
9583bc14 EJ |
1177 | * dropped. Returns true if they may be forwarded, false if they should be |
1178 | * dropped. | |
1179 | * | |
46c88433 | 1180 | * 'in_port' must be the xport that corresponds to flow->in_port. |
9583bc14 EJ |
1181 | * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull). |
1182 | * | |
1183 | * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as | |
1184 | * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as | |
1185 | * checked by input_vid_is_valid(). | |
1186 | * | |
1187 | * May also add tags to '*tags', although the current implementation only does | |
1188 | * so in one special case. | |
1189 | */ | |
1190 | static bool | |
46c88433 | 1191 | is_admissible(struct xlate_ctx *ctx, struct xport *in_port, |
9583bc14 EJ |
1192 | uint16_t vlan) |
1193 | { | |
46c88433 EJ |
1194 | struct xbundle *in_xbundle = in_port->xbundle; |
1195 | const struct xbridge *xbridge = ctx->xbridge; | |
9583bc14 | 1196 | struct flow *flow = &ctx->xin->flow; |
9583bc14 EJ |
1197 | |
1198 | /* Drop frames for reserved multicast addresses | |
1199 | * only if forward_bpdu option is absent. */ | |
46c88433 | 1200 | if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) { |
9583bc14 EJ |
1201 | xlate_report(ctx, "packet has reserved destination MAC, dropping"); |
1202 | return false; | |
1203 | } | |
1204 | ||
46c88433 | 1205 | if (in_xbundle->bond) { |
9583bc14 EJ |
1206 | struct mac_entry *mac; |
1207 | ||
46c88433 | 1208 | switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport, |
4a1b8f30 | 1209 | flow->dl_dst)) { |
9583bc14 EJ |
1210 | case BV_ACCEPT: |
1211 | break; | |
1212 | ||
1213 | case BV_DROP: | |
1214 | xlate_report(ctx, "bonding refused admissibility, dropping"); | |
1215 | return false; | |
1216 | ||
1217 | case BV_DROP_IF_MOVED: | |
509c0149 | 1218 | ovs_rwlock_rdlock(&xbridge->ml->rwlock); |
30618594 | 1219 | mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan); |
46c88433 | 1220 | if (mac && mac->port.p != in_xbundle->ofbundle && |
9583bc14 EJ |
1221 | (!is_gratuitous_arp(flow, &ctx->xout->wc) |
1222 | || mac_entry_is_grat_arp_locked(mac))) { | |
509c0149 | 1223 | ovs_rwlock_unlock(&xbridge->ml->rwlock); |
9583bc14 EJ |
1224 | xlate_report(ctx, "SLB bond thinks this packet looped back, " |
1225 | "dropping"); | |
1226 | return false; | |
1227 | } | |
509c0149 | 1228 | ovs_rwlock_unlock(&xbridge->ml->rwlock); |
9583bc14 EJ |
1229 | break; |
1230 | } | |
1231 | } | |
1232 | ||
1233 | return true; | |
1234 | } | |
1235 | ||
1236 | static void | |
1237 | xlate_normal(struct xlate_ctx *ctx) | |
1238 | { | |
33bf9176 BP |
1239 | struct flow_wildcards *wc = &ctx->xout->wc; |
1240 | struct flow *flow = &ctx->xin->flow; | |
46c88433 EJ |
1241 | struct xbundle *in_xbundle; |
1242 | struct xport *in_port; | |
9583bc14 | 1243 | struct mac_entry *mac; |
d6d5bbc9 | 1244 | void *mac_port; |
9583bc14 EJ |
1245 | uint16_t vlan; |
1246 | uint16_t vid; | |
1247 | ||
1248 | ctx->xout->has_normal = true; | |
1249 | ||
33bf9176 BP |
1250 | memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src); |
1251 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); | |
1dd35f8a | 1252 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); |
9583bc14 | 1253 | |
46c88433 EJ |
1254 | in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port, |
1255 | ctx->xin->packet != NULL, &in_port); | |
1256 | if (!in_xbundle) { | |
9583bc14 EJ |
1257 | xlate_report(ctx, "no input bundle, dropping"); |
1258 | return; | |
1259 | } | |
1260 | ||
1261 | /* Drop malformed frames. */ | |
33bf9176 BP |
1262 | if (flow->dl_type == htons(ETH_TYPE_VLAN) && |
1263 | !(flow->vlan_tci & htons(VLAN_CFI))) { | |
9583bc14 EJ |
1264 | if (ctx->xin->packet != NULL) { |
1265 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1266 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial " | |
1267 | "VLAN tag received on port %s", | |
46c88433 | 1268 | ctx->xbridge->name, in_xbundle->name); |
9583bc14 EJ |
1269 | } |
1270 | xlate_report(ctx, "partial VLAN tag, dropping"); | |
1271 | return; | |
1272 | } | |
1273 | ||
1274 | /* Drop frames on bundles reserved for mirroring. */ | |
46c88433 | 1275 | if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) { |
9583bc14 EJ |
1276 | if (ctx->xin->packet != NULL) { |
1277 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); | |
1278 | VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port " | |
1279 | "%s, which is reserved exclusively for mirroring", | |
46c88433 | 1280 | ctx->xbridge->name, in_xbundle->name); |
9583bc14 EJ |
1281 | } |
1282 | xlate_report(ctx, "input port is mirror output port, dropping"); | |
1283 | return; | |
1284 | } | |
1285 | ||
1286 | /* Check VLAN. */ | |
33bf9176 | 1287 | vid = vlan_tci_to_vid(flow->vlan_tci); |
46c88433 | 1288 | if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) { |
9583bc14 EJ |
1289 | xlate_report(ctx, "disallowed VLAN VID for this input port, dropping"); |
1290 | return; | |
1291 | } | |
46c88433 | 1292 | vlan = input_vid_to_vlan(in_xbundle, vid); |
9583bc14 EJ |
1293 | |
1294 | /* Check other admissibility requirements. */ | |
1295 | if (in_port && !is_admissible(ctx, in_port, vlan)) { | |
1296 | return; | |
1297 | } | |
1298 | ||
1299 | /* Learn source MAC. */ | |
1300 | if (ctx->xin->may_learn) { | |
46c88433 | 1301 | update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle); |
9583bc14 EJ |
1302 | } |
1303 | ||
1304 | /* Determine output bundle. */ | |
509c0149 | 1305 | ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock); |
30618594 | 1306 | mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan); |
d6d5bbc9 EJ |
1307 | mac_port = mac ? mac->port.p : NULL; |
1308 | ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock); | |
1309 | ||
1310 | if (mac_port) { | |
1311 | struct xbundle *mac_xbundle = xbundle_lookup(mac_port); | |
46c88433 | 1312 | if (mac_xbundle && mac_xbundle != in_xbundle) { |
9583bc14 | 1313 | xlate_report(ctx, "forwarding to learned port"); |
46c88433 EJ |
1314 | output_normal(ctx, mac_xbundle, vlan); |
1315 | } else if (!mac_xbundle) { | |
1316 | xlate_report(ctx, "learned port is unknown, dropping"); | |
9583bc14 EJ |
1317 | } else { |
1318 | xlate_report(ctx, "learned port is input port, dropping"); | |
1319 | } | |
1320 | } else { | |
46c88433 | 1321 | struct xbundle *xbundle; |
9583bc14 EJ |
1322 | |
1323 | xlate_report(ctx, "no learned MAC for destination, flooding"); | |
46c88433 EJ |
1324 | LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) { |
1325 | if (xbundle != in_xbundle | |
1326 | && xbundle_includes_vlan(xbundle, vlan) | |
1327 | && xbundle->floodable | |
1328 | && !xbundle_mirror_out(ctx->xbridge, xbundle)) { | |
1329 | output_normal(ctx, xbundle, vlan); | |
9583bc14 EJ |
1330 | } |
1331 | } | |
1332 | ctx->xout->nf_output_iface = NF_OUT_FLOOD; | |
1333 | } | |
1334 | } | |
1335 | ||
1336 | /* Compose SAMPLE action for sFlow or IPFIX. The given probability is | |
1337 | * the number of packets out of UINT32_MAX to sample. The given | |
1338 | * cookie is passed back in the callback for each sampled packet. | |
1339 | */ | |
1340 | static size_t | |
46c88433 | 1341 | compose_sample_action(const struct xbridge *xbridge, |
9583bc14 EJ |
1342 | struct ofpbuf *odp_actions, |
1343 | const struct flow *flow, | |
1344 | const uint32_t probability, | |
1345 | const union user_action_cookie *cookie, | |
1346 | const size_t cookie_size) | |
1347 | { | |
1348 | size_t sample_offset, actions_offset; | |
89a8a7f0 | 1349 | odp_port_t odp_port; |
9583bc14 | 1350 | int cookie_offset; |
89a8a7f0 | 1351 | uint32_t pid; |
9583bc14 EJ |
1352 | |
1353 | sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE); | |
1354 | ||
1355 | nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability); | |
1356 | ||
1357 | actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS); | |
89a8a7f0 EJ |
1358 | |
1359 | odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port); | |
1360 | pid = dpif_port_get_pid(xbridge->dpif, odp_port); | |
1361 | cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions); | |
9583bc14 EJ |
1362 | |
1363 | nl_msg_end_nested(odp_actions, actions_offset); | |
1364 | nl_msg_end_nested(odp_actions, sample_offset); | |
1365 | return cookie_offset; | |
1366 | } | |
1367 | ||
1368 | static void | |
46c88433 EJ |
1369 | compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci, |
1370 | odp_port_t odp_port, unsigned int n_outputs, | |
1371 | union user_action_cookie *cookie) | |
9583bc14 EJ |
1372 | { |
1373 | int ifindex; | |
1374 | ||
1375 | cookie->type = USER_ACTION_COOKIE_SFLOW; | |
1376 | cookie->sflow.vlan_tci = vlan_tci; | |
1377 | ||
1378 | /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output | |
1379 | * port information") for the interpretation of cookie->output. */ | |
1380 | switch (n_outputs) { | |
1381 | case 0: | |
1382 | /* 0x40000000 | 256 means "packet dropped for unknown reason". */ | |
1383 | cookie->sflow.output = 0x40000000 | 256; | |
1384 | break; | |
1385 | ||
1386 | case 1: | |
46c88433 | 1387 | ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port); |
9583bc14 EJ |
1388 | if (ifindex) { |
1389 | cookie->sflow.output = ifindex; | |
1390 | break; | |
1391 | } | |
1392 | /* Fall through. */ | |
1393 | default: | |
1394 | /* 0x80000000 means "multiple output ports. */ | |
1395 | cookie->sflow.output = 0x80000000 | n_outputs; | |
1396 | break; | |
1397 | } | |
1398 | } | |
1399 | ||
1400 | /* Compose SAMPLE action for sFlow bridge sampling. */ | |
1401 | static size_t | |
46c88433 | 1402 | compose_sflow_action(const struct xbridge *xbridge, |
9583bc14 EJ |
1403 | struct ofpbuf *odp_actions, |
1404 | const struct flow *flow, | |
4e022ec0 | 1405 | odp_port_t odp_port) |
9583bc14 EJ |
1406 | { |
1407 | uint32_t probability; | |
1408 | union user_action_cookie cookie; | |
1409 | ||
46c88433 | 1410 | if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) { |
9583bc14 EJ |
1411 | return 0; |
1412 | } | |
1413 | ||
46c88433 EJ |
1414 | probability = dpif_sflow_get_probability(xbridge->sflow); |
1415 | compose_sflow_cookie(xbridge, htons(0), odp_port, | |
4e022ec0 | 1416 | odp_port == ODPP_NONE ? 0 : 1, &cookie); |
9583bc14 | 1417 | |
46c88433 | 1418 | return compose_sample_action(xbridge, odp_actions, flow, probability, |
9583bc14 EJ |
1419 | &cookie, sizeof cookie.sflow); |
1420 | } | |
1421 | ||
1422 | static void | |
1423 | compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id, | |
1424 | uint32_t obs_domain_id, uint32_t obs_point_id, | |
1425 | union user_action_cookie *cookie) | |
1426 | { | |
1427 | cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE; | |
1428 | cookie->flow_sample.probability = probability; | |
1429 | cookie->flow_sample.collector_set_id = collector_set_id; | |
1430 | cookie->flow_sample.obs_domain_id = obs_domain_id; | |
1431 | cookie->flow_sample.obs_point_id = obs_point_id; | |
1432 | } | |
1433 | ||
1434 | static void | |
1435 | compose_ipfix_cookie(union user_action_cookie *cookie) | |
1436 | { | |
1437 | cookie->type = USER_ACTION_COOKIE_IPFIX; | |
1438 | } | |
1439 | ||
1440 | /* Compose SAMPLE action for IPFIX bridge sampling. */ | |
1441 | static void | |
46c88433 | 1442 | compose_ipfix_action(const struct xbridge *xbridge, |
9583bc14 EJ |
1443 | struct ofpbuf *odp_actions, |
1444 | const struct flow *flow) | |
1445 | { | |
1446 | uint32_t probability; | |
1447 | union user_action_cookie cookie; | |
1448 | ||
46c88433 | 1449 | if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) { |
9583bc14 EJ |
1450 | return; |
1451 | } | |
1452 | ||
46c88433 | 1453 | probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix); |
9583bc14 EJ |
1454 | compose_ipfix_cookie(&cookie); |
1455 | ||
46c88433 | 1456 | compose_sample_action(xbridge, odp_actions, flow, probability, |
9583bc14 EJ |
1457 | &cookie, sizeof cookie.ipfix); |
1458 | } | |
1459 | ||
1460 | /* SAMPLE action for sFlow must be first action in any given list of | |
1461 | * actions. At this point we do not have all information required to | |
1462 | * build it. So try to build sample action as complete as possible. */ | |
1463 | static void | |
1464 | add_sflow_action(struct xlate_ctx *ctx) | |
1465 | { | |
46c88433 | 1466 | ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge, |
9583bc14 | 1467 | &ctx->xout->odp_actions, |
4e022ec0 | 1468 | &ctx->xin->flow, ODPP_NONE); |
9583bc14 EJ |
1469 | ctx->sflow_odp_port = 0; |
1470 | ctx->sflow_n_outputs = 0; | |
1471 | } | |
1472 | ||
1473 | /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list | |
1474 | * of actions, eventually after the SAMPLE action for sFlow. */ | |
1475 | static void | |
1476 | add_ipfix_action(struct xlate_ctx *ctx) | |
1477 | { | |
46c88433 | 1478 | compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions, |
9583bc14 EJ |
1479 | &ctx->xin->flow); |
1480 | } | |
1481 | ||
1482 | /* Fix SAMPLE action according to data collected while composing ODP actions. | |
1483 | * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested | |
1484 | * USERSPACE action's user-cookie which is required for sflow. */ | |
1485 | static void | |
1486 | fix_sflow_action(struct xlate_ctx *ctx) | |
1487 | { | |
1488 | const struct flow *base = &ctx->base_flow; | |
1489 | union user_action_cookie *cookie; | |
1490 | ||
1491 | if (!ctx->user_cookie_offset) { | |
1492 | return; | |
1493 | } | |
1494 | ||
1495 | cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset, | |
1496 | sizeof cookie->sflow); | |
1497 | ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW); | |
1498 | ||
46c88433 | 1499 | compose_sflow_cookie(ctx->xbridge, base->vlan_tci, |
9583bc14 EJ |
1500 | ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie); |
1501 | } | |
1502 | ||
db7d4e46 | 1503 | static enum slow_path_reason |
642dc74d | 1504 | process_special(struct xlate_ctx *ctx, const struct flow *flow, |
46c88433 | 1505 | const struct xport *xport, const struct ofpbuf *packet) |
db7d4e46 | 1506 | { |
642dc74d | 1507 | struct flow_wildcards *wc = &ctx->xout->wc; |
46c88433 | 1508 | const struct xbridge *xbridge = ctx->xbridge; |
642dc74d | 1509 | |
46c88433 | 1510 | if (!xport) { |
db7d4e46 | 1511 | return 0; |
46c88433 | 1512 | } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) { |
db7d4e46 | 1513 | if (packet) { |
46c88433 | 1514 | cfm_process_heartbeat(xport->cfm, packet); |
db7d4e46 JP |
1515 | } |
1516 | return SLOW_CFM; | |
fab52e16 | 1517 | } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) { |
db7d4e46 | 1518 | if (packet) { |
46c88433 | 1519 | bfd_process_packet(xport->bfd, flow, packet); |
db7d4e46 JP |
1520 | } |
1521 | return SLOW_BFD; | |
46c88433 | 1522 | } else if (xport->xbundle && xport->xbundle->lacp |
db7d4e46 JP |
1523 | && flow->dl_type == htons(ETH_TYPE_LACP)) { |
1524 | if (packet) { | |
46c88433 | 1525 | lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet); |
db7d4e46 JP |
1526 | } |
1527 | return SLOW_LACP; | |
9d189a50 | 1528 | } else if (xbridge->stp && stp_should_process_flow(flow, wc)) { |
db7d4e46 | 1529 | if (packet) { |
9d189a50 | 1530 | stp_process_packet(xport, packet); |
db7d4e46 JP |
1531 | } |
1532 | return SLOW_STP; | |
1533 | } else { | |
1534 | return 0; | |
1535 | } | |
1536 | } | |
1537 | ||
9583bc14 | 1538 | static void |
4e022ec0 | 1539 | compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port, |
9583bc14 EJ |
1540 | bool check_stp) |
1541 | { | |
46c88433 | 1542 | const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port); |
1dd35f8a | 1543 | struct flow_wildcards *wc = &ctx->xout->wc; |
33bf9176 | 1544 | struct flow *flow = &ctx->xin->flow; |
9583bc14 | 1545 | ovs_be16 flow_vlan_tci; |
1362e248 | 1546 | uint32_t flow_pkt_mark; |
9583bc14 | 1547 | uint8_t flow_nw_tos; |
4e022ec0 | 1548 | odp_port_t out_port, odp_port; |
ca077186 | 1549 | uint8_t dscp; |
9583bc14 EJ |
1550 | |
1551 | /* If 'struct flow' gets additional metadata, we'll need to zero it out | |
1552 | * before traversing a patch port. */ | |
b0a17866 | 1553 | BUILD_ASSERT_DECL(FLOW_WC_SEQ == 21); |
9583bc14 | 1554 | |
46c88433 | 1555 | if (!xport) { |
9583bc14 EJ |
1556 | xlate_report(ctx, "Nonexistent output port"); |
1557 | return; | |
46c88433 | 1558 | } else if (xport->config & OFPUTIL_PC_NO_FWD) { |
9583bc14 EJ |
1559 | xlate_report(ctx, "OFPPC_NO_FWD set, skipping output"); |
1560 | return; | |
9d189a50 | 1561 | } else if (check_stp && !xport_stp_forward_state(xport)) { |
9583bc14 EJ |
1562 | xlate_report(ctx, "STP not in forwarding state, skipping output"); |
1563 | return; | |
1564 | } | |
1565 | ||
46c88433 EJ |
1566 | if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) { |
1567 | ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge, | |
1568 | xport->xbundle); | |
cdf5d3a5 EJ |
1569 | } |
1570 | ||
46c88433 EJ |
1571 | if (xport->peer) { |
1572 | const struct xport *peer = xport->peer; | |
9583bc14 | 1573 | struct flow old_flow = ctx->xin->flow; |
9583bc14 | 1574 | enum slow_path_reason special; |
9583bc14 | 1575 | |
46c88433 EJ |
1576 | ctx->xbridge = peer->xbridge; |
1577 | flow->in_port.ofp_port = peer->ofp_port; | |
33bf9176 BP |
1578 | flow->metadata = htonll(0); |
1579 | memset(&flow->tunnel, 0, sizeof flow->tunnel); | |
1580 | memset(flow->regs, 0, sizeof flow->regs); | |
9583bc14 | 1581 | |
642dc74d | 1582 | special = process_special(ctx, &ctx->xin->flow, peer, |
9583bc14 EJ |
1583 | ctx->xin->packet); |
1584 | if (special) { | |
04594cd5 | 1585 | ctx->xout->slow |= special; |
ddd3c975 | 1586 | } else if (may_receive(peer, ctx)) { |
9d189a50 | 1587 | if (xport_stp_forward_state(peer)) { |
4e022ec0 | 1588 | xlate_table_action(ctx, flow->in_port.ofp_port, 0, true); |
9583bc14 EJ |
1589 | } else { |
1590 | /* Forwarding is disabled by STP. Let OFPP_NORMAL and the | |
1591 | * learning action look at the packet, then drop it. */ | |
1592 | struct flow old_base_flow = ctx->base_flow; | |
1593 | size_t old_size = ctx->xout->odp_actions.size; | |
cdf5d3a5 | 1594 | mirror_mask_t old_mirrors = ctx->xout->mirrors; |
4e022ec0 | 1595 | xlate_table_action(ctx, flow->in_port.ofp_port, 0, true); |
cdf5d3a5 | 1596 | ctx->xout->mirrors = old_mirrors; |
9583bc14 EJ |
1597 | ctx->base_flow = old_base_flow; |
1598 | ctx->xout->odp_actions.size = old_size; | |
1599 | } | |
1600 | } | |
1601 | ||
1602 | ctx->xin->flow = old_flow; | |
832554e3 | 1603 | ctx->xbridge = xport->xbridge; |
9583bc14 EJ |
1604 | |
1605 | if (ctx->xin->resubmit_stats) { | |
46c88433 EJ |
1606 | netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats); |
1607 | netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats); | |
9583bc14 EJ |
1608 | } |
1609 | ||
1610 | return; | |
1611 | } | |
1612 | ||
33bf9176 | 1613 | flow_vlan_tci = flow->vlan_tci; |
1362e248 | 1614 | flow_pkt_mark = flow->pkt_mark; |
33bf9176 | 1615 | flow_nw_tos = flow->nw_tos; |
9583bc14 | 1616 | |
55954f6e | 1617 | if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) { |
1dd35f8a | 1618 | wc->masks.nw_tos |= IP_ECN_MASK; |
33bf9176 | 1619 | flow->nw_tos &= ~IP_DSCP_MASK; |
ca077186 | 1620 | flow->nw_tos |= dscp; |
9583bc14 EJ |
1621 | } |
1622 | ||
46c88433 | 1623 | if (xport->is_tunnel) { |
9583bc14 EJ |
1624 | /* Save tunnel metadata so that changes made due to |
1625 | * the Logical (tunnel) Port are not visible for any further | |
1626 | * matches, while explicit set actions on tunnel metadata are. | |
1627 | */ | |
33bf9176 | 1628 | struct flow_tnl flow_tnl = flow->tunnel; |
46c88433 | 1629 | odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc); |
4e022ec0 | 1630 | if (odp_port == ODPP_NONE) { |
9583bc14 EJ |
1631 | xlate_report(ctx, "Tunneling decided against output"); |
1632 | goto out; /* restore flow_nw_tos */ | |
1633 | } | |
33bf9176 | 1634 | if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) { |
9583bc14 EJ |
1635 | xlate_report(ctx, "Not tunneling to our own address"); |
1636 | goto out; /* restore flow_nw_tos */ | |
1637 | } | |
1638 | if (ctx->xin->resubmit_stats) { | |
46c88433 | 1639 | netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats); |
9583bc14 EJ |
1640 | } |
1641 | out_port = odp_port; | |
33bf9176 | 1642 | commit_odp_tunnel_action(flow, &ctx->base_flow, |
9583bc14 | 1643 | &ctx->xout->odp_actions); |
33bf9176 | 1644 | flow->tunnel = flow_tnl; /* Restore tunnel metadata */ |
9583bc14 | 1645 | } else { |
4e022ec0 | 1646 | ofp_port_t vlandev_port; |
1dd35f8a | 1647 | |
46c88433 EJ |
1648 | odp_port = xport->odp_port; |
1649 | if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) { | |
1dd35f8a JP |
1650 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); |
1651 | } | |
46c88433 | 1652 | vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port, |
33bf9176 | 1653 | flow->vlan_tci); |
9583bc14 EJ |
1654 | if (vlandev_port == ofp_port) { |
1655 | out_port = odp_port; | |
1656 | } else { | |
46c88433 | 1657 | out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port); |
33bf9176 | 1658 | flow->vlan_tci = htons(0); |
9583bc14 | 1659 | } |
9583bc14 | 1660 | } |
9583bc14 | 1661 | |
4e022ec0 | 1662 | if (out_port != ODPP_NONE) { |
7fd91025 BP |
1663 | ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow, |
1664 | &ctx->xout->odp_actions, | |
1665 | &ctx->xout->wc, | |
1666 | &ctx->mpls_depth_delta); | |
4e022ec0 AW |
1667 | nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT, |
1668 | out_port); | |
9583bc14 | 1669 | |
6cbbf4fa EJ |
1670 | ctx->sflow_odp_port = odp_port; |
1671 | ctx->sflow_n_outputs++; | |
1672 | ctx->xout->nf_output_iface = ofp_port; | |
1673 | } | |
1674 | ||
1675 | out: | |
9583bc14 | 1676 | /* Restore flow */ |
33bf9176 | 1677 | flow->vlan_tci = flow_vlan_tci; |
1362e248 | 1678 | flow->pkt_mark = flow_pkt_mark; |
33bf9176 | 1679 | flow->nw_tos = flow_nw_tos; |
9583bc14 EJ |
1680 | } |
1681 | ||
1682 | static void | |
4e022ec0 | 1683 | compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port) |
9583bc14 EJ |
1684 | { |
1685 | compose_output_action__(ctx, ofp_port, true); | |
1686 | } | |
1687 | ||
bb61b33d BP |
1688 | static void |
1689 | xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule) | |
bb61b33d BP |
1690 | { |
1691 | struct rule_dpif *old_rule = ctx->rule; | |
6f00e29b | 1692 | struct rule_actions *actions; |
bb61b33d BP |
1693 | |
1694 | if (ctx->xin->resubmit_stats) { | |
70742c7f | 1695 | rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats); |
bb61b33d BP |
1696 | } |
1697 | ||
98b07853 | 1698 | ctx->resubmits++; |
bb61b33d BP |
1699 | ctx->recurse++; |
1700 | ctx->rule = rule; | |
6f00e29b BP |
1701 | actions = rule_dpif_get_actions(rule); |
1702 | do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx); | |
1703 | rule_actions_unref(actions); | |
bb61b33d BP |
1704 | ctx->rule = old_rule; |
1705 | ctx->recurse--; | |
bb61b33d BP |
1706 | } |
1707 | ||
9583bc14 EJ |
1708 | static void |
1709 | xlate_table_action(struct xlate_ctx *ctx, | |
4e022ec0 | 1710 | ofp_port_t in_port, uint8_t table_id, bool may_packet_in) |
9583bc14 | 1711 | { |
98b07853 BP |
1712 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); |
1713 | ||
1714 | if (ctx->recurse >= MAX_RESUBMIT_RECURSION) { | |
1715 | VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times", | |
1716 | MAX_RESUBMIT_RECURSION); | |
1717 | } else if (ctx->resubmits >= MAX_RESUBMITS) { | |
1718 | VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS); | |
1719 | } else if (ctx->xout->odp_actions.size > UINT16_MAX) { | |
1720 | VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions"); | |
1721 | } else if (ctx->stack.size >= 65536) { | |
1722 | VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack"); | |
1723 | } else { | |
9583bc14 | 1724 | struct rule_dpif *rule; |
4e022ec0 | 1725 | ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port; |
9583bc14 EJ |
1726 | uint8_t old_table_id = ctx->table_id; |
1727 | ||
1728 | ctx->table_id = table_id; | |
1729 | ||
bb61b33d BP |
1730 | /* Look up a flow with 'in_port' as the input port. Then restore the |
1731 | * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will | |
1732 | * have surprising behavior). */ | |
4e022ec0 | 1733 | ctx->xin->flow.in_port.ofp_port = in_port; |
a2143702 BP |
1734 | rule_dpif_lookup_in_table(ctx->xbridge->ofproto, |
1735 | &ctx->xin->flow, &ctx->xout->wc, | |
1736 | table_id, &rule); | |
4e022ec0 | 1737 | ctx->xin->flow.in_port.ofp_port = old_in_port; |
9583bc14 | 1738 | |
ad3efdcb EJ |
1739 | if (ctx->xin->resubmit_hook) { |
1740 | ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse); | |
1741 | } | |
1742 | ||
a2143702 | 1743 | if (!rule && may_packet_in) { |
ad3efdcb EJ |
1744 | struct xport *xport; |
1745 | ||
ad3efdcb EJ |
1746 | /* XXX |
1747 | * check if table configuration flags | |
1748 | * OFPTC_TABLE_MISS_CONTROLLER, default. | |
1749 | * OFPTC_TABLE_MISS_CONTINUE, | |
1750 | * OFPTC_TABLE_MISS_DROP | |
1751 | * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */ | |
1752 | xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port); | |
70742c7f EJ |
1753 | choose_miss_rule(xport ? xport->config : 0, |
1754 | ctx->xbridge->miss_rule, | |
1755 | ctx->xbridge->no_packet_in_rule, &rule); | |
a2143702 BP |
1756 | } |
1757 | if (rule) { | |
bb61b33d | 1758 | xlate_recursively(ctx, rule); |
a2143702 | 1759 | rule_dpif_unref(rule); |
ad3efdcb EJ |
1760 | } |
1761 | ||
9583bc14 | 1762 | ctx->table_id = old_table_id; |
98b07853 | 1763 | return; |
9583bc14 | 1764 | } |
98b07853 BP |
1765 | |
1766 | ctx->exit = true; | |
9583bc14 EJ |
1767 | } |
1768 | ||
1769 | static void | |
1770 | xlate_ofpact_resubmit(struct xlate_ctx *ctx, | |
1771 | const struct ofpact_resubmit *resubmit) | |
1772 | { | |
4e022ec0 | 1773 | ofp_port_t in_port; |
9583bc14 EJ |
1774 | uint8_t table_id; |
1775 | ||
1776 | in_port = resubmit->in_port; | |
1777 | if (in_port == OFPP_IN_PORT) { | |
4e022ec0 | 1778 | in_port = ctx->xin->flow.in_port.ofp_port; |
9583bc14 EJ |
1779 | } |
1780 | ||
1781 | table_id = resubmit->table_id; | |
1782 | if (table_id == 255) { | |
1783 | table_id = ctx->table_id; | |
1784 | } | |
1785 | ||
1786 | xlate_table_action(ctx, in_port, table_id, false); | |
1787 | } | |
1788 | ||
1789 | static void | |
1790 | flood_packets(struct xlate_ctx *ctx, bool all) | |
1791 | { | |
46c88433 | 1792 | const struct xport *xport; |
9583bc14 | 1793 | |
46c88433 EJ |
1794 | HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) { |
1795 | if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) { | |
9583bc14 EJ |
1796 | continue; |
1797 | } | |
1798 | ||
1799 | if (all) { | |
46c88433 EJ |
1800 | compose_output_action__(ctx, xport->ofp_port, false); |
1801 | } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) { | |
1802 | compose_output_action(ctx, xport->ofp_port); | |
9583bc14 EJ |
1803 | } |
1804 | } | |
1805 | ||
1806 | ctx->xout->nf_output_iface = NF_OUT_FLOOD; | |
1807 | } | |
1808 | ||
1809 | static void | |
1810 | execute_controller_action(struct xlate_ctx *ctx, int len, | |
1811 | enum ofp_packet_in_reason reason, | |
1812 | uint16_t controller_id) | |
1813 | { | |
ada3a58d | 1814 | struct ofputil_packet_in *pin; |
9583bc14 EJ |
1815 | struct ofpbuf *packet; |
1816 | struct flow key; | |
1817 | ||
04594cd5 | 1818 | ctx->xout->slow |= SLOW_CONTROLLER; |
9583bc14 EJ |
1819 | if (!ctx->xin->packet) { |
1820 | return; | |
1821 | } | |
1822 | ||
1823 | packet = ofpbuf_clone(ctx->xin->packet); | |
1824 | ||
1825 | key.skb_priority = 0; | |
1362e248 | 1826 | key.pkt_mark = 0; |
9583bc14 EJ |
1827 | memset(&key.tunnel, 0, sizeof key.tunnel); |
1828 | ||
7fd91025 BP |
1829 | ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow, |
1830 | &ctx->xout->odp_actions, | |
1831 | &ctx->xout->wc, | |
1832 | &ctx->mpls_depth_delta); | |
9583bc14 EJ |
1833 | |
1834 | odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data, | |
1835 | ctx->xout->odp_actions.size, NULL, NULL); | |
1836 | ||
ada3a58d EJ |
1837 | pin = xmalloc(sizeof *pin); |
1838 | pin->packet_len = packet->size; | |
1839 | pin->packet = ofpbuf_steal_data(packet); | |
1840 | pin->reason = reason; | |
1841 | pin->controller_id = controller_id; | |
1842 | pin->table_id = ctx->table_id; | |
70742c7f | 1843 | pin->cookie = ctx->rule ? rule_dpif_get_flow_cookie(ctx->rule) : 0; |
9583bc14 | 1844 | |
ada3a58d EJ |
1845 | pin->send_len = len; |
1846 | flow_get_metadata(&ctx->xin->flow, &pin->fmd); | |
9583bc14 | 1847 | |
ada3a58d | 1848 | ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin); |
9583bc14 EJ |
1849 | ofpbuf_delete(packet); |
1850 | } | |
1851 | ||
b0a17866 | 1852 | static bool |
9cfef3d0 | 1853 | compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type) |
9583bc14 | 1854 | { |
33bf9176 BP |
1855 | struct flow_wildcards *wc = &ctx->xout->wc; |
1856 | struct flow *flow = &ctx->xin->flow; | |
1857 | ||
9583bc14 EJ |
1858 | ovs_assert(eth_type_mpls(eth_type)); |
1859 | ||
b0a17866 SH |
1860 | /* If mpls_depth_delta is negative then an MPLS POP action has been |
1861 | * composed and the resulting MPLS label stack is unknown. This means | |
1862 | * an MPLS PUSH action can't be composed as it needs to know either the | |
1863 | * top-most MPLS LSE to use as a template for the new MPLS LSE, or that | |
1864 | * there is no MPLS label stack present. Thus, stop processing. | |
1865 | * | |
1866 | * If mpls_depth_delta is positive then an MPLS PUSH action has been | |
1867 | * composed and no further MPLS PUSH action may be performed without | |
1868 | * losing MPLS LSE and ether type information held in xtx->xin->flow. | |
1869 | * Thus, stop processing. | |
1870 | * | |
1871 | * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE | |
1872 | * has been updated. Performing a MPLS PUSH action may be would result in | |
1873 | * losing MPLS LSE and ether type information held in xtx->xin->flow. | |
1874 | * Thus, stop processing. | |
1875 | * | |
1876 | * It is planned that in the future this case will be handled | |
1877 | * by recirculation */ | |
1878 | if (ctx->mpls_depth_delta || | |
1879 | ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) { | |
1880 | return true; | |
1881 | } | |
1882 | ||
33bf9176 | 1883 | memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); |
9583bc14 | 1884 | |
b0a17866 SH |
1885 | ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse; |
1886 | ||
1887 | if (eth_type_mpls(ctx->xin->flow.dl_type)) { | |
33bf9176 | 1888 | flow->mpls_lse &= ~htonl(MPLS_BOS_MASK); |
9583bc14 EJ |
1889 | } else { |
1890 | ovs_be32 label; | |
1891 | uint8_t tc, ttl; | |
1892 | ||
33bf9176 | 1893 | if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
9583bc14 EJ |
1894 | label = htonl(0x2); /* IPV6 Explicit Null. */ |
1895 | } else { | |
1896 | label = htonl(0x0); /* IPV4 Explicit Null. */ | |
1897 | } | |
1dd35f8a JP |
1898 | wc->masks.nw_tos |= IP_DSCP_MASK; |
1899 | wc->masks.nw_ttl = 0xff; | |
33bf9176 BP |
1900 | tc = (flow->nw_tos & IP_DSCP_MASK) >> 2; |
1901 | ttl = flow->nw_ttl ? flow->nw_ttl : 0x40; | |
1902 | flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label); | |
9583bc14 | 1903 | } |
33bf9176 | 1904 | flow->dl_type = eth_type; |
b0a17866 SH |
1905 | ctx->mpls_depth_delta++; |
1906 | ||
1907 | return false; | |
9583bc14 EJ |
1908 | } |
1909 | ||
b0a17866 | 1910 | static bool |
9cfef3d0 | 1911 | compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type) |
9583bc14 | 1912 | { |
33bf9176 | 1913 | struct flow_wildcards *wc = &ctx->xout->wc; |
33bf9176 | 1914 | |
b0a17866 SH |
1915 | if (!eth_type_mpls(ctx->xin->flow.dl_type)) { |
1916 | return true; | |
1917 | } | |
1918 | ||
1919 | /* If mpls_depth_delta is negative then an MPLS POP action has been | |
1920 | * composed. Performing another MPLS POP action | |
1921 | * would result in losing ether type that results from | |
1922 | * the already composed MPLS POP. Thus, stop processing. | |
1923 | * | |
1924 | * It is planned that in the future this case will be handled | |
1925 | * by recirculation */ | |
1926 | if (ctx->mpls_depth_delta < 0) { | |
1927 | return true; | |
1928 | } | |
9583bc14 | 1929 | |
33bf9176 | 1930 | memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); |
33bf9176 | 1931 | |
b0a17866 SH |
1932 | /* If mpls_depth_delta is positive then an MPLS PUSH action has been |
1933 | * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The | |
1934 | * flow's MPLS LSE should be restored to that value to allow any | |
1935 | * subsequent actions that update of the LSE to be executed correctly. | |
1936 | */ | |
1937 | if (ctx->mpls_depth_delta > 0) { | |
1938 | ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse; | |
9583bc14 | 1939 | } |
b0a17866 SH |
1940 | |
1941 | ctx->xin->flow.dl_type = eth_type; | |
1942 | ctx->mpls_depth_delta--; | |
1943 | ||
1944 | return false; | |
9583bc14 EJ |
1945 | } |
1946 | ||
1947 | static bool | |
1948 | compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids) | |
1949 | { | |
33bf9176 BP |
1950 | struct flow *flow = &ctx->xin->flow; |
1951 | ||
1952 | if (!is_ip_any(flow)) { | |
9583bc14 EJ |
1953 | return false; |
1954 | } | |
1955 | ||
1dd35f8a | 1956 | ctx->xout->wc.masks.nw_ttl = 0xff; |
33bf9176 BP |
1957 | if (flow->nw_ttl > 1) { |
1958 | flow->nw_ttl--; | |
9583bc14 EJ |
1959 | return false; |
1960 | } else { | |
1961 | size_t i; | |
1962 | ||
1963 | for (i = 0; i < ids->n_controllers; i++) { | |
1964 | execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, | |
1965 | ids->cnt_ids[i]); | |
1966 | } | |
1967 | ||
1968 | /* Stop processing for current table. */ | |
1969 | return true; | |
1970 | } | |
1971 | } | |
1972 | ||
1973 | static bool | |
9cfef3d0 | 1974 | compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl) |
9583bc14 EJ |
1975 | { |
1976 | if (!eth_type_mpls(ctx->xin->flow.dl_type)) { | |
1977 | return true; | |
1978 | } | |
1979 | ||
b0a17866 SH |
1980 | /* If mpls_depth_delta is negative then an MPLS POP action has been |
1981 | * executed and the resulting MPLS label stack is unknown. This means | |
1982 | * a SET MPLS TTL push action can't be executed as it needs to manipulate | |
1983 | * the top-most MPLS LSE. Thus, stop processing. | |
1984 | * | |
1985 | * It is planned that in the future this case will be handled | |
1986 | * by recirculation. | |
1987 | */ | |
1988 | if (ctx->mpls_depth_delta < 0) { | |
1989 | return true; | |
1990 | } | |
1991 | ||
f74e7df7 | 1992 | ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK); |
9583bc14 EJ |
1993 | set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl); |
1994 | return false; | |
1995 | } | |
1996 | ||
1997 | static bool | |
9cfef3d0 | 1998 | compose_dec_mpls_ttl_action(struct xlate_ctx *ctx) |
9583bc14 | 1999 | { |
33bf9176 BP |
2000 | struct flow *flow = &ctx->xin->flow; |
2001 | uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse); | |
1dd35f8a JP |
2002 | struct flow_wildcards *wc = &ctx->xout->wc; |
2003 | ||
1dd35f8a | 2004 | memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse); |
9583bc14 | 2005 | |
33bf9176 | 2006 | if (!eth_type_mpls(flow->dl_type)) { |
9583bc14 EJ |
2007 | return false; |
2008 | } | |
2009 | ||
2010 | if (ttl > 1) { | |
2011 | ttl--; | |
33bf9176 | 2012 | set_mpls_lse_ttl(&flow->mpls_lse, ttl); |
9583bc14 EJ |
2013 | return false; |
2014 | } else { | |
2015 | execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0); | |
2016 | ||
2017 | /* Stop processing for current table. */ | |
2018 | return true; | |
2019 | } | |
2020 | } | |
2021 | ||
2022 | static void | |
2023 | xlate_output_action(struct xlate_ctx *ctx, | |
4e022ec0 | 2024 | ofp_port_t port, uint16_t max_len, bool may_packet_in) |
9583bc14 | 2025 | { |
4e022ec0 | 2026 | ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface; |
9583bc14 EJ |
2027 | |
2028 | ctx->xout->nf_output_iface = NF_OUT_DROP; | |
2029 | ||
2030 | switch (port) { | |
2031 | case OFPP_IN_PORT: | |
4e022ec0 | 2032 | compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port); |
9583bc14 EJ |
2033 | break; |
2034 | case OFPP_TABLE: | |
4e022ec0 AW |
2035 | xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port, |
2036 | 0, may_packet_in); | |
9583bc14 EJ |
2037 | break; |
2038 | case OFPP_NORMAL: | |
2039 | xlate_normal(ctx); | |
2040 | break; | |
2041 | case OFPP_FLOOD: | |
2042 | flood_packets(ctx, false); | |
2043 | break; | |
2044 | case OFPP_ALL: | |
2045 | flood_packets(ctx, true); | |
2046 | break; | |
2047 | case OFPP_CONTROLLER: | |
2048 | execute_controller_action(ctx, max_len, OFPR_ACTION, 0); | |
2049 | break; | |
2050 | case OFPP_NONE: | |
2051 | break; | |
2052 | case OFPP_LOCAL: | |
2053 | default: | |
4e022ec0 | 2054 | if (port != ctx->xin->flow.in_port.ofp_port) { |
9583bc14 EJ |
2055 | compose_output_action(ctx, port); |
2056 | } else { | |
2057 | xlate_report(ctx, "skipping output to input port"); | |
2058 | } | |
2059 | break; | |
2060 | } | |
2061 | ||
2062 | if (prev_nf_output_iface == NF_OUT_FLOOD) { | |
2063 | ctx->xout->nf_output_iface = NF_OUT_FLOOD; | |
2064 | } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) { | |
2065 | ctx->xout->nf_output_iface = prev_nf_output_iface; | |
2066 | } else if (prev_nf_output_iface != NF_OUT_DROP && | |
2067 | ctx->xout->nf_output_iface != NF_OUT_FLOOD) { | |
2068 | ctx->xout->nf_output_iface = NF_OUT_MULTI; | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | static void | |
2073 | xlate_output_reg_action(struct xlate_ctx *ctx, | |
2074 | const struct ofpact_output_reg *or) | |
2075 | { | |
2076 | uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow); | |
2077 | if (port <= UINT16_MAX) { | |
2078 | union mf_subvalue value; | |
2079 | ||
2080 | memset(&value, 0xff, sizeof value); | |
2081 | mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks); | |
4e022ec0 AW |
2082 | xlate_output_action(ctx, u16_to_ofp(port), |
2083 | or->max_len, false); | |
9583bc14 EJ |
2084 | } |
2085 | } | |
2086 | ||
2087 | static void | |
2088 | xlate_enqueue_action(struct xlate_ctx *ctx, | |
2089 | const struct ofpact_enqueue *enqueue) | |
2090 | { | |
4e022ec0 | 2091 | ofp_port_t ofp_port = enqueue->port; |
9583bc14 EJ |
2092 | uint32_t queue_id = enqueue->queue; |
2093 | uint32_t flow_priority, priority; | |
2094 | int error; | |
2095 | ||
2096 | /* Translate queue to priority. */ | |
89a8a7f0 | 2097 | error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority); |
9583bc14 EJ |
2098 | if (error) { |
2099 | /* Fall back to ordinary output action. */ | |
2100 | xlate_output_action(ctx, enqueue->port, 0, false); | |
2101 | return; | |
2102 | } | |
2103 | ||
2104 | /* Check output port. */ | |
2105 | if (ofp_port == OFPP_IN_PORT) { | |
4e022ec0 AW |
2106 | ofp_port = ctx->xin->flow.in_port.ofp_port; |
2107 | } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) { | |
9583bc14 EJ |
2108 | return; |
2109 | } | |
2110 | ||
2111 | /* Add datapath actions. */ | |
2112 | flow_priority = ctx->xin->flow.skb_priority; | |
2113 | ctx->xin->flow.skb_priority = priority; | |
2114 | compose_output_action(ctx, ofp_port); | |
2115 | ctx->xin->flow.skb_priority = flow_priority; | |
2116 | ||
2117 | /* Update NetFlow output port. */ | |
2118 | if (ctx->xout->nf_output_iface == NF_OUT_DROP) { | |
2119 | ctx->xout->nf_output_iface = ofp_port; | |
2120 | } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) { | |
2121 | ctx->xout->nf_output_iface = NF_OUT_MULTI; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | static void | |
2126 | xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id) | |
2127 | { | |
2128 | uint32_t skb_priority; | |
2129 | ||
89a8a7f0 | 2130 | if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) { |
9583bc14 EJ |
2131 | ctx->xin->flow.skb_priority = skb_priority; |
2132 | } else { | |
2133 | /* Couldn't translate queue to a priority. Nothing to do. A warning | |
2134 | * has already been logged. */ | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | static bool | |
46c88433 | 2139 | slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_) |
9583bc14 | 2140 | { |
46c88433 EJ |
2141 | const struct xbridge *xbridge = xbridge_; |
2142 | struct xport *port; | |
9583bc14 EJ |
2143 | |
2144 | switch (ofp_port) { | |
2145 | case OFPP_IN_PORT: | |
2146 | case OFPP_TABLE: | |
2147 | case OFPP_NORMAL: | |
2148 | case OFPP_FLOOD: | |
2149 | case OFPP_ALL: | |
2150 | case OFPP_NONE: | |
2151 | return true; | |
2152 | case OFPP_CONTROLLER: /* Not supported by the bundle action. */ | |
2153 | return false; | |
2154 | default: | |
46c88433 | 2155 | port = get_ofp_port(xbridge, ofp_port); |
9583bc14 EJ |
2156 | return port ? port->may_enable : false; |
2157 | } | |
2158 | } | |
2159 | ||
2160 | static void | |
2161 | xlate_bundle_action(struct xlate_ctx *ctx, | |
2162 | const struct ofpact_bundle *bundle) | |
2163 | { | |
4e022ec0 | 2164 | ofp_port_t port; |
9583bc14 EJ |
2165 | |
2166 | port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc, | |
46c88433 EJ |
2167 | slave_enabled_cb, |
2168 | CONST_CAST(struct xbridge *, ctx->xbridge)); | |
9583bc14 | 2169 | if (bundle->dst.field) { |
f74e7df7 JP |
2170 | nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, |
2171 | &ctx->xout->wc); | |
9583bc14 EJ |
2172 | } else { |
2173 | xlate_output_action(ctx, port, 0, false); | |
2174 | } | |
2175 | } | |
2176 | ||
2177 | static void | |
2178 | xlate_learn_action(struct xlate_ctx *ctx, | |
2179 | const struct ofpact_learn *learn) | |
2180 | { | |
1b63b91e BP |
2181 | uint64_t ofpacts_stub[1024 / 8]; |
2182 | struct ofputil_flow_mod fm; | |
9583bc14 | 2183 | struct ofpbuf ofpacts; |
9583bc14 EJ |
2184 | |
2185 | ctx->xout->has_learn = true; | |
2186 | ||
2187 | learn_mask(learn, &ctx->xout->wc); | |
2188 | ||
2189 | if (!ctx->xin->may_learn) { | |
2190 | return; | |
2191 | } | |
2192 | ||
1b63b91e BP |
2193 | ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub); |
2194 | learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts); | |
2195 | ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm); | |
2196 | ofpbuf_uninit(&ofpacts); | |
9583bc14 EJ |
2197 | } |
2198 | ||
9583bc14 EJ |
2199 | static void |
2200 | xlate_fin_timeout(struct xlate_ctx *ctx, | |
2201 | const struct ofpact_fin_timeout *oft) | |
2202 | { | |
2203 | if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) { | |
70742c7f EJ |
2204 | rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout, |
2205 | oft->fin_hard_timeout); | |
9583bc14 EJ |
2206 | } |
2207 | } | |
2208 | ||
2209 | static void | |
2210 | xlate_sample_action(struct xlate_ctx *ctx, | |
2211 | const struct ofpact_sample *os) | |
2212 | { | |
2213 | union user_action_cookie cookie; | |
2214 | /* Scale the probability from 16-bit to 32-bit while representing | |
2215 | * the same percentage. */ | |
2216 | uint32_t probability = (os->probability << 16) | os->probability; | |
2217 | ||
7fd91025 BP |
2218 | ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow, |
2219 | &ctx->xout->odp_actions, | |
2220 | &ctx->xout->wc, | |
2221 | &ctx->mpls_depth_delta); | |
9583bc14 EJ |
2222 | |
2223 | compose_flow_sample_cookie(os->probability, os->collector_set_id, | |
2224 | os->obs_domain_id, os->obs_point_id, &cookie); | |
46c88433 | 2225 | compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow, |
9583bc14 EJ |
2226 | probability, &cookie, sizeof cookie.flow_sample); |
2227 | } | |
2228 | ||
2229 | static bool | |
46c88433 | 2230 | may_receive(const struct xport *xport, struct xlate_ctx *ctx) |
9583bc14 | 2231 | { |
46c88433 EJ |
2232 | if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp) |
2233 | ? OFPUTIL_PC_NO_RECV_STP | |
2234 | : OFPUTIL_PC_NO_RECV)) { | |
9583bc14 EJ |
2235 | return false; |
2236 | } | |
2237 | ||
2238 | /* Only drop packets here if both forwarding and learning are | |
2239 | * disabled. If just learning is enabled, we need to have | |
2240 | * OFPP_NORMAL and the learning action have a look at the packet | |
2241 | * before we can drop it. */ | |
9d189a50 | 2242 | if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) { |
9583bc14 EJ |
2243 | return false; |
2244 | } | |
2245 | ||
2246 | return true; | |
2247 | } | |
2248 | ||
9583bc14 EJ |
2249 | static void |
2250 | do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len, | |
2251 | struct xlate_ctx *ctx) | |
2252 | { | |
33bf9176 BP |
2253 | struct flow_wildcards *wc = &ctx->xout->wc; |
2254 | struct flow *flow = &ctx->xin->flow; | |
9583bc14 EJ |
2255 | const struct ofpact *a; |
2256 | ||
9583bc14 EJ |
2257 | OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) { |
2258 | struct ofpact_controller *controller; | |
2259 | const struct ofpact_metadata *metadata; | |
2260 | ||
2261 | if (ctx->exit) { | |
2262 | break; | |
2263 | } | |
2264 | ||
2265 | switch (a->type) { | |
2266 | case OFPACT_OUTPUT: | |
2267 | xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port, | |
2268 | ofpact_get_OUTPUT(a)->max_len, true); | |
2269 | break; | |
2270 | ||
7395c052 NZ |
2271 | case OFPACT_GROUP: |
2272 | /* XXX not yet implemented */ | |
2273 | break; | |
2274 | ||
9583bc14 EJ |
2275 | case OFPACT_CONTROLLER: |
2276 | controller = ofpact_get_CONTROLLER(a); | |
2277 | execute_controller_action(ctx, controller->max_len, | |
2278 | controller->reason, | |
2279 | controller->controller_id); | |
2280 | break; | |
2281 | ||
2282 | case OFPACT_ENQUEUE: | |
2283 | xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a)); | |
2284 | break; | |
2285 | ||
2286 | case OFPACT_SET_VLAN_VID: | |
f74e7df7 | 2287 | wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI); |
33bf9176 BP |
2288 | flow->vlan_tci &= ~htons(VLAN_VID_MASK); |
2289 | flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid) | |
2290 | | htons(VLAN_CFI)); | |
9583bc14 EJ |
2291 | break; |
2292 | ||
2293 | case OFPACT_SET_VLAN_PCP: | |
f74e7df7 | 2294 | wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI); |
33bf9176 BP |
2295 | flow->vlan_tci &= ~htons(VLAN_PCP_MASK); |
2296 | flow->vlan_tci |= | |
9583bc14 EJ |
2297 | htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT) |
2298 | | VLAN_CFI); | |
2299 | break; | |
2300 | ||
2301 | case OFPACT_STRIP_VLAN: | |
f74e7df7 | 2302 | memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); |
33bf9176 | 2303 | flow->vlan_tci = htons(0); |
9583bc14 EJ |
2304 | break; |
2305 | ||
2306 | case OFPACT_PUSH_VLAN: | |
2307 | /* XXX 802.1AD(QinQ) */ | |
f74e7df7 | 2308 | memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci); |
33bf9176 | 2309 | flow->vlan_tci = htons(VLAN_CFI); |
9583bc14 EJ |
2310 | break; |
2311 | ||
2312 | case OFPACT_SET_ETH_SRC: | |
f74e7df7 | 2313 | memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src); |
33bf9176 | 2314 | memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN); |
9583bc14 EJ |
2315 | break; |
2316 | ||
2317 | case OFPACT_SET_ETH_DST: | |
f74e7df7 | 2318 | memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst); |
33bf9176 | 2319 | memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN); |
9583bc14 EJ |
2320 | break; |
2321 | ||
2322 | case OFPACT_SET_IPV4_SRC: | |
f74e7df7 | 2323 | memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src); |
33bf9176 BP |
2324 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
2325 | flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4; | |
9583bc14 EJ |
2326 | } |
2327 | break; | |
2328 | ||
2329 | case OFPACT_SET_IPV4_DST: | |
f74e7df7 | 2330 | memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst); |
33bf9176 BP |
2331 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
2332 | flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4; | |
9583bc14 EJ |
2333 | } |
2334 | break; | |
2335 | ||
2336 | case OFPACT_SET_IPV4_DSCP: | |
f74e7df7 | 2337 | wc->masks.nw_tos |= IP_DSCP_MASK; |
9583bc14 | 2338 | /* OpenFlow 1.0 only supports IPv4. */ |
33bf9176 BP |
2339 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
2340 | flow->nw_tos &= ~IP_DSCP_MASK; | |
2341 | flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp; | |
9583bc14 EJ |
2342 | } |
2343 | break; | |
2344 | ||
2345 | case OFPACT_SET_L4_SRC_PORT: | |
33bf9176 | 2346 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); |
f74e7df7 | 2347 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); |
33bf9176 BP |
2348 | if (is_ip_any(flow)) { |
2349 | flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port); | |
9583bc14 EJ |
2350 | } |
2351 | break; | |
2352 | ||
2353 | case OFPACT_SET_L4_DST_PORT: | |
33bf9176 | 2354 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); |
f74e7df7 | 2355 | memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst); |
33bf9176 BP |
2356 | if (is_ip_any(flow)) { |
2357 | flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port); | |
9583bc14 EJ |
2358 | } |
2359 | break; | |
2360 | ||
2361 | case OFPACT_RESUBMIT: | |
2362 | xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a)); | |
2363 | break; | |
2364 | ||
2365 | case OFPACT_SET_TUNNEL: | |
33bf9176 | 2366 | flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id); |
9583bc14 EJ |
2367 | break; |
2368 | ||
2369 | case OFPACT_SET_QUEUE: | |
2370 | xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id); | |
2371 | break; | |
2372 | ||
2373 | case OFPACT_POP_QUEUE: | |
33bf9176 | 2374 | flow->skb_priority = ctx->orig_skb_priority; |
9583bc14 EJ |
2375 | break; |
2376 | ||
2377 | case OFPACT_REG_MOVE: | |
33bf9176 | 2378 | nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc); |
9583bc14 EJ |
2379 | break; |
2380 | ||
2381 | case OFPACT_REG_LOAD: | |
33bf9176 | 2382 | nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow); |
9583bc14 EJ |
2383 | break; |
2384 | ||
2385 | case OFPACT_STACK_PUSH: | |
33bf9176 BP |
2386 | nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc, |
2387 | &ctx->stack); | |
9583bc14 EJ |
2388 | break; |
2389 | ||
2390 | case OFPACT_STACK_POP: | |
f74e7df7 JP |
2391 | nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc, |
2392 | &ctx->stack); | |
9583bc14 EJ |
2393 | break; |
2394 | ||
2395 | case OFPACT_PUSH_MPLS: | |
b0a17866 SH |
2396 | if (compose_mpls_push_action(ctx, |
2397 | ofpact_get_PUSH_MPLS(a)->ethertype)) { | |
2398 | return; | |
2399 | } | |
9583bc14 EJ |
2400 | break; |
2401 | ||
2402 | case OFPACT_POP_MPLS: | |
b0a17866 SH |
2403 | if (compose_mpls_pop_action(ctx, |
2404 | ofpact_get_POP_MPLS(a)->ethertype)) { | |
2405 | return; | |
2406 | } | |
9583bc14 EJ |
2407 | break; |
2408 | ||
2409 | case OFPACT_SET_MPLS_TTL: | |
9cfef3d0 | 2410 | if (compose_set_mpls_ttl_action(ctx, |
9583bc14 | 2411 | ofpact_get_SET_MPLS_TTL(a)->ttl)) { |
ad3efdcb | 2412 | return; |
9583bc14 EJ |
2413 | } |
2414 | break; | |
2415 | ||
2416 | case OFPACT_DEC_MPLS_TTL: | |
9cfef3d0 | 2417 | if (compose_dec_mpls_ttl_action(ctx)) { |
ad3efdcb | 2418 | return; |
9583bc14 EJ |
2419 | } |
2420 | break; | |
2421 | ||
2422 | case OFPACT_DEC_TTL: | |
f74e7df7 | 2423 | wc->masks.nw_ttl = 0xff; |
9583bc14 | 2424 | if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) { |
ad3efdcb | 2425 | return; |
9583bc14 EJ |
2426 | } |
2427 | break; | |
2428 | ||
2429 | case OFPACT_NOTE: | |
2430 | /* Nothing to do. */ | |
2431 | break; | |
2432 | ||
2433 | case OFPACT_MULTIPATH: | |
33bf9176 | 2434 | multipath_execute(ofpact_get_MULTIPATH(a), flow, wc); |
9583bc14 EJ |
2435 | break; |
2436 | ||
2437 | case OFPACT_BUNDLE: | |
9583bc14 EJ |
2438 | xlate_bundle_action(ctx, ofpact_get_BUNDLE(a)); |
2439 | break; | |
2440 | ||
2441 | case OFPACT_OUTPUT_REG: | |
2442 | xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a)); | |
2443 | break; | |
2444 | ||
2445 | case OFPACT_LEARN: | |
2446 | xlate_learn_action(ctx, ofpact_get_LEARN(a)); | |
2447 | break; | |
2448 | ||
2449 | case OFPACT_EXIT: | |
2450 | ctx->exit = true; | |
2451 | break; | |
2452 | ||
2453 | case OFPACT_FIN_TIMEOUT: | |
33bf9176 | 2454 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); |
9583bc14 EJ |
2455 | ctx->xout->has_fin_timeout = true; |
2456 | xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a)); | |
2457 | break; | |
2458 | ||
2459 | case OFPACT_CLEAR_ACTIONS: | |
2460 | /* XXX | |
2461 | * Nothing to do because writa-actions is not supported for now. | |
2462 | * When writa-actions is supported, clear-actions also must | |
2463 | * be supported at the same time. | |
2464 | */ | |
2465 | break; | |
2466 | ||
2467 | case OFPACT_WRITE_METADATA: | |
2468 | metadata = ofpact_get_WRITE_METADATA(a); | |
33bf9176 BP |
2469 | flow->metadata &= ~metadata->mask; |
2470 | flow->metadata |= metadata->metadata & metadata->mask; | |
9583bc14 EJ |
2471 | break; |
2472 | ||
638a19b0 JR |
2473 | case OFPACT_METER: |
2474 | /* Not implemented yet. */ | |
2475 | break; | |
2476 | ||
9583bc14 EJ |
2477 | case OFPACT_GOTO_TABLE: { |
2478 | /* It is assumed that goto-table is the last action. */ | |
2479 | struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a); | |
9583bc14 EJ |
2480 | |
2481 | ovs_assert(ctx->table_id < ogt->table_id); | |
4468099e EJ |
2482 | xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port, |
2483 | ogt->table_id, true); | |
9583bc14 EJ |
2484 | break; |
2485 | } | |
2486 | ||
2487 | case OFPACT_SAMPLE: | |
2488 | xlate_sample_action(ctx, ofpact_get_SAMPLE(a)); | |
2489 | break; | |
2490 | } | |
2491 | } | |
9583bc14 EJ |
2492 | } |
2493 | ||
2494 | void | |
2495 | xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto, | |
2496 | const struct flow *flow, struct rule_dpif *rule, | |
2497 | uint8_t tcp_flags, const struct ofpbuf *packet) | |
2498 | { | |
2499 | xin->ofproto = ofproto; | |
2500 | xin->flow = *flow; | |
2501 | xin->packet = packet; | |
2502 | xin->may_learn = packet != NULL; | |
2503 | xin->rule = rule; | |
2504 | xin->ofpacts = NULL; | |
2505 | xin->ofpacts_len = 0; | |
2506 | xin->tcp_flags = tcp_flags; | |
2507 | xin->resubmit_hook = NULL; | |
2508 | xin->report_hook = NULL; | |
2509 | xin->resubmit_stats = NULL; | |
2510 | } | |
2511 | ||
2512 | void | |
2513 | xlate_out_uninit(struct xlate_out *xout) | |
2514 | { | |
2515 | if (xout) { | |
2516 | ofpbuf_uninit(&xout->odp_actions); | |
2517 | } | |
2518 | } | |
2519 | ||
2520 | /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts' | |
2521 | * into datapath actions, using 'ctx', and discards the datapath actions. */ | |
2522 | void | |
2523 | xlate_actions_for_side_effects(struct xlate_in *xin) | |
2524 | { | |
2525 | struct xlate_out xout; | |
2526 | ||
2527 | xlate_actions(xin, &xout); | |
2528 | xlate_out_uninit(&xout); | |
2529 | } | |
2530 | ||
2531 | static void | |
2532 | xlate_report(struct xlate_ctx *ctx, const char *s) | |
2533 | { | |
2534 | if (ctx->xin->report_hook) { | |
4d0acc70 | 2535 | ctx->xin->report_hook(ctx->xin, s, ctx->recurse); |
9583bc14 EJ |
2536 | } |
2537 | } | |
2538 | ||
2539 | void | |
2540 | xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src) | |
2541 | { | |
2542 | dst->wc = src->wc; | |
9583bc14 EJ |
2543 | dst->slow = src->slow; |
2544 | dst->has_learn = src->has_learn; | |
2545 | dst->has_normal = src->has_normal; | |
2546 | dst->has_fin_timeout = src->has_fin_timeout; | |
2547 | dst->nf_output_iface = src->nf_output_iface; | |
2548 | dst->mirrors = src->mirrors; | |
2549 | ||
2550 | ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub, | |
2551 | sizeof dst->odp_actions_stub); | |
2552 | ofpbuf_put(&dst->odp_actions, src->odp_actions.data, | |
2553 | src->odp_actions.size); | |
2554 | } | |
10e57640 EJ |
2555 | |
2556 | /* Returns a reference to the sflow handled associated with ofproto, or NULL if | |
2557 | * there is none. The caller is responsible for decrementing the results ref | |
2558 | * count with dpif_sflow_unref(). */ | |
2559 | struct dpif_sflow * | |
2560 | xlate_get_sflow(const struct ofproto_dpif *ofproto) | |
2561 | { | |
2562 | struct dpif_sflow *sflow = NULL; | |
2563 | struct xbridge *xbridge; | |
2564 | ||
2565 | ovs_rwlock_rdlock(&xlate_rwlock); | |
2566 | xbridge = xbridge_lookup(ofproto); | |
2567 | if (xbridge) { | |
2568 | sflow = dpif_sflow_ref(xbridge->sflow); | |
2569 | } | |
2570 | ovs_rwlock_unlock(&xlate_rwlock); | |
2571 | ||
2572 | return sflow; | |
2573 | } | |
2574 | ||
2575 | /* Returns a reference to the ipfix handled associated with ofproto, or NULL if | |
2576 | * there is none. The caller is responsible for decrementing the results ref | |
2577 | * count with dpif_ipfix_unref(). */ | |
2578 | struct dpif_ipfix * | |
2579 | xlate_get_ipfix(const struct ofproto_dpif *ofproto) | |
2580 | { | |
2581 | struct dpif_ipfix *ipfix = NULL; | |
2582 | struct xbridge *xbridge; | |
2583 | ||
2584 | ovs_rwlock_rdlock(&xlate_rwlock); | |
2585 | xbridge = xbridge_lookup(ofproto); | |
2586 | if (xbridge) { | |
2587 | ipfix = dpif_ipfix_ref(xbridge->ipfix); | |
2588 | } | |
2589 | ovs_rwlock_unlock(&xlate_rwlock); | |
2590 | ||
2591 | return ipfix; | |
2592 | } | |
9583bc14 | 2593 | \f |
55954f6e EJ |
2594 | static struct skb_priority_to_dscp * |
2595 | get_skb_priority(const struct xport *xport, uint32_t skb_priority) | |
2596 | { | |
2597 | struct skb_priority_to_dscp *pdscp; | |
2598 | uint32_t hash; | |
2599 | ||
2600 | hash = hash_int(skb_priority, 0); | |
2601 | HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) { | |
2602 | if (pdscp->skb_priority == skb_priority) { | |
2603 | return pdscp; | |
2604 | } | |
2605 | } | |
2606 | return NULL; | |
2607 | } | |
2608 | ||
2609 | static bool | |
2610 | dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority, | |
2611 | uint8_t *dscp) | |
2612 | { | |
2613 | struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority); | |
2614 | *dscp = pdscp ? pdscp->dscp : 0; | |
2615 | return pdscp != NULL; | |
2616 | } | |
2617 | ||
2618 | static void | |
2619 | clear_skb_priorities(struct xport *xport) | |
2620 | { | |
2621 | struct skb_priority_to_dscp *pdscp, *next; | |
2622 | ||
2623 | HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) { | |
2624 | hmap_remove(&xport->skb_priorities, &pdscp->hmap_node); | |
2625 | free(pdscp); | |
2626 | } | |
2627 | } | |
2628 | ||
ce4a6b76 BP |
2629 | static bool |
2630 | actions_output_to_local_port(const struct xlate_ctx *ctx) | |
2631 | { | |
46c88433 | 2632 | odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL); |
ce4a6b76 BP |
2633 | const struct nlattr *a; |
2634 | unsigned int left; | |
2635 | ||
2636 | NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data, | |
2637 | ctx->xout->odp_actions.size) { | |
2638 | if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT | |
2639 | && nl_attr_get_odp_port(a) == local_odp_port) { | |
2640 | return true; | |
2641 | } | |
2642 | } | |
2643 | return false; | |
2644 | } | |
9583bc14 | 2645 | |
91d6cd12 AW |
2646 | /* Thread safe call to xlate_actions__(). */ |
2647 | void | |
2648 | xlate_actions(struct xlate_in *xin, struct xlate_out *xout) | |
2649 | { | |
2650 | ovs_rwlock_rdlock(&xlate_rwlock); | |
2651 | xlate_actions__(xin, xout); | |
2652 | ovs_rwlock_unlock(&xlate_rwlock); | |
2653 | } | |
2654 | ||
9583bc14 | 2655 | /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts' |
56450a41 BP |
2656 | * into datapath actions in 'odp_actions', using 'ctx'. |
2657 | * | |
2658 | * The caller must take responsibility for eventually freeing 'xout', with | |
2659 | * xlate_out_uninit(). */ | |
91d6cd12 AW |
2660 | static void |
2661 | xlate_actions__(struct xlate_in *xin, struct xlate_out *xout) | |
2662 | OVS_REQ_RDLOCK(xlate_rwlock) | |
9583bc14 | 2663 | { |
33bf9176 BP |
2664 | struct flow_wildcards *wc = &xout->wc; |
2665 | struct flow *flow = &xin->flow; | |
10c44245 | 2666 | struct rule_dpif *rule = NULL; |
33bf9176 | 2667 | |
6f00e29b | 2668 | struct rule_actions *actions = NULL; |
9583bc14 EJ |
2669 | enum slow_path_reason special; |
2670 | const struct ofpact *ofpacts; | |
46c88433 | 2671 | struct xport *in_port; |
9583bc14 EJ |
2672 | struct flow orig_flow; |
2673 | struct xlate_ctx ctx; | |
2674 | size_t ofpacts_len; | |
62a7cc71 | 2675 | bool tnl_may_send; |
9583bc14 | 2676 | |
46c88433 | 2677 | COVERAGE_INC(xlate_actions); |
9583bc14 EJ |
2678 | |
2679 | /* Flow initialization rules: | |
2680 | * - 'base_flow' must match the kernel's view of the packet at the | |
2681 | * time that action processing starts. 'flow' represents any | |
2682 | * transformations we wish to make through actions. | |
2683 | * - By default 'base_flow' and 'flow' are the same since the input | |
2684 | * packet matches the output before any actions are applied. | |
2685 | * - When using VLAN splinters, 'base_flow''s VLAN is set to the value | |
2686 | * of the received packet as seen by the kernel. If we later output | |
2687 | * to another device without any modifications this will cause us to | |
2688 | * insert a new tag since the original one was stripped off by the | |
2689 | * VLAN device. | |
2690 | * - Tunnel metadata as received is retained in 'flow'. This allows | |
2691 | * tunnel metadata matching also in later tables. | |
2692 | * Since a kernel action for setting the tunnel metadata will only be | |
2693 | * generated with actual tunnel output, changing the tunnel metadata | |
2694 | * values in 'flow' (such as tun_id) will only have effect with a later | |
2695 | * tunnel output action. | |
2696 | * - Tunnel 'base_flow' is completely cleared since that is what the | |
2697 | * kernel does. If we wish to maintain the original values an action | |
2698 | * needs to be generated. */ | |
2699 | ||
2700 | ctx.xin = xin; | |
2701 | ctx.xout = xout; | |
46c88433 EJ |
2702 | ctx.xout->slow = 0; |
2703 | ctx.xout->has_learn = false; | |
2704 | ctx.xout->has_normal = false; | |
2705 | ctx.xout->has_fin_timeout = false; | |
2706 | ctx.xout->nf_output_iface = NF_OUT_DROP; | |
2707 | ctx.xout->mirrors = 0; | |
2708 | ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub, | |
2709 | sizeof ctx.xout->odp_actions_stub); | |
2710 | ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE); | |
2711 | ||
2712 | ctx.xbridge = xbridge_lookup(xin->ofproto); | |
2713 | if (!ctx.xbridge) { | |
dc24a00f | 2714 | goto out; |
46c88433 | 2715 | } |
9583bc14 | 2716 | |
9583bc14 EJ |
2717 | ctx.rule = xin->rule; |
2718 | ||
33bf9176 | 2719 | ctx.base_flow = *flow; |
9583bc14 | 2720 | memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel); |
33bf9176 | 2721 | ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst; |
9583bc14 | 2722 | |
33bf9176 BP |
2723 | flow_wildcards_init_catchall(wc); |
2724 | memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port); | |
1dd35f8a | 2725 | memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority); |
7431e171 | 2726 | memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type); |
1dd35f8a | 2727 | wc->masks.nw_frag |= FLOW_NW_FRAG_MASK; |
9583bc14 | 2728 | |
62a7cc71 | 2729 | tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc); |
46c88433 | 2730 | if (ctx.xbridge->has_netflow) { |
9b658910 | 2731 | netflow_mask_wc(flow, wc); |
9583bc14 EJ |
2732 | } |
2733 | ||
9583bc14 | 2734 | ctx.recurse = 0; |
98b07853 | 2735 | ctx.resubmits = 0; |
33bf9176 | 2736 | ctx.orig_skb_priority = flow->skb_priority; |
9583bc14 EJ |
2737 | ctx.table_id = 0; |
2738 | ctx.exit = false; | |
b0a17866 | 2739 | ctx.mpls_depth_delta = 0; |
9583bc14 | 2740 | |
10c44245 EJ |
2741 | if (!xin->ofpacts && !ctx.rule) { |
2742 | rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule); | |
2743 | if (ctx.xin->resubmit_stats) { | |
2744 | rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats); | |
2745 | } | |
2746 | ctx.rule = rule; | |
2747 | } | |
2748 | xout->fail_open = ctx.rule && rule_dpif_fail_open(ctx.rule); | |
2749 | ||
9583bc14 EJ |
2750 | if (xin->ofpacts) { |
2751 | ofpacts = xin->ofpacts; | |
2752 | ofpacts_len = xin->ofpacts_len; | |
10c44245 EJ |
2753 | } else if (ctx.rule) { |
2754 | actions = rule_dpif_get_actions(ctx.rule); | |
6f00e29b BP |
2755 | ofpacts = actions->ofpacts; |
2756 | ofpacts_len = actions->ofpacts_len; | |
9583bc14 EJ |
2757 | } else { |
2758 | NOT_REACHED(); | |
2759 | } | |
2760 | ||
2761 | ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack); | |
2762 | ||
ade6ad9c | 2763 | if (mbridge_has_mirrors(ctx.xbridge->mbridge)) { |
9583bc14 EJ |
2764 | /* Do this conditionally because the copy is expensive enough that it |
2765 | * shows up in profiles. */ | |
33bf9176 | 2766 | orig_flow = *flow; |
9583bc14 EJ |
2767 | } |
2768 | ||
33bf9176 | 2769 | if (flow->nw_frag & FLOW_NW_FRAG_ANY) { |
46c88433 | 2770 | switch (ctx.xbridge->frag) { |
9583bc14 EJ |
2771 | case OFPC_FRAG_NORMAL: |
2772 | /* We must pretend that transport ports are unavailable. */ | |
33bf9176 BP |
2773 | flow->tp_src = ctx.base_flow.tp_src = htons(0); |
2774 | flow->tp_dst = ctx.base_flow.tp_dst = htons(0); | |
9583bc14 EJ |
2775 | break; |
2776 | ||
2777 | case OFPC_FRAG_DROP: | |
dc24a00f | 2778 | goto out; |
9583bc14 EJ |
2779 | |
2780 | case OFPC_FRAG_REASM: | |
2781 | NOT_REACHED(); | |
2782 | ||
2783 | case OFPC_FRAG_NX_MATCH: | |
2784 | /* Nothing to do. */ | |
2785 | break; | |
2786 | ||
2787 | case OFPC_INVALID_TTL_TO_CONTROLLER: | |
2788 | NOT_REACHED(); | |
2789 | } | |
2790 | } | |
2791 | ||
46c88433 | 2792 | in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port); |
642dc74d | 2793 | special = process_special(&ctx, flow, in_port, ctx.xin->packet); |
9583bc14 | 2794 | if (special) { |
04594cd5 | 2795 | ctx.xout->slow |= special; |
9583bc14 | 2796 | } else { |
9583bc14 | 2797 | size_t sample_actions_len; |
9583bc14 | 2798 | |
4e022ec0 | 2799 | if (flow->in_port.ofp_port |
46c88433 EJ |
2800 | != vsp_realdev_to_vlandev(ctx.xbridge->ofproto, |
2801 | flow->in_port.ofp_port, | |
33bf9176 | 2802 | flow->vlan_tci)) { |
9583bc14 EJ |
2803 | ctx.base_flow.vlan_tci = 0; |
2804 | } | |
2805 | ||
2806 | add_sflow_action(&ctx); | |
2807 | add_ipfix_action(&ctx); | |
2808 | sample_actions_len = ctx.xout->odp_actions.size; | |
2809 | ||
62a7cc71 | 2810 | if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) { |
9583bc14 EJ |
2811 | do_xlate_actions(ofpacts, ofpacts_len, &ctx); |
2812 | ||
2813 | /* We've let OFPP_NORMAL and the learning action look at the | |
2814 | * packet, so drop it now if forwarding is disabled. */ | |
9d189a50 | 2815 | if (in_port && !xport_stp_forward_state(in_port)) { |
9583bc14 EJ |
2816 | ctx.xout->odp_actions.size = sample_actions_len; |
2817 | } | |
2818 | } | |
2819 | ||
46c88433 | 2820 | if (ctx.xbridge->has_in_band |
ce4a6b76 BP |
2821 | && in_band_must_output_to_local_port(flow) |
2822 | && !actions_output_to_local_port(&ctx)) { | |
9583bc14 EJ |
2823 | compose_output_action(&ctx, OFPP_LOCAL); |
2824 | } | |
aaa0fbae EJ |
2825 | |
2826 | fix_sflow_action(&ctx); | |
2827 | ||
46c88433 | 2828 | if (mbridge_has_mirrors(ctx.xbridge->mbridge)) { |
9583bc14 EJ |
2829 | add_mirror_actions(&ctx, &orig_flow); |
2830 | } | |
9583bc14 EJ |
2831 | } |
2832 | ||
542024c4 BP |
2833 | if (nl_attr_oversized(ctx.xout->odp_actions.size)) { |
2834 | /* These datapath actions are too big for a Netlink attribute, so we | |
2835 | * can't execute them. */ | |
2836 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1); | |
2837 | ||
2838 | VLOG_ERR_RL(&rl, "discarding oversize datapath actions"); | |
2839 | ofpbuf_clear(&ctx.xout->odp_actions); | |
2840 | } | |
2841 | ||
9583bc14 EJ |
2842 | ofpbuf_uninit(&ctx.stack); |
2843 | ||
2844 | /* Clear the metadata and register wildcard masks, because we won't | |
2845 | * use non-header fields as part of the cache. */ | |
33bf9176 BP |
2846 | memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata); |
2847 | memset(&wc->masks.regs, 0, sizeof wc->masks.regs); | |
dc24a00f EJ |
2848 | |
2849 | out: | |
91d6cd12 | 2850 | rule_actions_unref(actions); |
10c44245 | 2851 | rule_dpif_unref(rule); |
91d6cd12 AW |
2852 | } |
2853 | ||
2854 | /* Sends 'packet' out 'ofport'. | |
2855 | * May modify 'packet'. | |
2856 | * Returns 0 if successful, otherwise a positive errno value. */ | |
2857 | int | |
2858 | xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet) | |
2859 | { | |
91d6cd12 | 2860 | struct xport *xport; |
91d6cd12 | 2861 | struct ofpact_output output; |
91d6cd12 AW |
2862 | struct flow flow; |
2863 | union flow_in_port in_port_; | |
2864 | int error; | |
2865 | ||
91d6cd12 AW |
2866 | ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output); |
2867 | /* Use OFPP_NONE as the in_port to avoid special packet processing. */ | |
2868 | in_port_.ofp_port = OFPP_NONE; | |
2869 | flow_extract(packet, 0, 0, NULL, &in_port_, &flow); | |
2870 | ||
2871 | ovs_rwlock_rdlock(&xlate_rwlock); | |
2872 | xport = xport_lookup(ofport); | |
2873 | if (!xport) { | |
91d6cd12 | 2874 | ovs_rwlock_unlock(&xlate_rwlock); |
02ea2703 | 2875 | return EINVAL; |
91d6cd12 | 2876 | } |
91d6cd12 AW |
2877 | output.port = xport->ofp_port; |
2878 | output.max_len = 0; | |
690efec2 AW |
2879 | error = ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL, |
2880 | &output.ofpact, sizeof output, | |
2881 | packet); | |
dc24a00f | 2882 | ovs_rwlock_unlock(&xlate_rwlock); |
91d6cd12 | 2883 | return error; |
9583bc14 | 2884 | } |