]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif-xlate.c
odp-execute: Refine signatures for odp_execute_actions() callbacks.
[ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
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 51COVERAGE_DEFINE(xlate_actions);
9583bc14
EJ
52
53VLOG_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
63struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
64
46c88433
EJ
65struct 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
90struct 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
111struct 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
140struct 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() */
190static 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. */
195struct 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
202static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
203static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
204static struct hmap xports = HMAP_INITIALIZER(&xports);
205
206static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
207static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
208 struct xlate_ctx *);
91d6cd12
AW
209static void xlate_actions__(struct xlate_in *, struct xlate_out *)
210 OVS_REQ_RDLOCK(xlate_rwlock);
9583bc14
EJ
211static void xlate_normal(struct xlate_ctx *);
212static void xlate_report(struct xlate_ctx *, const char *);
4e022ec0 213static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
9583bc14 214 uint8_t table_id, bool may_packet_in);
46c88433
EJ
215static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
216static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
217static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 218 uint16_t vlan);
4e022ec0 219static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
9583bc14 220
46c88433
EJ
221static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
222static struct xbundle *xbundle_lookup(const struct ofbundle *);
5e6af486 223static struct xport *xport_lookup(const struct ofport_dpif *);
46c88433 224static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
225static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
226 uint32_t skb_priority);
227static void clear_skb_priorities(struct xport *);
228static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
229 uint8_t *dscp);
46c88433
EJ
230
231void
232xlate_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
289void
290xlate_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
319void
320xlate_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
360void
361xlate_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
383void
384xlate_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
465void
466xlate_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. */
518int
519xlate_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
576exit:
577 if (fitnessp) {
578 *fitnessp = fitness;
579 }
dc24a00f 580 ovs_rwlock_unlock(&xlate_rwlock);
8449c4d6
EJ
581 return error;
582}
583
46c88433
EJ
584static struct xbridge *
585xbridge_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
602static struct xbundle *
603xbundle_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
620static struct xport *
5e6af486 621xport_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
638static struct stp_port *
639xport_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
646static enum stp_state
647xport_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
653static bool
654xport_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.*/
662static bool
663stp_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
669static void
670stp_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
692static struct xport *
693get_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
706static odp_port_t
707ofp_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 713static bool
46c88433 714xbundle_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
720static bool
46c88433
EJ
721xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
722{
723 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
724}
725
726static mirror_mask_t
727xbundle_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
734static mirror_mask_t
735xbundle_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
742static mirror_mask_t
743xbundle_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
750static struct xbundle *
751lookup_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
795static void
796add_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. */
890static uint16_t
46c88433 891input_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. */
917static bool
46c88433 918input_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. */
971static uint16_t
46c88433 972output_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
990static void
46c88433 991output_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. */
1036static bool
1037is_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. */
1071static bool
1072is_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. */
1112static void
1113update_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
1150static void
1151update_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 */
1190static bool
46c88433 1191is_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
1236static void
1237xlate_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 */
1340static size_t
46c88433 1341compose_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
1368static void
46c88433
EJ
1369compose_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. */
1401static size_t
46c88433 1402compose_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
1422static void
1423compose_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
1434static void
1435compose_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. */
1441static void
46c88433 1442compose_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. */
1463static void
1464add_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. */
1475static void
1476add_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. */
1485static void
1486fix_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 1503static enum slow_path_reason
642dc74d 1504process_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 1538static void
4e022ec0 1539compose_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) {
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) {
1dd35f8a 1663 commit_odp_actions(flow, &ctx->base_flow,
b0a17866
SH
1664 &ctx->xout->odp_actions, &ctx->xout->wc,
1665 &ctx->mpls_depth_delta);
4e022ec0
AW
1666 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1667 out_port);
9583bc14 1668
6cbbf4fa
EJ
1669 ctx->sflow_odp_port = odp_port;
1670 ctx->sflow_n_outputs++;
1671 ctx->xout->nf_output_iface = ofp_port;
1672 }
1673
1674 out:
9583bc14 1675 /* Restore flow */
33bf9176 1676 flow->vlan_tci = flow_vlan_tci;
1362e248 1677 flow->pkt_mark = flow_pkt_mark;
33bf9176 1678 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
1679}
1680
1681static void
4e022ec0 1682compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
9583bc14
EJ
1683{
1684 compose_output_action__(ctx, ofp_port, true);
1685}
1686
bb61b33d
BP
1687static void
1688xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
1689{
1690 struct rule_dpif *old_rule = ctx->rule;
6f00e29b 1691 struct rule_actions *actions;
bb61b33d
BP
1692
1693 if (ctx->xin->resubmit_stats) {
70742c7f 1694 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
1695 }
1696
98b07853 1697 ctx->resubmits++;
bb61b33d
BP
1698 ctx->recurse++;
1699 ctx->rule = rule;
6f00e29b
BP
1700 actions = rule_dpif_get_actions(rule);
1701 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1702 rule_actions_unref(actions);
bb61b33d
BP
1703 ctx->rule = old_rule;
1704 ctx->recurse--;
bb61b33d
BP
1705}
1706
9583bc14
EJ
1707static void
1708xlate_table_action(struct xlate_ctx *ctx,
4e022ec0 1709 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
9583bc14 1710{
98b07853
BP
1711 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1712
1713 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1714 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1715 MAX_RESUBMIT_RECURSION);
1716 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1717 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1718 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1719 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1720 } else if (ctx->stack.size >= 65536) {
1721 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1722 } else {
9583bc14 1723 struct rule_dpif *rule;
4e022ec0 1724 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
1725 uint8_t old_table_id = ctx->table_id;
1726
1727 ctx->table_id = table_id;
1728
bb61b33d
BP
1729 /* Look up a flow with 'in_port' as the input port. Then restore the
1730 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1731 * have surprising behavior). */
4e022ec0 1732 ctx->xin->flow.in_port.ofp_port = in_port;
a2143702
BP
1733 rule_dpif_lookup_in_table(ctx->xbridge->ofproto,
1734 &ctx->xin->flow, &ctx->xout->wc,
1735 table_id, &rule);
4e022ec0 1736 ctx->xin->flow.in_port.ofp_port = old_in_port;
9583bc14 1737
ad3efdcb
EJ
1738 if (ctx->xin->resubmit_hook) {
1739 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1740 }
1741
a2143702 1742 if (!rule && may_packet_in) {
ad3efdcb
EJ
1743 struct xport *xport;
1744
ad3efdcb
EJ
1745 /* XXX
1746 * check if table configuration flags
1747 * OFPTC_TABLE_MISS_CONTROLLER, default.
1748 * OFPTC_TABLE_MISS_CONTINUE,
1749 * OFPTC_TABLE_MISS_DROP
1750 * When OF1.0, OFPTC_TABLE_MISS_CONTINUE is used. What to do? */
1751 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
70742c7f
EJ
1752 choose_miss_rule(xport ? xport->config : 0,
1753 ctx->xbridge->miss_rule,
1754 ctx->xbridge->no_packet_in_rule, &rule);
a2143702
BP
1755 }
1756 if (rule) {
bb61b33d 1757 xlate_recursively(ctx, rule);
a2143702 1758 rule_dpif_unref(rule);
ad3efdcb
EJ
1759 }
1760
9583bc14 1761 ctx->table_id = old_table_id;
98b07853 1762 return;
9583bc14 1763 }
98b07853
BP
1764
1765 ctx->exit = true;
9583bc14
EJ
1766}
1767
1768static void
1769xlate_ofpact_resubmit(struct xlate_ctx *ctx,
1770 const struct ofpact_resubmit *resubmit)
1771{
4e022ec0 1772 ofp_port_t in_port;
9583bc14
EJ
1773 uint8_t table_id;
1774
1775 in_port = resubmit->in_port;
1776 if (in_port == OFPP_IN_PORT) {
4e022ec0 1777 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
1778 }
1779
1780 table_id = resubmit->table_id;
1781 if (table_id == 255) {
1782 table_id = ctx->table_id;
1783 }
1784
1785 xlate_table_action(ctx, in_port, table_id, false);
1786}
1787
1788static void
1789flood_packets(struct xlate_ctx *ctx, bool all)
1790{
46c88433 1791 const struct xport *xport;
9583bc14 1792
46c88433
EJ
1793 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
1794 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
1795 continue;
1796 }
1797
1798 if (all) {
46c88433
EJ
1799 compose_output_action__(ctx, xport->ofp_port, false);
1800 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
1801 compose_output_action(ctx, xport->ofp_port);
9583bc14
EJ
1802 }
1803 }
1804
1805 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1806}
1807
1808static void
1809execute_controller_action(struct xlate_ctx *ctx, int len,
1810 enum ofp_packet_in_reason reason,
1811 uint16_t controller_id)
1812{
ada3a58d 1813 struct ofputil_packet_in *pin;
9583bc14
EJ
1814 struct ofpbuf *packet;
1815 struct flow key;
1816
1817 ovs_assert(!ctx->xout->slow || ctx->xout->slow == SLOW_CONTROLLER);
1818 ctx->xout->slow = SLOW_CONTROLLER;
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
1829 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
b0a17866
SH
1830 &ctx->xout->odp_actions, &ctx->xout->wc,
1831 &ctx->mpls_depth_delta);
9583bc14
EJ
1832
1833 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
1834 ctx->xout->odp_actions.size, NULL, NULL);
1835
ada3a58d
EJ
1836 pin = xmalloc(sizeof *pin);
1837 pin->packet_len = packet->size;
1838 pin->packet = ofpbuf_steal_data(packet);
1839 pin->reason = reason;
1840 pin->controller_id = controller_id;
1841 pin->table_id = ctx->table_id;
70742c7f 1842 pin->cookie = ctx->rule ? rule_dpif_get_flow_cookie(ctx->rule) : 0;
9583bc14 1843
ada3a58d
EJ
1844 pin->send_len = len;
1845 flow_get_metadata(&ctx->xin->flow, &pin->fmd);
9583bc14 1846
ada3a58d 1847 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
9583bc14
EJ
1848 ofpbuf_delete(packet);
1849}
1850
b0a17866 1851static bool
9cfef3d0 1852compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 1853{
33bf9176
BP
1854 struct flow_wildcards *wc = &ctx->xout->wc;
1855 struct flow *flow = &ctx->xin->flow;
1856
9583bc14
EJ
1857 ovs_assert(eth_type_mpls(eth_type));
1858
b0a17866
SH
1859 /* If mpls_depth_delta is negative then an MPLS POP action has been
1860 * composed and the resulting MPLS label stack is unknown. This means
1861 * an MPLS PUSH action can't be composed as it needs to know either the
1862 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
1863 * there is no MPLS label stack present. Thus, stop processing.
1864 *
1865 * If mpls_depth_delta is positive then an MPLS PUSH action has been
1866 * composed and no further MPLS PUSH action may be performed without
1867 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1868 * Thus, stop processing.
1869 *
1870 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
1871 * has been updated. Performing a MPLS PUSH action may be would result in
1872 * losing MPLS LSE and ether type information held in xtx->xin->flow.
1873 * Thus, stop processing.
1874 *
1875 * It is planned that in the future this case will be handled
1876 * by recirculation */
1877 if (ctx->mpls_depth_delta ||
1878 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
1879 return true;
1880 }
1881
33bf9176 1882 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
9583bc14 1883
b0a17866
SH
1884 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
1885
1886 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
33bf9176 1887 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
9583bc14
EJ
1888 } else {
1889 ovs_be32 label;
1890 uint8_t tc, ttl;
1891
33bf9176 1892 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
9583bc14
EJ
1893 label = htonl(0x2); /* IPV6 Explicit Null. */
1894 } else {
1895 label = htonl(0x0); /* IPV4 Explicit Null. */
1896 }
1dd35f8a
JP
1897 wc->masks.nw_tos |= IP_DSCP_MASK;
1898 wc->masks.nw_ttl = 0xff;
33bf9176
BP
1899 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1900 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
1901 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
9583bc14 1902 }
33bf9176 1903 flow->dl_type = eth_type;
b0a17866
SH
1904 ctx->mpls_depth_delta++;
1905
1906 return false;
9583bc14
EJ
1907}
1908
b0a17866 1909static bool
9cfef3d0 1910compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 1911{
33bf9176 1912 struct flow_wildcards *wc = &ctx->xout->wc;
33bf9176 1913
b0a17866
SH
1914 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1915 return true;
1916 }
1917
1918 /* If mpls_depth_delta is negative then an MPLS POP action has been
1919 * composed. Performing another MPLS POP action
1920 * would result in losing ether type that results from
1921 * the already composed MPLS POP. Thus, stop processing.
1922 *
1923 * It is planned that in the future this case will be handled
1924 * by recirculation */
1925 if (ctx->mpls_depth_delta < 0) {
1926 return true;
1927 }
9583bc14 1928
33bf9176 1929 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
33bf9176 1930
b0a17866
SH
1931 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
1932 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
1933 * flow's MPLS LSE should be restored to that value to allow any
1934 * subsequent actions that update of the LSE to be executed correctly.
1935 */
1936 if (ctx->mpls_depth_delta > 0) {
1937 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
9583bc14 1938 }
b0a17866
SH
1939
1940 ctx->xin->flow.dl_type = eth_type;
1941 ctx->mpls_depth_delta--;
1942
1943 return false;
9583bc14
EJ
1944}
1945
1946static bool
1947compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
1948{
33bf9176
BP
1949 struct flow *flow = &ctx->xin->flow;
1950
1951 if (!is_ip_any(flow)) {
9583bc14
EJ
1952 return false;
1953 }
1954
1dd35f8a 1955 ctx->xout->wc.masks.nw_ttl = 0xff;
33bf9176
BP
1956 if (flow->nw_ttl > 1) {
1957 flow->nw_ttl--;
9583bc14
EJ
1958 return false;
1959 } else {
1960 size_t i;
1961
1962 for (i = 0; i < ids->n_controllers; i++) {
1963 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
1964 ids->cnt_ids[i]);
1965 }
1966
1967 /* Stop processing for current table. */
1968 return true;
1969 }
1970}
1971
1972static bool
9cfef3d0 1973compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14
EJ
1974{
1975 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
1976 return true;
1977 }
1978
b0a17866
SH
1979 /* If mpls_depth_delta is negative then an MPLS POP action has been
1980 * executed and the resulting MPLS label stack is unknown. This means
1981 * a SET MPLS TTL push action can't be executed as it needs to manipulate
1982 * the top-most MPLS LSE. Thus, stop processing.
1983 *
1984 * It is planned that in the future this case will be handled
1985 * by recirculation.
1986 */
1987 if (ctx->mpls_depth_delta < 0) {
1988 return true;
1989 }
1990
f74e7df7 1991 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
9583bc14
EJ
1992 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
1993 return false;
1994}
1995
1996static bool
9cfef3d0 1997compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 1998{
33bf9176
BP
1999 struct flow *flow = &ctx->xin->flow;
2000 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
1dd35f8a
JP
2001 struct flow_wildcards *wc = &ctx->xout->wc;
2002
1dd35f8a 2003 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
9583bc14 2004
33bf9176 2005 if (!eth_type_mpls(flow->dl_type)) {
9583bc14
EJ
2006 return false;
2007 }
2008
2009 if (ttl > 1) {
2010 ttl--;
33bf9176 2011 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
9583bc14
EJ
2012 return false;
2013 } else {
2014 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2015
2016 /* Stop processing for current table. */
2017 return true;
2018 }
2019}
2020
2021static void
2022xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 2023 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 2024{
4e022ec0 2025 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
9583bc14
EJ
2026
2027 ctx->xout->nf_output_iface = NF_OUT_DROP;
2028
2029 switch (port) {
2030 case OFPP_IN_PORT:
4e022ec0 2031 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
9583bc14
EJ
2032 break;
2033 case OFPP_TABLE:
4e022ec0
AW
2034 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2035 0, may_packet_in);
9583bc14
EJ
2036 break;
2037 case OFPP_NORMAL:
2038 xlate_normal(ctx);
2039 break;
2040 case OFPP_FLOOD:
2041 flood_packets(ctx, false);
2042 break;
2043 case OFPP_ALL:
2044 flood_packets(ctx, true);
2045 break;
2046 case OFPP_CONTROLLER:
2047 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2048 break;
2049 case OFPP_NONE:
2050 break;
2051 case OFPP_LOCAL:
2052 default:
4e022ec0 2053 if (port != ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2054 compose_output_action(ctx, port);
2055 } else {
2056 xlate_report(ctx, "skipping output to input port");
2057 }
2058 break;
2059 }
2060
2061 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2062 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2063 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2064 ctx->xout->nf_output_iface = prev_nf_output_iface;
2065 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2066 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2067 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2068 }
2069}
2070
2071static void
2072xlate_output_reg_action(struct xlate_ctx *ctx,
2073 const struct ofpact_output_reg *or)
2074{
2075 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2076 if (port <= UINT16_MAX) {
2077 union mf_subvalue value;
2078
2079 memset(&value, 0xff, sizeof value);
2080 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
4e022ec0
AW
2081 xlate_output_action(ctx, u16_to_ofp(port),
2082 or->max_len, false);
9583bc14
EJ
2083 }
2084}
2085
2086static void
2087xlate_enqueue_action(struct xlate_ctx *ctx,
2088 const struct ofpact_enqueue *enqueue)
2089{
4e022ec0 2090 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
2091 uint32_t queue_id = enqueue->queue;
2092 uint32_t flow_priority, priority;
2093 int error;
2094
2095 /* Translate queue to priority. */
89a8a7f0 2096 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
2097 if (error) {
2098 /* Fall back to ordinary output action. */
2099 xlate_output_action(ctx, enqueue->port, 0, false);
2100 return;
2101 }
2102
2103 /* Check output port. */
2104 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
2105 ofp_port = ctx->xin->flow.in_port.ofp_port;
2106 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2107 return;
2108 }
2109
2110 /* Add datapath actions. */
2111 flow_priority = ctx->xin->flow.skb_priority;
2112 ctx->xin->flow.skb_priority = priority;
2113 compose_output_action(ctx, ofp_port);
2114 ctx->xin->flow.skb_priority = flow_priority;
2115
2116 /* Update NetFlow output port. */
2117 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2118 ctx->xout->nf_output_iface = ofp_port;
2119 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2120 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2121 }
2122}
2123
2124static void
2125xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2126{
2127 uint32_t skb_priority;
2128
89a8a7f0 2129 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
2130 ctx->xin->flow.skb_priority = skb_priority;
2131 } else {
2132 /* Couldn't translate queue to a priority. Nothing to do. A warning
2133 * has already been logged. */
2134 }
2135}
2136
2137static bool
46c88433 2138slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 2139{
46c88433
EJ
2140 const struct xbridge *xbridge = xbridge_;
2141 struct xport *port;
9583bc14
EJ
2142
2143 switch (ofp_port) {
2144 case OFPP_IN_PORT:
2145 case OFPP_TABLE:
2146 case OFPP_NORMAL:
2147 case OFPP_FLOOD:
2148 case OFPP_ALL:
2149 case OFPP_NONE:
2150 return true;
2151 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2152 return false;
2153 default:
46c88433 2154 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
2155 return port ? port->may_enable : false;
2156 }
2157}
2158
2159static void
2160xlate_bundle_action(struct xlate_ctx *ctx,
2161 const struct ofpact_bundle *bundle)
2162{
4e022ec0 2163 ofp_port_t port;
9583bc14
EJ
2164
2165 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
46c88433
EJ
2166 slave_enabled_cb,
2167 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 2168 if (bundle->dst.field) {
f74e7df7
JP
2169 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2170 &ctx->xout->wc);
9583bc14
EJ
2171 } else {
2172 xlate_output_action(ctx, port, 0, false);
2173 }
2174}
2175
2176static void
2177xlate_learn_action(struct xlate_ctx *ctx,
2178 const struct ofpact_learn *learn)
2179{
1b63b91e
BP
2180 uint64_t ofpacts_stub[1024 / 8];
2181 struct ofputil_flow_mod fm;
9583bc14 2182 struct ofpbuf ofpacts;
9583bc14
EJ
2183
2184 ctx->xout->has_learn = true;
2185
2186 learn_mask(learn, &ctx->xout->wc);
2187
2188 if (!ctx->xin->may_learn) {
2189 return;
2190 }
2191
1b63b91e
BP
2192 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2193 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2194 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2195 ofpbuf_uninit(&ofpacts);
9583bc14
EJ
2196}
2197
9583bc14
EJ
2198static void
2199xlate_fin_timeout(struct xlate_ctx *ctx,
2200 const struct ofpact_fin_timeout *oft)
2201{
2202 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
70742c7f
EJ
2203 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2204 oft->fin_hard_timeout);
9583bc14
EJ
2205 }
2206}
2207
2208static void
2209xlate_sample_action(struct xlate_ctx *ctx,
2210 const struct ofpact_sample *os)
2211{
2212 union user_action_cookie cookie;
2213 /* Scale the probability from 16-bit to 32-bit while representing
2214 * the same percentage. */
2215 uint32_t probability = (os->probability << 16) | os->probability;
2216
2217 commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
b0a17866
SH
2218 &ctx->xout->odp_actions, &ctx->xout->wc,
2219 &ctx->mpls_depth_delta);
9583bc14
EJ
2220
2221 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2222 os->obs_domain_id, os->obs_point_id, &cookie);
46c88433 2223 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
9583bc14
EJ
2224 probability, &cookie, sizeof cookie.flow_sample);
2225}
2226
2227static bool
46c88433 2228may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 2229{
46c88433
EJ
2230 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2231 ? OFPUTIL_PC_NO_RECV_STP
2232 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
2233 return false;
2234 }
2235
2236 /* Only drop packets here if both forwarding and learning are
2237 * disabled. If just learning is enabled, we need to have
2238 * OFPP_NORMAL and the learning action have a look at the packet
2239 * before we can drop it. */
9d189a50 2240 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
9583bc14
EJ
2241 return false;
2242 }
2243
2244 return true;
2245}
2246
9583bc14
EJ
2247static void
2248do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2249 struct xlate_ctx *ctx)
2250{
33bf9176
BP
2251 struct flow_wildcards *wc = &ctx->xout->wc;
2252 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
2253 const struct ofpact *a;
2254
9583bc14
EJ
2255 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2256 struct ofpact_controller *controller;
2257 const struct ofpact_metadata *metadata;
2258
2259 if (ctx->exit) {
2260 break;
2261 }
2262
2263 switch (a->type) {
2264 case OFPACT_OUTPUT:
2265 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2266 ofpact_get_OUTPUT(a)->max_len, true);
2267 break;
2268
7395c052
NZ
2269 case OFPACT_GROUP:
2270 /* XXX not yet implemented */
2271 break;
2272
9583bc14
EJ
2273 case OFPACT_CONTROLLER:
2274 controller = ofpact_get_CONTROLLER(a);
2275 execute_controller_action(ctx, controller->max_len,
2276 controller->reason,
2277 controller->controller_id);
2278 break;
2279
2280 case OFPACT_ENQUEUE:
2281 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2282 break;
2283
2284 case OFPACT_SET_VLAN_VID:
f74e7df7 2285 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
33bf9176
BP
2286 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2287 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2288 | htons(VLAN_CFI));
9583bc14
EJ
2289 break;
2290
2291 case OFPACT_SET_VLAN_PCP:
f74e7df7 2292 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
33bf9176
BP
2293 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2294 flow->vlan_tci |=
9583bc14
EJ
2295 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp << VLAN_PCP_SHIFT)
2296 | VLAN_CFI);
2297 break;
2298
2299 case OFPACT_STRIP_VLAN:
f74e7df7 2300 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 2301 flow->vlan_tci = htons(0);
9583bc14
EJ
2302 break;
2303
2304 case OFPACT_PUSH_VLAN:
2305 /* XXX 802.1AD(QinQ) */
f74e7df7 2306 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 2307 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
2308 break;
2309
2310 case OFPACT_SET_ETH_SRC:
f74e7df7 2311 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
33bf9176 2312 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
2313 break;
2314
2315 case OFPACT_SET_ETH_DST:
f74e7df7 2316 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
33bf9176 2317 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
2318 break;
2319
2320 case OFPACT_SET_IPV4_SRC:
f74e7df7 2321 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176
BP
2322 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2323 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
2324 }
2325 break;
2326
2327 case OFPACT_SET_IPV4_DST:
f74e7df7 2328 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176
BP
2329 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2330 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
2331 }
2332 break;
2333
2334 case OFPACT_SET_IPV4_DSCP:
f74e7df7 2335 wc->masks.nw_tos |= IP_DSCP_MASK;
9583bc14 2336 /* OpenFlow 1.0 only supports IPv4. */
33bf9176
BP
2337 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2338 flow->nw_tos &= ~IP_DSCP_MASK;
2339 flow->nw_tos |= ofpact_get_SET_IPV4_DSCP(a)->dscp;
9583bc14
EJ
2340 }
2341 break;
2342
2343 case OFPACT_SET_L4_SRC_PORT:
33bf9176 2344 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
f74e7df7 2345 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176
BP
2346 if (is_ip_any(flow)) {
2347 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
2348 }
2349 break;
2350
2351 case OFPACT_SET_L4_DST_PORT:
33bf9176 2352 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
f74e7df7 2353 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176
BP
2354 if (is_ip_any(flow)) {
2355 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
2356 }
2357 break;
2358
2359 case OFPACT_RESUBMIT:
2360 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2361 break;
2362
2363 case OFPACT_SET_TUNNEL:
33bf9176 2364 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
2365 break;
2366
2367 case OFPACT_SET_QUEUE:
2368 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2369 break;
2370
2371 case OFPACT_POP_QUEUE:
33bf9176 2372 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
2373 break;
2374
2375 case OFPACT_REG_MOVE:
33bf9176 2376 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
2377 break;
2378
2379 case OFPACT_REG_LOAD:
33bf9176 2380 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow);
9583bc14
EJ
2381 break;
2382
2383 case OFPACT_STACK_PUSH:
33bf9176
BP
2384 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2385 &ctx->stack);
9583bc14
EJ
2386 break;
2387
2388 case OFPACT_STACK_POP:
f74e7df7
JP
2389 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2390 &ctx->stack);
9583bc14
EJ
2391 break;
2392
2393 case OFPACT_PUSH_MPLS:
b0a17866
SH
2394 if (compose_mpls_push_action(ctx,
2395 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2396 return;
2397 }
9583bc14
EJ
2398 break;
2399
2400 case OFPACT_POP_MPLS:
b0a17866
SH
2401 if (compose_mpls_pop_action(ctx,
2402 ofpact_get_POP_MPLS(a)->ethertype)) {
2403 return;
2404 }
9583bc14
EJ
2405 break;
2406
2407 case OFPACT_SET_MPLS_TTL:
9cfef3d0 2408 if (compose_set_mpls_ttl_action(ctx,
9583bc14 2409 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
ad3efdcb 2410 return;
9583bc14
EJ
2411 }
2412 break;
2413
2414 case OFPACT_DEC_MPLS_TTL:
9cfef3d0 2415 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 2416 return;
9583bc14
EJ
2417 }
2418 break;
2419
2420 case OFPACT_DEC_TTL:
f74e7df7 2421 wc->masks.nw_ttl = 0xff;
9583bc14 2422 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 2423 return;
9583bc14
EJ
2424 }
2425 break;
2426
2427 case OFPACT_NOTE:
2428 /* Nothing to do. */
2429 break;
2430
2431 case OFPACT_MULTIPATH:
33bf9176 2432 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
2433 break;
2434
2435 case OFPACT_BUNDLE:
9583bc14
EJ
2436 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2437 break;
2438
2439 case OFPACT_OUTPUT_REG:
2440 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2441 break;
2442
2443 case OFPACT_LEARN:
2444 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2445 break;
2446
2447 case OFPACT_EXIT:
2448 ctx->exit = true;
2449 break;
2450
2451 case OFPACT_FIN_TIMEOUT:
33bf9176 2452 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
2453 ctx->xout->has_fin_timeout = true;
2454 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2455 break;
2456
2457 case OFPACT_CLEAR_ACTIONS:
2458 /* XXX
2459 * Nothing to do because writa-actions is not supported for now.
2460 * When writa-actions is supported, clear-actions also must
2461 * be supported at the same time.
2462 */
2463 break;
2464
2465 case OFPACT_WRITE_METADATA:
2466 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
2467 flow->metadata &= ~metadata->mask;
2468 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
2469 break;
2470
638a19b0
JR
2471 case OFPACT_METER:
2472 /* Not implemented yet. */
2473 break;
2474
9583bc14
EJ
2475 case OFPACT_GOTO_TABLE: {
2476 /* It is assumed that goto-table is the last action. */
2477 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14
EJ
2478
2479 ovs_assert(ctx->table_id < ogt->table_id);
4468099e
EJ
2480 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2481 ogt->table_id, true);
9583bc14
EJ
2482 break;
2483 }
2484
2485 case OFPACT_SAMPLE:
2486 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2487 break;
2488 }
2489 }
9583bc14
EJ
2490}
2491
2492void
2493xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2494 const struct flow *flow, struct rule_dpif *rule,
2495 uint8_t tcp_flags, const struct ofpbuf *packet)
2496{
2497 xin->ofproto = ofproto;
2498 xin->flow = *flow;
2499 xin->packet = packet;
2500 xin->may_learn = packet != NULL;
2501 xin->rule = rule;
2502 xin->ofpacts = NULL;
2503 xin->ofpacts_len = 0;
2504 xin->tcp_flags = tcp_flags;
2505 xin->resubmit_hook = NULL;
2506 xin->report_hook = NULL;
2507 xin->resubmit_stats = NULL;
2508}
2509
2510void
2511xlate_out_uninit(struct xlate_out *xout)
2512{
2513 if (xout) {
2514 ofpbuf_uninit(&xout->odp_actions);
2515 }
2516}
2517
2518/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2519 * into datapath actions, using 'ctx', and discards the datapath actions. */
2520void
2521xlate_actions_for_side_effects(struct xlate_in *xin)
2522{
2523 struct xlate_out xout;
2524
2525 xlate_actions(xin, &xout);
2526 xlate_out_uninit(&xout);
2527}
2528
2529static void
2530xlate_report(struct xlate_ctx *ctx, const char *s)
2531{
2532 if (ctx->xin->report_hook) {
4d0acc70 2533 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
9583bc14
EJ
2534 }
2535}
2536
2537void
2538xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2539{
2540 dst->wc = src->wc;
9583bc14
EJ
2541 dst->slow = src->slow;
2542 dst->has_learn = src->has_learn;
2543 dst->has_normal = src->has_normal;
2544 dst->has_fin_timeout = src->has_fin_timeout;
2545 dst->nf_output_iface = src->nf_output_iface;
2546 dst->mirrors = src->mirrors;
2547
2548 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2549 sizeof dst->odp_actions_stub);
2550 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2551 src->odp_actions.size);
2552}
10e57640
EJ
2553
2554/* Returns a reference to the sflow handled associated with ofproto, or NULL if
2555 * there is none. The caller is responsible for decrementing the results ref
2556 * count with dpif_sflow_unref(). */
2557struct dpif_sflow *
2558xlate_get_sflow(const struct ofproto_dpif *ofproto)
2559{
2560 struct dpif_sflow *sflow = NULL;
2561 struct xbridge *xbridge;
2562
2563 ovs_rwlock_rdlock(&xlate_rwlock);
2564 xbridge = xbridge_lookup(ofproto);
2565 if (xbridge) {
2566 sflow = dpif_sflow_ref(xbridge->sflow);
2567 }
2568 ovs_rwlock_unlock(&xlate_rwlock);
2569
2570 return sflow;
2571}
2572
2573/* Returns a reference to the ipfix handled associated with ofproto, or NULL if
2574 * there is none. The caller is responsible for decrementing the results ref
2575 * count with dpif_ipfix_unref(). */
2576struct dpif_ipfix *
2577xlate_get_ipfix(const struct ofproto_dpif *ofproto)
2578{
2579 struct dpif_ipfix *ipfix = NULL;
2580 struct xbridge *xbridge;
2581
2582 ovs_rwlock_rdlock(&xlate_rwlock);
2583 xbridge = xbridge_lookup(ofproto);
2584 if (xbridge) {
2585 ipfix = dpif_ipfix_ref(xbridge->ipfix);
2586 }
2587 ovs_rwlock_unlock(&xlate_rwlock);
2588
2589 return ipfix;
2590}
9583bc14 2591\f
55954f6e
EJ
2592static struct skb_priority_to_dscp *
2593get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2594{
2595 struct skb_priority_to_dscp *pdscp;
2596 uint32_t hash;
2597
2598 hash = hash_int(skb_priority, 0);
2599 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2600 if (pdscp->skb_priority == skb_priority) {
2601 return pdscp;
2602 }
2603 }
2604 return NULL;
2605}
2606
2607static bool
2608dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2609 uint8_t *dscp)
2610{
2611 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2612 *dscp = pdscp ? pdscp->dscp : 0;
2613 return pdscp != NULL;
2614}
2615
2616static void
2617clear_skb_priorities(struct xport *xport)
2618{
2619 struct skb_priority_to_dscp *pdscp, *next;
2620
2621 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2622 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2623 free(pdscp);
2624 }
2625}
2626
ce4a6b76
BP
2627static bool
2628actions_output_to_local_port(const struct xlate_ctx *ctx)
2629{
46c88433 2630 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
2631 const struct nlattr *a;
2632 unsigned int left;
2633
2634 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2635 ctx->xout->odp_actions.size) {
2636 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2637 && nl_attr_get_odp_port(a) == local_odp_port) {
2638 return true;
2639 }
2640 }
2641 return false;
2642}
9583bc14 2643
91d6cd12
AW
2644/* Thread safe call to xlate_actions__(). */
2645void
2646xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2647{
2648 ovs_rwlock_rdlock(&xlate_rwlock);
2649 xlate_actions__(xin, xout);
2650 ovs_rwlock_unlock(&xlate_rwlock);
2651}
2652
9583bc14 2653/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
56450a41
BP
2654 * into datapath actions in 'odp_actions', using 'ctx'.
2655 *
2656 * The caller must take responsibility for eventually freeing 'xout', with
2657 * xlate_out_uninit(). */
91d6cd12
AW
2658static void
2659xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
2660 OVS_REQ_RDLOCK(xlate_rwlock)
9583bc14 2661{
33bf9176
BP
2662 struct flow_wildcards *wc = &xout->wc;
2663 struct flow *flow = &xin->flow;
10c44245 2664 struct rule_dpif *rule = NULL;
33bf9176 2665
6f00e29b 2666 struct rule_actions *actions = NULL;
9583bc14
EJ
2667 enum slow_path_reason special;
2668 const struct ofpact *ofpacts;
46c88433 2669 struct xport *in_port;
9583bc14
EJ
2670 struct flow orig_flow;
2671 struct xlate_ctx ctx;
2672 size_t ofpacts_len;
62a7cc71 2673 bool tnl_may_send;
9583bc14 2674
46c88433 2675 COVERAGE_INC(xlate_actions);
9583bc14
EJ
2676
2677 /* Flow initialization rules:
2678 * - 'base_flow' must match the kernel's view of the packet at the
2679 * time that action processing starts. 'flow' represents any
2680 * transformations we wish to make through actions.
2681 * - By default 'base_flow' and 'flow' are the same since the input
2682 * packet matches the output before any actions are applied.
2683 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
2684 * of the received packet as seen by the kernel. If we later output
2685 * to another device without any modifications this will cause us to
2686 * insert a new tag since the original one was stripped off by the
2687 * VLAN device.
2688 * - Tunnel metadata as received is retained in 'flow'. This allows
2689 * tunnel metadata matching also in later tables.
2690 * Since a kernel action for setting the tunnel metadata will only be
2691 * generated with actual tunnel output, changing the tunnel metadata
2692 * values in 'flow' (such as tun_id) will only have effect with a later
2693 * tunnel output action.
2694 * - Tunnel 'base_flow' is completely cleared since that is what the
2695 * kernel does. If we wish to maintain the original values an action
2696 * needs to be generated. */
2697
2698 ctx.xin = xin;
2699 ctx.xout = xout;
46c88433
EJ
2700 ctx.xout->slow = 0;
2701 ctx.xout->has_learn = false;
2702 ctx.xout->has_normal = false;
2703 ctx.xout->has_fin_timeout = false;
2704 ctx.xout->nf_output_iface = NF_OUT_DROP;
2705 ctx.xout->mirrors = 0;
2706 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
2707 sizeof ctx.xout->odp_actions_stub);
2708 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
2709
2710 ctx.xbridge = xbridge_lookup(xin->ofproto);
2711 if (!ctx.xbridge) {
dc24a00f 2712 goto out;
46c88433 2713 }
9583bc14 2714
9583bc14
EJ
2715 ctx.rule = xin->rule;
2716
33bf9176 2717 ctx.base_flow = *flow;
9583bc14 2718 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
33bf9176 2719 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
9583bc14 2720
33bf9176
BP
2721 flow_wildcards_init_catchall(wc);
2722 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1dd35f8a 2723 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
7431e171 2724 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
1dd35f8a 2725 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
9583bc14 2726
62a7cc71 2727 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
46c88433 2728 if (ctx.xbridge->has_netflow) {
9b658910 2729 netflow_mask_wc(flow, wc);
9583bc14
EJ
2730 }
2731
9583bc14 2732 ctx.recurse = 0;
98b07853 2733 ctx.resubmits = 0;
33bf9176 2734 ctx.orig_skb_priority = flow->skb_priority;
9583bc14
EJ
2735 ctx.table_id = 0;
2736 ctx.exit = false;
b0a17866 2737 ctx.mpls_depth_delta = 0;
9583bc14 2738
10c44245
EJ
2739 if (!xin->ofpacts && !ctx.rule) {
2740 rule_dpif_lookup(ctx.xbridge->ofproto, flow, wc, &rule);
2741 if (ctx.xin->resubmit_stats) {
2742 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
2743 }
2744 ctx.rule = rule;
2745 }
2746 xout->fail_open = ctx.rule && rule_dpif_fail_open(ctx.rule);
2747
9583bc14
EJ
2748 if (xin->ofpacts) {
2749 ofpacts = xin->ofpacts;
2750 ofpacts_len = xin->ofpacts_len;
10c44245
EJ
2751 } else if (ctx.rule) {
2752 actions = rule_dpif_get_actions(ctx.rule);
6f00e29b
BP
2753 ofpacts = actions->ofpacts;
2754 ofpacts_len = actions->ofpacts_len;
9583bc14
EJ
2755 } else {
2756 NOT_REACHED();
2757 }
2758
2759 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
2760
ade6ad9c 2761 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
2762 /* Do this conditionally because the copy is expensive enough that it
2763 * shows up in profiles. */
33bf9176 2764 orig_flow = *flow;
9583bc14
EJ
2765 }
2766
33bf9176 2767 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
46c88433 2768 switch (ctx.xbridge->frag) {
9583bc14
EJ
2769 case OFPC_FRAG_NORMAL:
2770 /* We must pretend that transport ports are unavailable. */
33bf9176
BP
2771 flow->tp_src = ctx.base_flow.tp_src = htons(0);
2772 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
9583bc14
EJ
2773 break;
2774
2775 case OFPC_FRAG_DROP:
dc24a00f 2776 goto out;
9583bc14
EJ
2777
2778 case OFPC_FRAG_REASM:
2779 NOT_REACHED();
2780
2781 case OFPC_FRAG_NX_MATCH:
2782 /* Nothing to do. */
2783 break;
2784
2785 case OFPC_INVALID_TTL_TO_CONTROLLER:
2786 NOT_REACHED();
2787 }
2788 }
2789
46c88433 2790 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
642dc74d 2791 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
9583bc14
EJ
2792 if (special) {
2793 ctx.xout->slow = special;
2794 } else {
9583bc14 2795 size_t sample_actions_len;
9583bc14 2796
4e022ec0 2797 if (flow->in_port.ofp_port
46c88433
EJ
2798 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
2799 flow->in_port.ofp_port,
33bf9176 2800 flow->vlan_tci)) {
9583bc14
EJ
2801 ctx.base_flow.vlan_tci = 0;
2802 }
2803
2804 add_sflow_action(&ctx);
2805 add_ipfix_action(&ctx);
2806 sample_actions_len = ctx.xout->odp_actions.size;
2807
62a7cc71 2808 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
9583bc14
EJ
2809 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
2810
2811 /* We've let OFPP_NORMAL and the learning action look at the
2812 * packet, so drop it now if forwarding is disabled. */
9d189a50 2813 if (in_port && !xport_stp_forward_state(in_port)) {
9583bc14
EJ
2814 ctx.xout->odp_actions.size = sample_actions_len;
2815 }
2816 }
2817
46c88433 2818 if (ctx.xbridge->has_in_band
ce4a6b76
BP
2819 && in_band_must_output_to_local_port(flow)
2820 && !actions_output_to_local_port(&ctx)) {
9583bc14
EJ
2821 compose_output_action(&ctx, OFPP_LOCAL);
2822 }
aaa0fbae
EJ
2823
2824 fix_sflow_action(&ctx);
2825
46c88433 2826 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
2827 add_mirror_actions(&ctx, &orig_flow);
2828 }
9583bc14
EJ
2829 }
2830
542024c4
BP
2831 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
2832 /* These datapath actions are too big for a Netlink attribute, so we
2833 * can't execute them. */
2834 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2835
2836 VLOG_ERR_RL(&rl, "discarding oversize datapath actions");
2837 ofpbuf_clear(&ctx.xout->odp_actions);
2838 }
2839
9583bc14
EJ
2840 ofpbuf_uninit(&ctx.stack);
2841
2842 /* Clear the metadata and register wildcard masks, because we won't
2843 * use non-header fields as part of the cache. */
33bf9176
BP
2844 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2845 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
dc24a00f
EJ
2846
2847out:
91d6cd12 2848 rule_actions_unref(actions);
10c44245 2849 rule_dpif_unref(rule);
91d6cd12
AW
2850}
2851
2852/* Sends 'packet' out 'ofport'.
2853 * May modify 'packet'.
2854 * Returns 0 if successful, otherwise a positive errno value. */
2855int
2856xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
2857{
2858 uint64_t odp_actions_stub[1024 / 8];
2859 struct xport *xport;
2860 struct ofpbuf key, odp_actions;
2861 struct dpif_flow_stats stats;
2862 struct odputil_keybuf keybuf;
2863 struct ofpact_output output;
2864 struct xlate_out xout;
2865 struct xlate_in xin;
2866 struct flow flow;
2867 union flow_in_port in_port_;
2868 int error;
2869
2870 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
2871 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2872 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
2873 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
2874 in_port_.ofp_port = OFPP_NONE;
2875 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
2876
2877 ovs_rwlock_rdlock(&xlate_rwlock);
2878 xport = xport_lookup(ofport);
2879 if (!xport) {
2880 error = EINVAL;
2881 ovs_rwlock_unlock(&xlate_rwlock);
2882 goto out;
2883 }
2884
2885 odp_flow_key_from_flow(&key, &flow, ofp_port_to_odp_port(xport->xbridge, OFPP_LOCAL));
2886 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
2887 output.port = xport->ofp_port;
2888 output.max_len = 0;
2889 xlate_in_init(&xin, xport->xbridge->ofproto, &flow, NULL, 0, packet);
2890 xin.ofpacts_len = sizeof output;
2891 xin.ofpacts = &output.ofpact;
2892 xin.resubmit_stats = &stats;
2893 /* Calls xlate_actions__ directly, since the rdlock is acquired. */
2894 xlate_actions__(&xin, &xout);
2895 error = dpif_execute(xport->xbridge->dpif,
2896 key.data, key.size,
2897 xout.odp_actions.data, xout.odp_actions.size,
2898 packet);
dc24a00f 2899 ovs_rwlock_unlock(&xlate_rwlock);
6f00e29b 2900
91d6cd12
AW
2901out:
2902 xlate_out_uninit(&xout);
2903 return error;
9583bc14 2904}