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