]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-xlate.c
ofproto: Probe for masked set action support.
[mirror_ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
0934ebba 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
9583bc14
EJ
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 35#include "mac-learning.h"
6d95c4e8 36#include "mcast-snooping.h"
9583bc14
EJ
37#include "meta-flow.h"
38#include "multipath.h"
39#include "netdev-vport.h"
40#include "netlink.h"
41#include "nx-match.h"
42#include "odp-execute.h"
43#include "ofp-actions.h"
44#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 45#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 46#include "ofproto/ofproto-dpif-monitor.h"
9583bc14
EJ
47#include "ofproto/ofproto-dpif-sflow.h"
48#include "ofproto/ofproto-dpif.h"
6f00e29b 49#include "ofproto/ofproto-provider.h"
91088554 50#include "packet-dpif.h"
9583bc14
EJ
51#include "tunnel.h"
52#include "vlog.h"
53
46c88433 54COVERAGE_DEFINE(xlate_actions);
0f032e95 55COVERAGE_DEFINE(xlate_actions_oversize);
8bfd0fda 56COVERAGE_DEFINE(xlate_actions_mpls_overflow);
9583bc14
EJ
57
58VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
59
8a553e9a
EJ
60/* Maximum depth of flow table recursion (due to resubmit actions) in a
61 * flow translation. */
62#define MAX_RESUBMIT_RECURSION 64
adcf00ba
AZ
63#define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
64 internal table. */
8a553e9a 65
7bbdd84f
SH
66/* Timeout for internal rules created to handle recirculation */
67#define RECIRC_TIMEOUT 60
68
98b07853
BP
69/* Maximum number of resubmit actions in a flow translation, whether they are
70 * recursive or not. */
71#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
72
46c88433
EJ
73struct xbridge {
74 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
75 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
76
77 struct list xbundles; /* Owned xbundles. */
78 struct hmap xports; /* Indexed by ofp_port. */
79
80 char *name; /* Name used in log messages. */
89a8a7f0 81 struct dpif *dpif; /* Datapath interface. */
46c88433 82 struct mac_learning *ml; /* Mac learning handle. */
6d95c4e8 83 struct mcast_snooping *ms; /* Multicast Snooping handle. */
46c88433
EJ
84 struct mbridge *mbridge; /* Mirroring. */
85 struct dpif_sflow *sflow; /* SFlow handle, or null. */
86 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 87 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 88 struct stp *stp; /* STP or null if disabled. */
9efd308e 89 struct rstp *rstp; /* RSTP or null if disabled. */
46c88433 90
ec89fc6f
EJ
91 /* Special rules installed by ofproto-dpif. */
92 struct rule_dpif *miss_rule;
93 struct rule_dpif *no_packet_in_rule;
94
46c88433 95 enum ofp_config_flags frag; /* Fragmentation handling. */
46c88433
EJ
96 bool has_in_band; /* Bridge has in band control? */
97 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 98
adcf00ba
AZ
99 /* True if the datapath supports recirculation. */
100 bool enable_recirc;
101
4b97b70d
BP
102 /* True if the datapath supports variable-length
103 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
104 * False if the datapath supports only 8-byte (or shorter) userdata. */
105 bool variable_length_userdata;
8bfd0fda
BP
106
107 /* Number of MPLS label stack entries that the datapath supports
108 * in matches. */
109 size_t max_mpls_depth;
53477c2c
JR
110
111 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
112 * actions. */
113 bool masked_set_action;
46c88433
EJ
114};
115
116struct xbundle {
117 struct hmap_node hmap_node; /* In global 'xbundles' map. */
118 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
119
120 struct list list_node; /* In parent 'xbridges' list. */
121 struct xbridge *xbridge; /* Parent xbridge. */
122
123 struct list xports; /* Contains "struct xport"s. */
124
125 char *name; /* Name used in log messages. */
126 struct bond *bond; /* Nonnull iff more than one port. */
127 struct lacp *lacp; /* LACP handle or null. */
128
129 enum port_vlan_mode vlan_mode; /* VLAN mode. */
130 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
131 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
132 * NULL if all VLANs are trunked. */
133 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
134 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
135};
136
137struct xport {
138 struct hmap_node hmap_node; /* Node in global 'xports' map. */
139 struct ofport_dpif *ofport; /* Key in global 'xports map. */
140
141 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
142 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
143
144 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
145
146 struct list bundle_node; /* In parent xbundle (if it exists). */
147 struct xbundle *xbundle; /* Parent xbundle or null. */
148
149 struct netdev *netdev; /* 'ofport''s netdev. */
150
151 struct xbridge *xbridge; /* Parent bridge. */
152 struct xport *peer; /* Patch port peer or null. */
153
154 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 155 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 156 int stp_port_no; /* STP port number or -1 if not in use. */
f025bcb7 157 struct rstp_port *rstp_port; /* RSTP port or null. */
46c88433 158
55954f6e
EJ
159 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
160
46c88433
EJ
161 bool may_enable; /* May be enabled in bonds. */
162 bool is_tunnel; /* Is a tunnel port. */
163
164 struct cfm *cfm; /* CFM handle or null. */
165 struct bfd *bfd; /* BFD handle or null. */
166};
167
4d0acc70
EJ
168struct xlate_ctx {
169 struct xlate_in *xin;
170 struct xlate_out *xout;
171
46c88433 172 const struct xbridge *xbridge;
4d0acc70
EJ
173
174 /* Flow at the last commit. */
175 struct flow base_flow;
176
177 /* Tunnel IP destination address as received. This is stored separately
178 * as the base_flow.tunnel is cleared on init to reflect the datapath
179 * behavior. Used to make sure not to send tunneled output to ourselves,
180 * which might lead to an infinite loop. This could happen easily
181 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
182 * actually set the tun_dst field. */
183 ovs_be32 orig_tunnel_ip_dst;
184
185 /* Stack for the push and pop actions. Each stack element is of type
186 * "union mf_subvalue". */
187 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
188 struct ofpbuf stack;
189
190 /* The rule that we are currently translating, or NULL. */
191 struct rule_dpif *rule;
192
98b07853
BP
193 /* Resubmit statistics, via xlate_table_action(). */
194 int recurse; /* Current resubmit nesting depth. */
195 int resubmits; /* Total number of resubmits. */
5a070238 196 bool in_group; /* Currently translating ofgroup, if true. */
98b07853 197
4d0acc70
EJ
198 uint32_t orig_skb_priority; /* Priority when packet arrived. */
199 uint8_t table_id; /* OpenFlow table ID where flow was found. */
200 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 201 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
4d0acc70
EJ
202 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
203 bool exit; /* No further actions should be processed. */
7fdb60a7 204
92c08f09
SH
205 bool use_recirc; /* Should generate recirc? */
206 struct xlate_recirc recirc; /* Information used for generating
207 * recirculation actions */
208
7bbdd84f
SH
209 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
210 * This is a trigger for recirculation in cases where translating an action
211 * or looking up a flow requires access to the fields of the packet after
212 * the MPLS label stack that was originally present. */
213 bool was_mpls;
214
7fdb60a7
SH
215 /* OpenFlow 1.1+ action set.
216 *
217 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
218 * When translation is otherwise complete, ofpacts_execute_action_set()
219 * converts it to a set of "struct ofpact"s that can be translated into
220 * datapath actions. */
221 struct ofpbuf action_set; /* Action set. */
222 uint64_t action_set_stub[1024 / 8];
4d0acc70
EJ
223};
224
9583bc14
EJ
225/* A controller may use OFPP_NONE as the ingress port to indicate that
226 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
227 * when an input bundle is needed for validation (e.g., mirroring or
228 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
229 * any 'port' structs, so care must be taken when dealing with it. */
230static struct xbundle ofpp_none_bundle = {
231 .name = "OFPP_NONE",
232 .vlan_mode = PORT_VLAN_TRUNK
233};
9583bc14 234
55954f6e
EJ
235/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
236 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
237 * traffic egressing the 'ofport' with that priority should be marked with. */
238struct skb_priority_to_dscp {
239 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
240 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
241
242 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
243};
244
b256dc52
JS
245enum xc_type {
246 XC_RULE,
247 XC_BOND,
248 XC_NETDEV,
249 XC_NETFLOW,
250 XC_MIRROR,
251 XC_LEARN,
252 XC_NORMAL,
253 XC_FIN_TIMEOUT,
1e684d7d 254 XC_GROUP,
b256dc52
JS
255};
256
257/* xlate_cache entries hold enough information to perform the side effects of
258 * xlate_actions() for a rule, without needing to perform rule translation
259 * from scratch. The primary usage of these is to submit statistics to objects
260 * that a flow relates to, although they may be used for other effects as well
261 * (for instance, refreshing hard timeouts for learned flows). */
262struct xc_entry {
263 enum xc_type type;
264 union {
265 struct rule_dpif *rule;
266 struct {
267 struct netdev *tx;
268 struct netdev *rx;
269 struct bfd *bfd;
270 } dev;
271 struct {
272 struct netflow *netflow;
273 struct flow *flow;
274 ofp_port_t iface;
275 } nf;
276 struct {
277 struct mbridge *mbridge;
278 mirror_mask_t mirrors;
279 } mirror;
280 struct {
281 struct bond *bond;
282 struct flow *flow;
283 uint16_t vid;
284 } bond;
285 struct {
4165b5e0
JS
286 struct ofproto_dpif *ofproto;
287 struct ofputil_flow_mod *fm;
288 struct ofpbuf *ofpacts;
b256dc52
JS
289 } learn;
290 struct {
291 struct ofproto_dpif *ofproto;
292 struct flow *flow;
293 int vlan;
294 } normal;
295 struct {
296 struct rule_dpif *rule;
297 uint16_t idle;
298 uint16_t hard;
299 } fin;
1e684d7d
RW
300 struct {
301 struct group_dpif *group;
302 struct ofputil_bucket *bucket;
303 } group;
b256dc52
JS
304 } u;
305};
306
307#define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
308 entries = xcache->entries; \
309 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
310 entry; \
311 entry = ofpbuf_try_pull(&entries, sizeof *entry))
312
313struct xlate_cache {
314 struct ofpbuf entries;
315};
316
84f0f298
RW
317/* Xlate config contains hash maps of all bridges, bundles and ports.
318 * Xcfgp contains the pointer to the current xlate configuration.
319 * When the main thread needs to change the configuration, it copies xcfgp to
320 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
321 * does not block handler and revalidator threads. */
322struct xlate_cfg {
323 struct hmap xbridges;
324 struct hmap xbundles;
325 struct hmap xports;
326};
f439f23b
BP
327static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_TYPE_INITIALIZER;
328static struct xlate_cfg *new_xcfg = NULL;
46c88433
EJ
329
330static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
331static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
332 struct xlate_ctx *);
adcf00ba
AZ
333static void xlate_normal(struct xlate_ctx *);
334static void xlate_report(struct xlate_ctx *, const char *);
6d328fa2
SH
335static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
336 uint8_t table_id, bool may_packet_in,
337 bool honor_table_miss);
46c88433
EJ
338static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
339static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
340static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 341 uint16_t vlan);
4e022ec0 342static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
9583bc14 343
84f0f298
RW
344static struct xbridge *xbridge_lookup(struct xlate_cfg *,
345 const struct ofproto_dpif *);
346static struct xbundle *xbundle_lookup(struct xlate_cfg *,
347 const struct ofbundle *);
348static struct xport *xport_lookup(struct xlate_cfg *,
349 const struct ofport_dpif *);
46c88433 350static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
351static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
352 uint32_t skb_priority);
353static void clear_skb_priorities(struct xport *);
354static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
355 uint8_t *dscp);
46c88433 356
b256dc52
JS
357static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
358 enum xc_type type);
84f0f298
RW
359static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
360static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
361static void xlate_xport_init(struct xlate_cfg *, struct xport *);
9efd308e 362static void xlate_xbridge_set(struct xbridge *, struct dpif *,
84f0f298
RW
363 struct rule_dpif *miss_rule,
364 struct rule_dpif *no_packet_in_rule,
9efd308e
DV
365 const struct mac_learning *, struct stp *,
366 struct rstp *, const struct mcast_snooping *,
367 const struct mbridge *,
368 const struct dpif_sflow *,
369 const struct dpif_ipfix *,
370 const struct netflow *, enum ofp_config_flags,
84f0f298
RW
371 bool forward_bpdu, bool has_in_band,
372 bool enable_recirc,
373 bool variable_length_userdata,
53477c2c
JR
374 size_t max_mpls_depth,
375 bool masked_set_action);
84f0f298
RW
376static void xlate_xbundle_set(struct xbundle *xbundle,
377 enum port_vlan_mode vlan_mode, int vlan,
378 unsigned long *trunks, bool use_priority_tags,
379 const struct bond *bond, const struct lacp *lacp,
380 bool floodable);
381static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
382 const struct netdev *netdev, const struct cfm *cfm,
383 const struct bfd *bfd, int stp_port_no,
f025bcb7 384 const struct rstp_port *rstp_port,
84f0f298
RW
385 enum ofputil_port_config config,
386 enum ofputil_port_state state, bool is_tunnel,
387 bool may_enable);
388static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
389static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
390static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
391static void xlate_xbridge_copy(struct xbridge *);
392static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
393static void xlate_xport_copy(struct xbridge *, struct xbundle *,
394 struct xport *);
395static void xlate_xcfg_free(struct xlate_cfg *);
b256dc52 396
84f0f298
RW
397
398static void
399xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
400{
401 list_init(&xbridge->xbundles);
402 hmap_init(&xbridge->xports);
403 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
404 hash_pointer(xbridge->ofproto, 0));
405}
406
407static void
408xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
409{
410 list_init(&xbundle->xports);
411 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
412 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
413 hash_pointer(xbundle->ofbundle, 0));
414}
415
416static void
417xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
418{
419 hmap_init(&xport->skb_priorities);
420 hmap_insert(&xcfg->xports, &xport->hmap_node,
421 hash_pointer(xport->ofport, 0));
422 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
423 hash_ofp_port(xport->ofp_port));
424}
425
426static void
427xlate_xbridge_set(struct xbridge *xbridge,
428 struct dpif *dpif,
429 struct rule_dpif *miss_rule,
ec89fc6f
EJ
430 struct rule_dpif *no_packet_in_rule,
431 const struct mac_learning *ml, struct stp *stp,
9efd308e 432 struct rstp *rstp, const struct mcast_snooping *ms,
ec89fc6f 433 const struct mbridge *mbridge,
46c88433 434 const struct dpif_sflow *sflow,
ce3955be
EJ
435 const struct dpif_ipfix *ipfix,
436 const struct netflow *netflow, enum ofp_config_flags frag,
4b97b70d 437 bool forward_bpdu, bool has_in_band,
adcf00ba 438 bool enable_recirc,
8bfd0fda 439 bool variable_length_userdata,
53477c2c
JR
440 size_t max_mpls_depth,
441 bool masked_set_action)
46c88433 442{
46c88433
EJ
443 if (xbridge->ml != ml) {
444 mac_learning_unref(xbridge->ml);
445 xbridge->ml = mac_learning_ref(ml);
446 }
447
6d95c4e8
FL
448 if (xbridge->ms != ms) {
449 mcast_snooping_unref(xbridge->ms);
450 xbridge->ms = mcast_snooping_ref(ms);
451 }
452
46c88433
EJ
453 if (xbridge->mbridge != mbridge) {
454 mbridge_unref(xbridge->mbridge);
455 xbridge->mbridge = mbridge_ref(mbridge);
456 }
457
458 if (xbridge->sflow != sflow) {
459 dpif_sflow_unref(xbridge->sflow);
460 xbridge->sflow = dpif_sflow_ref(sflow);
461 }
462
463 if (xbridge->ipfix != ipfix) {
464 dpif_ipfix_unref(xbridge->ipfix);
465 xbridge->ipfix = dpif_ipfix_ref(ipfix);
466 }
467
9d189a50
EJ
468 if (xbridge->stp != stp) {
469 stp_unref(xbridge->stp);
470 xbridge->stp = stp_ref(stp);
471 }
472
9efd308e
DV
473 if (xbridge->rstp != rstp) {
474 rstp_unref(xbridge->rstp);
475 xbridge->rstp = rstp_ref(rstp);
476 }
477
ce3955be
EJ
478 if (xbridge->netflow != netflow) {
479 netflow_unref(xbridge->netflow);
480 xbridge->netflow = netflow_ref(netflow);
481 }
482
89a8a7f0 483 xbridge->dpif = dpif;
46c88433
EJ
484 xbridge->forward_bpdu = forward_bpdu;
485 xbridge->has_in_band = has_in_band;
46c88433 486 xbridge->frag = frag;
ec89fc6f
EJ
487 xbridge->miss_rule = miss_rule;
488 xbridge->no_packet_in_rule = no_packet_in_rule;
adcf00ba 489 xbridge->enable_recirc = enable_recirc;
4b97b70d 490 xbridge->variable_length_userdata = variable_length_userdata;
8bfd0fda 491 xbridge->max_mpls_depth = max_mpls_depth;
53477c2c 492 xbridge->masked_set_action = masked_set_action;
46c88433
EJ
493}
494
84f0f298
RW
495static void
496xlate_xbundle_set(struct xbundle *xbundle,
497 enum port_vlan_mode vlan_mode, int vlan,
498 unsigned long *trunks, bool use_priority_tags,
499 const struct bond *bond, const struct lacp *lacp,
500 bool floodable)
501{
502 ovs_assert(xbundle->xbridge);
503
504 xbundle->vlan_mode = vlan_mode;
505 xbundle->vlan = vlan;
506 xbundle->trunks = trunks;
507 xbundle->use_priority_tags = use_priority_tags;
508 xbundle->floodable = floodable;
509
510 if (xbundle->bond != bond) {
511 bond_unref(xbundle->bond);
512 xbundle->bond = bond_ref(bond);
513 }
514
515 if (xbundle->lacp != lacp) {
516 lacp_unref(xbundle->lacp);
517 xbundle->lacp = lacp_ref(lacp);
518 }
519}
520
521static void
522xlate_xport_set(struct xport *xport, odp_port_t odp_port,
523 const struct netdev *netdev, const struct cfm *cfm,
f025bcb7
JR
524 const struct bfd *bfd, int stp_port_no,
525 const struct rstp_port* rstp_port,
84f0f298
RW
526 enum ofputil_port_config config, enum ofputil_port_state state,
527 bool is_tunnel, bool may_enable)
528{
529 xport->config = config;
530 xport->state = state;
531 xport->stp_port_no = stp_port_no;
532 xport->is_tunnel = is_tunnel;
533 xport->may_enable = may_enable;
534 xport->odp_port = odp_port;
535
f025bcb7
JR
536 if (xport->rstp_port != rstp_port) {
537 rstp_port_unref(xport->rstp_port);
538 xport->rstp_port = rstp_port_ref(rstp_port);
539 }
540
84f0f298
RW
541 if (xport->cfm != cfm) {
542 cfm_unref(xport->cfm);
543 xport->cfm = cfm_ref(cfm);
544 }
545
546 if (xport->bfd != bfd) {
547 bfd_unref(xport->bfd);
548 xport->bfd = bfd_ref(bfd);
549 }
550
551 if (xport->netdev != netdev) {
552 netdev_close(xport->netdev);
553 xport->netdev = netdev_ref(netdev);
554 }
555}
556
557static void
558xlate_xbridge_copy(struct xbridge *xbridge)
559{
560 struct xbundle *xbundle;
561 struct xport *xport;
562 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
563 new_xbridge->ofproto = xbridge->ofproto;
564 new_xbridge->name = xstrdup(xbridge->name);
565 xlate_xbridge_init(new_xcfg, new_xbridge);
566
567 xlate_xbridge_set(new_xbridge,
568 xbridge->dpif, xbridge->miss_rule,
569 xbridge->no_packet_in_rule, xbridge->ml, xbridge->stp,
9efd308e
DV
570 xbridge->rstp, xbridge->ms, xbridge->mbridge,
571 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
572 xbridge->frag, xbridge->forward_bpdu,
573 xbridge->has_in_band, xbridge->enable_recirc,
574 xbridge->variable_length_userdata,
53477c2c 575 xbridge->max_mpls_depth, xbridge->masked_set_action);
84f0f298
RW
576 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
577 xlate_xbundle_copy(new_xbridge, xbundle);
578 }
579
580 /* Copy xports which are not part of a xbundle */
581 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
582 if (!xport->xbundle) {
583 xlate_xport_copy(new_xbridge, NULL, xport);
584 }
585 }
586}
587
588static void
589xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
590{
591 struct xport *xport;
592 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
593 new_xbundle->ofbundle = xbundle->ofbundle;
594 new_xbundle->xbridge = xbridge;
595 new_xbundle->name = xstrdup(xbundle->name);
596 xlate_xbundle_init(new_xcfg, new_xbundle);
597
598 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
599 xbundle->vlan, xbundle->trunks,
600 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
601 xbundle->floodable);
602 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
603 xlate_xport_copy(xbridge, new_xbundle, xport);
604 }
605}
606
607static void
608xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
609 struct xport *xport)
610{
611 struct skb_priority_to_dscp *pdscp, *new_pdscp;
612 struct xport *new_xport = xzalloc(sizeof *xport);
613 new_xport->ofport = xport->ofport;
614 new_xport->ofp_port = xport->ofp_port;
615 new_xport->xbridge = xbridge;
616 xlate_xport_init(new_xcfg, new_xport);
617
618 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
f025bcb7 619 xport->bfd, xport->stp_port_no, xport->rstp_port,
9efd308e
DV
620 xport->config, xport->state, xport->is_tunnel,
621 xport->may_enable);
84f0f298
RW
622
623 if (xport->peer) {
624 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
625 if (peer) {
626 new_xport->peer = peer;
627 new_xport->peer->peer = new_xport;
628 }
629 }
630
631 if (xbundle) {
632 new_xport->xbundle = xbundle;
633 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
634 }
635
636 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
637 new_pdscp = xmalloc(sizeof *pdscp);
638 new_pdscp->skb_priority = pdscp->skb_priority;
639 new_pdscp->dscp = pdscp->dscp;
640 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
641 hash_int(new_pdscp->skb_priority, 0));
642 }
643}
644
645/* Sets the current xlate configuration to new_xcfg and frees the old xlate
646 * configuration in xcfgp.
647 *
648 * This needs to be called after editing the xlate configuration.
649 *
650 * Functions that edit the new xlate configuration are
651 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
652 *
653 * A sample workflow:
654 *
655 * xlate_txn_start();
656 * ...
657 * edit_xlate_configuration();
658 * ...
659 * xlate_txn_commit(); */
46c88433 660void
84f0f298
RW
661xlate_txn_commit(void)
662{
663 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
664
665 ovsrcu_set(&xcfgp, new_xcfg);
666 ovsrcu_postpone(xlate_xcfg_free, xcfg);
667
668 new_xcfg = NULL;
669}
670
671/* Copies the current xlate configuration in xcfgp to new_xcfg.
672 *
673 * This needs to be called prior to editing the xlate configuration. */
674void
675xlate_txn_start(void)
676{
677 struct xbridge *xbridge;
678 struct xlate_cfg *xcfg;
679
680 ovs_assert(!new_xcfg);
681
682 new_xcfg = xmalloc(sizeof *new_xcfg);
683 hmap_init(&new_xcfg->xbridges);
684 hmap_init(&new_xcfg->xbundles);
685 hmap_init(&new_xcfg->xports);
686
687 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
688 if (!xcfg) {
689 return;
690 }
691
692 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
693 xlate_xbridge_copy(xbridge);
694 }
695}
696
697
698static void
699xlate_xcfg_free(struct xlate_cfg *xcfg)
700{
701 struct xbridge *xbridge, *next_xbridge;
702
703 if (!xcfg) {
704 return;
705 }
706
707 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
708 xlate_xbridge_remove(xcfg, xbridge);
709 }
710
711 hmap_destroy(&xcfg->xbridges);
712 hmap_destroy(&xcfg->xbundles);
713 hmap_destroy(&xcfg->xports);
714 free(xcfg);
715}
716
717void
718xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
719 struct dpif *dpif, struct rule_dpif *miss_rule,
720 struct rule_dpif *no_packet_in_rule,
721 const struct mac_learning *ml, struct stp *stp,
9efd308e 722 struct rstp *rstp, const struct mcast_snooping *ms,
84f0f298
RW
723 const struct mbridge *mbridge,
724 const struct dpif_sflow *sflow,
725 const struct dpif_ipfix *ipfix,
726 const struct netflow *netflow, enum ofp_config_flags frag,
9efd308e 727 bool forward_bpdu, bool has_in_band, bool enable_recirc,
53477c2c
JR
728 bool variable_length_userdata, size_t max_mpls_depth,
729 bool masked_set_action)
84f0f298
RW
730{
731 struct xbridge *xbridge;
732
733 ovs_assert(new_xcfg);
734
735 xbridge = xbridge_lookup(new_xcfg, ofproto);
736 if (!xbridge) {
737 xbridge = xzalloc(sizeof *xbridge);
738 xbridge->ofproto = ofproto;
739
740 xlate_xbridge_init(new_xcfg, xbridge);
741 }
742
743 free(xbridge->name);
744 xbridge->name = xstrdup(name);
745
746 xlate_xbridge_set(xbridge, dpif, miss_rule, no_packet_in_rule, ml, stp,
9efd308e
DV
747 rstp, ms, mbridge, sflow, ipfix, netflow, frag,
748 forward_bpdu, has_in_band, enable_recirc,
53477c2c
JR
749 variable_length_userdata, max_mpls_depth,
750 masked_set_action);
84f0f298
RW
751}
752
753static void
754xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
46c88433 755{
46c88433
EJ
756 struct xbundle *xbundle, *next_xbundle;
757 struct xport *xport, *next_xport;
758
759 if (!xbridge) {
760 return;
761 }
762
763 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
84f0f298 764 xlate_xport_remove(xcfg, xport);
46c88433
EJ
765 }
766
767 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
84f0f298 768 xlate_xbundle_remove(xcfg, xbundle);
46c88433
EJ
769 }
770
84f0f298 771 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
795cc5c1 772 mac_learning_unref(xbridge->ml);
6d95c4e8 773 mcast_snooping_unref(xbridge->ms);
795cc5c1
EJ
774 mbridge_unref(xbridge->mbridge);
775 dpif_sflow_unref(xbridge->sflow);
776 dpif_ipfix_unref(xbridge->ipfix);
777 stp_unref(xbridge->stp);
9efd308e 778 rstp_unref(xbridge->rstp);
795cc5c1 779 hmap_destroy(&xbridge->xports);
46c88433
EJ
780 free(xbridge->name);
781 free(xbridge);
782}
783
84f0f298
RW
784void
785xlate_remove_ofproto(struct ofproto_dpif *ofproto)
786{
787 struct xbridge *xbridge;
788
789 ovs_assert(new_xcfg);
790
791 xbridge = xbridge_lookup(new_xcfg, ofproto);
792 xlate_xbridge_remove(new_xcfg, xbridge);
793}
794
46c88433
EJ
795void
796xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
797 const char *name, enum port_vlan_mode vlan_mode, int vlan,
798 unsigned long *trunks, bool use_priority_tags,
799 const struct bond *bond, const struct lacp *lacp,
800 bool floodable)
801{
84f0f298 802 struct xbundle *xbundle;
46c88433 803
84f0f298
RW
804 ovs_assert(new_xcfg);
805
806 xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
807 if (!xbundle) {
808 xbundle = xzalloc(sizeof *xbundle);
809 xbundle->ofbundle = ofbundle;
84f0f298 810 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433 811
84f0f298 812 xlate_xbundle_init(new_xcfg, xbundle);
46c88433
EJ
813 }
814
46c88433
EJ
815 free(xbundle->name);
816 xbundle->name = xstrdup(name);
817
84f0f298
RW
818 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
819 use_priority_tags, bond, lacp, floodable);
46c88433
EJ
820}
821
84f0f298
RW
822static void
823xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
46c88433 824{
46c88433
EJ
825 struct xport *xport, *next;
826
827 if (!xbundle) {
828 return;
829 }
830
831 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
832 list_remove(&xport->bundle_node);
833 xport->xbundle = NULL;
834 }
835
84f0f298 836 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
46c88433
EJ
837 list_remove(&xbundle->list_node);
838 bond_unref(xbundle->bond);
839 lacp_unref(xbundle->lacp);
840 free(xbundle->name);
841 free(xbundle);
842}
843
84f0f298
RW
844void
845xlate_bundle_remove(struct ofbundle *ofbundle)
846{
847 struct xbundle *xbundle;
848
849 ovs_assert(new_xcfg);
850
851 xbundle = xbundle_lookup(new_xcfg, ofbundle);
852 xlate_xbundle_remove(new_xcfg, xbundle);
853}
854
46c88433
EJ
855void
856xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
857 struct ofport_dpif *ofport, ofp_port_t ofp_port,
858 odp_port_t odp_port, const struct netdev *netdev,
859 const struct cfm *cfm, const struct bfd *bfd,
f025bcb7
JR
860 struct ofport_dpif *peer, int stp_port_no,
861 const struct rstp_port *rstp_port,
55954f6e 862 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
863 enum ofputil_port_config config,
864 enum ofputil_port_state state, bool is_tunnel,
9d189a50 865 bool may_enable)
46c88433 866{
55954f6e 867 size_t i;
84f0f298
RW
868 struct xport *xport;
869
870 ovs_assert(new_xcfg);
46c88433 871
84f0f298 872 xport = xport_lookup(new_xcfg, ofport);
46c88433
EJ
873 if (!xport) {
874 xport = xzalloc(sizeof *xport);
875 xport->ofport = ofport;
84f0f298 876 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433
EJ
877 xport->ofp_port = ofp_port;
878
84f0f298 879 xlate_xport_init(new_xcfg, xport);
46c88433
EJ
880 }
881
882 ovs_assert(xport->ofp_port == ofp_port);
883
9efd308e 884 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, stp_port_no,
f025bcb7 885 rstp_port, config, state, is_tunnel, may_enable);
46c88433
EJ
886
887 if (xport->peer) {
888 xport->peer->peer = NULL;
889 }
84f0f298 890 xport->peer = xport_lookup(new_xcfg, peer);
46c88433
EJ
891 if (xport->peer) {
892 xport->peer->peer = xport;
893 }
894
895 if (xport->xbundle) {
896 list_remove(&xport->bundle_node);
897 }
84f0f298 898 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
899 if (xport->xbundle) {
900 list_insert(&xport->xbundle->xports, &xport->bundle_node);
901 }
55954f6e
EJ
902
903 clear_skb_priorities(xport);
904 for (i = 0; i < n_qdscp; i++) {
905 struct skb_priority_to_dscp *pdscp;
906 uint32_t skb_priority;
907
89a8a7f0
EJ
908 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
909 &skb_priority)) {
55954f6e
EJ
910 continue;
911 }
912
913 pdscp = xmalloc(sizeof *pdscp);
914 pdscp->skb_priority = skb_priority;
915 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
916 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
917 hash_int(pdscp->skb_priority, 0));
918 }
46c88433
EJ
919}
920
84f0f298
RW
921static void
922xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
46c88433 923{
46c88433
EJ
924 if (!xport) {
925 return;
926 }
927
928 if (xport->peer) {
929 xport->peer->peer = NULL;
930 xport->peer = NULL;
931 }
932
e621a12d
EJ
933 if (xport->xbundle) {
934 list_remove(&xport->bundle_node);
935 }
936
55954f6e
EJ
937 clear_skb_priorities(xport);
938 hmap_destroy(&xport->skb_priorities);
939
84f0f298 940 hmap_remove(&xcfg->xports, &xport->hmap_node);
46c88433
EJ
941 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
942
943 netdev_close(xport->netdev);
f025bcb7 944 rstp_port_unref(xport->rstp_port);
46c88433
EJ
945 cfm_unref(xport->cfm);
946 bfd_unref(xport->bfd);
947 free(xport);
948}
949
84f0f298
RW
950void
951xlate_ofport_remove(struct ofport_dpif *ofport)
952{
953 struct xport *xport;
954
955 ovs_assert(new_xcfg);
956
957 xport = xport_lookup(new_xcfg, ofport);
958 xlate_xport_remove(new_xcfg, xport);
959}
960
ef377a58
JR
961/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
962 * returns the corresponding struct xport, or NULL if none is found. */
963static struct xport *
964xlate_lookup_xport(const struct dpif_backer *backer, const struct flow *flow)
965{
966 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
967
968 return xport_lookup(xcfg, tnl_port_should_receive(flow)
969 ? tnl_port_receive(flow)
970 : odp_port_to_ofport(backer, flow->in_port.odp_port));
971}
972
973static struct ofproto_dpif *
974xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
975 ofp_port_t *ofp_in_port, const struct xport **xportp)
976{
977 const struct xport *xport;
978
979 *xportp = xport = xlate_lookup_xport(backer, flow);
980
981 if (xport) {
982 if (ofp_in_port) {
983 *ofp_in_port = xport->ofp_port;
984 }
985 return xport->xbridge->ofproto;
986 }
987
988 return NULL;
989}
990
991/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
992 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
993struct ofproto_dpif *
994xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
995 ofp_port_t *ofp_in_port)
996{
997 const struct xport *xport;
998
999 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1000}
1001
cc377352 1002/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
ef377a58 1003 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
cc377352 1004 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
dcc2c6cd
JR
1005 * handles for those protocols if they're enabled. Caller may use the returned
1006 * pointers until quiescing, for longer term use additional references must
1007 * be taken.
8449c4d6 1008 *
ef377a58 1009 * '*ofp_in_port' is set to OFPP_NONE if 'flow''s in_port does not exist.
8449c4d6 1010 *
ef377a58
JR
1011 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport.
1012 */
8449c4d6 1013int
5c476ea3
JR
1014xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1015 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1016 struct dpif_sflow **sflow, struct netflow **netflow,
1017 ofp_port_t *ofp_in_port)
8449c4d6 1018{
ef377a58 1019 struct ofproto_dpif *ofproto;
84f0f298 1020 const struct xport *xport;
8449c4d6 1021
ef377a58 1022 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
8449c4d6 1023
ef377a58
JR
1024 if (ofp_in_port && !xport) {
1025 *ofp_in_port = OFPP_NONE;
8449c4d6
EJ
1026 }
1027
cc377352
EJ
1028 if (!xport) {
1029 return ENODEV;
8449c4d6 1030 }
8449c4d6 1031
ef377a58
JR
1032 if (ofprotop) {
1033 *ofprotop = ofproto;
8449c4d6
EJ
1034 }
1035
1dfdb9b3 1036 if (ipfix) {
dcc2c6cd 1037 *ipfix = xport->xbridge->ipfix;
1dfdb9b3
EJ
1038 }
1039
1040 if (sflow) {
dcc2c6cd 1041 *sflow = xport->xbridge->sflow;
1dfdb9b3
EJ
1042 }
1043
1044 if (netflow) {
dcc2c6cd 1045 *netflow = xport->xbridge->netflow;
1dfdb9b3 1046 }
cc377352 1047 return 0;
8449c4d6
EJ
1048}
1049
46c88433 1050static struct xbridge *
84f0f298 1051xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
46c88433 1052{
84f0f298 1053 struct hmap *xbridges;
46c88433
EJ
1054 struct xbridge *xbridge;
1055
84f0f298 1056 if (!ofproto || !xcfg) {
5e6af486
EJ
1057 return NULL;
1058 }
1059
84f0f298
RW
1060 xbridges = &xcfg->xbridges;
1061
46c88433 1062 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
84f0f298 1063 xbridges) {
46c88433
EJ
1064 if (xbridge->ofproto == ofproto) {
1065 return xbridge;
1066 }
1067 }
1068 return NULL;
1069}
1070
1071static struct xbundle *
84f0f298 1072xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
46c88433 1073{
84f0f298 1074 struct hmap *xbundles;
46c88433
EJ
1075 struct xbundle *xbundle;
1076
84f0f298 1077 if (!ofbundle || !xcfg) {
5e6af486
EJ
1078 return NULL;
1079 }
1080
84f0f298
RW
1081 xbundles = &xcfg->xbundles;
1082
46c88433 1083 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
84f0f298 1084 xbundles) {
46c88433
EJ
1085 if (xbundle->ofbundle == ofbundle) {
1086 return xbundle;
1087 }
1088 }
1089 return NULL;
1090}
1091
1092static struct xport *
84f0f298 1093xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
46c88433 1094{
84f0f298 1095 struct hmap *xports;
46c88433
EJ
1096 struct xport *xport;
1097
84f0f298 1098 if (!ofport || !xcfg) {
5e6af486
EJ
1099 return NULL;
1100 }
1101
84f0f298
RW
1102 xports = &xcfg->xports;
1103
46c88433 1104 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
84f0f298 1105 xports) {
46c88433
EJ
1106 if (xport->ofport == ofport) {
1107 return xport;
1108 }
1109 }
1110 return NULL;
1111}
1112
40085e56
EJ
1113static struct stp_port *
1114xport_get_stp_port(const struct xport *xport)
1115{
92cf817b 1116 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
1117 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1118 : NULL;
1119}
9d189a50 1120
0d1cee12 1121static bool
9d189a50
EJ
1122xport_stp_learn_state(const struct xport *xport)
1123{
40085e56
EJ
1124 struct stp_port *sp = xport_get_stp_port(xport);
1125 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
9d189a50
EJ
1126}
1127
1128static bool
1129xport_stp_forward_state(const struct xport *xport)
1130{
40085e56
EJ
1131 struct stp_port *sp = xport_get_stp_port(xport);
1132 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
9d189a50
EJ
1133}
1134
0d1cee12 1135static bool
bacdb85a 1136xport_stp_should_forward_bpdu(const struct xport *xport)
0d1cee12
K
1137{
1138 struct stp_port *sp = xport_get_stp_port(xport);
bacdb85a 1139 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
0d1cee12
K
1140}
1141
9d189a50
EJ
1142/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1143 * were used to make the determination.*/
1144static bool
1145stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1146{
bbbca389 1147 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
9d189a50 1148 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
bbbca389 1149 return is_stp(flow);
9d189a50
EJ
1150}
1151
1152static void
1153stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1154{
40085e56 1155 struct stp_port *sp = xport_get_stp_port(xport);
9d189a50 1156 struct ofpbuf payload = *packet;
1f317cb5 1157 struct eth_header *eth = ofpbuf_data(&payload);
9d189a50
EJ
1158
1159 /* Sink packets on ports that have STP disabled when the bridge has
1160 * STP enabled. */
1161 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1162 return;
1163 }
1164
1165 /* Trim off padding on payload. */
1f317cb5
PS
1166 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1167 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
1168 }
1169
1170 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1f317cb5 1171 stp_received_bpdu(sp, ofpbuf_data(&payload), ofpbuf_size(&payload));
9d189a50
EJ
1172 }
1173}
1174
f025bcb7
JR
1175static enum rstp_state
1176xport_get_rstp_port_state(const struct xport *xport)
9efd308e 1177{
f025bcb7
JR
1178 return xport->rstp_port
1179 ? rstp_port_get_state(xport->rstp_port)
1180 : RSTP_DISABLED;
9efd308e
DV
1181}
1182
1183static bool
1184xport_rstp_learn_state(const struct xport *xport)
1185{
f025bcb7 1186 return rstp_learn_in_state(xport_get_rstp_port_state(xport));
9efd308e
DV
1187}
1188
1189static bool
1190xport_rstp_forward_state(const struct xport *xport)
1191{
f025bcb7 1192 return rstp_forward_in_state(xport_get_rstp_port_state(xport));
9efd308e
DV
1193}
1194
1195static bool
1196xport_rstp_should_manage_bpdu(const struct xport *xport)
1197{
f025bcb7 1198 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
9efd308e
DV
1199}
1200
1201static void
1202rstp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
1203{
9efd308e
DV
1204 struct ofpbuf payload = *packet;
1205 struct eth_header *eth = ofpbuf_data(&payload);
1206
f025bcb7
JR
1207 /* Sink packets on ports that have no RSTP. */
1208 if (!xport->rstp_port) {
9efd308e
DV
1209 return;
1210 }
1211
1212 /* Trim off padding on payload. */
1213 if (ofpbuf_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1214 ofpbuf_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
1215 }
1216
1217 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
f025bcb7
JR
1218 rstp_port_received_bpdu(xport->rstp_port, ofpbuf_data(&payload),
1219 ofpbuf_size(&payload));
9efd308e
DV
1220 }
1221}
1222
46c88433
EJ
1223static struct xport *
1224get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1225{
1226 struct xport *xport;
1227
1228 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1229 &xbridge->xports) {
1230 if (xport->ofp_port == ofp_port) {
1231 return xport;
1232 }
1233 }
1234 return NULL;
1235}
1236
1237static odp_port_t
1238ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1239{
1240 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1241 return xport ? xport->odp_port : ODPP_NONE;
1242}
1243
dd8cd4b4
SH
1244static bool
1245odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1246{
1247 struct xport *xport;
1248
1249 xport = get_ofp_port(ctx->xbridge, ofp_port);
1250 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
1251 xport->state & OFPUTIL_PS_LINK_DOWN) {
1252 return false;
1253 }
1254
1255 return true;
1256}
1257
1e684d7d 1258static struct ofputil_bucket *
dd8cd4b4
SH
1259group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1260 int depth);
1261
1262static bool
1263group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1264{
1265 struct group_dpif *group;
dd8cd4b4 1266
dc25893e
AZ
1267 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1268 struct ofputil_bucket *bucket;
dd8cd4b4 1269
dc25893e
AZ
1270 bucket = group_first_live_bucket(ctx, group, depth);
1271 group_dpif_unref(group);
1272 return bucket == NULL;
1273 }
dd8cd4b4 1274
dc25893e 1275 return false;
dd8cd4b4
SH
1276}
1277
1278#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1279
1280static bool
1281bucket_is_alive(const struct xlate_ctx *ctx,
1e684d7d 1282 struct ofputil_bucket *bucket, int depth)
dd8cd4b4
SH
1283{
1284 if (depth >= MAX_LIVENESS_RECURSION) {
1285 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1286
1287 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1288 MAX_LIVENESS_RECURSION);
1289 return false;
1290 }
1291
fdb1999b
AZ
1292 return (!ofputil_bucket_has_liveness(bucket)
1293 || (bucket->watch_port != OFPP_ANY
1294 && odp_port_is_alive(ctx, bucket->watch_port))
1295 || (bucket->watch_group != OFPG_ANY
1296 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
dd8cd4b4
SH
1297}
1298
1e684d7d 1299static struct ofputil_bucket *
dd8cd4b4
SH
1300group_first_live_bucket(const struct xlate_ctx *ctx,
1301 const struct group_dpif *group, int depth)
1302{
1303 struct ofputil_bucket *bucket;
1304 const struct list *buckets;
1305
1306 group_dpif_get_buckets(group, &buckets);
1307 LIST_FOR_EACH (bucket, list_node, buckets) {
1308 if (bucket_is_alive(ctx, bucket, depth)) {
1309 return bucket;
1310 }
1311 }
1312
1313 return NULL;
1314}
1315
1e684d7d 1316static struct ofputil_bucket *
fe7e5749
SH
1317group_best_live_bucket(const struct xlate_ctx *ctx,
1318 const struct group_dpif *group,
1319 uint32_t basis)
1320{
1e684d7d 1321 struct ofputil_bucket *best_bucket = NULL;
fe7e5749
SH
1322 uint32_t best_score = 0;
1323 int i = 0;
1324
1e684d7d 1325 struct ofputil_bucket *bucket;
fe7e5749
SH
1326 const struct list *buckets;
1327
1328 group_dpif_get_buckets(group, &buckets);
1329 LIST_FOR_EACH (bucket, list_node, buckets) {
1330 if (bucket_is_alive(ctx, bucket, 0)) {
7cb279c2 1331 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
1332 if (score >= best_score) {
1333 best_bucket = bucket;
1334 best_score = score;
1335 }
1336 }
1337 i++;
1338 }
1339
1340 return best_bucket;
1341}
1342
9583bc14 1343static bool
46c88433 1344xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
1345{
1346 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1347 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1348}
1349
1350static bool
46c88433
EJ
1351xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1352{
1353 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1354}
1355
1356static mirror_mask_t
1357xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1358{
1359 return xbundle != &ofpp_none_bundle
1360 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1361 : 0;
1362}
1363
1364static mirror_mask_t
1365xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1366{
46c88433
EJ
1367 return xbundle != &ofpp_none_bundle
1368 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1369 : 0;
9583bc14
EJ
1370}
1371
46c88433
EJ
1372static mirror_mask_t
1373xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1374{
46c88433
EJ
1375 return xbundle != &ofpp_none_bundle
1376 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1377 : 0;
1378}
1379
1380static struct xbundle *
1381lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1382 bool warn, struct xport **in_xportp)
1383{
1384 struct xport *xport;
9583bc14
EJ
1385
1386 /* Find the port and bundle for the received packet. */
46c88433
EJ
1387 xport = get_ofp_port(xbridge, in_port);
1388 if (in_xportp) {
1389 *in_xportp = xport;
9583bc14 1390 }
46c88433
EJ
1391 if (xport && xport->xbundle) {
1392 return xport->xbundle;
9583bc14
EJ
1393 }
1394
6362203b
YT
1395 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1396 * which a controller may use as the ingress port for traffic that
1397 * it is sourcing. */
1398 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
9583bc14
EJ
1399 return &ofpp_none_bundle;
1400 }
1401
1402 /* Odd. A few possible reasons here:
1403 *
1404 * - We deleted a port but there are still a few packets queued up
1405 * from it.
1406 *
1407 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1408 * we don't know about.
1409 *
1410 * - The ofproto client didn't configure the port as part of a bundle.
1411 * This is particularly likely to happen if a packet was received on the
1412 * port after it was created, but before the client had a chance to
1413 * configure its bundle.
1414 */
1415 if (warn) {
1416 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1417
1418 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
46c88433 1419 "port %"PRIu16, xbridge->name, in_port);
9583bc14
EJ
1420 }
1421 return NULL;
1422}
1423
1424static void
1425add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
1426{
46c88433 1427 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1428 mirror_mask_t mirrors;
46c88433 1429 struct xbundle *in_xbundle;
9583bc14
EJ
1430 uint16_t vlan;
1431 uint16_t vid;
cdf5d3a5
EJ
1432
1433 mirrors = ctx->xout->mirrors;
1434 ctx->xout->mirrors = 0;
9583bc14 1435
46c88433
EJ
1436 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
1437 ctx->xin->packet != NULL, NULL);
1438 if (!in_xbundle) {
9583bc14
EJ
1439 return;
1440 }
46c88433 1441 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
9583bc14
EJ
1442
1443 /* Drop frames on bundles reserved for mirroring. */
46c88433 1444 if (xbundle_mirror_out(xbridge, in_xbundle)) {
9583bc14
EJ
1445 if (ctx->xin->packet != NULL) {
1446 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1447 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1448 "%s, which is reserved exclusively for mirroring",
46c88433 1449 ctx->xbridge->name, in_xbundle->name);
9583bc14 1450 }
cc377352 1451 ofpbuf_clear(ctx->xout->odp_actions);
9583bc14
EJ
1452 return;
1453 }
1454
1455 /* Check VLAN. */
1456 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
46c88433 1457 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
1458 return;
1459 }
46c88433 1460 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14 1461
9583bc14
EJ
1462 if (!mirrors) {
1463 return;
1464 }
1465
1466 /* Restore the original packet before adding the mirror actions. */
1467 ctx->xin->flow = *orig_flow;
1468
1469 while (mirrors) {
ec7ceaed
EJ
1470 mirror_mask_t dup_mirrors;
1471 struct ofbundle *out;
1472 unsigned long *vlans;
1473 bool vlan_mirrored;
1474 bool has_mirror;
1475 int out_vlan;
1476
4b6ab2b0 1477 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
ec7ceaed
EJ
1478 &vlans, &dup_mirrors, &out, &out_vlan);
1479 ovs_assert(has_mirror);
1480
1481 if (vlans) {
9583bc14
EJ
1482 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
1483 }
ec7ceaed
EJ
1484 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
1485 free(vlans);
9583bc14 1486
ec7ceaed 1487 if (!vlan_mirrored) {
9583bc14
EJ
1488 mirrors = zero_rightmost_1bit(mirrors);
1489 continue;
1490 }
1491
ec7ceaed
EJ
1492 mirrors &= ~dup_mirrors;
1493 ctx->xout->mirrors |= dup_mirrors;
1494 if (out) {
84f0f298
RW
1495 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1496 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
46c88433
EJ
1497 if (out_xbundle) {
1498 output_normal(ctx, out_xbundle, vlan);
1499 }
ec7ceaed 1500 } else if (vlan != out_vlan
9583bc14 1501 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
46c88433 1502 struct xbundle *xbundle;
9583bc14 1503
46c88433
EJ
1504 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1505 if (xbundle_includes_vlan(xbundle, out_vlan)
1506 && !xbundle_mirror_out(xbridge, xbundle)) {
1507 output_normal(ctx, xbundle, out_vlan);
9583bc14
EJ
1508 }
1509 }
1510 }
1511 }
1512}
1513
1514/* Given 'vid', the VID obtained from the 802.1Q header that was received as
46c88433 1515 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
9583bc14
EJ
1516 * the bundle on which the packet was received, returns the VLAN to which the
1517 * packet belongs.
1518 *
1519 * Both 'vid' and the return value are in the range 0...4095. */
1520static uint16_t
46c88433 1521input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
9583bc14 1522{
46c88433 1523 switch (in_xbundle->vlan_mode) {
9583bc14 1524 case PORT_VLAN_ACCESS:
46c88433 1525 return in_xbundle->vlan;
9583bc14
EJ
1526 break;
1527
1528 case PORT_VLAN_TRUNK:
1529 return vid;
1530
1531 case PORT_VLAN_NATIVE_UNTAGGED:
1532 case PORT_VLAN_NATIVE_TAGGED:
46c88433 1533 return vid ? vid : in_xbundle->vlan;
9583bc14
EJ
1534
1535 default:
428b2edd 1536 OVS_NOT_REACHED();
9583bc14
EJ
1537 }
1538}
1539
46c88433 1540/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
9583bc14
EJ
1541 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1542 * a warning.
1543 *
1544 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1545 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1546 * 0...4095. */
1547static bool
46c88433 1548input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
9583bc14
EJ
1549{
1550 /* Allow any VID on the OFPP_NONE port. */
46c88433 1551 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1552 return true;
1553 }
1554
46c88433 1555 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1556 case PORT_VLAN_ACCESS:
1557 if (vid) {
1558 if (warn) {
1559 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1560 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
9583bc14 1561 "packet received on port %s configured as VLAN "
46c88433
EJ
1562 "%"PRIu16" access port", vid, in_xbundle->name,
1563 in_xbundle->vlan);
9583bc14
EJ
1564 }
1565 return false;
1566 }
1567 return true;
1568
1569 case PORT_VLAN_NATIVE_UNTAGGED:
1570 case PORT_VLAN_NATIVE_TAGGED:
1571 if (!vid) {
1572 /* Port must always carry its native VLAN. */
1573 return true;
1574 }
1575 /* Fall through. */
1576 case PORT_VLAN_TRUNK:
46c88433 1577 if (!xbundle_includes_vlan(in_xbundle, vid)) {
9583bc14
EJ
1578 if (warn) {
1579 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1580 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
9583bc14 1581 "received on port %s not configured for trunking "
46c88433 1582 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
9583bc14
EJ
1583 }
1584 return false;
1585 }
1586 return true;
1587
1588 default:
428b2edd 1589 OVS_NOT_REACHED();
9583bc14
EJ
1590 }
1591
1592}
1593
1594/* Given 'vlan', the VLAN that a packet belongs to, and
46c88433 1595 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
9583bc14
EJ
1596 * that should be included in the 802.1Q header. (If the return value is 0,
1597 * then the 802.1Q header should only be included in the packet if there is a
1598 * nonzero PCP.)
1599 *
1600 * Both 'vlan' and the return value are in the range 0...4095. */
1601static uint16_t
46c88433 1602output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
9583bc14 1603{
46c88433 1604 switch (out_xbundle->vlan_mode) {
9583bc14
EJ
1605 case PORT_VLAN_ACCESS:
1606 return 0;
1607
1608 case PORT_VLAN_TRUNK:
1609 case PORT_VLAN_NATIVE_TAGGED:
1610 return vlan;
1611
1612 case PORT_VLAN_NATIVE_UNTAGGED:
46c88433 1613 return vlan == out_xbundle->vlan ? 0 : vlan;
9583bc14
EJ
1614
1615 default:
428b2edd 1616 OVS_NOT_REACHED();
9583bc14
EJ
1617 }
1618}
1619
1620static void
46c88433 1621output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
9583bc14
EJ
1622 uint16_t vlan)
1623{
33bf9176 1624 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
9583bc14
EJ
1625 uint16_t vid;
1626 ovs_be16 tci, old_tci;
46c88433 1627 struct xport *xport;
9583bc14 1628
46c88433
EJ
1629 vid = output_vlan_to_vid(out_xbundle, vlan);
1630 if (list_is_empty(&out_xbundle->xports)) {
1631 /* Partially configured bundle with no slaves. Drop the packet. */
1632 return;
1633 } else if (!out_xbundle->bond) {
92c08f09 1634 ctx->use_recirc = false;
46c88433
EJ
1635 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1636 bundle_node);
9583bc14 1637 } else {
84f0f298 1638 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
54ecb5a2 1639 struct flow_wildcards *wc = &ctx->xout->wc;
84f0f298
RW
1640 struct xlate_recirc *xr = &ctx->recirc;
1641 struct ofport_dpif *ofport;
adcf00ba
AZ
1642
1643 if (ctx->xbridge->enable_recirc) {
92c08f09 1644 ctx->use_recirc = bond_may_recirc(
62ac1f20 1645 out_xbundle->bond, &xr->recirc_id, &xr->hash_basis);
adcf00ba 1646
92c08f09 1647 if (ctx->use_recirc) {
adcf00ba 1648 /* Only TCP mode uses recirculation. */
347bf289 1649 xr->hash_alg = OVS_HASH_ALG_L4;
adcf00ba 1650 bond_update_post_recirc_rules(out_xbundle->bond, false);
54ecb5a2
AZ
1651
1652 /* Recirculation does not require unmasking hash fields. */
1653 wc = NULL;
adcf00ba
AZ
1654 }
1655 }
46c88433 1656
54ecb5a2
AZ
1657 ofport = bond_choose_output_slave(out_xbundle->bond,
1658 &ctx->xin->flow, wc, vid);
84f0f298 1659 xport = xport_lookup(xcfg, ofport);
46c88433
EJ
1660
1661 if (!xport) {
9583bc14
EJ
1662 /* No slaves enabled, so drop packet. */
1663 return;
1664 }
d6fc5f57 1665
b256dc52
JS
1666 /* If ctx->xout->use_recirc is set, the main thread will handle stats
1667 * accounting for this bond. */
92c08f09 1668 if (!ctx->use_recirc) {
b256dc52
JS
1669 if (ctx->xin->resubmit_stats) {
1670 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1671 ctx->xin->resubmit_stats->n_bytes);
1672 }
1673 if (ctx->xin->xcache) {
1674 struct xc_entry *entry;
1675 struct flow *flow;
1676
1677 flow = &ctx->xin->flow;
1678 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1679 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1680 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1681 entry->u.bond.vid = vid;
1682 }
d6fc5f57 1683 }
9583bc14
EJ
1684 }
1685
33bf9176 1686 old_tci = *flow_tci;
9583bc14 1687 tci = htons(vid);
46c88433 1688 if (tci || out_xbundle->use_priority_tags) {
33bf9176 1689 tci |= *flow_tci & htons(VLAN_PCP_MASK);
9583bc14
EJ
1690 if (tci) {
1691 tci |= htons(VLAN_CFI);
1692 }
1693 }
33bf9176 1694 *flow_tci = tci;
9583bc14 1695
46c88433 1696 compose_output_action(ctx, xport->ofp_port);
33bf9176 1697 *flow_tci = old_tci;
9583bc14
EJ
1698}
1699
1700/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1701 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1702 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1703static bool
1704is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1705{
1706 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1707 return false;
1708 }
1709
1710 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1711 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1712 return false;
1713 }
1714
1715 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1716 if (flow->nw_proto == ARP_OP_REPLY) {
1717 return true;
1718 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1719 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1720 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1721
1722 return flow->nw_src == flow->nw_dst;
1723 } else {
1724 return false;
1725 }
1726}
1727
ff69c24a
FL
1728/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1729 * dropped. Returns true if they may be forwarded, false if they should be
1730 * dropped.
1731 *
1732 * 'in_port' must be the xport that corresponds to flow->in_port.
1733 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1734 *
1735 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1736 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1737 * checked by input_vid_is_valid().
1738 *
1739 * May also add tags to '*tags', although the current implementation only does
1740 * so in one special case.
1741 */
1742static bool
1743is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1744 uint16_t vlan)
1745{
1746 struct xbundle *in_xbundle = in_port->xbundle;
1747 const struct xbridge *xbridge = ctx->xbridge;
1748 struct flow *flow = &ctx->xin->flow;
1749
1750 /* Drop frames for reserved multicast addresses
1751 * only if forward_bpdu option is absent. */
1752 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1753 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1754 return false;
1755 }
1756
1757 if (in_xbundle->bond) {
1758 struct mac_entry *mac;
1759
1760 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1761 flow->dl_dst)) {
1762 case BV_ACCEPT:
1763 break;
1764
1765 case BV_DROP:
1766 xlate_report(ctx, "bonding refused admissibility, dropping");
1767 return false;
1768
1769 case BV_DROP_IF_MOVED:
1770 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1771 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1772 if (mac && mac->port.p != in_xbundle->ofbundle &&
1773 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1774 || mac_entry_is_grat_arp_locked(mac))) {
1775 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1776 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1777 "dropping");
1778 return false;
1779 }
1780 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1781 break;
1782 }
1783 }
1784
1785 return true;
1786}
1787
ee047520
BP
1788/* Checks whether a MAC learning update is necessary for MAC learning table
1789 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1790 * 'vlan'.
1791 *
1792 * Most packets processed through the MAC learning table do not actually
1793 * change it in any way. This function requires only a read lock on the MAC
1794 * learning table, so it is much cheaper in this common case.
1795 *
1796 * Keep the code here synchronized with that in update_learning_table__()
1797 * below. */
1798static bool
1799is_mac_learning_update_needed(const struct mac_learning *ml,
1800 const struct flow *flow,
1801 struct flow_wildcards *wc,
1802 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1803OVS_REQ_RDLOCK(ml->rwlock)
9583bc14
EJ
1804{
1805 struct mac_entry *mac;
1806
ee047520
BP
1807 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1808 return false;
1809 }
1810
1811 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1812 if (!mac || mac_entry_age(ml, mac)) {
1813 return true;
9583bc14
EJ
1814 }
1815
ee047520
BP
1816 if (is_gratuitous_arp(flow, wc)) {
1817 /* We don't want to learn from gratuitous ARP packets that are
1818 * reflected back over bond slaves so we lock the learning table. */
1819 if (!in_xbundle->bond) {
1820 return true;
1821 } else if (mac_entry_is_grat_arp_locked(mac)) {
1822 return false;
1823 }
1824 }
1825
1826 return mac->port.p != in_xbundle->ofbundle;
1827}
1828
1829
1830/* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1831 * received on 'in_xbundle' in 'vlan'.
1832 *
1833 * This code repeats all the checks in is_mac_learning_update_needed() because
1834 * the lock was released between there and here and thus the MAC learning state
1835 * could have changed.
1836 *
1837 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1838 * above. */
1839static void
1840update_learning_table__(const struct xbridge *xbridge,
1841 const struct flow *flow, struct flow_wildcards *wc,
1842 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1843OVS_REQ_WRLOCK(xbridge->ml->rwlock)
ee047520
BP
1844{
1845 struct mac_entry *mac;
1846
46c88433 1847 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
ee047520 1848 return;
9583bc14
EJ
1849 }
1850
46c88433 1851 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
9583bc14
EJ
1852 if (is_gratuitous_arp(flow, wc)) {
1853 /* We don't want to learn from gratuitous ARP packets that are
1854 * reflected back over bond slaves so we lock the learning table. */
46c88433 1855 if (!in_xbundle->bond) {
9583bc14
EJ
1856 mac_entry_set_grat_arp_lock(mac);
1857 } else if (mac_entry_is_grat_arp_locked(mac)) {
ee047520 1858 return;
9583bc14
EJ
1859 }
1860 }
1861
30618594 1862 if (mac->port.p != in_xbundle->ofbundle) {
9583bc14
EJ
1863 /* The log messages here could actually be useful in debugging,
1864 * so keep the rate limit relatively high. */
1865 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
ee047520 1866
9583bc14
EJ
1867 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1868 "on port %s in VLAN %d",
46c88433
EJ
1869 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1870 in_xbundle->name, vlan);
9583bc14 1871
46c88433 1872 mac->port.p = in_xbundle->ofbundle;
30618594 1873 mac_learning_changed(xbridge->ml);
9583bc14 1874 }
ee047520
BP
1875}
1876
1877static void
1878update_learning_table(const struct xbridge *xbridge,
1879 const struct flow *flow, struct flow_wildcards *wc,
1880 int vlan, struct xbundle *in_xbundle)
1881{
1882 bool need_update;
1883
1884 /* Don't learn the OFPP_NONE port. */
1885 if (in_xbundle == &ofpp_none_bundle) {
1886 return;
1887 }
1888
1889 /* First try the common case: no change to MAC learning table. */
1890 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1891 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1892 in_xbundle);
509c0149 1893 ovs_rwlock_unlock(&xbridge->ml->rwlock);
ee047520
BP
1894
1895 if (need_update) {
1896 /* Slow path: MAC learning table might need an update. */
1897 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1898 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1899 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1900 }
9583bc14
EJ
1901}
1902
86e2dcdd
FL
1903/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1904 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
1905static void
1906update_mcast_snooping_table__(const struct xbridge *xbridge,
1907 const struct flow *flow,
1908 struct mcast_snooping *ms,
1909 ovs_be32 ip4, int vlan,
1910 struct xbundle *in_xbundle)
1911 OVS_REQ_WRLOCK(ms->rwlock)
1912{
1913 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
1914
1915 switch (ntohs(flow->tp_src)) {
1916 case IGMP_HOST_MEMBERSHIP_REPORT:
1917 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1918 if (mcast_snooping_add_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1919 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
1920 IP_FMT" is on port %s in VLAN %d",
1921 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1922 }
1923 break;
1924 case IGMP_HOST_LEAVE_MESSAGE:
1925 if (mcast_snooping_leave_group(ms, ip4, vlan, in_xbundle->ofbundle)) {
1926 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
1927 IP_FMT" is on port %s in VLAN %d",
1928 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
1929 }
1930 break;
1931 case IGMP_HOST_MEMBERSHIP_QUERY:
1932 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
1933 in_xbundle->ofbundle)) {
1934 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
1935 IP_FMT" is on port %s in VLAN %d",
1936 xbridge->name, IP_ARGS(flow->nw_src),
1937 in_xbundle->name, vlan);
1938 }
1939 break;
1940 }
1941}
1942
1943/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
1944 * was received on 'in_xbundle' in 'vlan'. */
1945static void
1946update_mcast_snooping_table(const struct xbridge *xbridge,
1947 const struct flow *flow, int vlan,
1948 struct xbundle *in_xbundle)
1949{
1950 struct mcast_snooping *ms = xbridge->ms;
1951 struct xlate_cfg *xcfg;
1952 struct xbundle *mcast_xbundle;
1953 struct mcast_fport_bundle *fport;
1954
1955 /* Don't learn the OFPP_NONE port. */
1956 if (in_xbundle == &ofpp_none_bundle) {
1957 return;
1958 }
1959
1960 /* Don't learn from flood ports */
1961 mcast_xbundle = NULL;
1962 ovs_rwlock_wrlock(&ms->rwlock);
1963 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1964 LIST_FOR_EACH(fport, fport_node, &ms->fport_list) {
1965 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
1966 if (mcast_xbundle == in_xbundle) {
1967 break;
1968 }
1969 }
1970
1971 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
1972 update_mcast_snooping_table__(xbridge, flow, ms, flow->igmp_group_ip4,
1973 vlan, in_xbundle);
1974 }
1975 ovs_rwlock_unlock(&ms->rwlock);
1976}
1977
1978/* send the packet to ports having the multicast group learned */
1979static void
1980xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
1981 struct mcast_snooping *ms OVS_UNUSED,
1982 struct mcast_group *grp,
1983 struct xbundle *in_xbundle, uint16_t vlan)
1984 OVS_REQ_RDLOCK(ms->rwlock)
1985{
1986 struct xlate_cfg *xcfg;
1987 struct mcast_group_bundle *b;
1988 struct xbundle *mcast_xbundle;
1989
1990 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1991 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
1992 mcast_xbundle = xbundle_lookup(xcfg, b->port);
1993 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
1994 xlate_report(ctx, "forwarding to mcast group port");
1995 output_normal(ctx, mcast_xbundle, vlan);
1996 } else if (!mcast_xbundle) {
1997 xlate_report(ctx, "mcast group port is unknown, dropping");
1998 } else {
1999 xlate_report(ctx, "mcast group port is input port, dropping");
2000 }
2001 }
2002}
2003
2004/* send the packet to ports connected to multicast routers */
2005static void
2006xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2007 struct mcast_snooping *ms,
2008 struct xbundle *in_xbundle, uint16_t vlan)
2009 OVS_REQ_RDLOCK(ms->rwlock)
2010{
2011 struct xlate_cfg *xcfg;
2012 struct mcast_mrouter_bundle *mrouter;
2013 struct xbundle *mcast_xbundle;
2014
2015 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2016 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2017 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2018 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2019 xlate_report(ctx, "forwarding to mcast router port");
2020 output_normal(ctx, mcast_xbundle, vlan);
2021 } else if (!mcast_xbundle) {
2022 xlate_report(ctx, "mcast router port is unknown, dropping");
2023 } else {
2024 xlate_report(ctx, "mcast router port is input port, dropping");
2025 }
2026 }
2027}
2028
2029/* send the packet to ports flagged to be flooded */
2030static void
2031xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2032 struct mcast_snooping *ms,
2033 struct xbundle *in_xbundle, uint16_t vlan)
2034 OVS_REQ_RDLOCK(ms->rwlock)
2035{
2036 struct xlate_cfg *xcfg;
2037 struct mcast_fport_bundle *fport;
2038 struct xbundle *mcast_xbundle;
2039
2040 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2041 LIST_FOR_EACH(fport, fport_node, &ms->fport_list) {
2042 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2043 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2044 xlate_report(ctx, "forwarding to mcast flood port");
2045 output_normal(ctx, mcast_xbundle, vlan);
2046 } else if (!mcast_xbundle) {
2047 xlate_report(ctx, "mcast flood port is unknown, dropping");
2048 } else {
2049 xlate_report(ctx, "mcast flood port is input port, dropping");
2050 }
2051 }
2052}
2053
682800a4
FL
2054static void
2055xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2056 uint16_t vlan)
2057{
2058 struct xbundle *xbundle;
2059
2060 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2061 if (xbundle != in_xbundle
2062 && xbundle_includes_vlan(xbundle, vlan)
2063 && xbundle->floodable
2064 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2065 output_normal(ctx, xbundle, vlan);
2066 }
2067 }
2068 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2069}
2070
9583bc14
EJ
2071static void
2072xlate_normal(struct xlate_ctx *ctx)
2073{
33bf9176
BP
2074 struct flow_wildcards *wc = &ctx->xout->wc;
2075 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
2076 struct xbundle *in_xbundle;
2077 struct xport *in_port;
9583bc14 2078 struct mac_entry *mac;
d6d5bbc9 2079 void *mac_port;
9583bc14
EJ
2080 uint16_t vlan;
2081 uint16_t vid;
2082
2083 ctx->xout->has_normal = true;
2084
33bf9176
BP
2085 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2086 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1dd35f8a 2087 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 2088
46c88433
EJ
2089 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2090 ctx->xin->packet != NULL, &in_port);
2091 if (!in_xbundle) {
9583bc14
EJ
2092 xlate_report(ctx, "no input bundle, dropping");
2093 return;
2094 }
2095
2096 /* Drop malformed frames. */
33bf9176
BP
2097 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2098 !(flow->vlan_tci & htons(VLAN_CFI))) {
9583bc14
EJ
2099 if (ctx->xin->packet != NULL) {
2100 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2101 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2102 "VLAN tag received on port %s",
46c88433 2103 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2104 }
2105 xlate_report(ctx, "partial VLAN tag, dropping");
2106 return;
2107 }
2108
2109 /* Drop frames on bundles reserved for mirroring. */
46c88433 2110 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14
EJ
2111 if (ctx->xin->packet != NULL) {
2112 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2113 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2114 "%s, which is reserved exclusively for mirroring",
46c88433 2115 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2116 }
2117 xlate_report(ctx, "input port is mirror output port, dropping");
2118 return;
2119 }
2120
2121 /* Check VLAN. */
33bf9176 2122 vid = vlan_tci_to_vid(flow->vlan_tci);
46c88433 2123 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
2124 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2125 return;
2126 }
46c88433 2127 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14
EJ
2128
2129 /* Check other admissibility requirements. */
2130 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2131 return;
2132 }
2133
2134 /* Learn source MAC. */
2135 if (ctx->xin->may_learn) {
46c88433 2136 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
9583bc14 2137 }
b256dc52
JS
2138 if (ctx->xin->xcache) {
2139 struct xc_entry *entry;
2140
2141 /* Save enough info to update mac learning table later. */
2142 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
9edf6b48 2143 entry->u.normal.ofproto = ctx->xbridge->ofproto;
b256dc52
JS
2144 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2145 entry->u.normal.vlan = vlan;
2146 }
9583bc14
EJ
2147
2148 /* Determine output bundle. */
86e2dcdd
FL
2149 if (mcast_snooping_enabled(ctx->xbridge->ms)
2150 && !eth_addr_is_broadcast(flow->dl_dst)
2151 && eth_addr_is_multicast(flow->dl_dst)
2152 && flow->dl_type == htons(ETH_TYPE_IP)) {
2153 struct mcast_snooping *ms = ctx->xbridge->ms;
2154 struct mcast_group *grp;
2155
2156 if (flow->nw_proto == IPPROTO_IGMP) {
2157 if (ctx->xin->may_learn) {
2158 if (mcast_snooping_is_membership(flow->tp_src) ||
2159 mcast_snooping_is_query(flow->tp_src)) {
2160 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2161 in_xbundle);
2162 }
2163 }
d6d5bbc9 2164
86e2dcdd
FL
2165 if (mcast_snooping_is_membership(flow->tp_src)) {
2166 ovs_rwlock_rdlock(&ms->rwlock);
2167 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2168 ovs_rwlock_unlock(&ms->rwlock);
2169 } else {
2170 xlate_report(ctx, "multicast traffic, flooding");
2171 xlate_normal_flood(ctx, in_xbundle, vlan);
2172 }
2173 return;
2174 } else {
2175 if (ip_is_local_multicast(flow->nw_dst)) {
2176 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2177 * address in the 224.0.0.x range which are not IGMP must
2178 * be forwarded on all ports */
2179 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2180 xlate_normal_flood(ctx, in_xbundle, vlan);
2181 return;
2182 }
2183 }
2184
2185 /* forwarding to group base ports */
2186 ovs_rwlock_rdlock(&ms->rwlock);
2187 grp = mcast_snooping_lookup(ms, flow->nw_dst, vlan);
2188 if (grp) {
2189 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2190 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2191 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
9583bc14 2192 } else {
86e2dcdd
FL
2193 if (mcast_snooping_flood_unreg(ms)) {
2194 xlate_report(ctx, "unregistered multicast, flooding");
2195 xlate_normal_flood(ctx, in_xbundle, vlan);
2196 } else {
2197 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2198 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2199 }
9583bc14 2200 }
86e2dcdd 2201 ovs_rwlock_unlock(&ms->rwlock);
9583bc14 2202 } else {
86e2dcdd
FL
2203 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2204 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
2205 mac_port = mac ? mac->port.p : NULL;
2206 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2207
2208 if (mac_port) {
2209 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2210 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2211 if (mac_xbundle && mac_xbundle != in_xbundle) {
2212 xlate_report(ctx, "forwarding to learned port");
2213 output_normal(ctx, mac_xbundle, vlan);
2214 } else if (!mac_xbundle) {
2215 xlate_report(ctx, "learned port is unknown, dropping");
2216 } else {
2217 xlate_report(ctx, "learned port is input port, dropping");
2218 }
2219 } else {
2220 xlate_report(ctx, "no learned MAC for destination, flooding");
2221 xlate_normal_flood(ctx, in_xbundle, vlan);
2222 }
9583bc14
EJ
2223 }
2224}
2225
2226/* Compose SAMPLE action for sFlow or IPFIX. The given probability is
2227 * the number of packets out of UINT32_MAX to sample. The given
2228 * cookie is passed back in the callback for each sampled packet.
2229 */
2230static size_t
46c88433 2231compose_sample_action(const struct xbridge *xbridge,
9583bc14
EJ
2232 struct ofpbuf *odp_actions,
2233 const struct flow *flow,
2234 const uint32_t probability,
2235 const union user_action_cookie *cookie,
8b7ea2d4
WZ
2236 const size_t cookie_size,
2237 const odp_port_t tunnel_out_port)
9583bc14
EJ
2238{
2239 size_t sample_offset, actions_offset;
89a8a7f0 2240 odp_port_t odp_port;
9583bc14 2241 int cookie_offset;
89a8a7f0 2242 uint32_t pid;
9583bc14
EJ
2243
2244 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
2245
2246 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
2247
2248 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
89a8a7f0
EJ
2249
2250 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
9a159f74
AW
2251 pid = dpif_port_get_pid(xbridge->dpif, odp_port,
2252 flow_hash_5tuple(flow, 0));
2253 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
8b7ea2d4 2254 tunnel_out_port, odp_actions);
9583bc14
EJ
2255
2256 nl_msg_end_nested(odp_actions, actions_offset);
2257 nl_msg_end_nested(odp_actions, sample_offset);
2258 return cookie_offset;
2259}
2260
2261static void
46c88433
EJ
2262compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
2263 odp_port_t odp_port, unsigned int n_outputs,
2264 union user_action_cookie *cookie)
9583bc14
EJ
2265{
2266 int ifindex;
2267
2268 cookie->type = USER_ACTION_COOKIE_SFLOW;
2269 cookie->sflow.vlan_tci = vlan_tci;
2270
2271 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2272 * port information") for the interpretation of cookie->output. */
2273 switch (n_outputs) {
2274 case 0:
2275 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2276 cookie->sflow.output = 0x40000000 | 256;
2277 break;
2278
2279 case 1:
46c88433 2280 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
9583bc14
EJ
2281 if (ifindex) {
2282 cookie->sflow.output = ifindex;
2283 break;
2284 }
2285 /* Fall through. */
2286 default:
2287 /* 0x80000000 means "multiple output ports. */
2288 cookie->sflow.output = 0x80000000 | n_outputs;
2289 break;
2290 }
2291}
2292
2293/* Compose SAMPLE action for sFlow bridge sampling. */
2294static size_t
46c88433 2295compose_sflow_action(const struct xbridge *xbridge,
9583bc14
EJ
2296 struct ofpbuf *odp_actions,
2297 const struct flow *flow,
4e022ec0 2298 odp_port_t odp_port)
9583bc14
EJ
2299{
2300 uint32_t probability;
2301 union user_action_cookie cookie;
2302
46c88433 2303 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2304 return 0;
2305 }
2306
46c88433
EJ
2307 probability = dpif_sflow_get_probability(xbridge->sflow);
2308 compose_sflow_cookie(xbridge, htons(0), odp_port,
4e022ec0 2309 odp_port == ODPP_NONE ? 0 : 1, &cookie);
9583bc14 2310
46c88433 2311 return compose_sample_action(xbridge, odp_actions, flow, probability,
8b7ea2d4 2312 &cookie, sizeof cookie.sflow, ODPP_NONE);
9583bc14
EJ
2313}
2314
2315static void
2316compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
2317 uint32_t obs_domain_id, uint32_t obs_point_id,
2318 union user_action_cookie *cookie)
2319{
2320 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
2321 cookie->flow_sample.probability = probability;
2322 cookie->flow_sample.collector_set_id = collector_set_id;
2323 cookie->flow_sample.obs_domain_id = obs_domain_id;
2324 cookie->flow_sample.obs_point_id = obs_point_id;
2325}
2326
2327static void
8b7ea2d4
WZ
2328compose_ipfix_cookie(union user_action_cookie *cookie,
2329 odp_port_t output_odp_port)
9583bc14
EJ
2330{
2331 cookie->type = USER_ACTION_COOKIE_IPFIX;
8b7ea2d4 2332 cookie->ipfix.output_odp_port = output_odp_port;
9583bc14
EJ
2333}
2334
2335/* Compose SAMPLE action for IPFIX bridge sampling. */
2336static void
46c88433 2337compose_ipfix_action(const struct xbridge *xbridge,
9583bc14 2338 struct ofpbuf *odp_actions,
8b7ea2d4
WZ
2339 const struct flow *flow,
2340 odp_port_t output_odp_port)
9583bc14
EJ
2341{
2342 uint32_t probability;
2343 union user_action_cookie cookie;
8b7ea2d4 2344 odp_port_t tunnel_out_port = ODPP_NONE;
9583bc14 2345
46c88433 2346 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2347 return;
2348 }
2349
8b7ea2d4
WZ
2350 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2351 * number. */
2352 if (output_odp_port == ODPP_NONE &&
2353 !dpif_ipfix_get_bridge_exporter_input_sampling(xbridge->ipfix)) {
2354 return;
2355 }
2356
2357 /* For output case, output_odp_port is valid*/
2358 if (output_odp_port != ODPP_NONE) {
2359 if (!dpif_ipfix_get_bridge_exporter_output_sampling(xbridge->ipfix)) {
2360 return;
2361 }
2362 /* If tunnel sampling is enabled, put an additional option attribute:
2363 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2364 */
2365 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(xbridge->ipfix) &&
2366 dpif_ipfix_get_tunnel_port(xbridge->ipfix, output_odp_port) ) {
2367 tunnel_out_port = output_odp_port;
2368 }
2369 }
2370
46c88433 2371 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
8b7ea2d4 2372 compose_ipfix_cookie(&cookie, output_odp_port);
9583bc14 2373
46c88433 2374 compose_sample_action(xbridge, odp_actions, flow, probability,
8b7ea2d4 2375 &cookie, sizeof cookie.ipfix, tunnel_out_port);
9583bc14
EJ
2376}
2377
2378/* SAMPLE action for sFlow must be first action in any given list of
2379 * actions. At this point we do not have all information required to
2380 * build it. So try to build sample action as complete as possible. */
2381static void
2382add_sflow_action(struct xlate_ctx *ctx)
2383{
46c88433 2384 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
cc377352 2385 ctx->xout->odp_actions,
4e022ec0 2386 &ctx->xin->flow, ODPP_NONE);
9583bc14
EJ
2387 ctx->sflow_odp_port = 0;
2388 ctx->sflow_n_outputs = 0;
2389}
2390
2391/* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
2392 * of actions, eventually after the SAMPLE action for sFlow. */
2393static void
2394add_ipfix_action(struct xlate_ctx *ctx)
2395{
cc377352 2396 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
8b7ea2d4
WZ
2397 &ctx->xin->flow, ODPP_NONE);
2398}
2399
2400static void
2401add_ipfix_output_action(struct xlate_ctx *ctx, odp_port_t port)
2402{
2403 compose_ipfix_action(ctx->xbridge, ctx->xout->odp_actions,
2404 &ctx->xin->flow, port);
9583bc14
EJ
2405}
2406
2407/* Fix SAMPLE action according to data collected while composing ODP actions.
2408 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
2409 * USERSPACE action's user-cookie which is required for sflow. */
2410static void
2411fix_sflow_action(struct xlate_ctx *ctx)
2412{
2413 const struct flow *base = &ctx->base_flow;
2414 union user_action_cookie *cookie;
2415
2416 if (!ctx->user_cookie_offset) {
2417 return;
2418 }
2419
cc377352 2420 cookie = ofpbuf_at(ctx->xout->odp_actions, ctx->user_cookie_offset,
9583bc14
EJ
2421 sizeof cookie->sflow);
2422 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2423
46c88433 2424 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
9583bc14
EJ
2425 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
2426}
2427
db7d4e46 2428static enum slow_path_reason
642dc74d 2429process_special(struct xlate_ctx *ctx, const struct flow *flow,
46c88433 2430 const struct xport *xport, const struct ofpbuf *packet)
db7d4e46 2431{
642dc74d 2432 struct flow_wildcards *wc = &ctx->xout->wc;
46c88433 2433 const struct xbridge *xbridge = ctx->xbridge;
642dc74d 2434
46c88433 2435 if (!xport) {
db7d4e46 2436 return 0;
46c88433 2437 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 2438 if (packet) {
46c88433 2439 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46
JP
2440 }
2441 return SLOW_CFM;
fab52e16 2442 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 2443 if (packet) {
46c88433 2444 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
2445 /* If POLL received, immediately sends FINAL back. */
2446 if (bfd_should_send_packet(xport->bfd)) {
6d308b28 2447 ofproto_dpif_monitor_port_send_soon(xport->ofport);
60d02c72 2448 }
db7d4e46
JP
2449 }
2450 return SLOW_BFD;
46c88433 2451 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
2452 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2453 if (packet) {
46c88433 2454 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46
JP
2455 }
2456 return SLOW_LACP;
9efd308e
DV
2457 } else if ((xbridge->stp || xbridge->rstp) &&
2458 stp_should_process_flow(flow, wc)) {
db7d4e46 2459 if (packet) {
f025bcb7
JR
2460 xbridge->stp
2461 ? stp_process_packet(xport, packet)
2462 : rstp_process_packet(xport, packet);
db7d4e46
JP
2463 }
2464 return SLOW_STP;
2465 } else {
2466 return 0;
2467 }
2468}
2469
9583bc14 2470static void
4e022ec0 2471compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
9583bc14
EJ
2472 bool check_stp)
2473{
46c88433 2474 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1dd35f8a 2475 struct flow_wildcards *wc = &ctx->xout->wc;
33bf9176 2476 struct flow *flow = &ctx->xin->flow;
9583bc14 2477 ovs_be16 flow_vlan_tci;
1362e248 2478 uint32_t flow_pkt_mark;
9583bc14 2479 uint8_t flow_nw_tos;
4e022ec0 2480 odp_port_t out_port, odp_port;
ca077186 2481 uint8_t dscp;
9583bc14
EJ
2482
2483 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2484 * before traversing a patch port. */
0e612675 2485 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
9583bc14 2486
46c88433 2487 if (!xport) {
9583bc14
EJ
2488 xlate_report(ctx, "Nonexistent output port");
2489 return;
46c88433 2490 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
9583bc14
EJ
2491 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2492 return;
0d1cee12 2493 } else if (check_stp) {
bbbca389 2494 if (is_stp(&ctx->base_flow)) {
9efd308e
DV
2495 if (!xport_stp_should_forward_bpdu(xport) &&
2496 !xport_rstp_should_manage_bpdu(xport)) {
2497 if (ctx->xbridge->stp != NULL) {
2498 xlate_report(ctx, "STP not in listening state, "
2499 "skipping bpdu output");
2500 } else if (ctx->xbridge->rstp != NULL) {
2501 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2502 "skipping bpdu output");
2503 }
0d1cee12
K
2504 return;
2505 }
9efd308e
DV
2506 } else if (!xport_stp_forward_state(xport) ||
2507 !xport_rstp_forward_state(xport)) {
2508 if (ctx->xbridge->stp != NULL) {
2509 xlate_report(ctx, "STP not in forwarding state, "
2510 "skipping output");
2511 } else if (ctx->xbridge->rstp != NULL) {
2512 xlate_report(ctx, "RSTP not in forwarding state, "
2513 "skipping output");
2514 }
0d1cee12
K
2515 return;
2516 }
9583bc14
EJ
2517 }
2518
46c88433
EJ
2519 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
2520 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
2521 xport->xbundle);
cdf5d3a5
EJ
2522 }
2523
46c88433
EJ
2524 if (xport->peer) {
2525 const struct xport *peer = xport->peer;
9583bc14 2526 struct flow old_flow = ctx->xin->flow;
9583bc14 2527 enum slow_path_reason special;
9583bc14 2528
46c88433
EJ
2529 ctx->xbridge = peer->xbridge;
2530 flow->in_port.ofp_port = peer->ofp_port;
33bf9176
BP
2531 flow->metadata = htonll(0);
2532 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2533 memset(flow->regs, 0, sizeof flow->regs);
9583bc14 2534
642dc74d 2535 special = process_special(ctx, &ctx->xin->flow, peer,
9583bc14
EJ
2536 ctx->xin->packet);
2537 if (special) {
04594cd5 2538 ctx->xout->slow |= special;
ddd3c975 2539 } else if (may_receive(peer, ctx)) {
9efd308e 2540 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
6d328fa2 2541 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
9583bc14 2542 } else {
9efd308e
DV
2543 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2544 * the learning action look at the packet, then drop it. */
9583bc14 2545 struct flow old_base_flow = ctx->base_flow;
cc377352 2546 size_t old_size = ofpbuf_size(ctx->xout->odp_actions);
cdf5d3a5 2547 mirror_mask_t old_mirrors = ctx->xout->mirrors;
6d328fa2 2548 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
cdf5d3a5 2549 ctx->xout->mirrors = old_mirrors;
9583bc14 2550 ctx->base_flow = old_base_flow;
cc377352 2551 ofpbuf_set_size(ctx->xout->odp_actions, old_size);
9583bc14
EJ
2552 }
2553 }
2554
2555 ctx->xin->flow = old_flow;
832554e3 2556 ctx->xbridge = xport->xbridge;
9583bc14
EJ
2557
2558 if (ctx->xin->resubmit_stats) {
46c88433
EJ
2559 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
2560 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
a1aeea86
AW
2561 if (peer->bfd) {
2562 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
2563 }
9583bc14 2564 }
b256dc52
JS
2565 if (ctx->xin->xcache) {
2566 struct xc_entry *entry;
2567
2568 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2569 entry->u.dev.tx = netdev_ref(xport->netdev);
2570 entry->u.dev.rx = netdev_ref(peer->netdev);
2571 entry->u.dev.bfd = bfd_ref(peer->bfd);
2572 }
9583bc14
EJ
2573 return;
2574 }
2575
33bf9176 2576 flow_vlan_tci = flow->vlan_tci;
1362e248 2577 flow_pkt_mark = flow->pkt_mark;
33bf9176 2578 flow_nw_tos = flow->nw_tos;
9583bc14 2579
55954f6e 2580 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1ac9326d 2581 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 2582 flow->nw_tos &= ~IP_DSCP_MASK;
ca077186 2583 flow->nw_tos |= dscp;
9583bc14
EJ
2584 }
2585
46c88433 2586 if (xport->is_tunnel) {
9583bc14
EJ
2587 /* Save tunnel metadata so that changes made due to
2588 * the Logical (tunnel) Port are not visible for any further
2589 * matches, while explicit set actions on tunnel metadata are.
2590 */
33bf9176 2591 struct flow_tnl flow_tnl = flow->tunnel;
46c88433 2592 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
4e022ec0 2593 if (odp_port == ODPP_NONE) {
9583bc14
EJ
2594 xlate_report(ctx, "Tunneling decided against output");
2595 goto out; /* restore flow_nw_tos */
2596 }
33bf9176 2597 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
9583bc14
EJ
2598 xlate_report(ctx, "Not tunneling to our own address");
2599 goto out; /* restore flow_nw_tos */
2600 }
2601 if (ctx->xin->resubmit_stats) {
46c88433 2602 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14 2603 }
b256dc52
JS
2604 if (ctx->xin->xcache) {
2605 struct xc_entry *entry;
2606
2607 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
2608 entry->u.dev.tx = netdev_ref(xport->netdev);
2609 }
9583bc14 2610 out_port = odp_port;
33bf9176 2611 commit_odp_tunnel_action(flow, &ctx->base_flow,
cc377352 2612 ctx->xout->odp_actions);
33bf9176 2613 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
9583bc14 2614 } else {
46c88433 2615 odp_port = xport->odp_port;
7614e5d0 2616 out_port = odp_port;
46c88433 2617 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
7614e5d0
JR
2618 ofp_port_t vlandev_port;
2619
1dd35f8a 2620 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
7614e5d0
JR
2621 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
2622 ofp_port, flow->vlan_tci);
2623 if (vlandev_port != ofp_port) {
2624 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
2625 flow->vlan_tci = htons(0);
2626 }
9583bc14 2627 }
9583bc14 2628 }
9583bc14 2629
4e022ec0 2630 if (out_port != ODPP_NONE) {
7fd91025 2631 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
cc377352 2632 ctx->xout->odp_actions,
8bfd0fda 2633 &ctx->xout->wc);
adcf00ba 2634
92c08f09 2635 if (ctx->use_recirc) {
347bf289 2636 struct ovs_action_hash *act_hash;
92c08f09 2637 struct xlate_recirc *xr = &ctx->recirc;
adcf00ba 2638
347bf289 2639 /* Hash action. */
cc377352 2640 act_hash = nl_msg_put_unspec_uninit(ctx->xout->odp_actions,
347bf289
AZ
2641 OVS_ACTION_ATTR_HASH,
2642 sizeof *act_hash);
2643 act_hash->hash_alg = xr->hash_alg;
62ac1f20 2644 act_hash->hash_basis = xr->hash_basis;
347bf289
AZ
2645
2646 /* Recirc action. */
cc377352 2647 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC,
347bf289 2648 xr->recirc_id);
adcf00ba 2649 } else {
8b7ea2d4 2650 add_ipfix_output_action(ctx, out_port);
cc377352 2651 nl_msg_put_odp_port(ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
adcf00ba
AZ
2652 out_port);
2653 }
9583bc14 2654
6cbbf4fa
EJ
2655 ctx->sflow_odp_port = odp_port;
2656 ctx->sflow_n_outputs++;
2657 ctx->xout->nf_output_iface = ofp_port;
2658 }
2659
2660 out:
9583bc14 2661 /* Restore flow */
33bf9176 2662 flow->vlan_tci = flow_vlan_tci;
1362e248 2663 flow->pkt_mark = flow_pkt_mark;
33bf9176 2664 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
2665}
2666
2667static void
4e022ec0 2668compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
9583bc14
EJ
2669{
2670 compose_output_action__(ctx, ofp_port, true);
2671}
2672
bb61b33d
BP
2673static void
2674xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
2675{
2676 struct rule_dpif *old_rule = ctx->rule;
dc723c44 2677 const struct rule_actions *actions;
bb61b33d
BP
2678
2679 if (ctx->xin->resubmit_stats) {
70742c7f 2680 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
2681 }
2682
98b07853 2683 ctx->resubmits++;
bb61b33d
BP
2684 ctx->recurse++;
2685 ctx->rule = rule;
6f00e29b
BP
2686 actions = rule_dpif_get_actions(rule);
2687 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
bb61b33d
BP
2688 ctx->rule = old_rule;
2689 ctx->recurse--;
bb61b33d
BP
2690}
2691
bd3240ba
SH
2692static bool
2693xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 2694{
98b07853
BP
2695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2696
adcf00ba 2697 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
98b07853
BP
2698 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
2699 MAX_RESUBMIT_RECURSION);
adcf00ba 2700 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
98b07853 2701 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
cc377352 2702 } else if (ofpbuf_size(ctx->xout->odp_actions) > UINT16_MAX) {
98b07853 2703 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1f317cb5 2704 } else if (ofpbuf_size(&ctx->stack) >= 65536) {
98b07853
BP
2705 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
2706 } else {
bd3240ba
SH
2707 return true;
2708 }
2709
2710 return false;
2711}
2712
2713static void
6d328fa2
SH
2714xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2715 bool may_packet_in, bool honor_table_miss)
bd3240ba
SH
2716{
2717 if (xlate_resubmit_resource_check(ctx)) {
4e022ec0 2718 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
3f207910 2719 bool skip_wildcards = ctx->xin->skip_wildcards;
9583bc14 2720 uint8_t old_table_id = ctx->table_id;
3f207910 2721 struct rule_dpif *rule;
6d328fa2
SH
2722 enum rule_dpif_lookup_verdict verdict;
2723 enum ofputil_port_config config = 0;
9583bc14
EJ
2724
2725 ctx->table_id = table_id;
2726
bb61b33d
BP
2727 /* Look up a flow with 'in_port' as the input port. Then restore the
2728 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2729 * have surprising behavior). */
4e022ec0 2730 ctx->xin->flow.in_port.ofp_port = in_port;
6d328fa2
SH
2731 verdict = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
2732 &ctx->xin->flow,
2733 !skip_wildcards
2734 ? &ctx->xout->wc : NULL,
2735 honor_table_miss,
83709dfa 2736 &ctx->table_id, &rule,
d611866c
SH
2737 ctx->xin->xcache != NULL,
2738 ctx->xin->resubmit_stats);
4e022ec0 2739 ctx->xin->flow.in_port.ofp_port = old_in_port;
9583bc14 2740
ad3efdcb
EJ
2741 if (ctx->xin->resubmit_hook) {
2742 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
2743 }
2744
6d328fa2
SH
2745 switch (verdict) {
2746 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
2747 goto match;
2748 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER:
2749 if (may_packet_in) {
2750 struct xport *xport;
2751
2752 xport = get_ofp_port(ctx->xbridge,
2753 ctx->xin->flow.in_port.ofp_port);
2754 config = xport ? xport->config : 0;
2755 break;
2756 }
2757 /* Fall through to drop */
2758 case RULE_DPIF_LOOKUP_VERDICT_DROP:
2759 config = OFPUTIL_PC_NO_PACKET_IN;
2760 break;
6b83a3c5
SH
2761 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
2762 if (!ofproto_dpif_wants_packet_in_on_miss(ctx->xbridge->ofproto)) {
2763 config = OFPUTIL_PC_NO_PACKET_IN;
2764 }
2765 break;
6d328fa2
SH
2766 default:
2767 OVS_NOT_REACHED();
a2143702 2768 }
6d328fa2
SH
2769
2770 choose_miss_rule(config, ctx->xbridge->miss_rule,
83709dfa
JR
2771 ctx->xbridge->no_packet_in_rule, &rule,
2772 ctx->xin->xcache != NULL);
6d328fa2
SH
2773
2774match:
a2143702 2775 if (rule) {
83709dfa
JR
2776 /* Fill in the cache entry here instead of xlate_recursively
2777 * to make the reference counting more explicit. We take a
2778 * reference in the lookups above if we are going to cache the
2779 * rule. */
2780 if (ctx->xin->xcache) {
2781 struct xc_entry *entry;
2782
2783 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
2784 entry->u.rule = rule;
2785 }
bb61b33d 2786 xlate_recursively(ctx, rule);
ad3efdcb
EJ
2787 }
2788
9583bc14 2789 ctx->table_id = old_table_id;
98b07853 2790 return;
9583bc14 2791 }
98b07853
BP
2792
2793 ctx->exit = true;
9583bc14
EJ
2794}
2795
f4fb341b 2796static void
1e684d7d
RW
2797xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
2798 struct ofputil_bucket *bucket)
2799{
2800 if (ctx->xin->resubmit_stats) {
2801 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
2802 }
2803 if (ctx->xin->xcache) {
2804 struct xc_entry *entry;
2805
2806 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
2807 entry->u.group.group = group_dpif_ref(group);
2808 entry->u.group.bucket = bucket;
2809 }
2810}
2811
2812static void
2813xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
f4fb341b
SH
2814{
2815 uint64_t action_list_stub[1024 / 8];
2816 struct ofpbuf action_list, action_set;
2817
2818 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
2819 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2820
2821 ofpacts_execute_action_set(&action_list, &action_set);
2822 ctx->recurse++;
1f317cb5 2823 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
f4fb341b
SH
2824 ctx->recurse--;
2825
2826 ofpbuf_uninit(&action_set);
2827 ofpbuf_uninit(&action_list);
2828}
2829
2830static void
2831xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
2832{
1e684d7d 2833 struct ofputil_bucket *bucket;
f4fb341b
SH
2834 const struct list *buckets;
2835 struct flow old_flow = ctx->xin->flow;
2836
2837 group_dpif_get_buckets(group, &buckets);
2838
2839 LIST_FOR_EACH (bucket, list_node, buckets) {
2840 xlate_group_bucket(ctx, bucket);
2841 /* Roll back flow to previous state.
2842 * This is equivalent to cloning the packet for each bucket.
2843 *
2844 * As a side effect any subsequently applied actions will
2845 * also effectively be applied to a clone of the packet taken
2846 * just before applying the all or indirect group. */
2847 ctx->xin->flow = old_flow;
2848 }
1e684d7d 2849 xlate_group_stats(ctx, group, NULL);
f4fb341b
SH
2850}
2851
dd8cd4b4
SH
2852static void
2853xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
2854{
1e684d7d 2855 struct ofputil_bucket *bucket;
dd8cd4b4
SH
2856
2857 bucket = group_first_live_bucket(ctx, group, 0);
2858 if (bucket) {
2859 xlate_group_bucket(ctx, bucket);
1e684d7d 2860 xlate_group_stats(ctx, group, bucket);
dd8cd4b4
SH
2861 }
2862}
2863
fe7e5749
SH
2864static void
2865xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
2866{
2867 struct flow_wildcards *wc = &ctx->xout->wc;
1e684d7d 2868 struct ofputil_bucket *bucket;
fe7e5749
SH
2869 uint32_t basis;
2870
1d1aae0b 2871 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
fe7e5749
SH
2872 bucket = group_best_live_bucket(ctx, group, basis);
2873 if (bucket) {
2874 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1d1aae0b
SS
2875 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2876 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2877 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2878 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2879 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2880 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2881 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2882 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2883
fe7e5749 2884 xlate_group_bucket(ctx, bucket);
1e684d7d 2885 xlate_group_stats(ctx, group, bucket);
fe7e5749
SH
2886 }
2887}
2888
f4fb341b
SH
2889static void
2890xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
2891{
5a070238
BP
2892 ctx->in_group = true;
2893
f4fb341b
SH
2894 switch (group_dpif_get_type(group)) {
2895 case OFPGT11_ALL:
2896 case OFPGT11_INDIRECT:
2897 xlate_all_group(ctx, group);
2898 break;
2899 case OFPGT11_SELECT:
fe7e5749 2900 xlate_select_group(ctx, group);
f4fb341b 2901 break;
dd8cd4b4
SH
2902 case OFPGT11_FF:
2903 xlate_ff_group(ctx, group);
2904 break;
f4fb341b 2905 default:
428b2edd 2906 OVS_NOT_REACHED();
f4fb341b 2907 }
809c7548 2908 group_dpif_unref(group);
5a070238
BP
2909
2910 ctx->in_group = false;
2911}
2912
2913static bool
2914xlate_group_resource_check(struct xlate_ctx *ctx)
2915{
2916 if (!xlate_resubmit_resource_check(ctx)) {
2917 return false;
2918 } else if (ctx->in_group) {
2919 /* Prevent nested translation of OpenFlow groups.
2920 *
2921 * OpenFlow allows this restriction. We enforce this restriction only
2922 * because, with the current architecture, we would otherwise have to
2923 * take a possibly recursive read lock on the ofgroup rwlock, which is
2924 * unsafe given that POSIX allows taking a read lock to block if there
2925 * is a thread blocked on taking the write lock. Other solutions
2926 * without this restriction are also possible, but seem unwarranted
2927 * given the current limited use of groups. */
2928 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
2929
2930 VLOG_ERR_RL(&rl, "cannot recursively translate OpenFlow group");
2931 return false;
2932 } else {
2933 return true;
2934 }
f4fb341b
SH
2935}
2936
2937static bool
2938xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2939{
5a070238 2940 if (xlate_group_resource_check(ctx)) {
f4fb341b
SH
2941 struct group_dpif *group;
2942 bool got_group;
2943
2944 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2945 if (got_group) {
2946 xlate_group_action__(ctx, group);
2947 } else {
2948 return true;
2949 }
2950 }
2951
2952 return false;
2953}
2954
9583bc14
EJ
2955static void
2956xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2957 const struct ofpact_resubmit *resubmit)
2958{
4e022ec0 2959 ofp_port_t in_port;
9583bc14 2960 uint8_t table_id;
adcf00ba
AZ
2961 bool may_packet_in = false;
2962 bool honor_table_miss = false;
2963
2964 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
2965 /* Still allow missed packets to be sent to the controller
2966 * if resubmitting from an internal table. */
2967 may_packet_in = true;
2968 honor_table_miss = true;
2969 }
9583bc14
EJ
2970
2971 in_port = resubmit->in_port;
2972 if (in_port == OFPP_IN_PORT) {
4e022ec0 2973 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
2974 }
2975
2976 table_id = resubmit->table_id;
2977 if (table_id == 255) {
2978 table_id = ctx->table_id;
2979 }
2980
adcf00ba
AZ
2981 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2982 honor_table_miss);
9583bc14
EJ
2983}
2984
2985static void
2986flood_packets(struct xlate_ctx *ctx, bool all)
2987{
46c88433 2988 const struct xport *xport;
9583bc14 2989
46c88433
EJ
2990 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2991 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
2992 continue;
2993 }
2994
2995 if (all) {
46c88433
EJ
2996 compose_output_action__(ctx, xport->ofp_port, false);
2997 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2998 compose_output_action(ctx, xport->ofp_port);
9583bc14
EJ
2999 }
3000 }
3001
3002 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3003}
3004
3005static void
3006execute_controller_action(struct xlate_ctx *ctx, int len,
3007 enum ofp_packet_in_reason reason,
3008 uint16_t controller_id)
3009{
0fb7792a 3010 struct ofproto_packet_in *pin;
91088554 3011 struct dpif_packet *packet;
758c456d 3012 struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
9583bc14 3013
04594cd5 3014 ctx->xout->slow |= SLOW_CONTROLLER;
9583bc14
EJ
3015 if (!ctx->xin->packet) {
3016 return;
3017 }
3018
91088554 3019 packet = dpif_packet_clone_from_ofpbuf(ctx->xin->packet);
9583bc14 3020
7fd91025 3021 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3022 ctx->xout->odp_actions,
8bfd0fda 3023 &ctx->xout->wc);
9583bc14 3024
8cbf4f47 3025 odp_execute_actions(NULL, &packet, 1, false, &md,
cc377352
EJ
3026 ofpbuf_data(ctx->xout->odp_actions),
3027 ofpbuf_size(ctx->xout->odp_actions), NULL);
9583bc14 3028
ada3a58d 3029 pin = xmalloc(sizeof *pin);
91088554
DDP
3030 pin->up.packet_len = ofpbuf_size(&packet->ofpbuf);
3031 pin->up.packet = ofpbuf_steal_data(&packet->ofpbuf);
0fb7792a 3032 pin->up.reason = reason;
0fb7792a 3033 pin->up.table_id = ctx->table_id;
d4fa4e79
BP
3034 pin->up.cookie = (ctx->rule
3035 ? rule_dpif_get_flow_cookie(ctx->rule)
3036 : OVS_BE64_MAX);
0fb7792a 3037
0fb7792a 3038 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
9583bc14 3039
f11c7538 3040 pin->controller_id = controller_id;
d38a3c7b 3041 pin->send_len = len;
32260212
SH
3042 /* If a rule is a table-miss rule then this is
3043 * a table-miss handled by a table-miss rule.
3044 *
3045 * Else, if rule is internal and has a controller action,
3046 * the later being implied by the rule being processed here,
3047 * then this is a table-miss handled without a table-miss rule.
3048 *
3049 * Otherwise this is not a table-miss. */
3050 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
3051 if (ctx->rule) {
3052 if (rule_dpif_is_table_miss(ctx->rule)) {
3053 pin->miss_type = OFPROTO_PACKET_IN_MISS_FLOW;
3054 } else if (rule_dpif_is_internal(ctx->rule)) {
3055 pin->miss_type = OFPROTO_PACKET_IN_MISS_WITHOUT_FLOW;
3056 }
3057 }
ada3a58d 3058 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
91088554 3059 dpif_packet_delete(packet);
9583bc14
EJ
3060}
3061
7bbdd84f
SH
3062static void
3063compose_recirculate_action(struct xlate_ctx *ctx,
3064 const struct ofpact *ofpacts_base,
3065 const struct ofpact *ofpact_current,
3066 size_t ofpacts_base_len)
3067{
3068 uint32_t id;
3069 int error;
3070 unsigned ofpacts_len;
3071 struct match match;
3072 struct rule *rule;
3073 struct ofpbuf ofpacts;
3074
3075 ctx->exit = true;
3076
3077 ofpacts_len = ofpacts_base_len -
3078 ((uint8_t *)ofpact_current - (uint8_t *)ofpacts_base);
3079
3080 if (ctx->rule) {
3081 id = rule_dpif_get_recirc_id(ctx->rule);
3082 } else {
3083 /* In the case where ctx has no rule then allocate a recirc id.
3084 * The life-cycle of this recirc id is managed by associating it
3085 * with the internal rule that is created to to handle
3086 * recirculation below.
3087 *
3088 * The known use-case of this is packet_out which
3089 * translates actions without a rule */
3090 id = ofproto_dpif_alloc_recirc_id(ctx->xbridge->ofproto);
3091 }
3092 if (!id) {
3093 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3094 VLOG_ERR_RL(&rl, "Failed to allocate recirculation id");
3095 return;
3096 }
3097
3098 match_init_catchall(&match);
3099 match_set_recirc_id(&match, id);
3100 ofpbuf_use_const(&ofpacts, ofpact_current, ofpacts_len);
3101 error = ofproto_dpif_add_internal_flow(ctx->xbridge->ofproto, &match,
3102 RECIRC_RULE_PRIORITY,
3103 RECIRC_TIMEOUT, &ofpacts, &rule);
3104 if (error) {
3105 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3106 VLOG_ERR_RL(&rl, "Failed to add post recirculation flow %s",
3107 match_to_string(&match, 0));
3108 return;
3109 }
3110 /* If ctx has no rule then associate the recirc id, which
3111 * was allocated above, with the internal rule. This allows
3112 * the recirc id to be released when the internal rule times out. */
3113 if (!ctx->rule) {
3114 rule_set_recirc_id(rule, id);
3115 }
3116
3117 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3118 ctx->xout->odp_actions,
7bbdd84f 3119 &ctx->xout->wc);
cc377352 3120 nl_msg_put_u32(ctx->xout->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
7bbdd84f
SH
3121}
3122
8bfd0fda
BP
3123static void
3124compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 3125{
33bf9176
BP
3126 struct flow_wildcards *wc = &ctx->xout->wc;
3127 struct flow *flow = &ctx->xin->flow;
8bfd0fda 3128 int n;
33bf9176 3129
8bfd0fda 3130 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 3131
8bfd0fda
BP
3132 n = flow_count_mpls_labels(flow, wc);
3133 if (!n) {
8bfd0fda 3134 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
cc377352 3135 ctx->xout->odp_actions,
8bfd0fda
BP
3136 &ctx->xout->wc);
3137 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3138 if (ctx->xin->packet != NULL) {
3139 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3140 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3141 "MPLS push action can't be performed as it would "
3142 "have more MPLS LSEs than the %d supported.",
3143 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
9583bc14 3144 }
8bfd0fda
BP
3145 ctx->exit = true;
3146 return;
3147 } else if (n >= ctx->xbridge->max_mpls_depth) {
3148 COVERAGE_INC(xlate_actions_mpls_overflow);
3149 ctx->xout->slow |= SLOW_ACTION;
9583bc14 3150 }
b0a17866 3151
8bfd0fda 3152 flow_push_mpls(flow, n, mpls->ethertype, wc);
9583bc14
EJ
3153}
3154
8bfd0fda 3155static void
9cfef3d0 3156compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 3157{
33bf9176 3158 struct flow_wildcards *wc = &ctx->xout->wc;
8bfd0fda
BP
3159 struct flow *flow = &ctx->xin->flow;
3160 int n = flow_count_mpls_labels(flow, wc);
33bf9176 3161
7bbdd84f
SH
3162 if (flow_pop_mpls(flow, n, eth_type, wc)) {
3163 if (ctx->xbridge->enable_recirc && !eth_type_mpls(eth_type)) {
3164 ctx->was_mpls = true;
3165 }
3166 } else if (n >= FLOW_MAX_MPLS_LABELS) {
8bfd0fda
BP
3167 if (ctx->xin->packet != NULL) {
3168 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3169 VLOG_WARN_RL(&rl, "bridge %s: dropping packet on which an "
3170 "MPLS pop action can't be performed as it has "
3171 "more MPLS LSEs than the %d supported.",
3172 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3173 }
3174 ctx->exit = true;
cc377352 3175 ofpbuf_clear(ctx->xout->odp_actions);
9583bc14
EJ
3176 }
3177}
3178
3179static bool
3180compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3181{
33bf9176
BP
3182 struct flow *flow = &ctx->xin->flow;
3183
3184 if (!is_ip_any(flow)) {
9583bc14
EJ
3185 return false;
3186 }
3187
1dd35f8a 3188 ctx->xout->wc.masks.nw_ttl = 0xff;
33bf9176
BP
3189 if (flow->nw_ttl > 1) {
3190 flow->nw_ttl--;
9583bc14
EJ
3191 return false;
3192 } else {
3193 size_t i;
3194
3195 for (i = 0; i < ids->n_controllers; i++) {
3196 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3197 ids->cnt_ids[i]);
3198 }
3199
3200 /* Stop processing for current table. */
3201 return true;
3202 }
3203}
3204
8bfd0fda 3205static void
097d4939
JR
3206compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3207{
8bfd0fda
BP
3208 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3209 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
3210 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 3211 }
097d4939
JR
3212}
3213
8bfd0fda 3214static void
097d4939
JR
3215compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3216{
8bfd0fda
BP
3217 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3218 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
3219 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 3220 }
097d4939
JR
3221}
3222
8bfd0fda 3223static void
9cfef3d0 3224compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 3225{
8bfd0fda
BP
3226 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
3227 ctx->xout->wc.masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
3228 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 3229 }
9583bc14
EJ
3230}
3231
3232static bool
9cfef3d0 3233compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 3234{
33bf9176 3235 struct flow *flow = &ctx->xin->flow;
8bfd0fda 3236 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
1dd35f8a
JP
3237 struct flow_wildcards *wc = &ctx->xout->wc;
3238
1dd35f8a 3239 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
8bfd0fda
BP
3240 if (eth_type_mpls(flow->dl_type)) {
3241 if (ttl > 1) {
3242 ttl--;
3243 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3244 return false;
3245 } else {
3246 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
9583bc14 3247
8bfd0fda
BP
3248 /* Stop processing for current table. */
3249 return true;
3250 }
9583bc14 3251 } else {
9583bc14
EJ
3252 return true;
3253 }
3254}
3255
3256static void
3257xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 3258 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 3259{
4e022ec0 3260 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
9583bc14
EJ
3261
3262 ctx->xout->nf_output_iface = NF_OUT_DROP;
3263
3264 switch (port) {
3265 case OFPP_IN_PORT:
4e022ec0 3266 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
9583bc14
EJ
3267 break;
3268 case OFPP_TABLE:
4e022ec0 3269 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 3270 0, may_packet_in, true);
9583bc14
EJ
3271 break;
3272 case OFPP_NORMAL:
3273 xlate_normal(ctx);
3274 break;
3275 case OFPP_FLOOD:
3276 flood_packets(ctx, false);
3277 break;
3278 case OFPP_ALL:
3279 flood_packets(ctx, true);
3280 break;
3281 case OFPP_CONTROLLER:
3282 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
3283 break;
3284 case OFPP_NONE:
3285 break;
3286 case OFPP_LOCAL:
3287 default:
4e022ec0 3288 if (port != ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3289 compose_output_action(ctx, port);
3290 } else {
3291 xlate_report(ctx, "skipping output to input port");
3292 }
3293 break;
3294 }
3295
3296 if (prev_nf_output_iface == NF_OUT_FLOOD) {
3297 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
3298 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3299 ctx->xout->nf_output_iface = prev_nf_output_iface;
3300 } else if (prev_nf_output_iface != NF_OUT_DROP &&
3301 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3302 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3303 }
3304}
3305
3306static void
3307xlate_output_reg_action(struct xlate_ctx *ctx,
3308 const struct ofpact_output_reg *or)
3309{
3310 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3311 if (port <= UINT16_MAX) {
3312 union mf_subvalue value;
3313
3314 memset(&value, 0xff, sizeof value);
3315 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
4e022ec0
AW
3316 xlate_output_action(ctx, u16_to_ofp(port),
3317 or->max_len, false);
9583bc14
EJ
3318 }
3319}
3320
3321static void
3322xlate_enqueue_action(struct xlate_ctx *ctx,
3323 const struct ofpact_enqueue *enqueue)
3324{
4e022ec0 3325 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
3326 uint32_t queue_id = enqueue->queue;
3327 uint32_t flow_priority, priority;
3328 int error;
3329
3330 /* Translate queue to priority. */
89a8a7f0 3331 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
3332 if (error) {
3333 /* Fall back to ordinary output action. */
3334 xlate_output_action(ctx, enqueue->port, 0, false);
3335 return;
3336 }
3337
3338 /* Check output port. */
3339 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
3340 ofp_port = ctx->xin->flow.in_port.ofp_port;
3341 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3342 return;
3343 }
3344
3345 /* Add datapath actions. */
3346 flow_priority = ctx->xin->flow.skb_priority;
3347 ctx->xin->flow.skb_priority = priority;
3348 compose_output_action(ctx, ofp_port);
3349 ctx->xin->flow.skb_priority = flow_priority;
3350
3351 /* Update NetFlow output port. */
3352 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
3353 ctx->xout->nf_output_iface = ofp_port;
3354 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
3355 ctx->xout->nf_output_iface = NF_OUT_MULTI;
3356 }
3357}
3358
3359static void
3360xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3361{
3362 uint32_t skb_priority;
3363
89a8a7f0 3364 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
3365 ctx->xin->flow.skb_priority = skb_priority;
3366 } else {
3367 /* Couldn't translate queue to a priority. Nothing to do. A warning
3368 * has already been logged. */
3369 }
3370}
3371
3372static bool
46c88433 3373slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 3374{
46c88433
EJ
3375 const struct xbridge *xbridge = xbridge_;
3376 struct xport *port;
9583bc14
EJ
3377
3378 switch (ofp_port) {
3379 case OFPP_IN_PORT:
3380 case OFPP_TABLE:
3381 case OFPP_NORMAL:
3382 case OFPP_FLOOD:
3383 case OFPP_ALL:
3384 case OFPP_NONE:
3385 return true;
3386 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3387 return false;
3388 default:
46c88433 3389 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
3390 return port ? port->may_enable : false;
3391 }
3392}
3393
3394static void
3395xlate_bundle_action(struct xlate_ctx *ctx,
3396 const struct ofpact_bundle *bundle)
3397{
4e022ec0 3398 ofp_port_t port;
9583bc14
EJ
3399
3400 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
46c88433
EJ
3401 slave_enabled_cb,
3402 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 3403 if (bundle->dst.field) {
f74e7df7
JP
3404 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
3405 &ctx->xout->wc);
9583bc14
EJ
3406 } else {
3407 xlate_output_action(ctx, port, 0, false);
3408 }
3409}
3410
3411static void
4165b5e0
JS
3412xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3413 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
9583bc14 3414{
4165b5e0
JS
3415 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3416 if (ctx->xin->may_learn) {
3417 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3418 }
3419}
9583bc14 3420
4165b5e0
JS
3421static void
3422xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3423{
9583bc14 3424 ctx->xout->has_learn = true;
9583bc14
EJ
3425 learn_mask(learn, &ctx->xout->wc);
3426
b256dc52
JS
3427 if (ctx->xin->xcache) {
3428 struct xc_entry *entry;
3429
3430 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4165b5e0
JS
3431 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3432 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3433 entry->u.learn.ofpacts = ofpbuf_new(64);
3434 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3435 entry->u.learn.ofpacts);
3436 } else if (ctx->xin->may_learn) {
3437 uint64_t ofpacts_stub[1024 / 8];
3438 struct ofputil_flow_mod fm;
3439 struct ofpbuf ofpacts;
3440
3441 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3442 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3443 ofpbuf_uninit(&ofpacts);
b256dc52
JS
3444 }
3445}
3446
3447static void
3448xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3449 uint16_t idle_timeout, uint16_t hard_timeout)
3450{
3451 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3452 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
3453 }
9583bc14
EJ
3454}
3455
9583bc14
EJ
3456static void
3457xlate_fin_timeout(struct xlate_ctx *ctx,
3458 const struct ofpact_fin_timeout *oft)
3459{
b256dc52
JS
3460 if (ctx->rule) {
3461 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
3462 oft->fin_idle_timeout, oft->fin_hard_timeout);
3463 if (ctx->xin->xcache) {
3464 struct xc_entry *entry;
3465
3466 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
83709dfa
JR
3467 /* XC_RULE already holds a reference on the rule, none is taken
3468 * here. */
b256dc52
JS
3469 entry->u.fin.rule = ctx->rule;
3470 entry->u.fin.idle = oft->fin_idle_timeout;
3471 entry->u.fin.hard = oft->fin_hard_timeout;
b256dc52 3472 }
9583bc14
EJ
3473 }
3474}
3475
3476static void
3477xlate_sample_action(struct xlate_ctx *ctx,
3478 const struct ofpact_sample *os)
3479{
3480 union user_action_cookie cookie;
3481 /* Scale the probability from 16-bit to 32-bit while representing
3482 * the same percentage. */
3483 uint32_t probability = (os->probability << 16) | os->probability;
3484
4b97b70d
BP
3485 if (!ctx->xbridge->variable_length_userdata) {
3486 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
3487
3488 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
3489 "lacks support (needs Linux 3.10+ or kernel module from "
3490 "OVS 1.11+)");
3491 return;
3492 }
3493
7fd91025 3494 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
cc377352 3495 ctx->xout->odp_actions,
8bfd0fda 3496 &ctx->xout->wc);
9583bc14
EJ
3497
3498 compose_flow_sample_cookie(os->probability, os->collector_set_id,
3499 os->obs_domain_id, os->obs_point_id, &cookie);
cc377352 3500 compose_sample_action(ctx->xbridge, ctx->xout->odp_actions, &ctx->xin->flow,
8b7ea2d4
WZ
3501 probability, &cookie, sizeof cookie.flow_sample,
3502 ODPP_NONE);
9583bc14
EJ
3503}
3504
3505static bool
46c88433 3506may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 3507{
bbbca389 3508 if (xport->config & (is_stp(&ctx->xin->flow)
46c88433
EJ
3509 ? OFPUTIL_PC_NO_RECV_STP
3510 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
3511 return false;
3512 }
3513
3514 /* Only drop packets here if both forwarding and learning are
3515 * disabled. If just learning is enabled, we need to have
3516 * OFPP_NORMAL and the learning action have a look at the packet
3517 * before we can drop it. */
9efd308e
DV
3518 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
3519 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
9583bc14
EJ
3520 return false;
3521 }
3522
3523 return true;
3524}
3525
7fdb60a7
SH
3526static void
3527xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
3528{
3529 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
3530 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
3531 ofpact_pad(&ctx->action_set);
3532}
3533
3534static void
3535xlate_action_set(struct xlate_ctx *ctx)
3536{
3537 uint64_t action_list_stub[1024 / 64];
3538 struct ofpbuf action_list;
3539
3540 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3541 ofpacts_execute_action_set(&action_list, &ctx->action_set);
1f317cb5 3542 do_xlate_actions(ofpbuf_data(&action_list), ofpbuf_size(&action_list), ctx);
7fdb60a7
SH
3543 ofpbuf_uninit(&action_list);
3544}
3545
7bbdd84f
SH
3546static bool
3547ofpact_needs_recirculation_after_mpls(const struct xlate_ctx *ctx,
3548 const struct ofpact *a)
3549{
3550 struct flow_wildcards *wc = &ctx->xout->wc;
3551 struct flow *flow = &ctx->xin->flow;
3552
3553 switch (a->type) {
3554 case OFPACT_OUTPUT:
3555 case OFPACT_GROUP:
3556 case OFPACT_CONTROLLER:
3557 case OFPACT_STRIP_VLAN:
3558 case OFPACT_SET_VLAN_PCP:
3559 case OFPACT_SET_VLAN_VID:
3560 case OFPACT_ENQUEUE:
3561 case OFPACT_PUSH_VLAN:
3562 case OFPACT_SET_ETH_SRC:
3563 case OFPACT_SET_ETH_DST:
3564 case OFPACT_SET_TUNNEL:
3565 case OFPACT_SET_QUEUE:
3566 case OFPACT_POP_QUEUE:
3567 case OFPACT_POP_MPLS:
3568 case OFPACT_DEC_MPLS_TTL:
3569 case OFPACT_SET_MPLS_TTL:
3570 case OFPACT_SET_MPLS_TC:
3571 case OFPACT_SET_MPLS_LABEL:
3572 case OFPACT_NOTE:
3573 case OFPACT_OUTPUT_REG:
3574 case OFPACT_EXIT:
3575 case OFPACT_METER:
3576 case OFPACT_WRITE_METADATA:
3577 case OFPACT_WRITE_ACTIONS:
3578 case OFPACT_CLEAR_ACTIONS:
3579 case OFPACT_SAMPLE:
3580 return false;
3581
3582 case OFPACT_SET_IPV4_SRC:
3583 case OFPACT_SET_IPV4_DST:
3584 case OFPACT_SET_IP_DSCP:
3585 case OFPACT_SET_IP_ECN:
3586 case OFPACT_SET_IP_TTL:
3587 case OFPACT_SET_L4_SRC_PORT:
3588 case OFPACT_SET_L4_DST_PORT:
3589 case OFPACT_RESUBMIT:
3590 case OFPACT_STACK_PUSH:
3591 case OFPACT_STACK_POP:
3592 case OFPACT_DEC_TTL:
3593 case OFPACT_MULTIPATH:
3594 case OFPACT_BUNDLE:
3595 case OFPACT_LEARN:
3596 case OFPACT_FIN_TIMEOUT:
3597 case OFPACT_GOTO_TABLE:
3598 return true;
3599
3600 case OFPACT_REG_MOVE:
3601 return (mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
3602 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
3603
3604 case OFPACT_REG_LOAD:
3605 return mf_is_l3_or_higher(ofpact_get_REG_LOAD(a)->dst.field);
3606
3607 case OFPACT_SET_FIELD:
3608 return mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field);
3609
3610 case OFPACT_PUSH_MPLS:
3611 /* Recirculate if it is an IP packet with a zero ttl. This may
3612 * indicate that the packet was previously MPLS and an MPLS pop action
3613 * converted it to IP. In this case recirculating should reveal the IP
3614 * TTL which is used as the basis for a new MPLS LSE. */
3615 return (!flow_count_mpls_labels(flow, wc)
3616 && flow->nw_ttl == 0
3617 && is_ip_any(flow));
3618 }
3619
3620 OVS_NOT_REACHED();
3621}
3622
9583bc14
EJ
3623static void
3624do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
3625 struct xlate_ctx *ctx)
3626{
33bf9176
BP
3627 struct flow_wildcards *wc = &ctx->xout->wc;
3628 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
3629 const struct ofpact *a;
3630
f47ea021
JR
3631 /* dl_type already in the mask, not set below. */
3632
9583bc14
EJ
3633 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
3634 struct ofpact_controller *controller;
3635 const struct ofpact_metadata *metadata;
b2dd70be
JR
3636 const struct ofpact_set_field *set_field;
3637 const struct mf_field *mf;
9583bc14
EJ
3638
3639 if (ctx->exit) {
3640 break;
3641 }
3642
7bbdd84f
SH
3643 if (ctx->was_mpls && ofpact_needs_recirculation_after_mpls(ctx, a)) {
3644 compose_recirculate_action(ctx, ofpacts, a, ofpacts_len);
3645 return;
3646 }
3647
9583bc14
EJ
3648 switch (a->type) {
3649 case OFPACT_OUTPUT:
3650 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
3651 ofpact_get_OUTPUT(a)->max_len, true);
3652 break;
3653
7395c052 3654 case OFPACT_GROUP:
f4fb341b
SH
3655 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
3656 return;
3657 }
7395c052
NZ
3658 break;
3659
9583bc14
EJ
3660 case OFPACT_CONTROLLER:
3661 controller = ofpact_get_CONTROLLER(a);
3662 execute_controller_action(ctx, controller->max_len,
3663 controller->reason,
3664 controller->controller_id);
3665 break;
3666
3667 case OFPACT_ENQUEUE:
3668 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
3669 break;
3670
3671 case OFPACT_SET_VLAN_VID:
f74e7df7 3672 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
ca287d20
JR
3673 if (flow->vlan_tci & htons(VLAN_CFI) ||
3674 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
3675 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
3676 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
3677 | htons(VLAN_CFI));
3678 }
9583bc14
EJ
3679 break;
3680
3681 case OFPACT_SET_VLAN_PCP:
f74e7df7 3682 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
ca287d20
JR
3683 if (flow->vlan_tci & htons(VLAN_CFI) ||
3684 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
3685 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
3686 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
3687 << VLAN_PCP_SHIFT) | VLAN_CFI);
3688 }
9583bc14
EJ
3689 break;
3690
3691 case OFPACT_STRIP_VLAN:
f74e7df7 3692 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 3693 flow->vlan_tci = htons(0);
9583bc14
EJ
3694 break;
3695
3696 case OFPACT_PUSH_VLAN:
3697 /* XXX 802.1AD(QinQ) */
f74e7df7 3698 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 3699 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
3700 break;
3701
3702 case OFPACT_SET_ETH_SRC:
f74e7df7 3703 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
33bf9176 3704 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
3705 break;
3706
3707 case OFPACT_SET_ETH_DST:
f74e7df7 3708 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
33bf9176 3709 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
9583bc14
EJ
3710 break;
3711
3712 case OFPACT_SET_IPV4_SRC:
33bf9176 3713 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 3714 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 3715 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
3716 }
3717 break;
3718
3719 case OFPACT_SET_IPV4_DST:
33bf9176 3720 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 3721 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 3722 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
3723 }
3724 break;
3725
04f01c24
BP
3726 case OFPACT_SET_IP_DSCP:
3727 if (is_ip_any(flow)) {
f47ea021 3728 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 3729 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 3730 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
3731 }
3732 break;
3733
ff14eb7a
JR
3734 case OFPACT_SET_IP_ECN:
3735 if (is_ip_any(flow)) {
3736 wc->masks.nw_tos |= IP_ECN_MASK;
3737 flow->nw_tos &= ~IP_ECN_MASK;
3738 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
3739 }
3740 break;
3741
0c20dbe4
JR
3742 case OFPACT_SET_IP_TTL:
3743 if (is_ip_any(flow)) {
3744 wc->masks.nw_ttl = 0xff;
3745 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
3746 }
3747 break;
3748
9583bc14 3749 case OFPACT_SET_L4_SRC_PORT:
33bf9176 3750 if (is_ip_any(flow)) {
f47ea021
JR
3751 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3752 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 3753 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
3754 }
3755 break;
3756
3757 case OFPACT_SET_L4_DST_PORT:
33bf9176 3758 if (is_ip_any(flow)) {
f47ea021
JR
3759 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
3760 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 3761 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
3762 }
3763 break;
3764
3765 case OFPACT_RESUBMIT:
3766 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
3767 break;
3768
3769 case OFPACT_SET_TUNNEL:
33bf9176 3770 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
3771 break;
3772
3773 case OFPACT_SET_QUEUE:
3774 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
3775 break;
3776
3777 case OFPACT_POP_QUEUE:
33bf9176 3778 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
3779 break;
3780
3781 case OFPACT_REG_MOVE:
33bf9176 3782 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
3783 break;
3784
3785 case OFPACT_REG_LOAD:
f47ea021 3786 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
9583bc14
EJ
3787 break;
3788
b2dd70be
JR
3789 case OFPACT_SET_FIELD:
3790 set_field = ofpact_get_SET_FIELD(a);
3791 mf = set_field->field;
b2dd70be
JR
3792
3793 /* Set field action only ever overwrites packet's outermost
3794 * applicable header fields. Do nothing if no header exists. */
4479846e
JR
3795 if (mf->id == MFF_VLAN_VID) {
3796 wc->masks.vlan_tci |= htons(VLAN_CFI);
3797 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
3798 break;
3799 }
3800 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
3801 /* 'dl_type' is already unwildcarded. */
3802 && !eth_type_mpls(flow->dl_type)) {
3803 break;
b2dd70be 3804 }
4479846e
JR
3805
3806 mf_mask_field_and_prereqs(mf, &wc->masks);
3807 mf_set_flow_value(mf, &set_field->value, flow);
b2dd70be
JR
3808 break;
3809
9583bc14 3810 case OFPACT_STACK_PUSH:
33bf9176
BP
3811 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
3812 &ctx->stack);
9583bc14
EJ
3813 break;
3814
3815 case OFPACT_STACK_POP:
f74e7df7
JP
3816 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
3817 &ctx->stack);
9583bc14
EJ
3818 break;
3819
3820 case OFPACT_PUSH_MPLS:
8bfd0fda 3821 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
3822 break;
3823
3824 case OFPACT_POP_MPLS:
8bfd0fda 3825 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
3826 break;
3827
097d4939 3828 case OFPACT_SET_MPLS_LABEL:
8bfd0fda
BP
3829 compose_set_mpls_label_action(
3830 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
3831 break;
097d4939
JR
3832
3833 case OFPACT_SET_MPLS_TC:
8bfd0fda 3834 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
3835 break;
3836
9583bc14 3837 case OFPACT_SET_MPLS_TTL:
8bfd0fda 3838 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
3839 break;
3840
3841 case OFPACT_DEC_MPLS_TTL:
9cfef3d0 3842 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 3843 return;
9583bc14
EJ
3844 }
3845 break;
3846
3847 case OFPACT_DEC_TTL:
f74e7df7 3848 wc->masks.nw_ttl = 0xff;
9583bc14 3849 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 3850 return;
9583bc14
EJ
3851 }
3852 break;
3853
3854 case OFPACT_NOTE:
3855 /* Nothing to do. */
3856 break;
3857
3858 case OFPACT_MULTIPATH:
33bf9176 3859 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
3860 break;
3861
3862 case OFPACT_BUNDLE:
9583bc14
EJ
3863 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
3864 break;
3865
3866 case OFPACT_OUTPUT_REG:
3867 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
3868 break;
3869
3870 case OFPACT_LEARN:
3871 xlate_learn_action(ctx, ofpact_get_LEARN(a));
3872 break;
3873
3874 case OFPACT_EXIT:
3875 ctx->exit = true;
3876 break;
3877
3878 case OFPACT_FIN_TIMEOUT:
33bf9176 3879 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
3880 ctx->xout->has_fin_timeout = true;
3881 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
3882 break;
3883
3884 case OFPACT_CLEAR_ACTIONS:
7fdb60a7
SH
3885 ofpbuf_clear(&ctx->action_set);
3886 break;
3887
3888 case OFPACT_WRITE_ACTIONS:
3889 xlate_write_actions(ctx, a);
9583bc14
EJ
3890 break;
3891
3892 case OFPACT_WRITE_METADATA:
3893 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
3894 flow->metadata &= ~metadata->mask;
3895 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
3896 break;
3897
638a19b0
JR
3898 case OFPACT_METER:
3899 /* Not implemented yet. */
3900 break;
3901
9583bc14 3902 case OFPACT_GOTO_TABLE: {
9583bc14 3903 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14 3904
7bbdd84f
SH
3905 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
3906 * than ogt->table_id. This is to allow goto_table actions that
3907 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
3908 * after recirculation. */
3909 ovs_assert(ctx->table_id == TBL_INTERNAL
3910 || ctx->table_id < ogt->table_id);
4468099e 3911 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 3912 ogt->table_id, true, true);
9583bc14
EJ
3913 break;
3914 }
3915
3916 case OFPACT_SAMPLE:
3917 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
3918 break;
3919 }
3920 }
9583bc14
EJ
3921}
3922
3923void
3924xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
cc377352
EJ
3925 const struct flow *flow, ofp_port_t in_port,
3926 struct rule_dpif *rule, uint16_t tcp_flags,
3927 const struct ofpbuf *packet)
9583bc14
EJ
3928{
3929 xin->ofproto = ofproto;
3930 xin->flow = *flow;
cc377352 3931 xin->flow.in_port.ofp_port = in_port;
9583bc14
EJ
3932 xin->packet = packet;
3933 xin->may_learn = packet != NULL;
3934 xin->rule = rule;
b256dc52 3935 xin->xcache = NULL;
9583bc14
EJ
3936 xin->ofpacts = NULL;
3937 xin->ofpacts_len = 0;
3938 xin->tcp_flags = tcp_flags;
3939 xin->resubmit_hook = NULL;
3940 xin->report_hook = NULL;
3941 xin->resubmit_stats = NULL;
3f207910 3942 xin->skip_wildcards = false;
cc377352 3943 xin->odp_actions = NULL;
9583bc14
EJ
3944}
3945
3946void
3947xlate_out_uninit(struct xlate_out *xout)
3948{
cc377352
EJ
3949 if (xout && xout->odp_actions == &xout->odp_actions_buf) {
3950 ofpbuf_uninit(xout->odp_actions);
9583bc14
EJ
3951 }
3952}
3953
3954/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
3955 * into datapath actions, using 'ctx', and discards the datapath actions. */
3956void
3957xlate_actions_for_side_effects(struct xlate_in *xin)
3958{
3959 struct xlate_out xout;
3960
3961 xlate_actions(xin, &xout);
3962 xlate_out_uninit(&xout);
3963}
3964
3965static void
3966xlate_report(struct xlate_ctx *ctx, const char *s)
3967{
3968 if (ctx->xin->report_hook) {
4d0acc70 3969 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
9583bc14
EJ
3970 }
3971}
3972
3973void
3974xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
3975{
3976 dst->wc = src->wc;
9583bc14
EJ
3977 dst->slow = src->slow;
3978 dst->has_learn = src->has_learn;
3979 dst->has_normal = src->has_normal;
3980 dst->has_fin_timeout = src->has_fin_timeout;
3981 dst->nf_output_iface = src->nf_output_iface;
3982 dst->mirrors = src->mirrors;
3983
cc377352
EJ
3984 dst->odp_actions = &dst->odp_actions_buf;
3985 ofpbuf_use_stub(dst->odp_actions, dst->odp_actions_stub,
9583bc14 3986 sizeof dst->odp_actions_stub);
cc377352
EJ
3987 ofpbuf_put(dst->odp_actions, ofpbuf_data(src->odp_actions),
3988 ofpbuf_size(src->odp_actions));
9583bc14
EJ
3989}
3990\f
55954f6e
EJ
3991static struct skb_priority_to_dscp *
3992get_skb_priority(const struct xport *xport, uint32_t skb_priority)
3993{
3994 struct skb_priority_to_dscp *pdscp;
3995 uint32_t hash;
3996
3997 hash = hash_int(skb_priority, 0);
3998 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
3999 if (pdscp->skb_priority == skb_priority) {
4000 return pdscp;
4001 }
4002 }
4003 return NULL;
4004}
4005
4006static bool
4007dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4008 uint8_t *dscp)
4009{
4010 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4011 *dscp = pdscp ? pdscp->dscp : 0;
4012 return pdscp != NULL;
4013}
4014
4015static void
4016clear_skb_priorities(struct xport *xport)
4017{
4018 struct skb_priority_to_dscp *pdscp, *next;
4019
4020 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4021 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4022 free(pdscp);
4023 }
4024}
4025
ce4a6b76
BP
4026static bool
4027actions_output_to_local_port(const struct xlate_ctx *ctx)
4028{
46c88433 4029 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
4030 const struct nlattr *a;
4031 unsigned int left;
4032
cc377352
EJ
4033 NL_ATTR_FOR_EACH_UNSAFE (a, left, ofpbuf_data(ctx->xout->odp_actions),
4034 ofpbuf_size(ctx->xout->odp_actions)) {
ce4a6b76
BP
4035 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4036 && nl_attr_get_odp_port(a) == local_odp_port) {
4037 return true;
4038 }
4039 }
4040 return false;
4041}
9583bc14
EJ
4042
4043/* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
56450a41
BP
4044 * into datapath actions in 'odp_actions', using 'ctx'.
4045 *
4046 * The caller must take responsibility for eventually freeing 'xout', with
4047 * xlate_out_uninit(). */
84f0f298
RW
4048void
4049xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
9583bc14 4050{
84f0f298 4051 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
33bf9176
BP
4052 struct flow_wildcards *wc = &xout->wc;
4053 struct flow *flow = &xin->flow;
10c44245 4054 struct rule_dpif *rule = NULL;
33bf9176 4055
dc723c44 4056 const struct rule_actions *actions = NULL;
9583bc14
EJ
4057 enum slow_path_reason special;
4058 const struct ofpact *ofpacts;
46c88433 4059 struct xport *in_port;
9583bc14
EJ
4060 struct flow orig_flow;
4061 struct xlate_ctx ctx;
4062 size_t ofpacts_len;
62a7cc71 4063 bool tnl_may_send;
c56fac1b 4064 bool is_icmp;
9583bc14 4065
46c88433 4066 COVERAGE_INC(xlate_actions);
9583bc14
EJ
4067
4068 /* Flow initialization rules:
4069 * - 'base_flow' must match the kernel's view of the packet at the
4070 * time that action processing starts. 'flow' represents any
4071 * transformations we wish to make through actions.
4072 * - By default 'base_flow' and 'flow' are the same since the input
4073 * packet matches the output before any actions are applied.
4074 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
4075 * of the received packet as seen by the kernel. If we later output
4076 * to another device without any modifications this will cause us to
4077 * insert a new tag since the original one was stripped off by the
4078 * VLAN device.
4079 * - Tunnel metadata as received is retained in 'flow'. This allows
4080 * tunnel metadata matching also in later tables.
4081 * Since a kernel action for setting the tunnel metadata will only be
4082 * generated with actual tunnel output, changing the tunnel metadata
4083 * values in 'flow' (such as tun_id) will only have effect with a later
4084 * tunnel output action.
4085 * - Tunnel 'base_flow' is completely cleared since that is what the
4086 * kernel does. If we wish to maintain the original values an action
4087 * needs to be generated. */
4088
4089 ctx.xin = xin;
4090 ctx.xout = xout;
46c88433
EJ
4091 ctx.xout->slow = 0;
4092 ctx.xout->has_learn = false;
4093 ctx.xout->has_normal = false;
4094 ctx.xout->has_fin_timeout = false;
4095 ctx.xout->nf_output_iface = NF_OUT_DROP;
4096 ctx.xout->mirrors = 0;
cc377352
EJ
4097
4098 xout->odp_actions = xin->odp_actions;
4099 if (!xout->odp_actions) {
4100 xout->odp_actions = &xout->odp_actions_buf;
4101 ofpbuf_use_stub(xout->odp_actions, xout->odp_actions_stub,
4102 sizeof xout->odp_actions_stub);
4103 }
4104 ofpbuf_reserve(xout->odp_actions, NL_A_U32_SIZE);
46c88433 4105
84f0f298 4106 ctx.xbridge = xbridge_lookup(xcfg, xin->ofproto);
46c88433 4107 if (!ctx.xbridge) {
83709dfa 4108 return;
46c88433 4109 }
9583bc14 4110
9583bc14
EJ
4111 ctx.rule = xin->rule;
4112
33bf9176 4113 ctx.base_flow = *flow;
9583bc14 4114 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
33bf9176 4115 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
9583bc14 4116
33bf9176
BP
4117 flow_wildcards_init_catchall(wc);
4118 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
1dd35f8a 4119 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
7431e171 4120 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
0934ebba
BP
4121 if (is_ip_any(flow)) {
4122 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4123 }
c56fac1b 4124 is_icmp = is_icmpv4(flow) || is_icmpv6(flow);
9583bc14 4125
62a7cc71 4126 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
ce3955be 4127 if (ctx.xbridge->netflow) {
9b658910 4128 netflow_mask_wc(flow, wc);
9583bc14
EJ
4129 }
4130
9583bc14 4131 ctx.recurse = 0;
98b07853 4132 ctx.resubmits = 0;
5a070238 4133 ctx.in_group = false;
33bf9176 4134 ctx.orig_skb_priority = flow->skb_priority;
9583bc14
EJ
4135 ctx.table_id = 0;
4136 ctx.exit = false;
92c08f09 4137 ctx.use_recirc = false;
7bbdd84f 4138 ctx.was_mpls = false;
9583bc14 4139
10c44245 4140 if (!xin->ofpacts && !ctx.rule) {
6d328fa2
SH
4141 ctx.table_id = rule_dpif_lookup(ctx.xbridge->ofproto, flow,
4142 !xin->skip_wildcards ? wc : NULL,
d611866c
SH
4143 &rule, ctx.xin->xcache != NULL,
4144 ctx.xin->resubmit_stats);
10c44245
EJ
4145 if (ctx.xin->resubmit_stats) {
4146 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
4147 }
b256dc52
JS
4148 if (ctx.xin->xcache) {
4149 struct xc_entry *entry;
4150
4151 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
b256dc52
JS
4152 entry->u.rule = rule;
4153 }
10c44245
EJ
4154 ctx.rule = rule;
4155 }
ee1afdd5 4156 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
10c44245 4157
9583bc14
EJ
4158 if (xin->ofpacts) {
4159 ofpacts = xin->ofpacts;
4160 ofpacts_len = xin->ofpacts_len;
10c44245
EJ
4161 } else if (ctx.rule) {
4162 actions = rule_dpif_get_actions(ctx.rule);
6f00e29b
BP
4163 ofpacts = actions->ofpacts;
4164 ofpacts_len = actions->ofpacts_len;
9583bc14 4165 } else {
428b2edd 4166 OVS_NOT_REACHED();
9583bc14
EJ
4167 }
4168
4169 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
7fdb60a7
SH
4170 ofpbuf_use_stub(&ctx.action_set,
4171 ctx.action_set_stub, sizeof ctx.action_set_stub);
9583bc14 4172
ade6ad9c 4173 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
4174 /* Do this conditionally because the copy is expensive enough that it
4175 * shows up in profiles. */
33bf9176 4176 orig_flow = *flow;
9583bc14
EJ
4177 }
4178
33bf9176 4179 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
46c88433 4180 switch (ctx.xbridge->frag) {
9583bc14
EJ
4181 case OFPC_FRAG_NORMAL:
4182 /* We must pretend that transport ports are unavailable. */
33bf9176
BP
4183 flow->tp_src = ctx.base_flow.tp_src = htons(0);
4184 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
9583bc14
EJ
4185 break;
4186
4187 case OFPC_FRAG_DROP:
83709dfa 4188 return;
9583bc14
EJ
4189
4190 case OFPC_FRAG_REASM:
428b2edd 4191 OVS_NOT_REACHED();
9583bc14
EJ
4192
4193 case OFPC_FRAG_NX_MATCH:
4194 /* Nothing to do. */
4195 break;
4196
4197 case OFPC_INVALID_TTL_TO_CONTROLLER:
428b2edd 4198 OVS_NOT_REACHED();
9583bc14
EJ
4199 }
4200 }
4201
46c88433 4202 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
b256dc52
JS
4203 if (in_port && in_port->is_tunnel) {
4204 if (ctx.xin->resubmit_stats) {
4205 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
4206 if (in_port->bfd) {
4207 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
4208 }
4209 }
4210 if (ctx.xin->xcache) {
4211 struct xc_entry *entry;
4212
4213 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
4214 entry->u.dev.rx = netdev_ref(in_port->netdev);
4215 entry->u.dev.bfd = bfd_ref(in_port->bfd);
d6fc5f57
EJ
4216 }
4217 }
4218
642dc74d 4219 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
9583bc14 4220 if (special) {
04594cd5 4221 ctx.xout->slow |= special;
9583bc14 4222 } else {
9583bc14 4223 size_t sample_actions_len;
9583bc14 4224
4e022ec0 4225 if (flow->in_port.ofp_port
46c88433
EJ
4226 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
4227 flow->in_port.ofp_port,
33bf9176 4228 flow->vlan_tci)) {
9583bc14
EJ
4229 ctx.base_flow.vlan_tci = 0;
4230 }
4231
4232 add_sflow_action(&ctx);
4233 add_ipfix_action(&ctx);
cc377352 4234 sample_actions_len = ofpbuf_size(ctx.xout->odp_actions);
9583bc14 4235
62a7cc71 4236 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
9583bc14
EJ
4237 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
4238
4239 /* We've let OFPP_NORMAL and the learning action look at the
4240 * packet, so drop it now if forwarding is disabled. */
9efd308e
DV
4241 if (in_port && (!xport_stp_forward_state(in_port) ||
4242 !xport_rstp_forward_state(in_port))) {
cc377352 4243 ofpbuf_set_size(ctx.xout->odp_actions, sample_actions_len);
9583bc14
EJ
4244 }
4245 }
4246
1f317cb5 4247 if (ofpbuf_size(&ctx.action_set)) {
7fdb60a7
SH
4248 xlate_action_set(&ctx);
4249 }
4250
46c88433 4251 if (ctx.xbridge->has_in_band
ce4a6b76
BP
4252 && in_band_must_output_to_local_port(flow)
4253 && !actions_output_to_local_port(&ctx)) {
9583bc14
EJ
4254 compose_output_action(&ctx, OFPP_LOCAL);
4255 }
aaa0fbae
EJ
4256
4257 fix_sflow_action(&ctx);
4258
46c88433 4259 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
9583bc14
EJ
4260 add_mirror_actions(&ctx, &orig_flow);
4261 }
9583bc14
EJ
4262 }
4263
cc377352 4264 if (nl_attr_oversized(ofpbuf_size(ctx.xout->odp_actions))) {
542024c4 4265 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
4266 * can't hand them to the kernel directly. dpif_execute() can execute
4267 * them one by one with help, so just mark the result as SLOW_ACTION to
4268 * prevent the flow from being installed. */
4269 COVERAGE_INC(xlate_actions_oversize);
4270 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
4271 }
4272
b256dc52
JS
4273 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
4274 if (ctx.xin->resubmit_stats) {
4275 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
4276 ctx.xin->resubmit_stats->n_packets,
4277 ctx.xin->resubmit_stats->n_bytes);
4278 }
4279 if (ctx.xin->xcache) {
4280 struct xc_entry *entry;
4281
4282 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_MIRROR);
4283 entry->u.mirror.mbridge = mbridge_ref(ctx.xbridge->mbridge);
4284 entry->u.mirror.mirrors = xout->mirrors;
4285 }
4286 }
4287
4288 if (ctx.xbridge->netflow) {
b256dc52
JS
4289 /* Only update netflow if we don't have controller flow. We don't
4290 * report NetFlow expiration messages for such facets because they
4291 * are just part of the control logic for the network, not real
4292 * traffic. */
4293 if (ofpacts_len == 0
4294 || ofpacts->type != OFPACT_CONTROLLER
4295 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
4296 if (ctx.xin->resubmit_stats) {
d6fc5f57
EJ
4297 netflow_flow_update(ctx.xbridge->netflow, flow,
4298 xout->nf_output_iface,
4299 ctx.xin->resubmit_stats);
4300 }
b256dc52
JS
4301 if (ctx.xin->xcache) {
4302 struct xc_entry *entry;
4303
4304 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
4305 entry->u.nf.netflow = netflow_ref(ctx.xbridge->netflow);
4306 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
4307 entry->u.nf.iface = xout->nf_output_iface;
4308 }
d6fc5f57
EJ
4309 }
4310 }
4311
9583bc14 4312 ofpbuf_uninit(&ctx.stack);
7fdb60a7 4313 ofpbuf_uninit(&ctx.action_set);
9583bc14
EJ
4314
4315 /* Clear the metadata and register wildcard masks, because we won't
4316 * use non-header fields as part of the cache. */
c11c6faa 4317 flow_wildcards_clear_non_packet_fields(wc);
dc24a00f 4318
c56fac1b
BP
4319 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow uses
4320 * the low 8 bits of the 16-bit tp_src and tp_dst members to represent
4321 * these fields. The datapath interface, on the other hand, represents
4322 * them with just 8 bits each. This means that if the high 8 bits of the
4323 * masks for these fields somehow become set, then they will get chopped
4324 * off by a round trip through the datapath, and revalidation will spot
4325 * that as an inconsistency and delete the flow. Avoid the problem here by
4326 * making sure that only the low 8 bits of either field can be unwildcarded
4327 * for ICMP.
4328 */
4329 if (is_icmp) {
4330 wc->masks.tp_src &= htons(UINT8_MAX);
4331 wc->masks.tp_dst &= htons(UINT8_MAX);
4332 }
91d6cd12
AW
4333}
4334
4335/* Sends 'packet' out 'ofport'.
4336 * May modify 'packet'.
4337 * Returns 0 if successful, otherwise a positive errno value. */
4338int
4339xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4340{
84f0f298 4341 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
91d6cd12 4342 struct xport *xport;
91d6cd12 4343 struct ofpact_output output;
91d6cd12 4344 struct flow flow;
91d6cd12 4345
91d6cd12
AW
4346 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
4347 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
b5e7e61a
AZ
4348 flow_extract(packet, NULL, &flow);
4349 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12 4350
84f0f298 4351 xport = xport_lookup(xcfg, ofport);
91d6cd12 4352 if (!xport) {
02ea2703 4353 return EINVAL;
91d6cd12 4354 }
91d6cd12
AW
4355 output.port = xport->ofp_port;
4356 output.max_len = 0;
e491a67a
YT
4357
4358 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
4359 &output.ofpact, sizeof output,
4360 packet);
9583bc14 4361}
b256dc52
JS
4362
4363struct xlate_cache *
4364xlate_cache_new(void)
4365{
4366 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
4367
4368 ofpbuf_init(&xcache->entries, 512);
4369 return xcache;
4370}
4371
4372static struct xc_entry *
4373xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
4374{
4375 struct xc_entry *entry;
4376
4377 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
4378 entry->type = type;
4379
4380 return entry;
4381}
4382
4383static void
4384xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
4385{
4386 if (entry->u.dev.tx) {
4387 netdev_vport_inc_tx(entry->u.dev.tx, stats);
4388 }
4389 if (entry->u.dev.rx) {
4390 netdev_vport_inc_rx(entry->u.dev.rx, stats);
4391 }
4392 if (entry->u.dev.bfd) {
4393 bfd_account_rx(entry->u.dev.bfd, stats);
4394 }
4395}
4396
4397static void
4398xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
4399{
84f0f298 4400 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
b256dc52
JS
4401 struct xbridge *xbridge;
4402 struct xbundle *xbundle;
4403 struct flow_wildcards wc;
4404
84f0f298 4405 xbridge = xbridge_lookup(xcfg, ofproto);
b256dc52
JS
4406 if (!xbridge) {
4407 return;
4408 }
4409
4410 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
4411 NULL);
4412 if (!xbundle) {
4413 return;
4414 }
4415
4416 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
4417}
4418
4419/* Push stats and perform side effects of flow translation. */
4420void
0725e747 4421xlate_push_stats(struct xlate_cache *xcache,
b256dc52
JS
4422 const struct dpif_flow_stats *stats)
4423{
4424 struct xc_entry *entry;
4425 struct ofpbuf entries = xcache->entries;
4426
bc388583
BP
4427 if (!stats->n_packets) {
4428 return;
4429 }
4430
b256dc52
JS
4431 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4432 switch (entry->type) {
4433 case XC_RULE:
4434 rule_dpif_credit_stats(entry->u.rule, stats);
4435 break;
4436 case XC_BOND:
4437 bond_account(entry->u.bond.bond, entry->u.bond.flow,
4438 entry->u.bond.vid, stats->n_bytes);
4439 break;
4440 case XC_NETDEV:
4441 xlate_cache_netdev(entry, stats);
4442 break;
4443 case XC_NETFLOW:
4444 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
4445 entry->u.nf.iface, stats);
4446 break;
4447 case XC_MIRROR:
4448 mirror_update_stats(entry->u.mirror.mbridge,
4449 entry->u.mirror.mirrors,
4450 stats->n_packets, stats->n_bytes);
4451 break;
4452 case XC_LEARN:
0725e747 4453 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
b256dc52
JS
4454 break;
4455 case XC_NORMAL:
0725e747
BP
4456 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
4457 entry->u.normal.vlan);
b256dc52
JS
4458 break;
4459 case XC_FIN_TIMEOUT:
4460 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
4461 entry->u.fin.idle, entry->u.fin.hard);
4462 break;
1e684d7d
RW
4463 case XC_GROUP:
4464 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
4465 stats);
4466 break;
b256dc52
JS
4467 default:
4468 OVS_NOT_REACHED();
4469 }
4470 }
4471}
4472
4473static void
4474xlate_dev_unref(struct xc_entry *entry)
4475{
4476 if (entry->u.dev.tx) {
4477 netdev_close(entry->u.dev.tx);
4478 }
4479 if (entry->u.dev.rx) {
4480 netdev_close(entry->u.dev.rx);
4481 }
4482 if (entry->u.dev.bfd) {
4483 bfd_unref(entry->u.dev.bfd);
4484 }
4485}
4486
4487static void
4488xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
4489{
b256dc52
JS
4490 netflow_flow_clear(netflow, flow);
4491 netflow_unref(netflow);
4492 free(flow);
4493}
4494
4495void
4496xlate_cache_clear(struct xlate_cache *xcache)
4497{
4498 struct xc_entry *entry;
4499 struct ofpbuf entries;
4500
4501 if (!xcache) {
4502 return;
4503 }
4504
4505 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
4506 switch (entry->type) {
4507 case XC_RULE:
4508 rule_dpif_unref(entry->u.rule);
4509 break;
4510 case XC_BOND:
4511 free(entry->u.bond.flow);
4512 bond_unref(entry->u.bond.bond);
4513 break;
4514 case XC_NETDEV:
4515 xlate_dev_unref(entry);
4516 break;
4517 case XC_NETFLOW:
4518 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
4519 break;
4520 case XC_MIRROR:
4521 mbridge_unref(entry->u.mirror.mbridge);
4522 break;
4523 case XC_LEARN:
4165b5e0
JS
4524 free(entry->u.learn.fm);
4525 ofpbuf_delete(entry->u.learn.ofpacts);
b256dc52
JS
4526 break;
4527 case XC_NORMAL:
4528 free(entry->u.normal.flow);
4529 break;
4530 case XC_FIN_TIMEOUT:
83709dfa
JR
4531 /* 'u.fin.rule' is always already held as a XC_RULE, which
4532 * has already released it's reference above. */
b256dc52 4533 break;
1e684d7d
RW
4534 case XC_GROUP:
4535 group_dpif_unref(entry->u.group.group);
4536 break;
b256dc52
JS
4537 default:
4538 OVS_NOT_REACHED();
4539 }
4540 }
4541
4542 ofpbuf_clear(&xcache->entries);
4543}
4544
4545void
4546xlate_cache_delete(struct xlate_cache *xcache)
4547{
4548 xlate_cache_clear(xcache);
4549 ofpbuf_uninit(&xcache->entries);
4550 free(xcache);
4551}