]> git.proxmox.com Git - ovs.git/blame - ofproto/ofproto-dpif-xlate.c
ofproto-dpif-rid: Use separate pointers for actions and action set.
[ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
9bfe9334 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 19#include <errno.h>
a36de779
PS
20#include <arpa/inet.h>
21#include <net/if.h>
22#include <sys/socket.h>
23#include <netinet/in.h>
8449c4d6 24
53902038 25#include "tnl-neigh-cache.h"
db7d4e46 26#include "bfd.h"
9583bc14
EJ
27#include "bitmap.h"
28#include "bond.h"
29#include "bundle.h"
30#include "byte-order.h"
db7d4e46 31#include "cfm.h"
9583bc14
EJ
32#include "connmgr.h"
33#include "coverage.h"
e14deea0 34#include "dp-packet.h"
9583bc14
EJ
35#include "dpif.h"
36#include "dynamic-string.h"
f7f1ea29 37#include "in-band.h"
db7d4e46 38#include "lacp.h"
9583bc14 39#include "learn.h"
46c88433 40#include "list.h"
0477baa9 41#include "ovs-lldp.h"
9583bc14 42#include "mac-learning.h"
6d95c4e8 43#include "mcast-snooping.h"
9583bc14
EJ
44#include "meta-flow.h"
45#include "multipath.h"
46#include "netdev-vport.h"
47#include "netlink.h"
48#include "nx-match.h"
49#include "odp-execute.h"
50#include "ofp-actions.h"
51#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 52#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 53#include "ofproto/ofproto-dpif-monitor.h"
9583bc14
EJ
54#include "ofproto/ofproto-dpif-sflow.h"
55#include "ofproto/ofproto-dpif.h"
6f00e29b 56#include "ofproto/ofproto-provider.h"
c2b878e0 57#include "packets.h"
a36de779
PS
58#include "ovs-router.h"
59#include "tnl-ports.h"
9583bc14 60#include "tunnel.h"
e6211adc 61#include "openvswitch/vlog.h"
9583bc14 62
46c88433 63COVERAGE_DEFINE(xlate_actions);
0f032e95 64COVERAGE_DEFINE(xlate_actions_oversize);
7d031d7e 65COVERAGE_DEFINE(xlate_actions_too_many_output);
9583bc14
EJ
66
67VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
68
8a553e9a
EJ
69/* Maximum depth of flow table recursion (due to resubmit actions) in a
70 * flow translation. */
71#define MAX_RESUBMIT_RECURSION 64
adcf00ba
AZ
72#define MAX_INTERNAL_RESUBMITS 1 /* Max resbmits allowed using rules in
73 internal table. */
8a553e9a 74
98b07853
BP
75/* Maximum number of resubmit actions in a flow translation, whether they are
76 * recursive or not. */
77#define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
78
46c88433
EJ
79struct xbridge {
80 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
81 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
82
ca6ba700 83 struct ovs_list xbundles; /* Owned xbundles. */
46c88433
EJ
84 struct hmap xports; /* Indexed by ofp_port. */
85
86 char *name; /* Name used in log messages. */
89a8a7f0 87 struct dpif *dpif; /* Datapath interface. */
46c88433 88 struct mac_learning *ml; /* Mac learning handle. */
6d95c4e8 89 struct mcast_snooping *ms; /* Multicast Snooping handle. */
46c88433
EJ
90 struct mbridge *mbridge; /* Mirroring. */
91 struct dpif_sflow *sflow; /* SFlow handle, or null. */
92 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 93 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 94 struct stp *stp; /* STP or null if disabled. */
9efd308e 95 struct rstp *rstp; /* RSTP or null if disabled. */
46c88433 96
46c88433
EJ
97 bool has_in_band; /* Bridge has in band control? */
98 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 99
b440dd8c
JS
100 /* Datapath feature support. */
101 struct dpif_backer_support support;
46c88433
EJ
102};
103
104struct xbundle {
105 struct hmap_node hmap_node; /* In global 'xbundles' map. */
106 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
107
ca6ba700 108 struct ovs_list list_node; /* In parent 'xbridges' list. */
46c88433
EJ
109 struct xbridge *xbridge; /* Parent xbridge. */
110
ca6ba700 111 struct ovs_list xports; /* Contains "struct xport"s. */
46c88433
EJ
112
113 char *name; /* Name used in log messages. */
114 struct bond *bond; /* Nonnull iff more than one port. */
115 struct lacp *lacp; /* LACP handle or null. */
116
117 enum port_vlan_mode vlan_mode; /* VLAN mode. */
118 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
119 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
120 * NULL if all VLANs are trunked. */
121 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
122 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
123};
124
125struct xport {
126 struct hmap_node hmap_node; /* Node in global 'xports' map. */
127 struct ofport_dpif *ofport; /* Key in global 'xports map. */
128
129 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
130 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
131
132 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
133
ca6ba700 134 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
46c88433
EJ
135 struct xbundle *xbundle; /* Parent xbundle or null. */
136
137 struct netdev *netdev; /* 'ofport''s netdev. */
138
139 struct xbridge *xbridge; /* Parent bridge. */
140 struct xport *peer; /* Patch port peer or null. */
141
142 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 143 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 144 int stp_port_no; /* STP port number or -1 if not in use. */
f025bcb7 145 struct rstp_port *rstp_port; /* RSTP port or null. */
46c88433 146
55954f6e
EJ
147 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
148
46c88433
EJ
149 bool may_enable; /* May be enabled in bonds. */
150 bool is_tunnel; /* Is a tunnel port. */
151
152 struct cfm *cfm; /* CFM handle or null. */
153 struct bfd *bfd; /* BFD handle or null. */
0477baa9 154 struct lldp *lldp; /* LLDP handle or null. */
46c88433
EJ
155};
156
4d0acc70
EJ
157struct xlate_ctx {
158 struct xlate_in *xin;
159 struct xlate_out *xout;
160
46c88433 161 const struct xbridge *xbridge;
4d0acc70 162
621b8064 163 /* Flow tables version at the beginning of the translation. */
18721c4a 164 cls_version_t tables_version;
621b8064 165
4d0acc70
EJ
166 /* Flow at the last commit. */
167 struct flow base_flow;
168
169 /* Tunnel IP destination address as received. This is stored separately
170 * as the base_flow.tunnel is cleared on init to reflect the datapath
171 * behavior. Used to make sure not to send tunneled output to ourselves,
172 * which might lead to an infinite loop. This could happen easily
173 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
174 * actually set the tun_dst field. */
e4d3706c 175 struct in6_addr orig_tunnel_ipv6_dst;
4d0acc70
EJ
176
177 /* Stack for the push and pop actions. Each stack element is of type
178 * "union mf_subvalue". */
4d0acc70
EJ
179 struct ofpbuf stack;
180
181 /* The rule that we are currently translating, or NULL. */
182 struct rule_dpif *rule;
183
49a73e0c
BP
184 /* Flow translation populates this with wildcards relevant in translation.
185 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
186 * null, this is a pointer to uninitialized scratch memory. This allows
187 * code to blindly write to 'ctx->wc' without worrying about whether the
188 * caller really wants wildcards. */
189 struct flow_wildcards *wc;
190
1520ef4f
BP
191 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
192 * this is the same pointer. When 'xin->odp_actions' is null, this points
193 * to a scratch ofpbuf. This allows code to add actions to
194 * 'ctx->odp_actions' without worrying about whether the caller really
195 * wants actions. */
196 struct ofpbuf *odp_actions;
197
98b07853
BP
198 /* Resubmit statistics, via xlate_table_action(). */
199 int recurse; /* Current resubmit nesting depth. */
200 int resubmits; /* Total number of resubmits. */
5a070238 201 bool in_group; /* Currently translating ofgroup, if true. */
029ca940 202 bool in_action_set; /* Currently translating action_set, if true. */
98b07853 203
4d0acc70 204 uint8_t table_id; /* OpenFlow table ID where flow was found. */
8b1e5560
JR
205 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
206 uint32_t orig_skb_priority; /* Priority when packet arrived. */
4d0acc70 207 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 208 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
2031ef97 209 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
4d0acc70 210 bool exit; /* No further actions should be processed. */
3d6151f3 211 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
7fdb60a7 212
e672ff9b
JR
213 /* These are used for non-bond recirculation. The recirculation IDs are
214 * stored in xout and must be associated with a datapath flow (ukey),
215 * otherwise they will be freed when the xout is uninitialized.
216 *
217 *
218 * Steps in Recirculation Translation
219 * ==================================
220 *
221 * At some point during translation, the code recognizes the need for
222 * recirculation. For example, recirculation is necessary when, after
223 * popping the last MPLS label, an action or a match tries to examine or
224 * modify a field that has been newly revealed following the MPLS label.
225 *
226 * The simplest part of the work to be done is to commit existing changes to
227 * the packet, which produces datapath actions corresponding to the changes,
228 * and after this, add an OVS_ACTION_ATTR_RECIRC datapath action.
229 *
230 * The main problem here is preserving state. When the datapath executes
231 * OVS_ACTION_ATTR_RECIRC, it will upcall to userspace to get a translation
232 * for the post-recirculation actions. At this point userspace has to
233 * resume the translation where it left off, which means that it has to
234 * execute the following:
235 *
236 * - The action that prompted recirculation, and any actions following
237 * it within the same flow.
238 *
239 * - If the action that prompted recirculation was invoked within a
240 * NXAST_RESUBMIT, then any actions following the resubmit. These
241 * "resubmit"s can be nested, so this has to go all the way up the
242 * control stack.
243 *
244 * - The OpenFlow 1.1+ action set.
245 *
246 * State that actions and flow table lookups can depend on, such as the
247 * following, must also be preserved:
248 *
249 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
250 *
251 * - Action set, stack
252 *
253 * - The table ID and cookie of the flow being translated at each level
254 * of the control stack (since OFPAT_CONTROLLER actions send these to
255 * the controller).
256 *
257 * Translation allows for the control of this state preservation via these
258 * members. When a need for recirculation is identified, the translation
259 * process:
260 *
261 * 1. Sets 'recirc_action_offset' to the current size of 'action_set'. The
262 * action set is part of what needs to be preserved, so this allows the
263 * action set and the additional state to share the 'action_set' buffer.
264 * Later steps can tell that setup for recirculation is in progress from
265 * the nonnegative value of 'recirc_action_offset'.
266 *
267 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
268 * translation process.
269 *
270 * 3. Adds an OFPACT_UNROLL_XLATE action to 'action_set'. This action
271 * holds the current table ID and cookie so that they can be restored
272 * during a post-recirculation upcall translation.
273 *
274 * 4. Adds the action that prompted recirculation and any actions following
275 * it within the same flow to 'action_set', so that they can be executed
276 * during a post-recirculation upcall translation.
277 *
278 * 5. Returns.
279 *
280 * 6. The action that prompted recirculation might be nested in a stack of
281 * nested "resubmit"s that have actions remaining. Each of these notices
282 * that we're exiting (from 'exit') and that recirculation setup is in
283 * progress (from 'recirc_action_offset') and responds by adding more
284 * OFPACT_UNROLL_XLATE actions to 'action_set', as necessary, and any
285 * actions that were yet unprocessed.
286 *
287 * The caller stores all the state produced by this process associated with
288 * the recirculation ID. For post-recirculation upcall translation, the
289 * caller passes it back in for the new translation to execute. The
290 * process yielded a set of ofpacts that can be translated directly, so it
291 * is not much of a special case at that point.
292 */
293 int recirc_action_offset; /* Offset in 'action_set' to actions to be
294 * executed after recirculation, or -1. */
295 int last_unroll_offset; /* Offset in 'action_set' to the latest unroll
296 * action, or -1. */
297
7bbdd84f
SH
298 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
299 * This is a trigger for recirculation in cases where translating an action
300 * or looking up a flow requires access to the fields of the packet after
1d741d6d 301 * the MPLS label stack that was originally present. */
7bbdd84f
SH
302 bool was_mpls;
303
07659514
JS
304 /* True if conntrack has been performed on this packet during processing
305 * on the current bridge. This is used to determine whether conntrack
306 * state from the datapath should be honored after recirculation. */
307 bool conntracked;
308
9ac0aada
JR
309 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
310 struct ofpact_nat *ct_nat_action;
311
7fdb60a7
SH
312 /* OpenFlow 1.1+ action set.
313 *
314 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
315 * When translation is otherwise complete, ofpacts_execute_action_set()
316 * converts it to a set of "struct ofpact"s that can be translated into
ed9c9e3e 317 * datapath actions. */
c61f3870 318 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
7fdb60a7 319 struct ofpbuf action_set; /* Action set. */
fff1b9c0
JR
320
321 enum xlate_error error; /* Translation failed. */
4d0acc70
EJ
322};
323
fff1b9c0
JR
324const char *xlate_strerror(enum xlate_error error)
325{
326 switch (error) {
327 case XLATE_OK:
328 return "OK";
329 case XLATE_BRIDGE_NOT_FOUND:
330 return "Bridge not found";
331 case XLATE_RECURSION_TOO_DEEP:
332 return "Recursion too deep";
333 case XLATE_TOO_MANY_RESUBMITS:
334 return "Too many resubmits";
335 case XLATE_STACK_TOO_DEEP:
336 return "Stack too deep";
337 case XLATE_NO_RECIRCULATION_CONTEXT:
338 return "No recirculation context";
339 case XLATE_RECIRCULATION_CONFLICT:
340 return "Recirculation conflict";
341 case XLATE_TOO_MANY_MPLS_LABELS:
342 return "Too many MPLS labels";
343 }
344 return "Unknown error";
345}
346
ed9c9e3e 347static void xlate_action_set(struct xlate_ctx *ctx);
704bb0bf 348static void xlate_commit_actions(struct xlate_ctx *ctx);
ed9c9e3e 349
1d741d6d
JR
350static void
351ctx_trigger_recirculation(struct xlate_ctx *ctx)
352{
353 ctx->exit = true;
354 ctx->recirc_action_offset = ctx->action_set.size;
355}
356
357static bool
358ctx_first_recirculation_action(const struct xlate_ctx *ctx)
359{
360 return ctx->recirc_action_offset == ctx->action_set.size;
361}
362
e672ff9b
JR
363static inline bool
364exit_recirculates(const struct xlate_ctx *ctx)
365{
366 /* When recirculating the 'recirc_action_offset' has a non-negative value.
367 */
368 return ctx->recirc_action_offset >= 0;
369}
370
371static void compose_recirculate_action(struct xlate_ctx *ctx);
372
9583bc14
EJ
373/* A controller may use OFPP_NONE as the ingress port to indicate that
374 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
375 * when an input bundle is needed for validation (e.g., mirroring or
376 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
377 * any 'port' structs, so care must be taken when dealing with it. */
378static struct xbundle ofpp_none_bundle = {
379 .name = "OFPP_NONE",
380 .vlan_mode = PORT_VLAN_TRUNK
381};
9583bc14 382
55954f6e
EJ
383/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
384 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
385 * traffic egressing the 'ofport' with that priority should be marked with. */
386struct skb_priority_to_dscp {
387 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
388 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
389
390 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
391};
392
b256dc52
JS
393enum xc_type {
394 XC_RULE,
395 XC_BOND,
396 XC_NETDEV,
397 XC_NETFLOW,
398 XC_MIRROR,
399 XC_LEARN,
400 XC_NORMAL,
401 XC_FIN_TIMEOUT,
1e684d7d 402 XC_GROUP,
53902038 403 XC_TNL_NEIGH,
b256dc52
JS
404};
405
406/* xlate_cache entries hold enough information to perform the side effects of
407 * xlate_actions() for a rule, without needing to perform rule translation
408 * from scratch. The primary usage of these is to submit statistics to objects
409 * that a flow relates to, although they may be used for other effects as well
410 * (for instance, refreshing hard timeouts for learned flows). */
411struct xc_entry {
412 enum xc_type type;
413 union {
414 struct rule_dpif *rule;
415 struct {
416 struct netdev *tx;
417 struct netdev *rx;
418 struct bfd *bfd;
419 } dev;
420 struct {
421 struct netflow *netflow;
422 struct flow *flow;
423 ofp_port_t iface;
424 } nf;
425 struct {
426 struct mbridge *mbridge;
427 mirror_mask_t mirrors;
428 } mirror;
429 struct {
430 struct bond *bond;
431 struct flow *flow;
432 uint16_t vid;
433 } bond;
434 struct {
4165b5e0
JS
435 struct ofproto_dpif *ofproto;
436 struct ofputil_flow_mod *fm;
437 struct ofpbuf *ofpacts;
b256dc52
JS
438 } learn;
439 struct {
440 struct ofproto_dpif *ofproto;
441 struct flow *flow;
442 int vlan;
443 } normal;
444 struct {
445 struct rule_dpif *rule;
446 uint16_t idle;
447 uint16_t hard;
448 } fin;
1e684d7d
RW
449 struct {
450 struct group_dpif *group;
451 struct ofputil_bucket *bucket;
452 } group;
a36de779
PS
453 struct {
454 char br_name[IFNAMSIZ];
0b411137 455 struct in6_addr d_ipv6;
53902038 456 } tnl_neigh_cache;
b256dc52
JS
457 } u;
458};
459
460#define XC_ENTRY_FOR_EACH(entry, entries, xcache) \
461 entries = xcache->entries; \
462 for (entry = ofpbuf_try_pull(&entries, sizeof *entry); \
463 entry; \
464 entry = ofpbuf_try_pull(&entries, sizeof *entry))
465
466struct xlate_cache {
467 struct ofpbuf entries;
468};
469
84f0f298
RW
470/* Xlate config contains hash maps of all bridges, bundles and ports.
471 * Xcfgp contains the pointer to the current xlate configuration.
472 * When the main thread needs to change the configuration, it copies xcfgp to
473 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
474 * does not block handler and revalidator threads. */
475struct xlate_cfg {
476 struct hmap xbridges;
477 struct hmap xbundles;
478 struct hmap xports;
479};
b1b72f2d 480static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
f439f23b 481static struct xlate_cfg *new_xcfg = NULL;
46c88433
EJ
482
483static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
484static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
485 struct xlate_ctx *);
adcf00ba 486static void xlate_normal(struct xlate_ctx *);
c1b3756c
BP
487static inline void xlate_report(struct xlate_ctx *, const char *, ...)
488 OVS_PRINTF_FORMAT(2, 3);
6d328fa2
SH
489static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
490 uint8_t table_id, bool may_packet_in,
491 bool honor_table_miss);
46c88433
EJ
492static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
493static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
494static void output_normal(struct xlate_ctx *, const struct xbundle *,
9583bc14 495 uint16_t vlan);
e93ef1c7
JR
496
497/* Optional bond recirculation parameter to compose_output_action(). */
498struct xlate_bond_recirc {
499 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
500 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
501 uint32_t hash_basis; /* Compute hash for recirc before. */
502};
503
504static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
505 const struct xlate_bond_recirc *xr);
9583bc14 506
84f0f298
RW
507static struct xbridge *xbridge_lookup(struct xlate_cfg *,
508 const struct ofproto_dpif *);
509static struct xbundle *xbundle_lookup(struct xlate_cfg *,
510 const struct ofbundle *);
511static struct xport *xport_lookup(struct xlate_cfg *,
512 const struct ofport_dpif *);
46c88433 513static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
514static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
515 uint32_t skb_priority);
516static void clear_skb_priorities(struct xport *);
16194afd 517static size_t count_skb_priorities(const struct xport *);
55954f6e
EJ
518static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
519 uint8_t *dscp);
46c88433 520
b256dc52
JS
521static struct xc_entry *xlate_cache_add_entry(struct xlate_cache *xc,
522 enum xc_type type);
84f0f298
RW
523static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
524static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
525static void xlate_xport_init(struct xlate_cfg *, struct xport *);
9efd308e 526static void xlate_xbridge_set(struct xbridge *, struct dpif *,
9efd308e
DV
527 const struct mac_learning *, struct stp *,
528 struct rstp *, const struct mcast_snooping *,
529 const struct mbridge *,
530 const struct dpif_sflow *,
531 const struct dpif_ipfix *,
2f47cdf4 532 const struct netflow *,
84f0f298 533 bool forward_bpdu, bool has_in_band,
b440dd8c 534 const struct dpif_backer_support *);
84f0f298
RW
535static void xlate_xbundle_set(struct xbundle *xbundle,
536 enum port_vlan_mode vlan_mode, int vlan,
537 unsigned long *trunks, bool use_priority_tags,
538 const struct bond *bond, const struct lacp *lacp,
539 bool floodable);
540static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
541 const struct netdev *netdev, const struct cfm *cfm,
0477baa9
DF
542 const struct bfd *bfd, const struct lldp *lldp,
543 int stp_port_no, const struct rstp_port *rstp_port,
84f0f298
RW
544 enum ofputil_port_config config,
545 enum ofputil_port_state state, bool is_tunnel,
546 bool may_enable);
547static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
548static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
549static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
550static void xlate_xbridge_copy(struct xbridge *);
551static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
552static void xlate_xport_copy(struct xbridge *, struct xbundle *,
553 struct xport *);
554static void xlate_xcfg_free(struct xlate_cfg *);
b256dc52 555
34dd0d78 556static inline void
c1b3756c 557xlate_report(struct xlate_ctx *ctx, const char *format, ...)
34dd0d78
JR
558{
559 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
c1b3756c
BP
560 va_list args;
561
562 va_start(args, format);
563 ctx->xin->report_hook(ctx->xin, ctx->recurse, format, args);
564 va_end(args);
34dd0d78
JR
565 }
566}
84f0f298 567
fff1b9c0
JR
568static struct vlog_rate_limit error_report_rl = VLOG_RATE_LIMIT_INIT(1, 5);
569
570#define XLATE_REPORT_ERROR(CTX, ...) \
571 do { \
572 if (OVS_UNLIKELY((CTX)->xin->report_hook)) { \
573 xlate_report(CTX, __VA_ARGS__); \
574 } else { \
575 VLOG_ERR_RL(&error_report_rl, __VA_ARGS__); \
576 } \
577 } while (0)
578
d6bef3cc
BP
579static inline void
580xlate_report_actions(struct xlate_ctx *ctx, const char *title,
581 const struct ofpact *ofpacts, size_t ofpacts_len)
582{
583 if (OVS_UNLIKELY(ctx->xin->report_hook)) {
584 struct ds s = DS_EMPTY_INITIALIZER;
585 ofpacts_format(ofpacts, ofpacts_len, &s);
586 xlate_report(ctx, "%s: %s", title, ds_cstr(&s));
587 ds_destroy(&s);
588 }
589}
590
84f0f298
RW
591static void
592xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
593{
594 list_init(&xbridge->xbundles);
595 hmap_init(&xbridge->xports);
596 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
597 hash_pointer(xbridge->ofproto, 0));
598}
599
600static void
601xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
602{
603 list_init(&xbundle->xports);
604 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
605 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
606 hash_pointer(xbundle->ofbundle, 0));
607}
608
609static void
610xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
611{
612 hmap_init(&xport->skb_priorities);
613 hmap_insert(&xcfg->xports, &xport->hmap_node,
614 hash_pointer(xport->ofport, 0));
615 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
616 hash_ofp_port(xport->ofp_port));
617}
618
619static void
620xlate_xbridge_set(struct xbridge *xbridge,
621 struct dpif *dpif,
ec89fc6f 622 const struct mac_learning *ml, struct stp *stp,
9efd308e 623 struct rstp *rstp, const struct mcast_snooping *ms,
ec89fc6f 624 const struct mbridge *mbridge,
46c88433 625 const struct dpif_sflow *sflow,
ce3955be 626 const struct dpif_ipfix *ipfix,
2f47cdf4 627 const struct netflow *netflow,
4b97b70d 628 bool forward_bpdu, bool has_in_band,
b440dd8c 629 const struct dpif_backer_support *support)
46c88433 630{
46c88433
EJ
631 if (xbridge->ml != ml) {
632 mac_learning_unref(xbridge->ml);
633 xbridge->ml = mac_learning_ref(ml);
634 }
635
6d95c4e8
FL
636 if (xbridge->ms != ms) {
637 mcast_snooping_unref(xbridge->ms);
638 xbridge->ms = mcast_snooping_ref(ms);
639 }
640
46c88433
EJ
641 if (xbridge->mbridge != mbridge) {
642 mbridge_unref(xbridge->mbridge);
643 xbridge->mbridge = mbridge_ref(mbridge);
644 }
645
646 if (xbridge->sflow != sflow) {
647 dpif_sflow_unref(xbridge->sflow);
648 xbridge->sflow = dpif_sflow_ref(sflow);
649 }
650
651 if (xbridge->ipfix != ipfix) {
652 dpif_ipfix_unref(xbridge->ipfix);
653 xbridge->ipfix = dpif_ipfix_ref(ipfix);
654 }
655
9d189a50
EJ
656 if (xbridge->stp != stp) {
657 stp_unref(xbridge->stp);
658 xbridge->stp = stp_ref(stp);
659 }
660
9efd308e
DV
661 if (xbridge->rstp != rstp) {
662 rstp_unref(xbridge->rstp);
663 xbridge->rstp = rstp_ref(rstp);
664 }
665
ce3955be
EJ
666 if (xbridge->netflow != netflow) {
667 netflow_unref(xbridge->netflow);
668 xbridge->netflow = netflow_ref(netflow);
669 }
670
89a8a7f0 671 xbridge->dpif = dpif;
46c88433
EJ
672 xbridge->forward_bpdu = forward_bpdu;
673 xbridge->has_in_band = has_in_band;
b440dd8c 674 xbridge->support = *support;
46c88433
EJ
675}
676
84f0f298
RW
677static void
678xlate_xbundle_set(struct xbundle *xbundle,
679 enum port_vlan_mode vlan_mode, int vlan,
680 unsigned long *trunks, bool use_priority_tags,
681 const struct bond *bond, const struct lacp *lacp,
682 bool floodable)
683{
684 ovs_assert(xbundle->xbridge);
685
686 xbundle->vlan_mode = vlan_mode;
687 xbundle->vlan = vlan;
688 xbundle->trunks = trunks;
689 xbundle->use_priority_tags = use_priority_tags;
690 xbundle->floodable = floodable;
691
692 if (xbundle->bond != bond) {
693 bond_unref(xbundle->bond);
694 xbundle->bond = bond_ref(bond);
695 }
696
697 if (xbundle->lacp != lacp) {
698 lacp_unref(xbundle->lacp);
699 xbundle->lacp = lacp_ref(lacp);
700 }
701}
702
703static void
704xlate_xport_set(struct xport *xport, odp_port_t odp_port,
705 const struct netdev *netdev, const struct cfm *cfm,
0477baa9 706 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
f025bcb7 707 const struct rstp_port* rstp_port,
84f0f298
RW
708 enum ofputil_port_config config, enum ofputil_port_state state,
709 bool is_tunnel, bool may_enable)
710{
711 xport->config = config;
712 xport->state = state;
713 xport->stp_port_no = stp_port_no;
714 xport->is_tunnel = is_tunnel;
715 xport->may_enable = may_enable;
716 xport->odp_port = odp_port;
717
f025bcb7
JR
718 if (xport->rstp_port != rstp_port) {
719 rstp_port_unref(xport->rstp_port);
720 xport->rstp_port = rstp_port_ref(rstp_port);
721 }
722
84f0f298
RW
723 if (xport->cfm != cfm) {
724 cfm_unref(xport->cfm);
725 xport->cfm = cfm_ref(cfm);
726 }
727
728 if (xport->bfd != bfd) {
729 bfd_unref(xport->bfd);
730 xport->bfd = bfd_ref(bfd);
731 }
732
0477baa9
DF
733 if (xport->lldp != lldp) {
734 lldp_unref(xport->lldp);
735 xport->lldp = lldp_ref(lldp);
736 }
737
84f0f298
RW
738 if (xport->netdev != netdev) {
739 netdev_close(xport->netdev);
740 xport->netdev = netdev_ref(netdev);
741 }
742}
743
744static void
745xlate_xbridge_copy(struct xbridge *xbridge)
746{
747 struct xbundle *xbundle;
748 struct xport *xport;
749 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
750 new_xbridge->ofproto = xbridge->ofproto;
751 new_xbridge->name = xstrdup(xbridge->name);
752 xlate_xbridge_init(new_xcfg, new_xbridge);
753
754 xlate_xbridge_set(new_xbridge,
34dd0d78 755 xbridge->dpif, xbridge->ml, xbridge->stp,
9efd308e
DV
756 xbridge->rstp, xbridge->ms, xbridge->mbridge,
757 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
b440dd8c
JS
758 xbridge->forward_bpdu, xbridge->has_in_band,
759 &xbridge->support);
84f0f298
RW
760 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
761 xlate_xbundle_copy(new_xbridge, xbundle);
762 }
763
764 /* Copy xports which are not part of a xbundle */
765 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
766 if (!xport->xbundle) {
767 xlate_xport_copy(new_xbridge, NULL, xport);
768 }
769 }
770}
771
772static void
773xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
774{
775 struct xport *xport;
776 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
777 new_xbundle->ofbundle = xbundle->ofbundle;
778 new_xbundle->xbridge = xbridge;
779 new_xbundle->name = xstrdup(xbundle->name);
780 xlate_xbundle_init(new_xcfg, new_xbundle);
781
782 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode,
783 xbundle->vlan, xbundle->trunks,
784 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
785 xbundle->floodable);
786 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
787 xlate_xport_copy(xbridge, new_xbundle, xport);
788 }
789}
790
791static void
792xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
793 struct xport *xport)
794{
795 struct skb_priority_to_dscp *pdscp, *new_pdscp;
796 struct xport *new_xport = xzalloc(sizeof *xport);
797 new_xport->ofport = xport->ofport;
798 new_xport->ofp_port = xport->ofp_port;
799 new_xport->xbridge = xbridge;
800 xlate_xport_init(new_xcfg, new_xport);
801
802 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
0477baa9
DF
803 xport->bfd, xport->lldp, xport->stp_port_no,
804 xport->rstp_port, xport->config, xport->state,
805 xport->is_tunnel, xport->may_enable);
84f0f298
RW
806
807 if (xport->peer) {
808 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
809 if (peer) {
810 new_xport->peer = peer;
811 new_xport->peer->peer = new_xport;
812 }
813 }
814
815 if (xbundle) {
816 new_xport->xbundle = xbundle;
817 list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
818 }
819
820 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
821 new_pdscp = xmalloc(sizeof *pdscp);
822 new_pdscp->skb_priority = pdscp->skb_priority;
823 new_pdscp->dscp = pdscp->dscp;
824 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
825 hash_int(new_pdscp->skb_priority, 0));
826 }
827}
828
829/* Sets the current xlate configuration to new_xcfg and frees the old xlate
830 * configuration in xcfgp.
831 *
832 * This needs to be called after editing the xlate configuration.
833 *
834 * Functions that edit the new xlate configuration are
835 * xlate_<ofport/bundle/ofport>_set and xlate_<ofport/bundle/ofport>_remove.
836 *
837 * A sample workflow:
838 *
839 * xlate_txn_start();
840 * ...
841 * edit_xlate_configuration();
842 * ...
843 * xlate_txn_commit(); */
46c88433 844void
84f0f298
RW
845xlate_txn_commit(void)
846{
847 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
848
849 ovsrcu_set(&xcfgp, new_xcfg);
40a9c4c2
AW
850 ovsrcu_synchronize();
851 xlate_xcfg_free(xcfg);
84f0f298
RW
852 new_xcfg = NULL;
853}
854
855/* Copies the current xlate configuration in xcfgp to new_xcfg.
856 *
857 * This needs to be called prior to editing the xlate configuration. */
858void
859xlate_txn_start(void)
860{
861 struct xbridge *xbridge;
862 struct xlate_cfg *xcfg;
863
864 ovs_assert(!new_xcfg);
865
866 new_xcfg = xmalloc(sizeof *new_xcfg);
867 hmap_init(&new_xcfg->xbridges);
868 hmap_init(&new_xcfg->xbundles);
869 hmap_init(&new_xcfg->xports);
870
871 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
872 if (!xcfg) {
873 return;
874 }
875
876 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
877 xlate_xbridge_copy(xbridge);
878 }
879}
880
881
882static void
883xlate_xcfg_free(struct xlate_cfg *xcfg)
884{
885 struct xbridge *xbridge, *next_xbridge;
886
887 if (!xcfg) {
888 return;
889 }
890
891 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
892 xlate_xbridge_remove(xcfg, xbridge);
893 }
894
895 hmap_destroy(&xcfg->xbridges);
896 hmap_destroy(&xcfg->xbundles);
897 hmap_destroy(&xcfg->xports);
898 free(xcfg);
899}
900
901void
902xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
34dd0d78 903 struct dpif *dpif,
84f0f298 904 const struct mac_learning *ml, struct stp *stp,
9efd308e 905 struct rstp *rstp, const struct mcast_snooping *ms,
84f0f298
RW
906 const struct mbridge *mbridge,
907 const struct dpif_sflow *sflow,
908 const struct dpif_ipfix *ipfix,
2f47cdf4 909 const struct netflow *netflow,
b440dd8c
JS
910 bool forward_bpdu, bool has_in_band,
911 const struct dpif_backer_support *support)
84f0f298
RW
912{
913 struct xbridge *xbridge;
914
915 ovs_assert(new_xcfg);
916
917 xbridge = xbridge_lookup(new_xcfg, ofproto);
918 if (!xbridge) {
919 xbridge = xzalloc(sizeof *xbridge);
920 xbridge->ofproto = ofproto;
921
922 xlate_xbridge_init(new_xcfg, xbridge);
923 }
924
925 free(xbridge->name);
926 xbridge->name = xstrdup(name);
927
34dd0d78 928 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
b440dd8c 929 netflow, forward_bpdu, has_in_band, support);
84f0f298
RW
930}
931
932static void
933xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
46c88433 934{
46c88433
EJ
935 struct xbundle *xbundle, *next_xbundle;
936 struct xport *xport, *next_xport;
937
938 if (!xbridge) {
939 return;
940 }
941
942 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
84f0f298 943 xlate_xport_remove(xcfg, xport);
46c88433
EJ
944 }
945
946 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
84f0f298 947 xlate_xbundle_remove(xcfg, xbundle);
46c88433
EJ
948 }
949
84f0f298 950 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
795cc5c1 951 mac_learning_unref(xbridge->ml);
6d95c4e8 952 mcast_snooping_unref(xbridge->ms);
795cc5c1
EJ
953 mbridge_unref(xbridge->mbridge);
954 dpif_sflow_unref(xbridge->sflow);
955 dpif_ipfix_unref(xbridge->ipfix);
956 stp_unref(xbridge->stp);
9efd308e 957 rstp_unref(xbridge->rstp);
795cc5c1 958 hmap_destroy(&xbridge->xports);
46c88433
EJ
959 free(xbridge->name);
960 free(xbridge);
961}
962
84f0f298
RW
963void
964xlate_remove_ofproto(struct ofproto_dpif *ofproto)
965{
966 struct xbridge *xbridge;
967
968 ovs_assert(new_xcfg);
969
970 xbridge = xbridge_lookup(new_xcfg, ofproto);
971 xlate_xbridge_remove(new_xcfg, xbridge);
972}
973
46c88433
EJ
974void
975xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
976 const char *name, enum port_vlan_mode vlan_mode, int vlan,
977 unsigned long *trunks, bool use_priority_tags,
978 const struct bond *bond, const struct lacp *lacp,
979 bool floodable)
980{
84f0f298 981 struct xbundle *xbundle;
46c88433 982
84f0f298
RW
983 ovs_assert(new_xcfg);
984
985 xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
986 if (!xbundle) {
987 xbundle = xzalloc(sizeof *xbundle);
988 xbundle->ofbundle = ofbundle;
84f0f298 989 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433 990
84f0f298 991 xlate_xbundle_init(new_xcfg, xbundle);
46c88433
EJ
992 }
993
46c88433
EJ
994 free(xbundle->name);
995 xbundle->name = xstrdup(name);
996
84f0f298
RW
997 xlate_xbundle_set(xbundle, vlan_mode, vlan, trunks,
998 use_priority_tags, bond, lacp, floodable);
46c88433
EJ
999}
1000
84f0f298
RW
1001static void
1002xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
46c88433 1003{
5f03c983 1004 struct xport *xport;
46c88433
EJ
1005
1006 if (!xbundle) {
1007 return;
1008 }
1009
5f03c983 1010 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
46c88433
EJ
1011 xport->xbundle = NULL;
1012 }
1013
84f0f298 1014 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
46c88433
EJ
1015 list_remove(&xbundle->list_node);
1016 bond_unref(xbundle->bond);
1017 lacp_unref(xbundle->lacp);
1018 free(xbundle->name);
1019 free(xbundle);
1020}
1021
84f0f298
RW
1022void
1023xlate_bundle_remove(struct ofbundle *ofbundle)
1024{
1025 struct xbundle *xbundle;
1026
1027 ovs_assert(new_xcfg);
1028
1029 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1030 xlate_xbundle_remove(new_xcfg, xbundle);
1031}
1032
46c88433
EJ
1033void
1034xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1035 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1036 odp_port_t odp_port, const struct netdev *netdev,
1037 const struct cfm *cfm, const struct bfd *bfd,
0477baa9
DF
1038 const struct lldp *lldp, struct ofport_dpif *peer,
1039 int stp_port_no, const struct rstp_port *rstp_port,
55954f6e 1040 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
1041 enum ofputil_port_config config,
1042 enum ofputil_port_state state, bool is_tunnel,
9d189a50 1043 bool may_enable)
46c88433 1044{
55954f6e 1045 size_t i;
84f0f298
RW
1046 struct xport *xport;
1047
1048 ovs_assert(new_xcfg);
46c88433 1049
84f0f298 1050 xport = xport_lookup(new_xcfg, ofport);
46c88433
EJ
1051 if (!xport) {
1052 xport = xzalloc(sizeof *xport);
1053 xport->ofport = ofport;
84f0f298 1054 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433
EJ
1055 xport->ofp_port = ofp_port;
1056
84f0f298 1057 xlate_xport_init(new_xcfg, xport);
46c88433
EJ
1058 }
1059
1060 ovs_assert(xport->ofp_port == ofp_port);
1061
0477baa9
DF
1062 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1063 stp_port_no, rstp_port, config, state, is_tunnel,
1064 may_enable);
46c88433
EJ
1065
1066 if (xport->peer) {
1067 xport->peer->peer = NULL;
1068 }
84f0f298 1069 xport->peer = xport_lookup(new_xcfg, peer);
46c88433
EJ
1070 if (xport->peer) {
1071 xport->peer->peer = xport;
1072 }
1073
1074 if (xport->xbundle) {
1075 list_remove(&xport->bundle_node);
1076 }
84f0f298 1077 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
1078 if (xport->xbundle) {
1079 list_insert(&xport->xbundle->xports, &xport->bundle_node);
1080 }
55954f6e
EJ
1081
1082 clear_skb_priorities(xport);
1083 for (i = 0; i < n_qdscp; i++) {
1084 struct skb_priority_to_dscp *pdscp;
1085 uint32_t skb_priority;
1086
89a8a7f0
EJ
1087 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1088 &skb_priority)) {
55954f6e
EJ
1089 continue;
1090 }
1091
1092 pdscp = xmalloc(sizeof *pdscp);
1093 pdscp->skb_priority = skb_priority;
1094 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1095 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1096 hash_int(pdscp->skb_priority, 0));
1097 }
46c88433
EJ
1098}
1099
84f0f298
RW
1100static void
1101xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
46c88433 1102{
46c88433
EJ
1103 if (!xport) {
1104 return;
1105 }
1106
1107 if (xport->peer) {
1108 xport->peer->peer = NULL;
1109 xport->peer = NULL;
1110 }
1111
e621a12d
EJ
1112 if (xport->xbundle) {
1113 list_remove(&xport->bundle_node);
1114 }
1115
55954f6e
EJ
1116 clear_skb_priorities(xport);
1117 hmap_destroy(&xport->skb_priorities);
1118
84f0f298 1119 hmap_remove(&xcfg->xports, &xport->hmap_node);
46c88433
EJ
1120 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1121
1122 netdev_close(xport->netdev);
f025bcb7 1123 rstp_port_unref(xport->rstp_port);
46c88433
EJ
1124 cfm_unref(xport->cfm);
1125 bfd_unref(xport->bfd);
0477baa9 1126 lldp_unref(xport->lldp);
46c88433
EJ
1127 free(xport);
1128}
1129
84f0f298
RW
1130void
1131xlate_ofport_remove(struct ofport_dpif *ofport)
1132{
1133 struct xport *xport;
1134
1135 ovs_assert(new_xcfg);
1136
1137 xport = xport_lookup(new_xcfg, ofport);
1138 xlate_xport_remove(new_xcfg, xport);
1139}
1140
ef377a58
JR
1141static struct ofproto_dpif *
1142xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1143 ofp_port_t *ofp_in_port, const struct xport **xportp)
1144{
e672ff9b 1145 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
ef377a58 1146 const struct xport *xport;
f9038ef6 1147
e672ff9b
JR
1148 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1149 ? tnl_port_receive(flow)
1150 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1151 if (OVS_UNLIKELY(!xport)) {
1152 return NULL;
ef377a58 1153 }
e672ff9b 1154 *xportp = xport;
f9038ef6 1155 if (ofp_in_port) {
e672ff9b 1156 *ofp_in_port = xport->ofp_port;
f9038ef6 1157 }
e672ff9b 1158 return xport->xbridge->ofproto;
ef377a58
JR
1159}
1160
1161/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1162 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1163struct ofproto_dpif *
1164xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1165 ofp_port_t *ofp_in_port)
1166{
1167 const struct xport *xport;
1168
1169 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1170}
1171
cc377352 1172/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
ef377a58 1173 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
cc377352 1174 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
dcc2c6cd
JR
1175 * handles for those protocols if they're enabled. Caller may use the returned
1176 * pointers until quiescing, for longer term use additional references must
1177 * be taken.
8449c4d6 1178 *
f9038ef6 1179 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
ef377a58 1180 */
8449c4d6 1181int
5c476ea3
JR
1182xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1183 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1184 struct dpif_sflow **sflow, struct netflow **netflow,
1185 ofp_port_t *ofp_in_port)
8449c4d6 1186{
ef377a58 1187 struct ofproto_dpif *ofproto;
84f0f298 1188 const struct xport *xport;
8449c4d6 1189
ef377a58 1190 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
8449c4d6 1191
f9038ef6 1192 if (!ofproto) {
cc377352 1193 return ENODEV;
8449c4d6 1194 }
8449c4d6 1195
ef377a58
JR
1196 if (ofprotop) {
1197 *ofprotop = ofproto;
8449c4d6
EJ
1198 }
1199
1dfdb9b3 1200 if (ipfix) {
f9038ef6 1201 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1dfdb9b3
EJ
1202 }
1203
1204 if (sflow) {
f9038ef6 1205 *sflow = xport ? xport->xbridge->sflow : NULL;
1dfdb9b3
EJ
1206 }
1207
1208 if (netflow) {
f9038ef6 1209 *netflow = xport ? xport->xbridge->netflow : NULL;
1dfdb9b3 1210 }
f9038ef6 1211
cc377352 1212 return 0;
8449c4d6
EJ
1213}
1214
46c88433 1215static struct xbridge *
84f0f298 1216xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
46c88433 1217{
84f0f298 1218 struct hmap *xbridges;
46c88433
EJ
1219 struct xbridge *xbridge;
1220
84f0f298 1221 if (!ofproto || !xcfg) {
5e6af486
EJ
1222 return NULL;
1223 }
1224
84f0f298
RW
1225 xbridges = &xcfg->xbridges;
1226
46c88433 1227 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
84f0f298 1228 xbridges) {
46c88433
EJ
1229 if (xbridge->ofproto == ofproto) {
1230 return xbridge;
1231 }
1232 }
1233 return NULL;
1234}
1235
1236static struct xbundle *
84f0f298 1237xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
46c88433 1238{
84f0f298 1239 struct hmap *xbundles;
46c88433
EJ
1240 struct xbundle *xbundle;
1241
84f0f298 1242 if (!ofbundle || !xcfg) {
5e6af486
EJ
1243 return NULL;
1244 }
1245
84f0f298
RW
1246 xbundles = &xcfg->xbundles;
1247
46c88433 1248 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
84f0f298 1249 xbundles) {
46c88433
EJ
1250 if (xbundle->ofbundle == ofbundle) {
1251 return xbundle;
1252 }
1253 }
1254 return NULL;
1255}
1256
1257static struct xport *
84f0f298 1258xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
46c88433 1259{
84f0f298 1260 struct hmap *xports;
46c88433
EJ
1261 struct xport *xport;
1262
84f0f298 1263 if (!ofport || !xcfg) {
5e6af486
EJ
1264 return NULL;
1265 }
1266
84f0f298
RW
1267 xports = &xcfg->xports;
1268
46c88433 1269 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
84f0f298 1270 xports) {
46c88433
EJ
1271 if (xport->ofport == ofport) {
1272 return xport;
1273 }
1274 }
1275 return NULL;
1276}
1277
40085e56
EJ
1278static struct stp_port *
1279xport_get_stp_port(const struct xport *xport)
1280{
92cf817b 1281 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
1282 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1283 : NULL;
1284}
9d189a50 1285
0d1cee12 1286static bool
9d189a50
EJ
1287xport_stp_learn_state(const struct xport *xport)
1288{
40085e56 1289 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1290 return sp
1291 ? stp_learn_in_state(stp_port_get_state(sp))
1292 : true;
9d189a50
EJ
1293}
1294
1295static bool
1296xport_stp_forward_state(const struct xport *xport)
1297{
40085e56 1298 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1299 return sp
1300 ? stp_forward_in_state(stp_port_get_state(sp))
1301 : true;
9d189a50
EJ
1302}
1303
0d1cee12 1304static bool
bacdb85a 1305xport_stp_should_forward_bpdu(const struct xport *xport)
0d1cee12
K
1306{
1307 struct stp_port *sp = xport_get_stp_port(xport);
bacdb85a 1308 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
0d1cee12
K
1309}
1310
9d189a50
EJ
1311/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1312 * were used to make the determination.*/
1313static bool
1314stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1315{
bbbca389 1316 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
9d189a50 1317 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
bbbca389 1318 return is_stp(flow);
9d189a50
EJ
1319}
1320
1321static void
cf62fa4c 1322stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9d189a50 1323{
40085e56 1324 struct stp_port *sp = xport_get_stp_port(xport);
cf62fa4c
PS
1325 struct dp_packet payload = *packet;
1326 struct eth_header *eth = dp_packet_data(&payload);
9d189a50
EJ
1327
1328 /* Sink packets on ports that have STP disabled when the bridge has
1329 * STP enabled. */
1330 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1331 return;
1332 }
1333
1334 /* Trim off padding on payload. */
cf62fa4c
PS
1335 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1336 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
1337 }
1338
cf62fa4c
PS
1339 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1340 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
9d189a50
EJ
1341 }
1342}
1343
f025bcb7
JR
1344static enum rstp_state
1345xport_get_rstp_port_state(const struct xport *xport)
9efd308e 1346{
f025bcb7
JR
1347 return xport->rstp_port
1348 ? rstp_port_get_state(xport->rstp_port)
1349 : RSTP_DISABLED;
9efd308e
DV
1350}
1351
1352static bool
1353xport_rstp_learn_state(const struct xport *xport)
1354{
4b5f1996
DV
1355 return xport->xbridge->rstp && xport->rstp_port
1356 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1357 : true;
9efd308e
DV
1358}
1359
1360static bool
1361xport_rstp_forward_state(const struct xport *xport)
1362{
4b5f1996
DV
1363 return xport->xbridge->rstp && xport->rstp_port
1364 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1365 : true;
9efd308e
DV
1366}
1367
1368static bool
1369xport_rstp_should_manage_bpdu(const struct xport *xport)
1370{
f025bcb7 1371 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
9efd308e
DV
1372}
1373
1374static void
cf62fa4c 1375rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9efd308e 1376{
cf62fa4c
PS
1377 struct dp_packet payload = *packet;
1378 struct eth_header *eth = dp_packet_data(&payload);
9efd308e 1379
f025bcb7
JR
1380 /* Sink packets on ports that have no RSTP. */
1381 if (!xport->rstp_port) {
9efd308e
DV
1382 return;
1383 }
1384
1385 /* Trim off padding on payload. */
cf62fa4c
PS
1386 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1387 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9efd308e
DV
1388 }
1389
cf62fa4c
PS
1390 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1391 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1392 dp_packet_size(&payload));
9efd308e
DV
1393 }
1394}
1395
46c88433
EJ
1396static struct xport *
1397get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1398{
1399 struct xport *xport;
1400
1401 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1402 &xbridge->xports) {
1403 if (xport->ofp_port == ofp_port) {
1404 return xport;
1405 }
1406 }
1407 return NULL;
1408}
1409
1410static odp_port_t
1411ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1412{
1413 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1414 return xport ? xport->odp_port : ODPP_NONE;
1415}
1416
dd8cd4b4
SH
1417static bool
1418odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1419{
086fa873
BP
1420 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1421 return xport && xport->may_enable;
dd8cd4b4
SH
1422}
1423
1e684d7d 1424static struct ofputil_bucket *
dd8cd4b4
SH
1425group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1426 int depth);
1427
1428static bool
1429group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1430{
1431 struct group_dpif *group;
dd8cd4b4 1432
dc25893e
AZ
1433 if (group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group)) {
1434 struct ofputil_bucket *bucket;
dd8cd4b4 1435
dc25893e
AZ
1436 bucket = group_first_live_bucket(ctx, group, depth);
1437 group_dpif_unref(group);
1438 return bucket == NULL;
1439 }
dd8cd4b4 1440
dc25893e 1441 return false;
dd8cd4b4
SH
1442}
1443
1444#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1445
1446static bool
1447bucket_is_alive(const struct xlate_ctx *ctx,
1e684d7d 1448 struct ofputil_bucket *bucket, int depth)
dd8cd4b4
SH
1449{
1450 if (depth >= MAX_LIVENESS_RECURSION) {
1451 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1452
1453 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
1454 MAX_LIVENESS_RECURSION);
1455 return false;
1456 }
1457
fdb1999b
AZ
1458 return (!ofputil_bucket_has_liveness(bucket)
1459 || (bucket->watch_port != OFPP_ANY
1460 && odp_port_is_alive(ctx, bucket->watch_port))
1461 || (bucket->watch_group != OFPG_ANY
1462 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
dd8cd4b4
SH
1463}
1464
1e684d7d 1465static struct ofputil_bucket *
dd8cd4b4
SH
1466group_first_live_bucket(const struct xlate_ctx *ctx,
1467 const struct group_dpif *group, int depth)
1468{
1469 struct ofputil_bucket *bucket;
ca6ba700 1470 const struct ovs_list *buckets;
dd8cd4b4
SH
1471
1472 group_dpif_get_buckets(group, &buckets);
1473 LIST_FOR_EACH (bucket, list_node, buckets) {
1474 if (bucket_is_alive(ctx, bucket, depth)) {
1475 return bucket;
1476 }
1477 }
1478
1479 return NULL;
1480}
1481
1e684d7d 1482static struct ofputil_bucket *
fe7e5749
SH
1483group_best_live_bucket(const struct xlate_ctx *ctx,
1484 const struct group_dpif *group,
1485 uint32_t basis)
1486{
1e684d7d 1487 struct ofputil_bucket *best_bucket = NULL;
fe7e5749
SH
1488 uint32_t best_score = 0;
1489 int i = 0;
1490
1e684d7d 1491 struct ofputil_bucket *bucket;
ca6ba700 1492 const struct ovs_list *buckets;
fe7e5749
SH
1493
1494 group_dpif_get_buckets(group, &buckets);
1495 LIST_FOR_EACH (bucket, list_node, buckets) {
1496 if (bucket_is_alive(ctx, bucket, 0)) {
7cb279c2 1497 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
1498 if (score >= best_score) {
1499 best_bucket = bucket;
1500 best_score = score;
1501 }
1502 }
1503 i++;
1504 }
1505
1506 return best_bucket;
1507}
1508
9583bc14 1509static bool
46c88433 1510xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
1511{
1512 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1513 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1514}
1515
1516static bool
46c88433
EJ
1517xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
1518{
1519 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
1520}
1521
1522static mirror_mask_t
1523xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1524{
1525 return xbundle != &ofpp_none_bundle
1526 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1527 : 0;
1528}
1529
1530static mirror_mask_t
1531xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1532{
46c88433
EJ
1533 return xbundle != &ofpp_none_bundle
1534 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1535 : 0;
9583bc14
EJ
1536}
1537
46c88433
EJ
1538static mirror_mask_t
1539xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1540{
46c88433
EJ
1541 return xbundle != &ofpp_none_bundle
1542 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1543 : 0;
1544}
1545
1546static struct xbundle *
1547lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
1548 bool warn, struct xport **in_xportp)
1549{
1550 struct xport *xport;
9583bc14
EJ
1551
1552 /* Find the port and bundle for the received packet. */
46c88433
EJ
1553 xport = get_ofp_port(xbridge, in_port);
1554 if (in_xportp) {
1555 *in_xportp = xport;
9583bc14 1556 }
46c88433
EJ
1557 if (xport && xport->xbundle) {
1558 return xport->xbundle;
9583bc14
EJ
1559 }
1560
6362203b
YT
1561 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1562 * which a controller may use as the ingress port for traffic that
1563 * it is sourcing. */
1564 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
9583bc14
EJ
1565 return &ofpp_none_bundle;
1566 }
1567
1568 /* Odd. A few possible reasons here:
1569 *
1570 * - We deleted a port but there are still a few packets queued up
1571 * from it.
1572 *
1573 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1574 * we don't know about.
1575 *
1576 * - The ofproto client didn't configure the port as part of a bundle.
1577 * This is particularly likely to happen if a packet was received on the
1578 * port after it was created, but before the client had a chance to
1579 * configure its bundle.
1580 */
1581 if (warn) {
1582 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1583
1584 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
46c88433 1585 "port %"PRIu16, xbridge->name, in_port);
9583bc14
EJ
1586 }
1587 return NULL;
1588}
1589
1590static void
7efbc3b7
BP
1591mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1592 mirror_mask_t mirrors)
9583bc14 1593{
7efbc3b7
BP
1594 bool warn = ctx->xin->packet != NULL;
1595 uint16_t vid = vlan_tci_to_vid(ctx->xin->flow.vlan_tci);
1596 if (!input_vid_is_valid(vid, xbundle, warn)) {
9583bc14
EJ
1597 return;
1598 }
7efbc3b7 1599 uint16_t vlan = input_vid_to_vlan(xbundle, vid);
9583bc14 1600
7efbc3b7 1601 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1602
7efbc3b7
BP
1603 /* Don't mirror to destinations that we've already mirrored to. */
1604 mirrors &= ~ctx->mirrors;
9583bc14
EJ
1605 if (!mirrors) {
1606 return;
1607 }
1608
7efbc3b7
BP
1609 /* Record these mirrors so that we don't mirror to them again. */
1610 ctx->mirrors |= mirrors;
1611
1612 if (ctx->xin->resubmit_stats) {
1613 mirror_update_stats(xbridge->mbridge, mirrors,
1614 ctx->xin->resubmit_stats->n_packets,
1615 ctx->xin->resubmit_stats->n_bytes);
1616 }
1617 if (ctx->xin->xcache) {
1618 struct xc_entry *entry;
1619
1620 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
1621 entry->u.mirror.mbridge = mbridge_ref(xbridge->mbridge);
1622 entry->u.mirror.mirrors = mirrors;
1623 }
9583bc14
EJ
1624
1625 while (mirrors) {
7efbc3b7 1626 const unsigned long *vlans;
ec7ceaed
EJ
1627 mirror_mask_t dup_mirrors;
1628 struct ofbundle *out;
ec7ceaed
EJ
1629 int out_vlan;
1630
7efbc3b7
BP
1631 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1632 &vlans, &dup_mirrors, &out, &out_vlan);
ec7ceaed
EJ
1633 ovs_assert(has_mirror);
1634
1635 if (vlans) {
49a73e0c 1636 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
9583bc14
EJ
1637 }
1638
7efbc3b7 1639 if (vlans && !bitmap_is_set(vlans, vlan)) {
9583bc14
EJ
1640 mirrors = zero_rightmost_1bit(mirrors);
1641 continue;
1642 }
1643
ec7ceaed 1644 mirrors &= ~dup_mirrors;
3d6151f3 1645 ctx->mirrors |= dup_mirrors;
ec7ceaed 1646 if (out) {
84f0f298
RW
1647 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1648 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
46c88433
EJ
1649 if (out_xbundle) {
1650 output_normal(ctx, out_xbundle, vlan);
1651 }
ec7ceaed 1652 } else if (vlan != out_vlan
7efbc3b7 1653 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
46c88433 1654 struct xbundle *xbundle;
9583bc14 1655
46c88433
EJ
1656 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
1657 if (xbundle_includes_vlan(xbundle, out_vlan)
1658 && !xbundle_mirror_out(xbridge, xbundle)) {
1659 output_normal(ctx, xbundle, out_vlan);
9583bc14
EJ
1660 }
1661 }
1662 }
1663 }
1664}
1665
7efbc3b7
BP
1666static void
1667mirror_ingress_packet(struct xlate_ctx *ctx)
1668{
1669 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
1670 bool warn = ctx->xin->packet != NULL;
1671 struct xbundle *xbundle = lookup_input_bundle(
1672 ctx->xbridge, ctx->xin->flow.in_port.ofp_port, warn, NULL);
1673 if (xbundle) {
1674 mirror_packet(ctx, xbundle,
1675 xbundle_mirror_src(ctx->xbridge, xbundle));
1676 }
1677 }
1678}
1679
9583bc14 1680/* Given 'vid', the VID obtained from the 802.1Q header that was received as
46c88433 1681 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
9583bc14
EJ
1682 * the bundle on which the packet was received, returns the VLAN to which the
1683 * packet belongs.
1684 *
1685 * Both 'vid' and the return value are in the range 0...4095. */
1686static uint16_t
46c88433 1687input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
9583bc14 1688{
46c88433 1689 switch (in_xbundle->vlan_mode) {
9583bc14 1690 case PORT_VLAN_ACCESS:
46c88433 1691 return in_xbundle->vlan;
9583bc14
EJ
1692 break;
1693
1694 case PORT_VLAN_TRUNK:
1695 return vid;
1696
1697 case PORT_VLAN_NATIVE_UNTAGGED:
1698 case PORT_VLAN_NATIVE_TAGGED:
46c88433 1699 return vid ? vid : in_xbundle->vlan;
9583bc14
EJ
1700
1701 default:
428b2edd 1702 OVS_NOT_REACHED();
9583bc14
EJ
1703 }
1704}
1705
46c88433 1706/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
9583bc14
EJ
1707 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1708 * a warning.
1709 *
1710 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1711 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1712 * 0...4095. */
1713static bool
46c88433 1714input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
9583bc14
EJ
1715{
1716 /* Allow any VID on the OFPP_NONE port. */
46c88433 1717 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1718 return true;
1719 }
1720
46c88433 1721 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1722 case PORT_VLAN_ACCESS:
1723 if (vid) {
1724 if (warn) {
1725 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1726 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
9583bc14 1727 "packet received on port %s configured as VLAN "
46c88433
EJ
1728 "%"PRIu16" access port", vid, in_xbundle->name,
1729 in_xbundle->vlan);
9583bc14
EJ
1730 }
1731 return false;
1732 }
1733 return true;
1734
1735 case PORT_VLAN_NATIVE_UNTAGGED:
1736 case PORT_VLAN_NATIVE_TAGGED:
1737 if (!vid) {
1738 /* Port must always carry its native VLAN. */
1739 return true;
1740 }
1741 /* Fall through. */
1742 case PORT_VLAN_TRUNK:
46c88433 1743 if (!xbundle_includes_vlan(in_xbundle, vid)) {
9583bc14
EJ
1744 if (warn) {
1745 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
46c88433 1746 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
9583bc14 1747 "received on port %s not configured for trunking "
46c88433 1748 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
9583bc14
EJ
1749 }
1750 return false;
1751 }
1752 return true;
1753
1754 default:
428b2edd 1755 OVS_NOT_REACHED();
9583bc14
EJ
1756 }
1757
1758}
1759
1760/* Given 'vlan', the VLAN that a packet belongs to, and
46c88433 1761 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
9583bc14
EJ
1762 * that should be included in the 802.1Q header. (If the return value is 0,
1763 * then the 802.1Q header should only be included in the packet if there is a
1764 * nonzero PCP.)
1765 *
1766 * Both 'vlan' and the return value are in the range 0...4095. */
1767static uint16_t
46c88433 1768output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
9583bc14 1769{
46c88433 1770 switch (out_xbundle->vlan_mode) {
9583bc14
EJ
1771 case PORT_VLAN_ACCESS:
1772 return 0;
1773
1774 case PORT_VLAN_TRUNK:
1775 case PORT_VLAN_NATIVE_TAGGED:
1776 return vlan;
1777
1778 case PORT_VLAN_NATIVE_UNTAGGED:
46c88433 1779 return vlan == out_xbundle->vlan ? 0 : vlan;
9583bc14
EJ
1780
1781 default:
428b2edd 1782 OVS_NOT_REACHED();
9583bc14
EJ
1783 }
1784}
1785
1786static void
46c88433 1787output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
9583bc14
EJ
1788 uint16_t vlan)
1789{
33bf9176 1790 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
9583bc14
EJ
1791 uint16_t vid;
1792 ovs_be16 tci, old_tci;
46c88433 1793 struct xport *xport;
e93ef1c7
JR
1794 struct xlate_bond_recirc xr;
1795 bool use_recirc = false;
9583bc14 1796
46c88433
EJ
1797 vid = output_vlan_to_vid(out_xbundle, vlan);
1798 if (list_is_empty(&out_xbundle->xports)) {
1799 /* Partially configured bundle with no slaves. Drop the packet. */
1800 return;
1801 } else if (!out_xbundle->bond) {
1802 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1803 bundle_node);
9583bc14 1804 } else {
84f0f298 1805 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
49a73e0c 1806 struct flow_wildcards *wc = ctx->wc;
84f0f298 1807 struct ofport_dpif *ofport;
adcf00ba 1808
2494ccd7 1809 if (ctx->xbridge->support.odp.recirc) {
e93ef1c7
JR
1810 use_recirc = bond_may_recirc(
1811 out_xbundle->bond, &xr.recirc_id, &xr.hash_basis);
adcf00ba 1812
e93ef1c7 1813 if (use_recirc) {
adcf00ba 1814 /* Only TCP mode uses recirculation. */
e93ef1c7 1815 xr.hash_alg = OVS_HASH_ALG_L4;
adcf00ba 1816 bond_update_post_recirc_rules(out_xbundle->bond, false);
54ecb5a2
AZ
1817
1818 /* Recirculation does not require unmasking hash fields. */
1819 wc = NULL;
adcf00ba
AZ
1820 }
1821 }
46c88433 1822
54ecb5a2
AZ
1823 ofport = bond_choose_output_slave(out_xbundle->bond,
1824 &ctx->xin->flow, wc, vid);
84f0f298 1825 xport = xport_lookup(xcfg, ofport);
46c88433
EJ
1826
1827 if (!xport) {
9583bc14
EJ
1828 /* No slaves enabled, so drop packet. */
1829 return;
1830 }
d6fc5f57 1831
e93ef1c7 1832 /* If use_recirc is set, the main thread will handle stats
b256dc52 1833 * accounting for this bond. */
e93ef1c7 1834 if (!use_recirc) {
b256dc52
JS
1835 if (ctx->xin->resubmit_stats) {
1836 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1837 ctx->xin->resubmit_stats->n_bytes);
1838 }
1839 if (ctx->xin->xcache) {
1840 struct xc_entry *entry;
1841 struct flow *flow;
1842
1843 flow = &ctx->xin->flow;
1844 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
1845 entry->u.bond.bond = bond_ref(out_xbundle->bond);
1846 entry->u.bond.flow = xmemdup(flow, sizeof *flow);
1847 entry->u.bond.vid = vid;
1848 }
d6fc5f57 1849 }
9583bc14
EJ
1850 }
1851
33bf9176 1852 old_tci = *flow_tci;
9583bc14 1853 tci = htons(vid);
46c88433 1854 if (tci || out_xbundle->use_priority_tags) {
33bf9176 1855 tci |= *flow_tci & htons(VLAN_PCP_MASK);
9583bc14
EJ
1856 if (tci) {
1857 tci |= htons(VLAN_CFI);
1858 }
1859 }
33bf9176 1860 *flow_tci = tci;
9583bc14 1861
e93ef1c7 1862 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
33bf9176 1863 *flow_tci = old_tci;
9583bc14
EJ
1864}
1865
1866/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1867 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1868 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1869static bool
1870is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1871{
1872 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1873 return false;
1874 }
1875
1876 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1877 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1878 return false;
1879 }
1880
1881 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1882 if (flow->nw_proto == ARP_OP_REPLY) {
1883 return true;
1884 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1885 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1886 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1887
1888 return flow->nw_src == flow->nw_dst;
1889 } else {
1890 return false;
1891 }
1892}
1893
ff69c24a
FL
1894/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1895 * dropped. Returns true if they may be forwarded, false if they should be
1896 * dropped.
1897 *
1898 * 'in_port' must be the xport that corresponds to flow->in_port.
1899 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1900 *
1901 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1902 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1903 * checked by input_vid_is_valid().
1904 *
1905 * May also add tags to '*tags', although the current implementation only does
1906 * so in one special case.
1907 */
1908static bool
1909is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1910 uint16_t vlan)
1911{
1912 struct xbundle *in_xbundle = in_port->xbundle;
1913 const struct xbridge *xbridge = ctx->xbridge;
1914 struct flow *flow = &ctx->xin->flow;
1915
1916 /* Drop frames for reserved multicast addresses
1917 * only if forward_bpdu option is absent. */
1918 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1919 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1920 return false;
1921 }
1922
1923 if (in_xbundle->bond) {
1924 struct mac_entry *mac;
1925
1926 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1927 flow->dl_dst)) {
1928 case BV_ACCEPT:
1929 break;
1930
1931 case BV_DROP:
1932 xlate_report(ctx, "bonding refused admissibility, dropping");
1933 return false;
1934
1935 case BV_DROP_IF_MOVED:
1936 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1937 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
9d078ec2
BP
1938 if (mac
1939 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
49a73e0c 1940 && (!is_gratuitous_arp(flow, ctx->wc)
9d078ec2 1941 || mac_entry_is_grat_arp_locked(mac))) {
ff69c24a
FL
1942 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1943 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1944 "dropping");
1945 return false;
1946 }
1947 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1948 break;
1949 }
1950 }
1951
1952 return true;
1953}
1954
ee047520
BP
1955/* Checks whether a MAC learning update is necessary for MAC learning table
1956 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1957 * 'vlan'.
1958 *
1959 * Most packets processed through the MAC learning table do not actually
1960 * change it in any way. This function requires only a read lock on the MAC
1961 * learning table, so it is much cheaper in this common case.
1962 *
1963 * Keep the code here synchronized with that in update_learning_table__()
1964 * below. */
1965static bool
1966is_mac_learning_update_needed(const struct mac_learning *ml,
1967 const struct flow *flow,
1968 struct flow_wildcards *wc,
1969 int vlan, struct xbundle *in_xbundle)
d6fc5f57 1970OVS_REQ_RDLOCK(ml->rwlock)
9583bc14
EJ
1971{
1972 struct mac_entry *mac;
1973
ee047520
BP
1974 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1975 return false;
1976 }
1977
1978 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1979 if (!mac || mac_entry_age(ml, mac)) {
1980 return true;
9583bc14
EJ
1981 }
1982
ee047520
BP
1983 if (is_gratuitous_arp(flow, wc)) {
1984 /* We don't want to learn from gratuitous ARP packets that are
1985 * reflected back over bond slaves so we lock the learning table. */
1986 if (!in_xbundle->bond) {
1987 return true;
1988 } else if (mac_entry_is_grat_arp_locked(mac)) {
1989 return false;
1990 }
1991 }
1992
9d078ec2 1993 return mac_entry_get_port(ml, mac) != in_xbundle->ofbundle;
ee047520
BP
1994}
1995
1996
1997/* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1998 * received on 'in_xbundle' in 'vlan'.
1999 *
2000 * This code repeats all the checks in is_mac_learning_update_needed() because
2001 * the lock was released between there and here and thus the MAC learning state
2002 * could have changed.
2003 *
2004 * Keep the code here synchronized with that in is_mac_learning_update_needed()
2005 * above. */
2006static void
2007update_learning_table__(const struct xbridge *xbridge,
2008 const struct flow *flow, struct flow_wildcards *wc,
2009 int vlan, struct xbundle *in_xbundle)
d6fc5f57 2010OVS_REQ_WRLOCK(xbridge->ml->rwlock)
ee047520
BP
2011{
2012 struct mac_entry *mac;
2013
46c88433 2014 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
ee047520 2015 return;
9583bc14
EJ
2016 }
2017
46c88433 2018 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
9583bc14
EJ
2019 if (is_gratuitous_arp(flow, wc)) {
2020 /* We don't want to learn from gratuitous ARP packets that are
2021 * reflected back over bond slaves so we lock the learning table. */
46c88433 2022 if (!in_xbundle->bond) {
9583bc14
EJ
2023 mac_entry_set_grat_arp_lock(mac);
2024 } else if (mac_entry_is_grat_arp_locked(mac)) {
ee047520 2025 return;
9583bc14
EJ
2026 }
2027 }
2028
9d078ec2 2029 if (mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle) {
9583bc14
EJ
2030 /* The log messages here could actually be useful in debugging,
2031 * so keep the rate limit relatively high. */
2032 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
ee047520 2033
9583bc14
EJ
2034 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2035 "on port %s in VLAN %d",
46c88433
EJ
2036 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
2037 in_xbundle->name, vlan);
9583bc14 2038
9d078ec2 2039 mac_entry_set_port(xbridge->ml, mac, in_xbundle->ofbundle);
9583bc14 2040 }
ee047520
BP
2041}
2042
2043static void
2044update_learning_table(const struct xbridge *xbridge,
2045 const struct flow *flow, struct flow_wildcards *wc,
2046 int vlan, struct xbundle *in_xbundle)
2047{
2048 bool need_update;
2049
2050 /* Don't learn the OFPP_NONE port. */
2051 if (in_xbundle == &ofpp_none_bundle) {
2052 return;
2053 }
2054
2055 /* First try the common case: no change to MAC learning table. */
2056 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2057 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
2058 in_xbundle);
509c0149 2059 ovs_rwlock_unlock(&xbridge->ml->rwlock);
ee047520
BP
2060
2061 if (need_update) {
2062 /* Slow path: MAC learning table might need an update. */
2063 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
2064 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
2065 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2066 }
9583bc14
EJ
2067}
2068
86e2dcdd
FL
2069/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2070 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2071static void
06994f87
TLSC
2072update_mcast_snooping_table4__(const struct xbridge *xbridge,
2073 const struct flow *flow,
2074 struct mcast_snooping *ms, int vlan,
2075 struct xbundle *in_xbundle,
2076 const struct dp_packet *packet)
86e2dcdd
FL
2077 OVS_REQ_WRLOCK(ms->rwlock)
2078{
2079 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
e3102e42 2080 int count;
06994f87 2081 ovs_be32 ip4 = flow->igmp_group_ip4;
86e2dcdd
FL
2082
2083 switch (ntohs(flow->tp_src)) {
2084 case IGMP_HOST_MEMBERSHIP_REPORT:
2085 case IGMPV2_HOST_MEMBERSHIP_REPORT:
964a4d5f 2086 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
86e2dcdd
FL
2087 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping learned that "
2088 IP_FMT" is on port %s in VLAN %d",
2089 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2090 }
2091 break;
2092 case IGMP_HOST_LEAVE_MESSAGE:
964a4d5f 2093 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
86e2dcdd
FL
2094 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping leaving "
2095 IP_FMT" is on port %s in VLAN %d",
2096 xbridge->name, IP_ARGS(ip4), in_xbundle->name, vlan);
2097 }
2098 break;
2099 case IGMP_HOST_MEMBERSHIP_QUERY:
2100 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2101 in_xbundle->ofbundle)) {
2102 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query from "
2103 IP_FMT" is on port %s in VLAN %d",
2104 xbridge->name, IP_ARGS(flow->nw_src),
2105 in_xbundle->name, vlan);
2106 }
2107 break;
e3102e42
TLSC
2108 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2109 if ((count = mcast_snooping_add_report(ms, packet, vlan,
2110 in_xbundle->ofbundle))) {
2111 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2112 "addresses on port %s in VLAN %d",
2113 xbridge->name, count, in_xbundle->name, vlan);
2114 }
2115 break;
86e2dcdd
FL
2116 }
2117}
2118
06994f87
TLSC
2119static void
2120update_mcast_snooping_table6__(const struct xbridge *xbridge,
2121 const struct flow *flow,
2122 struct mcast_snooping *ms, int vlan,
2123 struct xbundle *in_xbundle,
2124 const struct dp_packet *packet)
2125 OVS_REQ_WRLOCK(ms->rwlock)
2126{
2127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 30);
2128 int count;
2129
2130 switch (ntohs(flow->tp_src)) {
2131 case MLD_QUERY:
2132 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2133 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2134 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping query on port %s"
2135 "in VLAN %d",
2136 xbridge->name, in_xbundle->name, vlan);
2137 }
2138 break;
2139 case MLD_REPORT:
2140 case MLD_DONE:
2141 case MLD2_REPORT:
2142 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2143 if (count) {
2144 VLOG_DBG_RL(&rl, "bridge %s: multicast snooping processed %d "
2145 "addresses on port %s in VLAN %d",
2146 xbridge->name, count, in_xbundle->name, vlan);
2147 }
2148 break;
2149 }
2150}
2151
86e2dcdd
FL
2152/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2153 * was received on 'in_xbundle' in 'vlan'. */
2154static void
2155update_mcast_snooping_table(const struct xbridge *xbridge,
2156 const struct flow *flow, int vlan,
e3102e42
TLSC
2157 struct xbundle *in_xbundle,
2158 const struct dp_packet *packet)
86e2dcdd
FL
2159{
2160 struct mcast_snooping *ms = xbridge->ms;
2161 struct xlate_cfg *xcfg;
2162 struct xbundle *mcast_xbundle;
f4ae6e23 2163 struct mcast_port_bundle *fport;
86e2dcdd
FL
2164
2165 /* Don't learn the OFPP_NONE port. */
2166 if (in_xbundle == &ofpp_none_bundle) {
2167 return;
2168 }
2169
2170 /* Don't learn from flood ports */
2171 mcast_xbundle = NULL;
2172 ovs_rwlock_wrlock(&ms->rwlock);
2173 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2174 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2175 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2176 if (mcast_xbundle == in_xbundle) {
2177 break;
2178 }
2179 }
2180
2181 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
06994f87
TLSC
2182 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2183 update_mcast_snooping_table4__(xbridge, flow, ms, vlan,
2184 in_xbundle, packet);
2185 } else {
2186 update_mcast_snooping_table6__(xbridge, flow, ms, vlan,
2187 in_xbundle, packet);
2188 }
86e2dcdd
FL
2189 }
2190 ovs_rwlock_unlock(&ms->rwlock);
2191}
2192
2193/* send the packet to ports having the multicast group learned */
2194static void
2195xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2196 struct mcast_snooping *ms OVS_UNUSED,
2197 struct mcast_group *grp,
2198 struct xbundle *in_xbundle, uint16_t vlan)
2199 OVS_REQ_RDLOCK(ms->rwlock)
2200{
2201 struct xlate_cfg *xcfg;
2202 struct mcast_group_bundle *b;
2203 struct xbundle *mcast_xbundle;
2204
2205 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2206 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2207 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2208 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2209 xlate_report(ctx, "forwarding to mcast group port");
2210 output_normal(ctx, mcast_xbundle, vlan);
2211 } else if (!mcast_xbundle) {
2212 xlate_report(ctx, "mcast group port is unknown, dropping");
2213 } else {
2214 xlate_report(ctx, "mcast group port is input port, dropping");
2215 }
2216 }
2217}
2218
2219/* send the packet to ports connected to multicast routers */
2220static void
2221xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2222 struct mcast_snooping *ms,
2223 struct xbundle *in_xbundle, uint16_t vlan)
2224 OVS_REQ_RDLOCK(ms->rwlock)
2225{
2226 struct xlate_cfg *xcfg;
2227 struct mcast_mrouter_bundle *mrouter;
2228 struct xbundle *mcast_xbundle;
2229
2230 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2231 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2232 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
2233 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2234 xlate_report(ctx, "forwarding to mcast router port");
2235 output_normal(ctx, mcast_xbundle, vlan);
2236 } else if (!mcast_xbundle) {
2237 xlate_report(ctx, "mcast router port is unknown, dropping");
2238 } else {
2239 xlate_report(ctx, "mcast router port is input port, dropping");
2240 }
2241 }
2242}
2243
2244/* send the packet to ports flagged to be flooded */
2245static void
2246xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2247 struct mcast_snooping *ms,
2248 struct xbundle *in_xbundle, uint16_t vlan)
2249 OVS_REQ_RDLOCK(ms->rwlock)
2250{
2251 struct xlate_cfg *xcfg;
f4ae6e23 2252 struct mcast_port_bundle *fport;
86e2dcdd
FL
2253 struct xbundle *mcast_xbundle;
2254
2255 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2256 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2257 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2258 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2259 xlate_report(ctx, "forwarding to mcast flood port");
2260 output_normal(ctx, mcast_xbundle, vlan);
2261 } else if (!mcast_xbundle) {
2262 xlate_report(ctx, "mcast flood port is unknown, dropping");
2263 } else {
2264 xlate_report(ctx, "mcast flood port is input port, dropping");
2265 }
2266 }
2267}
2268
8e04a33f
FL
2269/* forward the Reports to configured ports */
2270static void
2271xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2272 struct mcast_snooping *ms,
2273 struct xbundle *in_xbundle, uint16_t vlan)
2274 OVS_REQ_RDLOCK(ms->rwlock)
2275{
2276 struct xlate_cfg *xcfg;
2277 struct mcast_port_bundle *rport;
2278 struct xbundle *mcast_xbundle;
2279
2280 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2281 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2282 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2283 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2284 xlate_report(ctx, "forwarding Report to mcast flagged port");
2285 output_normal(ctx, mcast_xbundle, vlan);
2286 } else if (!mcast_xbundle) {
2287 xlate_report(ctx, "mcast port is unknown, dropping the Report");
2288 } else {
2289 xlate_report(ctx, "mcast port is input port, dropping the Report");
2290 }
2291 }
2292}
2293
682800a4
FL
2294static void
2295xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
2296 uint16_t vlan)
2297{
2298 struct xbundle *xbundle;
2299
2300 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2301 if (xbundle != in_xbundle
2302 && xbundle_includes_vlan(xbundle, vlan)
2303 && xbundle->floodable
2304 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
2305 output_normal(ctx, xbundle, vlan);
2306 }
2307 }
2031ef97 2308 ctx->nf_output_iface = NF_OUT_FLOOD;
682800a4
FL
2309}
2310
9583bc14
EJ
2311static void
2312xlate_normal(struct xlate_ctx *ctx)
2313{
49a73e0c 2314 struct flow_wildcards *wc = ctx->wc;
33bf9176 2315 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
2316 struct xbundle *in_xbundle;
2317 struct xport *in_port;
9583bc14 2318 struct mac_entry *mac;
d6d5bbc9 2319 void *mac_port;
9583bc14
EJ
2320 uint16_t vlan;
2321 uint16_t vid;
2322
33bf9176
BP
2323 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2324 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1dd35f8a 2325 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 2326
46c88433
EJ
2327 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
2328 ctx->xin->packet != NULL, &in_port);
2329 if (!in_xbundle) {
9583bc14
EJ
2330 xlate_report(ctx, "no input bundle, dropping");
2331 return;
2332 }
2333
2334 /* Drop malformed frames. */
33bf9176
BP
2335 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
2336 !(flow->vlan_tci & htons(VLAN_CFI))) {
9583bc14
EJ
2337 if (ctx->xin->packet != NULL) {
2338 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2339 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
2340 "VLAN tag received on port %s",
46c88433 2341 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2342 }
2343 xlate_report(ctx, "partial VLAN tag, dropping");
2344 return;
2345 }
2346
2347 /* Drop frames on bundles reserved for mirroring. */
46c88433 2348 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14
EJ
2349 if (ctx->xin->packet != NULL) {
2350 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2351 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2352 "%s, which is reserved exclusively for mirroring",
46c88433 2353 ctx->xbridge->name, in_xbundle->name);
9583bc14
EJ
2354 }
2355 xlate_report(ctx, "input port is mirror output port, dropping");
2356 return;
2357 }
2358
2359 /* Check VLAN. */
33bf9176 2360 vid = vlan_tci_to_vid(flow->vlan_tci);
46c88433 2361 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
9583bc14
EJ
2362 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
2363 return;
2364 }
46c88433 2365 vlan = input_vid_to_vlan(in_xbundle, vid);
9583bc14
EJ
2366
2367 /* Check other admissibility requirements. */
2368 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2369 return;
2370 }
2371
2372 /* Learn source MAC. */
2373 if (ctx->xin->may_learn) {
46c88433 2374 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
9583bc14 2375 }
b256dc52
JS
2376 if (ctx->xin->xcache) {
2377 struct xc_entry *entry;
2378
2379 /* Save enough info to update mac learning table later. */
2380 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
9edf6b48 2381 entry->u.normal.ofproto = ctx->xbridge->ofproto;
b256dc52
JS
2382 entry->u.normal.flow = xmemdup(flow, sizeof *flow);
2383 entry->u.normal.vlan = vlan;
2384 }
9583bc14
EJ
2385
2386 /* Determine output bundle. */
86e2dcdd
FL
2387 if (mcast_snooping_enabled(ctx->xbridge->ms)
2388 && !eth_addr_is_broadcast(flow->dl_dst)
2389 && eth_addr_is_multicast(flow->dl_dst)
06994f87 2390 && is_ip_any(flow)) {
86e2dcdd 2391 struct mcast_snooping *ms = ctx->xbridge->ms;
06994f87 2392 struct mcast_group *grp = NULL;
86e2dcdd 2393
06994f87 2394 if (is_igmp(flow)) {
d29f137b
TLSC
2395 if (mcast_snooping_is_membership(flow->tp_src) ||
2396 mcast_snooping_is_query(flow->tp_src)) {
2397 if (ctx->xin->may_learn) {
86e2dcdd 2398 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
e3102e42 2399 in_xbundle, ctx->xin->packet);
d29f137b
TLSC
2400 }
2401 /*
2402 * IGMP packets need to take the slow path, in order to be
2403 * processed for mdb updates. That will prevent expires
2404 * firing off even after hosts have sent reports.
2405 */
2406 ctx->xout->slow |= SLOW_ACTION;
86e2dcdd 2407 }
d6d5bbc9 2408
86e2dcdd
FL
2409 if (mcast_snooping_is_membership(flow->tp_src)) {
2410 ovs_rwlock_rdlock(&ms->rwlock);
2411 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
8e04a33f
FL
2412 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2413 * forward IGMP Membership Reports only to those ports where
2414 * multicast routers are attached. Alternatively stated: a
2415 * snooping switch should not forward IGMP Membership Reports
2416 * to ports on which only hosts are attached.
2417 * An administrative control may be provided to override this
2418 * restriction, allowing the report messages to be flooded to
2419 * other ports. */
2420 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
86e2dcdd
FL
2421 ovs_rwlock_unlock(&ms->rwlock);
2422 } else {
2423 xlate_report(ctx, "multicast traffic, flooding");
2424 xlate_normal_flood(ctx, in_xbundle, vlan);
2425 }
2426 return;
06994f87
TLSC
2427 } else if (is_mld(flow)) {
2428 ctx->xout->slow |= SLOW_ACTION;
2429 if (ctx->xin->may_learn) {
2430 update_mcast_snooping_table(ctx->xbridge, flow, vlan,
2431 in_xbundle, ctx->xin->packet);
2432 }
2433 if (is_mld_report(flow)) {
2434 ovs_rwlock_rdlock(&ms->rwlock);
2435 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2436 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, vlan);
2437 ovs_rwlock_unlock(&ms->rwlock);
2438 } else {
2439 xlate_report(ctx, "MLD query, flooding");
2440 xlate_normal_flood(ctx, in_xbundle, vlan);
2441 }
86e2dcdd 2442 } else {
06994f87
TLSC
2443 if ((flow->dl_type == htons(ETH_TYPE_IP)
2444 && ip_is_local_multicast(flow->nw_dst))
2445 || (flow->dl_type == htons(ETH_TYPE_IPV6)
2446 && ipv6_is_all_hosts(&flow->ipv6_dst))) {
86e2dcdd
FL
2447 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2448 * address in the 224.0.0.x range which are not IGMP must
2449 * be forwarded on all ports */
2450 xlate_report(ctx, "RFC4541: section 2.1.2, item 2, flooding");
2451 xlate_normal_flood(ctx, in_xbundle, vlan);
2452 return;
2453 }
2454 }
2455
2456 /* forwarding to group base ports */
2457 ovs_rwlock_rdlock(&ms->rwlock);
06994f87
TLSC
2458 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2459 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2460 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2461 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2462 }
86e2dcdd
FL
2463 if (grp) {
2464 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, vlan);
2465 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2466 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
9583bc14 2467 } else {
86e2dcdd
FL
2468 if (mcast_snooping_flood_unreg(ms)) {
2469 xlate_report(ctx, "unregistered multicast, flooding");
2470 xlate_normal_flood(ctx, in_xbundle, vlan);
2471 } else {
2472 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, vlan);
2473 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, vlan);
2474 }
9583bc14 2475 }
86e2dcdd 2476 ovs_rwlock_unlock(&ms->rwlock);
9583bc14 2477 } else {
86e2dcdd
FL
2478 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2479 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
9d078ec2 2480 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
86e2dcdd
FL
2481 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2482
2483 if (mac_port) {
2484 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2485 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2486 if (mac_xbundle && mac_xbundle != in_xbundle) {
2487 xlate_report(ctx, "forwarding to learned port");
2488 output_normal(ctx, mac_xbundle, vlan);
2489 } else if (!mac_xbundle) {
2490 xlate_report(ctx, "learned port is unknown, dropping");
2491 } else {
2492 xlate_report(ctx, "learned port is input port, dropping");
2493 }
2494 } else {
2495 xlate_report(ctx, "no learned MAC for destination, flooding");
2496 xlate_normal_flood(ctx, in_xbundle, vlan);
2497 }
9583bc14
EJ
2498 }
2499}
2500
a6092018
BP
2501/* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2502 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2503 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2504 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2505 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
2506 * OVS_USERSPACE_ATTR_ACTIONS attribute is added.
9583bc14
EJ
2507 */
2508static size_t
a6092018 2509compose_sample_action(struct xlate_ctx *ctx,
9583bc14
EJ
2510 const uint32_t probability,
2511 const union user_action_cookie *cookie,
8b7ea2d4 2512 const size_t cookie_size,
7321bda3
NM
2513 const odp_port_t tunnel_out_port,
2514 bool include_actions)
9583bc14 2515{
a6092018
BP
2516 size_t sample_offset = nl_msg_start_nested(ctx->odp_actions,
2517 OVS_ACTION_ATTR_SAMPLE);
9583bc14 2518
a6092018 2519 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
9583bc14 2520
a6092018
BP
2521 size_t actions_offset = nl_msg_start_nested(ctx->odp_actions,
2522 OVS_SAMPLE_ATTR_ACTIONS);
9583bc14 2523
a6092018
BP
2524 odp_port_t odp_port = ofp_port_to_odp_port(
2525 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2526 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2527 flow_hash_5tuple(&ctx->xin->flow, 0));
2528 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2529 tunnel_out_port,
2530 include_actions,
2531 ctx->odp_actions);
89a8a7f0 2532
a6092018
BP
2533 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2534 nl_msg_end_nested(ctx->odp_actions, sample_offset);
9583bc14 2535
9583bc14
EJ
2536 return cookie_offset;
2537}
2538
a6092018
BP
2539/* If sFLow is not enabled, returns 0 without doing anything.
2540 *
2541 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2542 * in 'ctx'. This action is a template because some of the information needed
2543 * to fill it out is not available until flow translation is complete. In this
2544 * case, this functions returns an offset, which is always nonzero, to pass
2545 * later to fix_sflow_action() to fill in the rest of the template. */
9583bc14 2546static size_t
a6092018 2547compose_sflow_action(struct xlate_ctx *ctx)
9583bc14 2548{
a6092018
BP
2549 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2550 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2551 return 0;
2552 }
2553
a6092018
BP
2554 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2555 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
7321bda3
NM
2556 &cookie, sizeof cookie.sflow, ODPP_NONE,
2557 true);
9583bc14
EJ
2558}
2559
a6092018
BP
2560/* If IPFIX is enabled, this appends a "sample" action to implement IPFIX to
2561 * 'ctx->odp_actions'. */
9583bc14 2562static void
a6092018 2563compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
9583bc14 2564{
a6092018 2565 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
8b7ea2d4 2566 odp_port_t tunnel_out_port = ODPP_NONE;
9583bc14 2567
a6092018 2568 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2569 return;
2570 }
2571
8b7ea2d4
WZ
2572 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2573 * number. */
2574 if (output_odp_port == ODPP_NONE &&
a6092018 2575 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
8b7ea2d4
WZ
2576 return;
2577 }
2578
2579 /* For output case, output_odp_port is valid*/
2580 if (output_odp_port != ODPP_NONE) {
a6092018 2581 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
8b7ea2d4
WZ
2582 return;
2583 }
2584 /* If tunnel sampling is enabled, put an additional option attribute:
2585 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2586 */
a6092018
BP
2587 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2588 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
8b7ea2d4
WZ
2589 tunnel_out_port = output_odp_port;
2590 }
2591 }
2592
a6092018
BP
2593 union user_action_cookie cookie = {
2594 .ipfix = {
2595 .type = USER_ACTION_COOKIE_IPFIX,
2596 .output_odp_port = output_odp_port,
2597 }
2598 };
2599 compose_sample_action(ctx,
2600 dpif_ipfix_get_bridge_exporter_probability(ipfix),
7321bda3
NM
2601 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2602 false);
9583bc14
EJ
2603}
2604
a6092018
BP
2605/* Fix "sample" action according to data collected while composing ODP actions,
2606 * as described in compose_sflow_action().
2607 *
2608 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
9583bc14 2609static void
a6092018 2610fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
9583bc14
EJ
2611{
2612 const struct flow *base = &ctx->base_flow;
2613 union user_action_cookie *cookie;
2614
a6092018 2615 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
9583bc14
EJ
2616 sizeof cookie->sflow);
2617 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2618
a6092018
BP
2619 cookie->type = USER_ACTION_COOKIE_SFLOW;
2620 cookie->sflow.vlan_tci = base->vlan_tci;
2621
2622 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2623 * port information") for the interpretation of cookie->output. */
2624 switch (ctx->sflow_n_outputs) {
2625 case 0:
2626 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2627 cookie->sflow.output = 0x40000000 | 256;
2628 break;
2629
2630 case 1:
2631 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2632 ctx->xbridge->sflow, ctx->sflow_odp_port);
2633 if (cookie->sflow.output) {
2634 break;
2635 }
2636 /* Fall through. */
2637 default:
2638 /* 0x80000000 means "multiple output ports. */
2639 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2640 break;
2641 }
9583bc14
EJ
2642}
2643
515793d5
BP
2644static bool
2645process_special(struct xlate_ctx *ctx, const struct xport *xport)
db7d4e46 2646{
515793d5 2647 const struct flow *flow = &ctx->xin->flow;
49a73e0c 2648 struct flow_wildcards *wc = ctx->wc;
46c88433 2649 const struct xbridge *xbridge = ctx->xbridge;
515793d5
BP
2650 const struct dp_packet *packet = ctx->xin->packet;
2651 enum slow_path_reason slow;
642dc74d 2652
46c88433 2653 if (!xport) {
515793d5 2654 slow = 0;
46c88433 2655 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 2656 if (packet) {
46c88433 2657 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46 2658 }
515793d5 2659 slow = SLOW_CFM;
fab52e16 2660 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 2661 if (packet) {
46c88433 2662 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
2663 /* If POLL received, immediately sends FINAL back. */
2664 if (bfd_should_send_packet(xport->bfd)) {
6d308b28 2665 ofproto_dpif_monitor_port_send_soon(xport->ofport);
60d02c72 2666 }
db7d4e46 2667 }
515793d5 2668 slow = SLOW_BFD;
46c88433 2669 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
2670 && flow->dl_type == htons(ETH_TYPE_LACP)) {
2671 if (packet) {
46c88433 2672 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46 2673 }
515793d5 2674 slow = SLOW_LACP;
9efd308e
DV
2675 } else if ((xbridge->stp || xbridge->rstp) &&
2676 stp_should_process_flow(flow, wc)) {
db7d4e46 2677 if (packet) {
f025bcb7
JR
2678 xbridge->stp
2679 ? stp_process_packet(xport, packet)
2680 : rstp_process_packet(xport, packet);
db7d4e46 2681 }
515793d5 2682 slow = SLOW_STP;
19aef6ef 2683 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
0477baa9
DF
2684 if (packet) {
2685 lldp_process_packet(xport->lldp, packet);
2686 }
515793d5 2687 slow = SLOW_LLDP;
db7d4e46 2688 } else {
515793d5
BP
2689 slow = 0;
2690 }
2691
2692 if (slow) {
2693 ctx->xout->slow |= slow;
2694 return true;
2695 } else {
2696 return false;
db7d4e46
JP
2697 }
2698}
2699
a36de779
PS
2700static int
2701tnl_route_lookup_flow(const struct flow *oflow,
c2b878e0 2702 struct in6_addr *ip, struct xport **out_port)
a36de779
PS
2703{
2704 char out_dev[IFNAMSIZ];
2705 struct xbridge *xbridge;
2706 struct xlate_cfg *xcfg;
c2b878e0
TLSC
2707 struct in6_addr gw;
2708 struct in6_addr dst;
a36de779 2709
c2b878e0
TLSC
2710 dst = flow_tnl_dst(&oflow->tunnel);
2711 if (!ovs_router_lookup(&dst, out_dev, &gw)) {
a36de779
PS
2712 return -ENOENT;
2713 }
2714
c2b878e0
TLSC
2715 if (ipv6_addr_is_set(&gw) &&
2716 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
a36de779
PS
2717 *ip = gw;
2718 } else {
c2b878e0 2719 *ip = dst;
a36de779
PS
2720 }
2721
2722 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2723 ovs_assert(xcfg);
2724
2725 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
2726 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
2727 struct xport *port;
2728
2729 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
2730 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
2731 *out_port = port;
2732 return 0;
2733 }
2734 }
2735 }
2736 }
2737 return -ENOENT;
2738}
2739
2740static int
cdd42eda
JG
2741compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
2742 struct dp_packet *packet)
a36de779 2743{
cdd42eda 2744 struct xbridge *xbridge = out_dev->xbridge;
a36de779
PS
2745 struct ofpact_output output;
2746 struct flow flow;
2747
2748 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
cf62fa4c 2749 flow_extract(packet, &flow);
cdd42eda
JG
2750 flow.in_port.ofp_port = out_dev->ofp_port;
2751 output.port = OFPP_TABLE;
a36de779
PS
2752 output.max_len = 0;
2753
cdd42eda
JG
2754 return ofproto_dpif_execute_actions__(xbridge->ofproto, &flow, NULL,
2755 &output.ofpact, sizeof output,
2756 ctx->recurse, ctx->resubmits, packet);
a36de779
PS
2757}
2758
c2b878e0
TLSC
2759static void
2760tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
2761 const struct eth_addr eth_src,
2762 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
2763{
2764 struct dp_packet packet;
2765
2766 dp_packet_init(&packet, 0);
2767 compose_nd(&packet, eth_src, ipv6_src, ipv6_dst);
2768 compose_table_xlate(ctx, out_dev, &packet);
2769 dp_packet_uninit(&packet);
2770}
2771
a36de779 2772static void
cdd42eda 2773tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
74ff3298 2774 const struct eth_addr eth_src,
a36de779
PS
2775 ovs_be32 ip_src, ovs_be32 ip_dst)
2776{
cf62fa4c 2777 struct dp_packet packet;
a36de779 2778
cf62fa4c 2779 dp_packet_init(&packet, 0);
eb0b295e
BP
2780 compose_arp(&packet, ARP_OP_REQUEST,
2781 eth_src, eth_addr_zero, true, ip_src, ip_dst);
a36de779 2782
cdd42eda 2783 compose_table_xlate(ctx, out_dev, &packet);
cf62fa4c 2784 dp_packet_uninit(&packet);
a36de779
PS
2785}
2786
2787static int
81de18ec 2788build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
a36de779
PS
2789 const struct flow *flow, odp_port_t tunnel_odp_port)
2790{
2791 struct ovs_action_push_tnl tnl_push_data;
2792 struct xport *out_dev = NULL;
c2b878e0
TLSC
2793 ovs_be32 s_ip = 0, d_ip = 0;
2794 struct in6_addr s_ip6 = in6addr_any;
2795 struct in6_addr d_ip6 = in6addr_any;
74ff3298
JR
2796 struct eth_addr smac;
2797 struct eth_addr dmac;
a36de779 2798 int err;
c2b878e0
TLSC
2799 char buf_sip6[INET6_ADDRSTRLEN];
2800 char buf_dip6[INET6_ADDRSTRLEN];
a36de779 2801
c2b878e0 2802 err = tnl_route_lookup_flow(flow, &d_ip6, &out_dev);
a36de779 2803 if (err) {
81de18ec 2804 xlate_report(ctx, "native tunnel routing failed");
a36de779
PS
2805 return err;
2806 }
c2b878e0
TLSC
2807
2808 xlate_report(ctx, "tunneling to %s via %s",
2809 ipv6_string_mapped(buf_dip6, &d_ip6),
2810 netdev_get_name(out_dev->netdev));
a36de779
PS
2811
2812 /* Use mac addr of bridge port of the peer. */
74ff3298 2813 err = netdev_get_etheraddr(out_dev->netdev, &smac);
a36de779 2814 if (err) {
81de18ec 2815 xlate_report(ctx, "tunnel output device lacks Ethernet address");
a36de779
PS
2816 return err;
2817 }
2818
c2b878e0
TLSC
2819 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
2820 if (d_ip) {
2821 err = netdev_get_in4(out_dev->netdev, (struct in_addr *) &s_ip, NULL);
2822 if (err) {
2823 xlate_report(ctx, "tunnel output device lacks IPv4 address");
2824 return err;
2825 }
2826 in6_addr_set_mapped_ipv4(&s_ip6, s_ip);
2827 } else {
2828 err = netdev_get_in6(out_dev->netdev, &s_ip6);
2829 if (err) {
2830 xlate_report(ctx, "tunnel output device lacks IPv6 address");
2831 return err;
2832 }
a36de779
PS
2833 }
2834
c2b878e0 2835 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
a36de779 2836 if (err) {
c2b878e0
TLSC
2837 xlate_report(ctx, "neighbor cache miss for %s on bridge %s, "
2838 "sending %s request",
2839 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
2840 if (d_ip) {
2841 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
2842 } else {
2843 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
2844 }
a36de779
PS
2845 return err;
2846 }
c2b878e0 2847
a36de779
PS
2848 if (ctx->xin->xcache) {
2849 struct xc_entry *entry;
2850
53902038
TLSC
2851 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
2852 ovs_strlcpy(entry->u.tnl_neigh_cache.br_name, out_dev->xbridge->name,
2853 sizeof entry->u.tnl_neigh_cache.br_name);
c2b878e0 2854 entry->u.tnl_neigh_cache.d_ipv6 = d_ip6;
a36de779 2855 }
81de18ec 2856
c2b878e0
TLSC
2857 xlate_report(ctx, "tunneling from "ETH_ADDR_FMT" %s"
2858 " to "ETH_ADDR_FMT" %s",
2859 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
2860 ETH_ADDR_ARGS(dmac), buf_dip6);
2861
a36de779 2862 err = tnl_port_build_header(xport->ofport, flow,
370e373b 2863 dmac, smac, &s_ip6, &tnl_push_data);
a36de779
PS
2864 if (err) {
2865 return err;
2866 }
2867 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
2868 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
1520ef4f 2869 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
a36de779
PS
2870 return 0;
2871}
2872
704bb0bf
JS
2873static void
2874xlate_commit_actions(struct xlate_ctx *ctx)
2875{
2876 bool use_masked = ctx->xbridge->support.masked_set_action;
2877
2878 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2879 ctx->odp_actions, ctx->wc,
2880 use_masked);
2881}
2882
07659514
JS
2883static void
2884clear_conntrack(struct flow *flow)
2885{
2886 flow->ct_state = 0;
2887 flow->ct_zone = 0;
8e53fe8c 2888 flow->ct_mark = 0;
9daf2348 2889 memset(&flow->ct_label, 0, sizeof flow->ct_label);
07659514
JS
2890}
2891
9583bc14 2892static void
4e022ec0 2893compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
e93ef1c7 2894 const struct xlate_bond_recirc *xr, bool check_stp)
9583bc14 2895{
46c88433 2896 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
49a73e0c 2897 struct flow_wildcards *wc = ctx->wc;
33bf9176 2898 struct flow *flow = &ctx->xin->flow;
a36de779 2899 struct flow_tnl flow_tnl;
9583bc14 2900 ovs_be16 flow_vlan_tci;
1362e248 2901 uint32_t flow_pkt_mark;
9583bc14 2902 uint8_t flow_nw_tos;
4e022ec0 2903 odp_port_t out_port, odp_port;
a36de779 2904 bool tnl_push_pop_send = false;
ca077186 2905 uint8_t dscp;
9583bc14
EJ
2906
2907 /* If 'struct flow' gets additional metadata, we'll need to zero it out
2908 * before traversing a patch port. */
ffe4c74f 2909 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
a36de779 2910 memset(&flow_tnl, 0, sizeof flow_tnl);
9583bc14 2911
46c88433 2912 if (!xport) {
9583bc14
EJ
2913 xlate_report(ctx, "Nonexistent output port");
2914 return;
46c88433 2915 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
9583bc14
EJ
2916 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
2917 return;
0d1cee12 2918 } else if (check_stp) {
bbbca389 2919 if (is_stp(&ctx->base_flow)) {
9efd308e
DV
2920 if (!xport_stp_should_forward_bpdu(xport) &&
2921 !xport_rstp_should_manage_bpdu(xport)) {
2922 if (ctx->xbridge->stp != NULL) {
2923 xlate_report(ctx, "STP not in listening state, "
2924 "skipping bpdu output");
2925 } else if (ctx->xbridge->rstp != NULL) {
2926 xlate_report(ctx, "RSTP not managing BPDU in this state, "
2927 "skipping bpdu output");
2928 }
0d1cee12
K
2929 return;
2930 }
9efd308e
DV
2931 } else if (!xport_stp_forward_state(xport) ||
2932 !xport_rstp_forward_state(xport)) {
2933 if (ctx->xbridge->stp != NULL) {
2934 xlate_report(ctx, "STP not in forwarding state, "
2935 "skipping output");
2936 } else if (ctx->xbridge->rstp != NULL) {
2937 xlate_report(ctx, "RSTP not in forwarding state, "
2938 "skipping output");
2939 }
0d1cee12
K
2940 return;
2941 }
9583bc14
EJ
2942 }
2943
46c88433
EJ
2944 if (xport->peer) {
2945 const struct xport *peer = xport->peer;
9583bc14 2946 struct flow old_flow = ctx->xin->flow;
07659514 2947 bool old_conntrack = ctx->conntracked;
7f5fe537 2948 bool old_was_mpls = ctx->was_mpls;
18721c4a 2949 cls_version_t old_version = ctx->tables_version;
1774d762
JR
2950 struct ofpbuf old_stack = ctx->stack;
2951 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
ed9c9e3e
JR
2952 struct ofpbuf old_action_set = ctx->action_set;
2953 uint64_t actset_stub[1024 / 8];
9583bc14 2954
1774d762 2955 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
ed9c9e3e 2956 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
46c88433
EJ
2957 ctx->xbridge = peer->xbridge;
2958 flow->in_port.ofp_port = peer->ofp_port;
33bf9176
BP
2959 flow->metadata = htonll(0);
2960 memset(&flow->tunnel, 0, sizeof flow->tunnel);
2961 memset(flow->regs, 0, sizeof flow->regs);
c61f3870 2962 flow->actset_output = OFPP_UNSET;
07659514
JS
2963 ctx->conntracked = false;
2964 clear_conntrack(flow);
9583bc14 2965
621b8064
JR
2966 /* The bridge is now known so obtain its table version. */
2967 ctx->tables_version
2968 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
2969
515793d5 2970 if (!process_special(ctx, peer) && may_receive(peer, ctx)) {
9efd308e 2971 if (xport_stp_forward_state(peer) && xport_rstp_forward_state(peer)) {
e672ff9b 2972 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
ed9c9e3e 2973 if (ctx->action_set.size) {
e672ff9b
JR
2974 /* Translate action set only if not dropping the packet and
2975 * not recirculating. */
2976 if (!exit_recirculates(ctx)) {
2977 xlate_action_set(ctx);
2978 }
2979 }
2980 /* Check if need to recirculate. */
2981 if (exit_recirculates(ctx)) {
2982 compose_recirculate_action(ctx);
ed9c9e3e 2983 }
9583bc14 2984 } else {
9efd308e
DV
2985 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
2986 * the learning action look at the packet, then drop it. */
9583bc14 2987 struct flow old_base_flow = ctx->base_flow;
1520ef4f 2988 size_t old_size = ctx->odp_actions->size;
3d6151f3 2989 mirror_mask_t old_mirrors = ctx->mirrors;
f3d5b473 2990
e672ff9b 2991 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true);
3d6151f3 2992 ctx->mirrors = old_mirrors;
9583bc14 2993 ctx->base_flow = old_base_flow;
1520ef4f 2994 ctx->odp_actions->size = old_size;
e672ff9b
JR
2995
2996 /* Undo changes that may have been done for recirculation. */
2997 if (exit_recirculates(ctx)) {
2998 ctx->action_set.size = ctx->recirc_action_offset;
2999 ctx->recirc_action_offset = -1;
3000 ctx->last_unroll_offset = -1;
3001 }
9583bc14
EJ
3002 }
3003 }
3004
3005 ctx->xin->flow = old_flow;
832554e3 3006 ctx->xbridge = xport->xbridge;
ed9c9e3e
JR
3007 ofpbuf_uninit(&ctx->action_set);
3008 ctx->action_set = old_action_set;
1774d762
JR
3009 ofpbuf_uninit(&ctx->stack);
3010 ctx->stack = old_stack;
9583bc14 3011
621b8064
JR
3012 /* Restore calling bridge's lookup version. */
3013 ctx->tables_version = old_version;
3014
7f5fe537
JR
3015 /* The peer bridge popping MPLS should have no effect on the original
3016 * bridge. */
3017 ctx->was_mpls = old_was_mpls;
3018
07659514
JS
3019 /* The peer bridge's conntrack execution should have no effect on the
3020 * original bridge. */
3021 ctx->conntracked = old_conntrack;
3022
f3d5b473
JR
3023 /* The fact that the peer bridge exits (for any reason) does not mean
3024 * that the original bridge should exit. Specifically, if the peer
3025 * bridge recirculates (which typically modifies the packet), the
3026 * original bridge must continue processing with the original, not the
3027 * recirculated packet! */
3028 ctx->exit = false;
3029
fff1b9c0
JR
3030 /* Peer bridge errors do not propagate back. */
3031 ctx->error = XLATE_OK;
3032
9583bc14 3033 if (ctx->xin->resubmit_stats) {
46c88433
EJ
3034 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
3035 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
a1aeea86
AW
3036 if (peer->bfd) {
3037 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
3038 }
9583bc14 3039 }
b256dc52
JS
3040 if (ctx->xin->xcache) {
3041 struct xc_entry *entry;
3042
3043 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3044 entry->u.dev.tx = netdev_ref(xport->netdev);
3045 entry->u.dev.rx = netdev_ref(peer->netdev);
3046 entry->u.dev.bfd = bfd_ref(peer->bfd);
3047 }
9583bc14
EJ
3048 return;
3049 }
3050
33bf9176 3051 flow_vlan_tci = flow->vlan_tci;
1362e248 3052 flow_pkt_mark = flow->pkt_mark;
33bf9176 3053 flow_nw_tos = flow->nw_tos;
9583bc14 3054
16194afd
DDP
3055 if (count_skb_priorities(xport)) {
3056 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3057 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3058 wc->masks.nw_tos |= IP_DSCP_MASK;
3059 flow->nw_tos &= ~IP_DSCP_MASK;
3060 flow->nw_tos |= dscp;
3061 }
9583bc14
EJ
3062 }
3063
46c88433 3064 if (xport->is_tunnel) {
c2b878e0 3065 struct in6_addr dst;
9583bc14
EJ
3066 /* Save tunnel metadata so that changes made due to
3067 * the Logical (tunnel) Port are not visible for any further
3068 * matches, while explicit set actions on tunnel metadata are.
3069 */
a36de779 3070 flow_tnl = flow->tunnel;
49a73e0c 3071 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
4e022ec0 3072 if (odp_port == ODPP_NONE) {
9583bc14
EJ
3073 xlate_report(ctx, "Tunneling decided against output");
3074 goto out; /* restore flow_nw_tos */
3075 }
c2b878e0
TLSC
3076 dst = flow_tnl_dst(&flow->tunnel);
3077 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
9583bc14
EJ
3078 xlate_report(ctx, "Not tunneling to our own address");
3079 goto out; /* restore flow_nw_tos */
3080 }
3081 if (ctx->xin->resubmit_stats) {
46c88433 3082 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14 3083 }
b256dc52
JS
3084 if (ctx->xin->xcache) {
3085 struct xc_entry *entry;
3086
3087 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3088 entry->u.dev.tx = netdev_ref(xport->netdev);
3089 }
9583bc14 3090 out_port = odp_port;
a36de779 3091 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
81de18ec 3092 xlate_report(ctx, "output to native tunnel");
a36de779
PS
3093 tnl_push_pop_send = true;
3094 } else {
81de18ec 3095 xlate_report(ctx, "output to kernel tunnel");
1520ef4f 3096 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
a36de779
PS
3097 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3098 }
9583bc14 3099 } else {
46c88433 3100 odp_port = xport->odp_port;
7614e5d0 3101 out_port = odp_port;
46c88433 3102 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
7614e5d0
JR
3103 ofp_port_t vlandev_port;
3104
1dd35f8a 3105 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
7614e5d0
JR
3106 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto,
3107 ofp_port, flow->vlan_tci);
3108 if (vlandev_port != ofp_port) {
3109 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
3110 flow->vlan_tci = htons(0);
3111 }
9583bc14 3112 }
9583bc14 3113 }
9583bc14 3114
4e022ec0 3115 if (out_port != ODPP_NONE) {
704bb0bf 3116 xlate_commit_actions(ctx);
adcf00ba 3117
e93ef1c7 3118 if (xr) {
347bf289 3119 struct ovs_action_hash *act_hash;
adcf00ba 3120
347bf289 3121 /* Hash action. */
1520ef4f 3122 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
347bf289
AZ
3123 OVS_ACTION_ATTR_HASH,
3124 sizeof *act_hash);
3125 act_hash->hash_alg = xr->hash_alg;
62ac1f20 3126 act_hash->hash_basis = xr->hash_basis;
347bf289
AZ
3127
3128 /* Recirc action. */
1520ef4f 3129 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
347bf289 3130 xr->recirc_id);
adcf00ba 3131 } else {
a36de779
PS
3132
3133 if (tnl_push_pop_send) {
3134 build_tunnel_send(ctx, xport, flow, odp_port);
3135 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3136 } else {
3137 odp_port_t odp_tnl_port = ODPP_NONE;
3138
3139 /* XXX: Write better Filter for tunnel port. We can use inport
3140 * int tunnel-port flow to avoid these checks completely. */
3141 if (ofp_port == OFPP_LOCAL &&
3142 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3143
3144 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3145 }
3146
3147 if (odp_tnl_port != ODPP_NONE) {
1520ef4f 3148 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3149 OVS_ACTION_ATTR_TUNNEL_POP,
3150 odp_tnl_port);
3151 } else {
3152 /* Tunnel push-pop action is not compatible with
3153 * IPFIX action. */
a6092018 3154 compose_ipfix_action(ctx, out_port);
1520ef4f 3155 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3156 OVS_ACTION_ATTR_OUTPUT,
3157 out_port);
3158 }
3159 }
adcf00ba 3160 }
9583bc14 3161
6cbbf4fa
EJ
3162 ctx->sflow_odp_port = odp_port;
3163 ctx->sflow_n_outputs++;
2031ef97 3164 ctx->nf_output_iface = ofp_port;
6cbbf4fa
EJ
3165 }
3166
7efbc3b7
BP
3167 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3168 mirror_packet(ctx, xport->xbundle,
3169 xbundle_mirror_dst(xport->xbundle->xbridge,
3170 xport->xbundle));
3171 }
3172
6cbbf4fa 3173 out:
9583bc14 3174 /* Restore flow */
33bf9176 3175 flow->vlan_tci = flow_vlan_tci;
1362e248 3176 flow->pkt_mark = flow_pkt_mark;
33bf9176 3177 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
3178}
3179
3180static void
e93ef1c7
JR
3181compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3182 const struct xlate_bond_recirc *xr)
9583bc14 3183{
e93ef1c7 3184 compose_output_action__(ctx, ofp_port, xr, true);
9583bc14
EJ
3185}
3186
bb61b33d
BP
3187static void
3188xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
bb61b33d
BP
3189{
3190 struct rule_dpif *old_rule = ctx->rule;
8b1e5560 3191 ovs_be64 old_cookie = ctx->rule_cookie;
dc723c44 3192 const struct rule_actions *actions;
bb61b33d
BP
3193
3194 if (ctx->xin->resubmit_stats) {
70742c7f 3195 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
3196 }
3197
98b07853 3198 ctx->resubmits++;
bb61b33d
BP
3199 ctx->recurse++;
3200 ctx->rule = rule;
8b1e5560 3201 ctx->rule_cookie = rule_dpif_get_flow_cookie(rule);
6f00e29b
BP
3202 actions = rule_dpif_get_actions(rule);
3203 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
8b1e5560 3204 ctx->rule_cookie = old_cookie;
bb61b33d
BP
3205 ctx->rule = old_rule;
3206 ctx->recurse--;
bb61b33d
BP
3207}
3208
bd3240ba
SH
3209static bool
3210xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 3211{
adcf00ba 3212 if (ctx->recurse >= MAX_RESUBMIT_RECURSION + MAX_INTERNAL_RESUBMITS) {
fff1b9c0
JR
3213 XLATE_REPORT_ERROR(ctx, "resubmit actions recursed over %d times",
3214 MAX_RESUBMIT_RECURSION);
3215 ctx->error = XLATE_RECURSION_TOO_DEEP;
adcf00ba 3216 } else if (ctx->resubmits >= MAX_RESUBMITS + MAX_INTERNAL_RESUBMITS) {
fff1b9c0
JR
3217 XLATE_REPORT_ERROR(ctx, "over %d resubmit actions", MAX_RESUBMITS);
3218 ctx->error = XLATE_TOO_MANY_RESUBMITS;
1520ef4f 3219 } else if (ctx->odp_actions->size > UINT16_MAX) {
fff1b9c0
JR
3220 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of actions");
3221 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3222 ctx->exit = true; /* XXX: translation still terminated! */
6fd6ed71 3223 } else if (ctx->stack.size >= 65536) {
fff1b9c0
JR
3224 XLATE_REPORT_ERROR(ctx, "resubmits yielded over 64 kB of stack");
3225 ctx->error = XLATE_STACK_TOO_DEEP;
98b07853 3226 } else {
bd3240ba
SH
3227 return true;
3228 }
3229
3230 return false;
3231}
3232
3233static void
6d328fa2
SH
3234xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
3235 bool may_packet_in, bool honor_table_miss)
bd3240ba 3236{
1d741d6d
JR
3237 /* Check if we need to recirculate before matching in a table. */
3238 if (ctx->was_mpls) {
3239 ctx_trigger_recirculation(ctx);
3240 return;
3241 }
bd3240ba 3242 if (xlate_resubmit_resource_check(ctx)) {
9583bc14 3243 uint8_t old_table_id = ctx->table_id;
3f207910 3244 struct rule_dpif *rule;
9583bc14
EJ
3245
3246 ctx->table_id = table_id;
3247
34dd0d78 3248 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
621b8064 3249 ctx->tables_version,
49a73e0c 3250 &ctx->xin->flow, ctx->xin->wc,
34dd0d78
JR
3251 ctx->xin->resubmit_stats,
3252 &ctx->table_id, in_port,
3253 may_packet_in, honor_table_miss);
9583bc14 3254
a8c31348
BP
3255 if (OVS_UNLIKELY(ctx->xin->resubmit_hook)) {
3256 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse + 1);
ad3efdcb
EJ
3257 }
3258
a2143702 3259 if (rule) {
83709dfa
JR
3260 /* Fill in the cache entry here instead of xlate_recursively
3261 * to make the reference counting more explicit. We take a
3262 * reference in the lookups above if we are going to cache the
3263 * rule. */
3264 if (ctx->xin->xcache) {
3265 struct xc_entry *entry;
3266
3267 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
3268 entry->u.rule = rule;
1e1e1d19 3269 rule_dpif_ref(rule);
83709dfa 3270 }
bb61b33d 3271 xlate_recursively(ctx, rule);
ad3efdcb
EJ
3272 }
3273
9583bc14 3274 ctx->table_id = old_table_id;
98b07853 3275 return;
9583bc14
EJ
3276 }
3277}
3278
f4fb341b 3279static void
1e684d7d
RW
3280xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3281 struct ofputil_bucket *bucket)
3282{
3283 if (ctx->xin->resubmit_stats) {
3284 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3285 }
3286 if (ctx->xin->xcache) {
3287 struct xc_entry *entry;
3288
3289 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
3290 entry->u.group.group = group_dpif_ref(group);
3291 entry->u.group.bucket = bucket;
3292 }
3293}
3294
3295static void
3296xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
f4fb341b
SH
3297{
3298 uint64_t action_list_stub[1024 / 8];
3299 struct ofpbuf action_list, action_set;
5b09e569
JR
3300 struct flow old_flow = ctx->xin->flow;
3301 bool old_was_mpls = ctx->was_mpls;
f4fb341b
SH
3302
3303 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
3304 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
3305
3306 ofpacts_execute_action_set(&action_list, &action_set);
3307 ctx->recurse++;
6fd6ed71 3308 do_xlate_actions(action_list.data, action_list.size, ctx);
f4fb341b
SH
3309 ctx->recurse--;
3310
3311 ofpbuf_uninit(&action_set);
3312 ofpbuf_uninit(&action_list);
5b09e569 3313
e672ff9b
JR
3314 /* Check if need to recirculate. */
3315 if (exit_recirculates(ctx)) {
3316 compose_recirculate_action(ctx);
3317 }
3318
5b09e569
JR
3319 /* Roll back flow to previous state.
3320 * This is equivalent to cloning the packet for each bucket.
3321 *
3322 * As a side effect any subsequently applied actions will
3323 * also effectively be applied to a clone of the packet taken
3324 * just before applying the all or indirect group.
3325 *
3326 * Note that group buckets are action sets, hence they cannot modify the
3327 * main action set. Also any stack actions are ignored when executing an
3328 * action set, so group buckets cannot change the stack either.
3329 * However, we do allow resubmit actions in group buckets, which could
3330 * break the above assumptions. It is up to the controller to not mess up
3331 * with the action_set and stack in the tables resubmitted to from
3332 * group buckets. */
3333 ctx->xin->flow = old_flow;
3334
3335 /* The group bucket popping MPLS should have no effect after bucket
3336 * execution. */
3337 ctx->was_mpls = old_was_mpls;
3338
3339 /* The fact that the group bucket exits (for any reason) does not mean that
3340 * the translation after the group action should exit. Specifically, if
3341 * the group bucket recirculates (which typically modifies the packet), the
3342 * actions after the group action must continue processing with the
3343 * original, not the recirculated packet! */
3344 ctx->exit = false;
f4fb341b
SH
3345}
3346
3347static void
3348xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3349{
1e684d7d 3350 struct ofputil_bucket *bucket;
ca6ba700 3351 const struct ovs_list *buckets;
f4fb341b
SH
3352
3353 group_dpif_get_buckets(group, &buckets);
3354
3355 LIST_FOR_EACH (bucket, list_node, buckets) {
3356 xlate_group_bucket(ctx, bucket);
f4fb341b 3357 }
1e684d7d 3358 xlate_group_stats(ctx, group, NULL);
f4fb341b
SH
3359}
3360
dd8cd4b4
SH
3361static void
3362xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3363{
1e684d7d 3364 struct ofputil_bucket *bucket;
dd8cd4b4
SH
3365
3366 bucket = group_first_live_bucket(ctx, group, 0);
3367 if (bucket) {
3368 xlate_group_bucket(ctx, bucket);
1e684d7d 3369 xlate_group_stats(ctx, group, bucket);
dd8cd4b4
SH
3370 }
3371}
3372
fe7e5749 3373static void
7565c3e4 3374xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
fe7e5749 3375{
49a73e0c 3376 struct flow_wildcards *wc = ctx->wc;
1e684d7d 3377 struct ofputil_bucket *bucket;
fe7e5749
SH
3378 uint32_t basis;
3379
1d1aae0b 3380 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
80e3509d 3381 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
fe7e5749
SH
3382 bucket = group_best_live_bucket(ctx, group, basis);
3383 if (bucket) {
fe7e5749 3384 xlate_group_bucket(ctx, bucket);
1e684d7d 3385 xlate_group_stats(ctx, group, bucket);
fe7e5749
SH
3386 }
3387}
3388
0c4b9393
SH
3389static void
3390xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3391{
3392 struct mf_bitmap hash_fields = MF_BITMAP_INITIALIZER;
0c4b9393
SH
3393 const struct field_array *fields;
3394 struct ofputil_bucket *bucket;
3395 uint32_t basis;
3396 int i;
3397
3398 fields = group_dpif_get_fields(group);
3399 basis = hash_uint64(group_dpif_get_selection_method_param(group));
3400
3401 /* Determine which fields to hash */
3402 for (i = 0; i < MFF_N_IDS; i++) {
3403 if (bitmap_is_set(fields->used.bm, i)) {
3404 const struct mf_field *mf;
3405
3406 /* If the field is already present in 'hash_fields' then
3407 * this loop has already checked that it and its pre-requisites
3408 * are present in the flow and its pre-requisites have
3409 * already been added to 'hash_fields'. There is nothing more
3410 * to do here and as an optimisation the loop can continue. */
3411 if (bitmap_is_set(hash_fields.bm, i)) {
3412 continue;
3413 }
3414
3415 mf = mf_from_id(i);
3416
3417 /* Only hash a field if it and its pre-requisites are present
3418 * in the flow. */
3419 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow)) {
3420 continue;
3421 }
3422
3423 /* Hash both the field and its pre-requisites */
3424 mf_bitmap_set_field_and_prereqs(mf, &hash_fields);
3425 }
3426 }
3427
3428 /* Hash the fields */
3429 for (i = 0; i < MFF_N_IDS; i++) {
3430 if (bitmap_is_set(hash_fields.bm, i)) {
3431 const struct mf_field *mf = mf_from_id(i);
3432 union mf_value value;
3433 int j;
3434
3435 mf_get_value(mf, &ctx->xin->flow, &value);
3436 /* This seems inefficient but so does apply_mask() */
3437 for (j = 0; j < mf->n_bytes; j++) {
3438 ((uint8_t *) &value)[j] &= ((uint8_t *) &fields->value[i])[j];
3439 }
3440 basis = hash_bytes(&value, mf->n_bytes, basis);
3441
1cb20095
JG
3442 /* For tunnels, hash in whether the field is present. */
3443 if (mf_is_tun_metadata(mf)) {
3444 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
3445 }
3446
49a73e0c 3447 mf_mask_field(mf, &ctx->wc->masks);
0c4b9393
SH
3448 }
3449 }
3450
3451 bucket = group_best_live_bucket(ctx, group, basis);
3452 if (bucket) {
3453 xlate_group_bucket(ctx, bucket);
3454 xlate_group_stats(ctx, group, bucket);
3455 }
3456}
3457
7565c3e4
SH
3458static void
3459xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3460{
3461 const char *selection_method = group_dpif_get_selection_method(group);
3462
3463 if (selection_method[0] == '\0') {
3464 xlate_default_select_group(ctx, group);
0c4b9393
SH
3465 } else if (!strcasecmp("hash", selection_method)) {
3466 xlate_hash_fields_select_group(ctx, group);
7565c3e4
SH
3467 } else {
3468 /* Parsing of groups should ensure this never happens */
3469 OVS_NOT_REACHED();
3470 }
3471}
3472
f4fb341b
SH
3473static void
3474xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3475{
0eb48fe1 3476 bool was_in_group = ctx->in_group;
5a070238
BP
3477 ctx->in_group = true;
3478
f4fb341b
SH
3479 switch (group_dpif_get_type(group)) {
3480 case OFPGT11_ALL:
3481 case OFPGT11_INDIRECT:
3482 xlate_all_group(ctx, group);
3483 break;
3484 case OFPGT11_SELECT:
fe7e5749 3485 xlate_select_group(ctx, group);
f4fb341b 3486 break;
dd8cd4b4
SH
3487 case OFPGT11_FF:
3488 xlate_ff_group(ctx, group);
3489 break;
f4fb341b 3490 default:
428b2edd 3491 OVS_NOT_REACHED();
f4fb341b 3492 }
809c7548 3493 group_dpif_unref(group);
5a070238 3494
0eb48fe1 3495 ctx->in_group = was_in_group;
f4fb341b
SH
3496}
3497
3498static bool
3499xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3500{
0eb48fe1 3501 if (xlate_resubmit_resource_check(ctx)) {
f4fb341b
SH
3502 struct group_dpif *group;
3503 bool got_group;
3504
3505 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
3506 if (got_group) {
3507 xlate_group_action__(ctx, group);
3508 } else {
3509 return true;
3510 }
3511 }
3512
3513 return false;
3514}
3515
9583bc14
EJ
3516static void
3517xlate_ofpact_resubmit(struct xlate_ctx *ctx,
3518 const struct ofpact_resubmit *resubmit)
3519{
4e022ec0 3520 ofp_port_t in_port;
9583bc14 3521 uint8_t table_id;
adcf00ba
AZ
3522 bool may_packet_in = false;
3523 bool honor_table_miss = false;
3524
3525 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
3526 /* Still allow missed packets to be sent to the controller
3527 * if resubmitting from an internal table. */
3528 may_packet_in = true;
3529 honor_table_miss = true;
3530 }
9583bc14
EJ
3531
3532 in_port = resubmit->in_port;
3533 if (in_port == OFPP_IN_PORT) {
4e022ec0 3534 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
3535 }
3536
3537 table_id = resubmit->table_id;
3538 if (table_id == 255) {
3539 table_id = ctx->table_id;
3540 }
3541
adcf00ba
AZ
3542 xlate_table_action(ctx, in_port, table_id, may_packet_in,
3543 honor_table_miss);
9583bc14
EJ
3544}
3545
3546static void
3547flood_packets(struct xlate_ctx *ctx, bool all)
3548{
46c88433 3549 const struct xport *xport;
9583bc14 3550
46c88433
EJ
3551 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
3552 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3553 continue;
3554 }
3555
3556 if (all) {
e93ef1c7 3557 compose_output_action__(ctx, xport->ofp_port, NULL, false);
46c88433 3558 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
e93ef1c7 3559 compose_output_action(ctx, xport->ofp_port, NULL);
9583bc14
EJ
3560 }
3561 }
3562
2031ef97 3563 ctx->nf_output_iface = NF_OUT_FLOOD;
9583bc14
EJ
3564}
3565
3566static void
3567execute_controller_action(struct xlate_ctx *ctx, int len,
3568 enum ofp_packet_in_reason reason,
3569 uint16_t controller_id)
3570{
e14deea0 3571 struct dp_packet *packet;
9583bc14 3572
04594cd5 3573 ctx->xout->slow |= SLOW_CONTROLLER;
b476e2f2 3574 xlate_commit_actions(ctx);
9583bc14
EJ
3575 if (!ctx->xin->packet) {
3576 return;
3577 }
3578
cf62fa4c 3579 packet = dp_packet_clone(ctx->xin->packet);
9583bc14 3580
41ccaa24 3581 odp_execute_actions(NULL, &packet, 1, false,
1520ef4f 3582 ctx->odp_actions->data, ctx->odp_actions->size, NULL);
9583bc14 3583
9bfe9334
BP
3584 /* A packet sent by an action in a table-miss rule is considered an
3585 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
3586 * it will get translated back to OFPR_ACTION for those versions. */
3587 if (reason == OFPR_ACTION
3588 && ctx->rule && rule_dpif_is_table_miss(ctx->rule)) {
3589 reason = OFPR_EXPLICIT_MISS;
3590 }
3591
3592 size_t packet_len = dp_packet_size(packet);
0fb7792a 3593
a2b53dec
BP
3594 struct ofproto_async_msg *am = xmalloc(sizeof *am);
3595 *am = (struct ofproto_async_msg) {
9bfe9334 3596 .controller_id = controller_id,
a2b53dec
BP
3597 .oam = OAM_PACKET_IN,
3598 .pin = {
3599 .up = {
3600 .packet = dp_packet_steal_data(packet),
3601 .len = packet_len,
3602 .reason = reason,
3603 .table_id = ctx->table_id,
3604 .cookie = ctx->rule_cookie,
3605 },
3606 .max_len = len,
9bfe9334 3607 },
9bfe9334 3608 };
a2b53dec 3609 flow_get_metadata(&ctx->xin->flow, &am->pin.up.flow_metadata);
9583bc14 3610
a2b53dec 3611 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
e14deea0 3612 dp_packet_delete(packet);
9583bc14
EJ
3613}
3614
7bbdd84f 3615static void
07659514 3616compose_recirculate_action__(struct xlate_ctx *ctx, uint8_t table)
7bbdd84f 3617{
e672ff9b 3618 struct recirc_metadata md;
7bbdd84f 3619 uint32_t id;
7bbdd84f 3620
e672ff9b 3621 recirc_metadata_from_flow(&md, &ctx->xin->flow);
7bbdd84f 3622
e672ff9b 3623 ovs_assert(ctx->recirc_action_offset >= 0);
7bbdd84f 3624
2082425c 3625 struct recirc_state state = {
07659514 3626 .table_id = table,
2082425c
BP
3627 .ofproto = ctx->xbridge->ofproto,
3628 .metadata = md,
5c1b2314
BP
3629 .stack = ctx->stack.data,
3630 .n_stack = ctx->stack.size / sizeof(union mf_subvalue),
29bae541 3631 .mirrors = ctx->mirrors,
07659514 3632 .conntracked = ctx->conntracked,
417509fa
BP
3633 .ofpacts = ((struct ofpact *) ctx->action_set.data
3634 + ctx->recirc_action_offset / sizeof(struct ofpact)),
3635 .ofpacts_len = ctx->action_set.size - ctx->recirc_action_offset,
3636 .action_set = ctx->action_set.data,
2082425c 3637 .action_set_len = ctx->recirc_action_offset,
2082425c
BP
3638 };
3639
fbf5d6ec
JR
3640 /* Allocate a unique recirc id for the given metadata state in the
3641 * flow. An existing id, with a new reference to the corresponding
3642 * recirculation context, will be returned if possible.
3643 * The life-cycle of this recirc id is managed by associating it
3644 * with the udpif key ('ukey') created for each new datapath flow. */
3645 id = recirc_alloc_id_ctx(&state);
3646 if (!id) {
3647 XLATE_REPORT_ERROR(ctx, "Failed to allocate recirculation id");
3648 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
3649 return;
7bbdd84f 3650 }
fbf5d6ec 3651 recirc_refs_add(&ctx->xout->recircs, id);
7bbdd84f 3652
1520ef4f 3653 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
e672ff9b
JR
3654
3655 /* Undo changes done by recirculation. */
3656 ctx->action_set.size = ctx->recirc_action_offset;
3657 ctx->recirc_action_offset = -1;
3658 ctx->last_unroll_offset = -1;
7bbdd84f
SH
3659}
3660
07659514
JS
3661/* Called only when ctx->recirc_action_offset is set. */
3662static void
3663compose_recirculate_action(struct xlate_ctx *ctx)
3664{
3665 xlate_commit_actions(ctx);
3666 compose_recirculate_action__(ctx, 0);
3667}
3668
e37b8437
JS
3669/* Fork the pipeline here. The current packet will continue processing the
3670 * current action list. A clone of the current packet will recirculate, skip
3671 * the remainder of the current action list and asynchronously resume pipeline
3672 * processing in 'table' with the current metadata and action set. */
3673static void
3674compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
3675{
3676 ctx->recirc_action_offset = ctx->action_set.size;
3677 compose_recirculate_action__(ctx, table);
3678}
3679
8bfd0fda
BP
3680static void
3681compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 3682{
33bf9176 3683 struct flow *flow = &ctx->xin->flow;
8bfd0fda 3684 int n;
33bf9176 3685
8bfd0fda 3686 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 3687
49a73e0c 3688 n = flow_count_mpls_labels(flow, ctx->wc);
8bfd0fda 3689 if (!n) {
704bb0bf 3690 xlate_commit_actions(ctx);
8bfd0fda
BP
3691 } else if (n >= FLOW_MAX_MPLS_LABELS) {
3692 if (ctx->xin->packet != NULL) {
fff1b9c0 3693 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
8bfd0fda
BP
3694 "MPLS push action can't be performed as it would "
3695 "have more MPLS LSEs than the %d supported.",
3696 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
9583bc14 3697 }
fff1b9c0 3698 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
8bfd0fda 3699 return;
9583bc14 3700 }
b0a17866 3701
49a73e0c 3702 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc);
9583bc14
EJ
3703}
3704
8bfd0fda 3705static void
9cfef3d0 3706compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 3707{
8bfd0fda 3708 struct flow *flow = &ctx->xin->flow;
49a73e0c 3709 int n = flow_count_mpls_labels(flow, ctx->wc);
33bf9176 3710
49a73e0c 3711 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
2494ccd7 3712 if (ctx->xbridge->support.odp.recirc) {
7bbdd84f
SH
3713 ctx->was_mpls = true;
3714 }
3715 } else if (n >= FLOW_MAX_MPLS_LABELS) {
8bfd0fda 3716 if (ctx->xin->packet != NULL) {
fff1b9c0 3717 XLATE_REPORT_ERROR(ctx, "bridge %s: dropping packet on which an "
8bfd0fda
BP
3718 "MPLS pop action can't be performed as it has "
3719 "more MPLS LSEs than the %d supported.",
3720 ctx->xbridge->name, FLOW_MAX_MPLS_LABELS);
3721 }
fff1b9c0 3722 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
1520ef4f 3723 ofpbuf_clear(ctx->odp_actions);
9583bc14
EJ
3724 }
3725}
3726
3727static bool
3728compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
3729{
33bf9176
BP
3730 struct flow *flow = &ctx->xin->flow;
3731
3732 if (!is_ip_any(flow)) {
9583bc14
EJ
3733 return false;
3734 }
3735
49a73e0c 3736 ctx->wc->masks.nw_ttl = 0xff;
33bf9176
BP
3737 if (flow->nw_ttl > 1) {
3738 flow->nw_ttl--;
9583bc14
EJ
3739 return false;
3740 } else {
3741 size_t i;
3742
3743 for (i = 0; i < ids->n_controllers; i++) {
3744 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
3745 ids->cnt_ids[i]);
3746 }
3747
3748 /* Stop processing for current table. */
3749 return true;
3750 }
3751}
3752
8bfd0fda 3753static void
097d4939
JR
3754compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
3755{
8bfd0fda 3756 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3757 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
8bfd0fda 3758 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 3759 }
097d4939
JR
3760}
3761
8bfd0fda 3762static void
097d4939
JR
3763compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
3764{
8bfd0fda 3765 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3766 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
8bfd0fda 3767 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 3768 }
097d4939
JR
3769}
3770
8bfd0fda 3771static void
9cfef3d0 3772compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 3773{
8bfd0fda 3774 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 3775 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda 3776 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 3777 }
9583bc14
EJ
3778}
3779
3780static bool
9cfef3d0 3781compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 3782{
33bf9176 3783 struct flow *flow = &ctx->xin->flow;
1dd35f8a 3784
8bfd0fda 3785 if (eth_type_mpls(flow->dl_type)) {
22d38fca
JR
3786 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
3787
49a73e0c 3788 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda
BP
3789 if (ttl > 1) {
3790 ttl--;
3791 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
3792 return false;
3793 } else {
3794 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
8bfd0fda 3795 }
9583bc14 3796 }
22d38fca
JR
3797
3798 /* Stop processing for current table. */
3799 return true;
9583bc14
EJ
3800}
3801
3802static void
3803xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 3804 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 3805{
2031ef97 3806 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
9583bc14 3807
2031ef97 3808 ctx->nf_output_iface = NF_OUT_DROP;
9583bc14
EJ
3809
3810 switch (port) {
3811 case OFPP_IN_PORT:
e93ef1c7 3812 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
9583bc14
EJ
3813 break;
3814 case OFPP_TABLE:
4e022ec0 3815 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 3816 0, may_packet_in, true);
9583bc14
EJ
3817 break;
3818 case OFPP_NORMAL:
3819 xlate_normal(ctx);
3820 break;
3821 case OFPP_FLOOD:
3822 flood_packets(ctx, false);
3823 break;
3824 case OFPP_ALL:
3825 flood_packets(ctx, true);
3826 break;
3827 case OFPP_CONTROLLER:
3a11fd5b 3828 execute_controller_action(ctx, max_len,
029ca940
SS
3829 (ctx->in_group ? OFPR_GROUP
3830 : ctx->in_action_set ? OFPR_ACTION_SET
3831 : OFPR_ACTION),
3832 0);
9583bc14
EJ
3833 break;
3834 case OFPP_NONE:
3835 break;
3836 case OFPP_LOCAL:
3837 default:
4e022ec0 3838 if (port != ctx->xin->flow.in_port.ofp_port) {
e93ef1c7 3839 compose_output_action(ctx, port, NULL);
9583bc14
EJ
3840 } else {
3841 xlate_report(ctx, "skipping output to input port");
3842 }
3843 break;
3844 }
3845
3846 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2031ef97
BP
3847 ctx->nf_output_iface = NF_OUT_FLOOD;
3848 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
3849 ctx->nf_output_iface = prev_nf_output_iface;
9583bc14 3850 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2031ef97
BP
3851 ctx->nf_output_iface != NF_OUT_FLOOD) {
3852 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
3853 }
3854}
3855
3856static void
3857xlate_output_reg_action(struct xlate_ctx *ctx,
3858 const struct ofpact_output_reg *or)
3859{
3860 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
3861 if (port <= UINT16_MAX) {
3862 union mf_subvalue value;
3863
3864 memset(&value, 0xff, sizeof value);
49a73e0c 3865 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
4e022ec0
AW
3866 xlate_output_action(ctx, u16_to_ofp(port),
3867 or->max_len, false);
9583bc14
EJ
3868 }
3869}
3870
3871static void
3872xlate_enqueue_action(struct xlate_ctx *ctx,
3873 const struct ofpact_enqueue *enqueue)
3874{
4e022ec0 3875 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
3876 uint32_t queue_id = enqueue->queue;
3877 uint32_t flow_priority, priority;
3878 int error;
3879
3880 /* Translate queue to priority. */
89a8a7f0 3881 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
3882 if (error) {
3883 /* Fall back to ordinary output action. */
3884 xlate_output_action(ctx, enqueue->port, 0, false);
3885 return;
3886 }
3887
3888 /* Check output port. */
3889 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
3890 ofp_port = ctx->xin->flow.in_port.ofp_port;
3891 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
3892 return;
3893 }
3894
3895 /* Add datapath actions. */
3896 flow_priority = ctx->xin->flow.skb_priority;
3897 ctx->xin->flow.skb_priority = priority;
e93ef1c7 3898 compose_output_action(ctx, ofp_port, NULL);
9583bc14
EJ
3899 ctx->xin->flow.skb_priority = flow_priority;
3900
3901 /* Update NetFlow output port. */
2031ef97
BP
3902 if (ctx->nf_output_iface == NF_OUT_DROP) {
3903 ctx->nf_output_iface = ofp_port;
3904 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
3905 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
3906 }
3907}
3908
3909static void
3910xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
3911{
3912 uint32_t skb_priority;
3913
89a8a7f0 3914 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
3915 ctx->xin->flow.skb_priority = skb_priority;
3916 } else {
3917 /* Couldn't translate queue to a priority. Nothing to do. A warning
3918 * has already been logged. */
3919 }
3920}
3921
3922static bool
46c88433 3923slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 3924{
46c88433
EJ
3925 const struct xbridge *xbridge = xbridge_;
3926 struct xport *port;
9583bc14
EJ
3927
3928 switch (ofp_port) {
3929 case OFPP_IN_PORT:
3930 case OFPP_TABLE:
3931 case OFPP_NORMAL:
3932 case OFPP_FLOOD:
3933 case OFPP_ALL:
3934 case OFPP_NONE:
3935 return true;
3936 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
3937 return false;
3938 default:
46c88433 3939 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
3940 return port ? port->may_enable : false;
3941 }
3942}
3943
3944static void
3945xlate_bundle_action(struct xlate_ctx *ctx,
3946 const struct ofpact_bundle *bundle)
3947{
4e022ec0 3948 ofp_port_t port;
9583bc14 3949
49a73e0c 3950 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
46c88433 3951 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 3952 if (bundle->dst.field) {
49a73e0c 3953 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
9583bc14
EJ
3954 } else {
3955 xlate_output_action(ctx, port, 0, false);
3956 }
3957}
3958
3959static void
4165b5e0
JS
3960xlate_learn_action__(struct xlate_ctx *ctx, const struct ofpact_learn *learn,
3961 struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
9583bc14 3962{
4165b5e0
JS
3963 learn_execute(learn, &ctx->xin->flow, fm, ofpacts);
3964 if (ctx->xin->may_learn) {
3965 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, fm);
3966 }
3967}
9583bc14 3968
4165b5e0
JS
3969static void
3970xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
3971{
49a73e0c 3972 learn_mask(learn, ctx->wc);
9583bc14 3973
b256dc52
JS
3974 if (ctx->xin->xcache) {
3975 struct xc_entry *entry;
3976
3977 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4165b5e0
JS
3978 entry->u.learn.ofproto = ctx->xbridge->ofproto;
3979 entry->u.learn.fm = xmalloc(sizeof *entry->u.learn.fm);
3980 entry->u.learn.ofpacts = ofpbuf_new(64);
3981 xlate_learn_action__(ctx, learn, entry->u.learn.fm,
3982 entry->u.learn.ofpacts);
3983 } else if (ctx->xin->may_learn) {
3984 uint64_t ofpacts_stub[1024 / 8];
3985 struct ofputil_flow_mod fm;
3986 struct ofpbuf ofpacts;
3987
3988 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
3989 xlate_learn_action__(ctx, learn, &fm, &ofpacts);
3990 ofpbuf_uninit(&ofpacts);
b256dc52
JS
3991 }
3992}
3993
3994static void
3995xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
3996 uint16_t idle_timeout, uint16_t hard_timeout)
3997{
3998 if (tcp_flags & (TCP_FIN | TCP_RST)) {
3999 rule_dpif_reduce_timeouts(rule, idle_timeout, hard_timeout);
4000 }
9583bc14
EJ
4001}
4002
9583bc14
EJ
4003static void
4004xlate_fin_timeout(struct xlate_ctx *ctx,
4005 const struct ofpact_fin_timeout *oft)
4006{
b256dc52
JS
4007 if (ctx->rule) {
4008 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4009 oft->fin_idle_timeout, oft->fin_hard_timeout);
4010 if (ctx->xin->xcache) {
4011 struct xc_entry *entry;
4012
4013 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
83709dfa
JR
4014 /* XC_RULE already holds a reference on the rule, none is taken
4015 * here. */
b256dc52
JS
4016 entry->u.fin.rule = ctx->rule;
4017 entry->u.fin.idle = oft->fin_idle_timeout;
4018 entry->u.fin.hard = oft->fin_hard_timeout;
b256dc52 4019 }
9583bc14
EJ
4020 }
4021}
4022
4023static void
4024xlate_sample_action(struct xlate_ctx *ctx,
4025 const struct ofpact_sample *os)
4026{
e824d78d
JR
4027 /* Scale the probability from 16-bit to 32-bit while representing
4028 * the same percentage. */
4029 uint32_t probability = (os->probability << 16) | os->probability;
4030
b440dd8c 4031 if (!ctx->xbridge->support.variable_length_userdata) {
e824d78d
JR
4032 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4033
4034 VLOG_ERR_RL(&rl, "ignoring NXAST_SAMPLE action because datapath "
4035 "lacks support (needs Linux 3.10+ or kernel module from "
4036 "OVS 1.11+)");
4037 return;
4038 }
4039
704bb0bf 4040 xlate_commit_actions(ctx);
e824d78d 4041
a6092018
BP
4042 union user_action_cookie cookie = {
4043 .flow_sample = {
4044 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4045 .probability = os->probability,
4046 .collector_set_id = os->collector_set_id,
4047 .obs_domain_id = os->obs_domain_id,
4048 .obs_point_id = os->obs_point_id,
4049 }
4050 };
4051 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
4052 ODPP_NONE, false);
9583bc14
EJ
4053}
4054
4055static bool
46c88433 4056may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 4057{
bbbca389 4058 if (xport->config & (is_stp(&ctx->xin->flow)
46c88433
EJ
4059 ? OFPUTIL_PC_NO_RECV_STP
4060 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
4061 return false;
4062 }
4063
4064 /* Only drop packets here if both forwarding and learning are
4065 * disabled. If just learning is enabled, we need to have
4066 * OFPP_NORMAL and the learning action have a look at the packet
4067 * before we can drop it. */
9efd308e
DV
4068 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
4069 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
9583bc14
EJ
4070 return false;
4071 }
4072
4073 return true;
4074}
4075
7fdb60a7
SH
4076static void
4077xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
4078{
c61f3870
BP
4079 const struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
4080 size_t on_len = ofpact_nest_get_action_len(on);
4081 const struct ofpact *inner;
4082
4083 /* Maintain actset_output depending on the contents of the action set:
4084 *
4085 * - OFPP_UNSET, if there is no "output" action.
4086 *
4087 * - The output port, if there is an "output" action and no "group"
4088 * action.
4089 *
4090 * - OFPP_UNSET, if there is a "group" action.
4091 */
4092 if (!ctx->action_set_has_group) {
4093 OFPACT_FOR_EACH (inner, on->actions, on_len) {
4094 if (inner->type == OFPACT_OUTPUT) {
4095 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(inner)->port;
4096 } else if (inner->type == OFPACT_GROUP) {
4097 ctx->xin->flow.actset_output = OFPP_UNSET;
4098 ctx->action_set_has_group = true;
9055ca9a 4099 break;
c61f3870
BP
4100 }
4101 }
4102 }
4103
4104 ofpbuf_put(&ctx->action_set, on->actions, on_len);
7fdb60a7
SH
4105}
4106
4107static void
4108xlate_action_set(struct xlate_ctx *ctx)
4109{
4110 uint64_t action_list_stub[1024 / 64];
4111 struct ofpbuf action_list;
4112
029ca940 4113 ctx->in_action_set = true;
7fdb60a7
SH
4114 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
4115 ofpacts_execute_action_set(&action_list, &ctx->action_set);
ed9c9e3e
JR
4116 /* Clear the action set, as it is not needed any more. */
4117 ofpbuf_clear(&ctx->action_set);
6fd6ed71 4118 do_xlate_actions(action_list.data, action_list.size, ctx);
029ca940 4119 ctx->in_action_set = false;
7fdb60a7
SH
4120 ofpbuf_uninit(&action_list);
4121}
4122
e672ff9b
JR
4123static void
4124recirc_put_unroll_xlate(struct xlate_ctx *ctx)
4125{
4126 struct ofpact_unroll_xlate *unroll;
4127
4128 unroll = ctx->last_unroll_offset < 0
4129 ? NULL
4130 : ALIGNED_CAST(struct ofpact_unroll_xlate *,
4131 (char *)ctx->action_set.data + ctx->last_unroll_offset);
4132
4133 /* Restore the table_id and rule cookie for a potential PACKET
4134 * IN if needed. */
4135 if (!unroll ||
4136 (ctx->table_id != unroll->rule_table_id
4137 || ctx->rule_cookie != unroll->rule_cookie)) {
4138
4139 ctx->last_unroll_offset = ctx->action_set.size;
4140 unroll = ofpact_put_UNROLL_XLATE(&ctx->action_set);
4141 unroll->rule_table_id = ctx->table_id;
4142 unroll->rule_cookie = ctx->rule_cookie;
4143 }
4144}
4145
4146
4147/* Copy remaining actions to the action_set to be executed after recirculation.
4148 * UNROLL_XLATE action is inserted, if not already done so, before actions that
a2b53dec
BP
4149 * may generate asynchronous messages from the current table and without
4150 * matching another rule. */
e672ff9b
JR
4151static void
4152recirc_unroll_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4153 struct xlate_ctx *ctx)
4154{
4155 const struct ofpact *a;
4156
4157 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4158 switch (a->type) {
a2b53dec 4159 /* May generate asynchronous messages. */
e672ff9b
JR
4160 case OFPACT_OUTPUT_REG:
4161 case OFPACT_GROUP:
4162 case OFPACT_OUTPUT:
4163 case OFPACT_CONTROLLER:
4164 case OFPACT_DEC_MPLS_TTL:
4165 case OFPACT_DEC_TTL:
4166 recirc_put_unroll_xlate(ctx);
4167 break;
4168
4169 /* These may not generate PACKET INs. */
4170 case OFPACT_SET_TUNNEL:
4171 case OFPACT_REG_MOVE:
4172 case OFPACT_SET_FIELD:
4173 case OFPACT_STACK_PUSH:
4174 case OFPACT_STACK_POP:
4175 case OFPACT_LEARN:
4176 case OFPACT_WRITE_METADATA:
4177 case OFPACT_RESUBMIT: /* May indirectly generate PACKET INs, */
4178 case OFPACT_GOTO_TABLE: /* but from a different table and rule. */
4179 case OFPACT_ENQUEUE:
4180 case OFPACT_SET_VLAN_VID:
4181 case OFPACT_SET_VLAN_PCP:
4182 case OFPACT_STRIP_VLAN:
4183 case OFPACT_PUSH_VLAN:
4184 case OFPACT_SET_ETH_SRC:
4185 case OFPACT_SET_ETH_DST:
4186 case OFPACT_SET_IPV4_SRC:
4187 case OFPACT_SET_IPV4_DST:
4188 case OFPACT_SET_IP_DSCP:
4189 case OFPACT_SET_IP_ECN:
4190 case OFPACT_SET_IP_TTL:
4191 case OFPACT_SET_L4_SRC_PORT:
4192 case OFPACT_SET_L4_DST_PORT:
4193 case OFPACT_SET_QUEUE:
4194 case OFPACT_POP_QUEUE:
4195 case OFPACT_PUSH_MPLS:
4196 case OFPACT_POP_MPLS:
4197 case OFPACT_SET_MPLS_LABEL:
4198 case OFPACT_SET_MPLS_TC:
4199 case OFPACT_SET_MPLS_TTL:
4200 case OFPACT_MULTIPATH:
4201 case OFPACT_BUNDLE:
4202 case OFPACT_EXIT:
4203 case OFPACT_UNROLL_XLATE:
4204 case OFPACT_FIN_TIMEOUT:
4205 case OFPACT_CLEAR_ACTIONS:
4206 case OFPACT_WRITE_ACTIONS:
4207 case OFPACT_METER:
4208 case OFPACT_SAMPLE:
d4abaff5 4209 case OFPACT_DEBUG_RECIRC:
07659514 4210 case OFPACT_CT:
9ac0aada 4211 case OFPACT_NAT:
e672ff9b
JR
4212 break;
4213
4214 /* These need not be copied for restoration. */
4215 case OFPACT_NOTE:
4216 case OFPACT_CONJUNCTION:
4217 continue;
4218 }
4219 /* Copy the action over. */
4220 ofpbuf_put(&ctx->action_set, a, OFPACT_ALIGN(a->len));
4221 }
4222}
4223
1d741d6d
JR
4224#define CHECK_MPLS_RECIRCULATION() \
4225 if (ctx->was_mpls) { \
4226 ctx_trigger_recirculation(ctx); \
4227 break; \
4228 }
4229#define CHECK_MPLS_RECIRCULATION_IF(COND) \
4230 if (COND) { \
4231 CHECK_MPLS_RECIRCULATION(); \
4232 }
4233
8e53fe8c
JS
4234static void
4235put_ct_mark(const struct flow *flow, struct flow *base_flow,
4236 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4237{
4238 struct {
4239 uint32_t key;
4240 uint32_t mask;
4241 } odp_attr;
4242
4243 odp_attr.key = flow->ct_mark;
4244 odp_attr.mask = wc->masks.ct_mark;
4245
4246 if (odp_attr.mask && odp_attr.key != base_flow->ct_mark) {
4247 nl_msg_put_unspec(odp_actions, OVS_CT_ATTR_MARK, &odp_attr,
4248 sizeof(odp_attr));
4249 }
4250}
4251
9daf2348
JS
4252static void
4253put_ct_label(const struct flow *flow, struct flow *base_flow,
4254 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
4255{
4256 if (!ovs_u128_is_zero(&wc->masks.ct_label)
4257 && !ovs_u128_equals(&flow->ct_label, &base_flow->ct_label)) {
4258 struct {
4259 ovs_u128 key;
4260 ovs_u128 mask;
4261 } *odp_ct_label;
4262
4263 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
4264 OVS_CT_ATTR_LABELS,
4265 sizeof(*odp_ct_label));
4266 odp_ct_label->key = flow->ct_label;
4267 odp_ct_label->mask = wc->masks.ct_label;
4268 }
4269}
4270
d787ad39
JS
4271static void
4272put_ct_helper(struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
4273{
4274 if (ofc->alg) {
4275 if (ofc->alg == IPPORT_FTP) {
4276 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
4277 } else {
4278 VLOG_WARN("Cannot serialize ct_helper %d\n", ofc->alg);
4279 }
4280 }
4281}
4282
9ac0aada
JR
4283static void
4284put_ct_nat(struct xlate_ctx *ctx)
4285{
4286 struct ofpact_nat *ofn = ctx->ct_nat_action;
4287 size_t nat_offset;
4288
4289 if (!ofn) {
4290 return;
4291 }
4292
4293 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
4294 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
4295 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
4296 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
4297 if (ofn->flags & NX_NAT_F_PERSISTENT) {
4298 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
4299 }
4300 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
4301 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
4302 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
4303 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
4304 }
4305 if (ofn->range_af == AF_INET) {
73e8bc23 4306 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
9ac0aada
JR
4307 ofn->range.addr.ipv4.min);
4308 if (ofn->range.addr.ipv4.max &&
73e8bc23
BP
4309 (ntohl(ofn->range.addr.ipv4.max)
4310 > ntohl(ofn->range.addr.ipv4.min))) {
4311 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4312 ofn->range.addr.ipv4.max);
9ac0aada
JR
4313 }
4314 } else if (ofn->range_af == AF_INET6) {
4315 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
4316 &ofn->range.addr.ipv6.min,
4317 sizeof ofn->range.addr.ipv6.min);
4318 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
4319 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
4320 sizeof ofn->range.addr.ipv6.max) > 0) {
4321 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
4322 &ofn->range.addr.ipv6.max,
4323 sizeof ofn->range.addr.ipv6.max);
4324 }
4325 }
4326 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
4327 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
4328 ofn->range.proto.min);
4329 if (ofn->range.proto.max &&
4330 ofn->range.proto.max > ofn->range.proto.min) {
4331 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
4332 ofn->range.proto.max);
4333 }
4334 }
4335 }
4336 nl_msg_end_nested(ctx->odp_actions, nat_offset);
4337}
4338
07659514
JS
4339static void
4340compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
4341{
9daf2348 4342 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
8e53fe8c 4343 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
07659514
JS
4344 size_t ct_offset;
4345 uint16_t zone;
4346
4347 /* Ensure that any prior actions are applied before composing the new
4348 * conntrack action. */
4349 xlate_commit_actions(ctx);
4350
8e53fe8c 4351 /* Process nested actions first, to populate the key. */
9ac0aada 4352 ctx->ct_nat_action = NULL;
8e53fe8c
JS
4353 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
4354
07659514
JS
4355 if (ofc->zone_src.field) {
4356 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
4357 } else {
4358 zone = ofc->zone_imm;
4359 }
4360
4361 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
4362 if (ofc->flags & NX_CT_F_COMMIT) {
4363 nl_msg_put_flag(ctx->odp_actions, OVS_CT_ATTR_COMMIT);
4364 }
4365 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
8e53fe8c 4366 put_ct_mark(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
9daf2348 4367 put_ct_label(&ctx->xin->flow, &ctx->base_flow, ctx->odp_actions, ctx->wc);
d787ad39 4368 put_ct_helper(ctx->odp_actions, ofc);
9ac0aada
JR
4369 put_ct_nat(ctx);
4370 ctx->ct_nat_action = NULL;
07659514
JS
4371 nl_msg_end_nested(ctx->odp_actions, ct_offset);
4372
8e53fe8c
JS
4373 /* Restore the original ct fields in the key. These should only be exposed
4374 * after recirculation to another table. */
4375 ctx->base_flow.ct_mark = old_ct_mark;
9daf2348 4376 ctx->base_flow.ct_label = old_ct_label;
8e53fe8c 4377
07659514
JS
4378 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
4379 /* If we do not recirculate as part of this action, hide the results of
4380 * connection tracking from subsequent recirculations. */
4381 ctx->conntracked = false;
4382 } else {
4383 /* Use ct_* fields from datapath during recirculation upcall. */
4384 ctx->conntracked = true;
e37b8437 4385 compose_recirculate_and_fork(ctx, ofc->recirc_table);
07659514
JS
4386 }
4387}
4388
9583bc14
EJ
4389static void
4390do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
4391 struct xlate_ctx *ctx)
4392{
49a73e0c 4393 struct flow_wildcards *wc = ctx->wc;
33bf9176 4394 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
4395 const struct ofpact *a;
4396
a36de779 4397 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
53902038 4398 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
a36de779 4399 }
f47ea021
JR
4400 /* dl_type already in the mask, not set below. */
4401
9583bc14
EJ
4402 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
4403 struct ofpact_controller *controller;
4404 const struct ofpact_metadata *metadata;
b2dd70be
JR
4405 const struct ofpact_set_field *set_field;
4406 const struct mf_field *mf;
9583bc14 4407
fff1b9c0
JR
4408 if (ctx->error) {
4409 break;
4410 }
4411
e672ff9b
JR
4412 if (ctx->exit) {
4413 /* Check if need to store the remaining actions for later
4414 * execution. */
4415 if (exit_recirculates(ctx)) {
4416 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4417 ((uint8_t *)a -
4418 (uint8_t *)ofpacts)),
4419 ctx);
4420 }
4421 break;
7bbdd84f
SH
4422 }
4423
9583bc14
EJ
4424 switch (a->type) {
4425 case OFPACT_OUTPUT:
4426 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
4427 ofpact_get_OUTPUT(a)->max_len, true);
4428 break;
4429
7395c052 4430 case OFPACT_GROUP:
f4fb341b 4431 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
1d741d6d 4432 /* Group could not be found. */
f4fb341b
SH
4433 return;
4434 }
7395c052
NZ
4435 break;
4436
9583bc14
EJ
4437 case OFPACT_CONTROLLER:
4438 controller = ofpact_get_CONTROLLER(a);
4439 execute_controller_action(ctx, controller->max_len,
4440 controller->reason,
4441 controller->controller_id);
4442 break;
4443
4444 case OFPACT_ENQUEUE:
16194afd
DDP
4445 memset(&wc->masks.skb_priority, 0xff,
4446 sizeof wc->masks.skb_priority);
9583bc14
EJ
4447 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
4448 break;
4449
4450 case OFPACT_SET_VLAN_VID:
f74e7df7 4451 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
ca287d20
JR
4452 if (flow->vlan_tci & htons(VLAN_CFI) ||
4453 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
4454 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
4455 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
4456 | htons(VLAN_CFI));
4457 }
9583bc14
EJ
4458 break;
4459
4460 case OFPACT_SET_VLAN_PCP:
f74e7df7 4461 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
ca287d20
JR
4462 if (flow->vlan_tci & htons(VLAN_CFI) ||
4463 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
4464 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
4465 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
4466 << VLAN_PCP_SHIFT) | VLAN_CFI);
4467 }
9583bc14
EJ
4468 break;
4469
4470 case OFPACT_STRIP_VLAN:
f74e7df7 4471 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 4472 flow->vlan_tci = htons(0);
9583bc14
EJ
4473 break;
4474
4475 case OFPACT_PUSH_VLAN:
4476 /* XXX 802.1AD(QinQ) */
f74e7df7 4477 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
33bf9176 4478 flow->vlan_tci = htons(VLAN_CFI);
9583bc14
EJ
4479 break;
4480
4481 case OFPACT_SET_ETH_SRC:
74ff3298
JR
4482 WC_MASK_FIELD(wc, dl_src);
4483 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
9583bc14
EJ
4484 break;
4485
4486 case OFPACT_SET_ETH_DST:
74ff3298
JR
4487 WC_MASK_FIELD(wc, dl_dst);
4488 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
9583bc14
EJ
4489 break;
4490
4491 case OFPACT_SET_IPV4_SRC:
1d741d6d 4492 CHECK_MPLS_RECIRCULATION();
33bf9176 4493 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 4494 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 4495 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
4496 }
4497 break;
4498
4499 case OFPACT_SET_IPV4_DST:
1d741d6d 4500 CHECK_MPLS_RECIRCULATION();
33bf9176 4501 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 4502 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 4503 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
4504 }
4505 break;
4506
04f01c24 4507 case OFPACT_SET_IP_DSCP:
1d741d6d 4508 CHECK_MPLS_RECIRCULATION();
04f01c24 4509 if (is_ip_any(flow)) {
f47ea021 4510 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 4511 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 4512 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
4513 }
4514 break;
4515
ff14eb7a 4516 case OFPACT_SET_IP_ECN:
1d741d6d 4517 CHECK_MPLS_RECIRCULATION();
ff14eb7a
JR
4518 if (is_ip_any(flow)) {
4519 wc->masks.nw_tos |= IP_ECN_MASK;
4520 flow->nw_tos &= ~IP_ECN_MASK;
4521 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
4522 }
4523 break;
4524
0c20dbe4 4525 case OFPACT_SET_IP_TTL:
1d741d6d 4526 CHECK_MPLS_RECIRCULATION();
0c20dbe4
JR
4527 if (is_ip_any(flow)) {
4528 wc->masks.nw_ttl = 0xff;
4529 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
4530 }
4531 break;
4532
9583bc14 4533 case OFPACT_SET_L4_SRC_PORT:
1d741d6d 4534 CHECK_MPLS_RECIRCULATION();
b8778a0d 4535 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
4536 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4537 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 4538 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
4539 }
4540 break;
4541
4542 case OFPACT_SET_L4_DST_PORT:
1d741d6d 4543 CHECK_MPLS_RECIRCULATION();
b8778a0d 4544 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
4545 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
4546 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 4547 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
4548 }
4549 break;
4550
4551 case OFPACT_RESUBMIT:
4552 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
4553 break;
4554
4555 case OFPACT_SET_TUNNEL:
33bf9176 4556 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
4557 break;
4558
4559 case OFPACT_SET_QUEUE:
16194afd
DDP
4560 memset(&wc->masks.skb_priority, 0xff,
4561 sizeof wc->masks.skb_priority);
9583bc14
EJ
4562 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
4563 break;
4564
4565 case OFPACT_POP_QUEUE:
16194afd
DDP
4566 memset(&wc->masks.skb_priority, 0xff,
4567 sizeof wc->masks.skb_priority);
33bf9176 4568 flow->skb_priority = ctx->orig_skb_priority;
9583bc14
EJ
4569 break;
4570
4571 case OFPACT_REG_MOVE:
1d741d6d
JR
4572 CHECK_MPLS_RECIRCULATION_IF(
4573 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->dst.field) ||
4574 mf_is_l3_or_higher(ofpact_get_REG_MOVE(a)->src.field));
33bf9176 4575 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
9583bc14
EJ
4576 break;
4577
b2dd70be 4578 case OFPACT_SET_FIELD:
1d741d6d
JR
4579 CHECK_MPLS_RECIRCULATION_IF(
4580 mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field));
b2dd70be
JR
4581 set_field = ofpact_get_SET_FIELD(a);
4582 mf = set_field->field;
b2dd70be
JR
4583
4584 /* Set field action only ever overwrites packet's outermost
4585 * applicable header fields. Do nothing if no header exists. */
4479846e
JR
4586 if (mf->id == MFF_VLAN_VID) {
4587 wc->masks.vlan_tci |= htons(VLAN_CFI);
4588 if (!(flow->vlan_tci & htons(VLAN_CFI))) {
4589 break;
4590 }
4591 } else if ((mf->id == MFF_MPLS_LABEL || mf->id == MFF_MPLS_TC)
4592 /* 'dl_type' is already unwildcarded. */
4593 && !eth_type_mpls(flow->dl_type)) {
4594 break;
b2dd70be 4595 }
67794d56 4596 /* A flow may wildcard nw_frag. Do nothing if setting a transport
b8778a0d 4597 * header field on a packet that does not have them. */
5e2e998a 4598 mf_mask_field_and_prereqs(mf, wc);
b8778a0d
JR
4599 if (mf_are_prereqs_ok(mf, flow)) {
4600 mf_set_flow_value_masked(mf, &set_field->value,
4601 &set_field->mask, flow);
4602 }
b2dd70be
JR
4603 break;
4604
9583bc14 4605 case OFPACT_STACK_PUSH:
1d741d6d
JR
4606 CHECK_MPLS_RECIRCULATION_IF(
4607 mf_is_l3_or_higher(ofpact_get_STACK_PUSH(a)->subfield.field));
33bf9176
BP
4608 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
4609 &ctx->stack);
9583bc14
EJ
4610 break;
4611
4612 case OFPACT_STACK_POP:
1d741d6d
JR
4613 CHECK_MPLS_RECIRCULATION_IF(
4614 mf_is_l3_or_higher(ofpact_get_STACK_POP(a)->subfield.field));
f74e7df7
JP
4615 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
4616 &ctx->stack);
9583bc14
EJ
4617 break;
4618
4619 case OFPACT_PUSH_MPLS:
1d741d6d
JR
4620 /* Recirculate if it is an IP packet with a zero ttl. This may
4621 * indicate that the packet was previously MPLS and an MPLS pop
4622 * action converted it to IP. In this case recirculating should
4623 * reveal the IP TTL which is used as the basis for a new MPLS
4624 * LSE. */
4625 CHECK_MPLS_RECIRCULATION_IF(
4626 !flow_count_mpls_labels(flow, wc)
4627 && flow->nw_ttl == 0
4628 && is_ip_any(flow));
8bfd0fda 4629 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
4630 break;
4631
4632 case OFPACT_POP_MPLS:
1d741d6d 4633 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4634 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
4635 break;
4636
097d4939 4637 case OFPACT_SET_MPLS_LABEL:
1d741d6d 4638 CHECK_MPLS_RECIRCULATION();
8bfd0fda
BP
4639 compose_set_mpls_label_action(
4640 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
1d741d6d 4641 break;
097d4939
JR
4642
4643 case OFPACT_SET_MPLS_TC:
1d741d6d 4644 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4645 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
4646 break;
4647
9583bc14 4648 case OFPACT_SET_MPLS_TTL:
1d741d6d 4649 CHECK_MPLS_RECIRCULATION();
8bfd0fda 4650 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
4651 break;
4652
4653 case OFPACT_DEC_MPLS_TTL:
1d741d6d 4654 CHECK_MPLS_RECIRCULATION();
9cfef3d0 4655 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 4656 return;
9583bc14
EJ
4657 }
4658 break;
4659
4660 case OFPACT_DEC_TTL:
1d741d6d 4661 CHECK_MPLS_RECIRCULATION();
f74e7df7 4662 wc->masks.nw_ttl = 0xff;
9583bc14 4663 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 4664 return;
9583bc14
EJ
4665 }
4666 break;
4667
4668 case OFPACT_NOTE:
4669 /* Nothing to do. */
4670 break;
4671
4672 case OFPACT_MULTIPATH:
1d741d6d 4673 CHECK_MPLS_RECIRCULATION();
33bf9176 4674 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
9583bc14
EJ
4675 break;
4676
4677 case OFPACT_BUNDLE:
1d741d6d 4678 CHECK_MPLS_RECIRCULATION();
9583bc14
EJ
4679 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
4680 break;
4681
4682 case OFPACT_OUTPUT_REG:
4683 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
4684 break;
4685
4686 case OFPACT_LEARN:
1d741d6d 4687 CHECK_MPLS_RECIRCULATION();
9583bc14
EJ
4688 xlate_learn_action(ctx, ofpact_get_LEARN(a));
4689 break;
4690
afc3987b
BP
4691 case OFPACT_CONJUNCTION: {
4692 /* A flow with a "conjunction" action represents part of a special
4693 * kind of "set membership match". Such a flow should not actually
4694 * get executed, but it could via, say, a "packet-out", even though
4695 * that wouldn't be useful. Log it to help debugging. */
4696 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
4697 VLOG_INFO_RL(&rl, "executing no-op conjunction action");
18080541 4698 break;
afc3987b 4699 }
18080541 4700
9583bc14
EJ
4701 case OFPACT_EXIT:
4702 ctx->exit = true;
4703 break;
4704
e672ff9b
JR
4705 case OFPACT_UNROLL_XLATE: {
4706 struct ofpact_unroll_xlate *unroll = ofpact_get_UNROLL_XLATE(a);
4707
4708 /* Restore translation context data that was stored earlier. */
4709 ctx->table_id = unroll->rule_table_id;
4710 ctx->rule_cookie = unroll->rule_cookie;
4711 break;
4712 }
9583bc14 4713 case OFPACT_FIN_TIMEOUT:
1d741d6d 4714 CHECK_MPLS_RECIRCULATION();
33bf9176 4715 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
4716 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
4717 break;
4718
4719 case OFPACT_CLEAR_ACTIONS:
7fdb60a7 4720 ofpbuf_clear(&ctx->action_set);
c61f3870
BP
4721 ctx->xin->flow.actset_output = OFPP_UNSET;
4722 ctx->action_set_has_group = false;
7fdb60a7
SH
4723 break;
4724
4725 case OFPACT_WRITE_ACTIONS:
4726 xlate_write_actions(ctx, a);
9583bc14
EJ
4727 break;
4728
4729 case OFPACT_WRITE_METADATA:
4730 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
4731 flow->metadata &= ~metadata->mask;
4732 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
4733 break;
4734
638a19b0
JR
4735 case OFPACT_METER:
4736 /* Not implemented yet. */
4737 break;
4738
9583bc14 4739 case OFPACT_GOTO_TABLE: {
9583bc14 4740 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14 4741
7bbdd84f
SH
4742 /* Allow ctx->table_id == TBL_INTERNAL, which will be greater
4743 * than ogt->table_id. This is to allow goto_table actions that
4744 * triggered recirculation: ctx->table_id will be TBL_INTERNAL
4745 * after recirculation. */
4746 ovs_assert(ctx->table_id == TBL_INTERNAL
4747 || ctx->table_id < ogt->table_id);
4468099e 4748 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
6d328fa2 4749 ogt->table_id, true, true);
9583bc14
EJ
4750 break;
4751 }
4752
4753 case OFPACT_SAMPLE:
4754 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
4755 break;
d4abaff5 4756
07659514
JS
4757 case OFPACT_CT:
4758 CHECK_MPLS_RECIRCULATION();
4759 compose_conntrack_action(ctx, ofpact_get_CT(a));
4760 break;
4761
9ac0aada
JR
4762 case OFPACT_NAT:
4763 /* This will be processed by compose_conntrack_action(). */
4764 ctx->ct_nat_action = ofpact_get_NAT(a);
4765 break;
4766
d4abaff5
BP
4767 case OFPACT_DEBUG_RECIRC:
4768 ctx_trigger_recirculation(ctx);
4769 a = ofpact_next(a);
4770 break;
9583bc14 4771 }
1d741d6d
JR
4772
4773 /* Check if need to store this and the remaining actions for later
4774 * execution. */
fff1b9c0 4775 if (!ctx->error && ctx->exit && ctx_first_recirculation_action(ctx)) {
1d741d6d
JR
4776 recirc_unroll_actions(a, OFPACT_ALIGN(ofpacts_len -
4777 ((uint8_t *)a -
4778 (uint8_t *)ofpacts)),
4779 ctx);
4780 break;
4781 }
9583bc14 4782 }
9583bc14
EJ
4783}
4784
4785void
4786xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
cc377352
EJ
4787 const struct flow *flow, ofp_port_t in_port,
4788 struct rule_dpif *rule, uint16_t tcp_flags,
1520ef4f
BP
4789 const struct dp_packet *packet, struct flow_wildcards *wc,
4790 struct ofpbuf *odp_actions)
9583bc14
EJ
4791{
4792 xin->ofproto = ofproto;
4793 xin->flow = *flow;
cc377352 4794 xin->flow.in_port.ofp_port = in_port;
c61f3870 4795 xin->flow.actset_output = OFPP_UNSET;
9583bc14
EJ
4796 xin->packet = packet;
4797 xin->may_learn = packet != NULL;
4798 xin->rule = rule;
b256dc52 4799 xin->xcache = NULL;
9583bc14
EJ
4800 xin->ofpacts = NULL;
4801 xin->ofpacts_len = 0;
4802 xin->tcp_flags = tcp_flags;
4803 xin->resubmit_hook = NULL;
4804 xin->report_hook = NULL;
4805 xin->resubmit_stats = NULL;
cdd42eda
JG
4806 xin->recurse = 0;
4807 xin->resubmits = 0;
49a73e0c 4808 xin->wc = wc;
1520ef4f 4809 xin->odp_actions = odp_actions;
e672ff9b
JR
4810
4811 /* Do recirc lookup. */
4812 xin->recirc = flow->recirc_id
4813 ? recirc_id_node_find(flow->recirc_id)
4814 : NULL;
9583bc14
EJ
4815}
4816
4817void
4818xlate_out_uninit(struct xlate_out *xout)
4819{
e672ff9b 4820 if (xout) {
fbf5d6ec 4821 recirc_refs_unref(&xout->recircs);
9583bc14
EJ
4822 }
4823}
4824
4825/* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
4826 * into datapath actions, using 'ctx', and discards the datapath actions. */
4827void
4828xlate_actions_for_side_effects(struct xlate_in *xin)
4829{
4830 struct xlate_out xout;
fff1b9c0
JR
4831 enum xlate_error error;
4832
4833 error = xlate_actions(xin, &xout);
4834 if (error) {
4835 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4836
4837 VLOG_WARN_RL(&rl, "xlate_actions failed (%s)!", xlate_strerror(error));
4838 }
9583bc14 4839
9583bc14
EJ
4840 xlate_out_uninit(&xout);
4841}
9583bc14 4842\f
55954f6e
EJ
4843static struct skb_priority_to_dscp *
4844get_skb_priority(const struct xport *xport, uint32_t skb_priority)
4845{
4846 struct skb_priority_to_dscp *pdscp;
4847 uint32_t hash;
4848
4849 hash = hash_int(skb_priority, 0);
4850 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
4851 if (pdscp->skb_priority == skb_priority) {
4852 return pdscp;
4853 }
4854 }
4855 return NULL;
4856}
4857
4858static bool
4859dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
4860 uint8_t *dscp)
4861{
4862 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
4863 *dscp = pdscp ? pdscp->dscp : 0;
4864 return pdscp != NULL;
4865}
4866
16194afd
DDP
4867static size_t
4868count_skb_priorities(const struct xport *xport)
4869{
4870 return hmap_count(&xport->skb_priorities);
4871}
4872
55954f6e
EJ
4873static void
4874clear_skb_priorities(struct xport *xport)
4875{
4876 struct skb_priority_to_dscp *pdscp, *next;
4877
4878 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
4879 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
4880 free(pdscp);
4881 }
4882}
4883
ce4a6b76
BP
4884static bool
4885actions_output_to_local_port(const struct xlate_ctx *ctx)
4886{
46c88433 4887 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
4888 const struct nlattr *a;
4889 unsigned int left;
4890
1520ef4f
BP
4891 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
4892 ctx->odp_actions->size) {
ce4a6b76
BP
4893 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
4894 && nl_attr_get_odp_port(a) == local_odp_port) {
4895 return true;
4896 }
4897 }
4898 return false;
4899}
9583bc14 4900
5e2a6702 4901#if defined(__linux__)
7d031d7e
BP
4902/* Returns the maximum number of packets that the Linux kernel is willing to
4903 * queue up internally to certain kinds of software-implemented ports, or the
4904 * default (and rarely modified) value if it cannot be determined. */
4905static int
4906netdev_max_backlog(void)
4907{
4908 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
4909 static int max_backlog = 1000; /* The normal default value. */
4910
4911 if (ovsthread_once_start(&once)) {
4912 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
4913 FILE *stream;
4914 int n;
4915
4916 stream = fopen(filename, "r");
4917 if (!stream) {
120c348f 4918 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
7d031d7e
BP
4919 } else {
4920 if (fscanf(stream, "%d", &n) != 1) {
4921 VLOG_WARN("%s: read error", filename);
4922 } else if (n <= 100) {
4923 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
4924 } else {
4925 max_backlog = n;
4926 }
4927 fclose(stream);
4928 }
4929 ovsthread_once_done(&once);
4930
4931 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
4932 }
4933
4934 return max_backlog;
4935}
4936
4937/* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
4938 * 'odp_actions'. */
4939static int
4940count_output_actions(const struct ofpbuf *odp_actions)
4941{
4942 const struct nlattr *a;
4943 size_t left;
4944 int n = 0;
4945
6fd6ed71 4946 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
7d031d7e
BP
4947 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
4948 n++;
4949 }
4950 }
4951 return n;
4952}
5e2a6702 4953#endif /* defined(__linux__) */
7d031d7e
BP
4954
4955/* Returns true if 'odp_actions' contains more output actions than the datapath
4956 * can reliably handle in one go. On Linux, this is the value of the
4957 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
4958 * packets that the kernel is willing to queue up for processing while the
4959 * datapath is processing a set of actions. */
4960static bool
5e2a6702 4961too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
7d031d7e
BP
4962{
4963#ifdef __linux__
6fd6ed71 4964 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
7d031d7e
BP
4965 && count_output_actions(odp_actions) > netdev_max_backlog());
4966#else
4967 /* OSes other than Linux might have similar limits, but we don't know how
4968 * to determine them.*/
4969 return false;
4970#endif
4971}
4972
234c3da9
BP
4973static void
4974xlate_wc_init(struct xlate_ctx *ctx)
4975{
4976 flow_wildcards_init_catchall(ctx->wc);
4977
4978 /* Some fields we consider to always be examined. */
5e2e998a
JR
4979 WC_MASK_FIELD(ctx->wc, in_port);
4980 WC_MASK_FIELD(ctx->wc, dl_type);
234c3da9 4981 if (is_ip_any(&ctx->xin->flow)) {
5e2e998a 4982 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
234c3da9
BP
4983 }
4984
4985 if (ctx->xbridge->support.odp.recirc) {
4986 /* Always exactly match recirc_id when datapath supports
4987 * recirculation. */
5e2e998a 4988 WC_MASK_FIELD(ctx->wc, recirc_id);
234c3da9
BP
4989 }
4990
4991 if (ctx->xbridge->netflow) {
4992 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
4993 }
4994
4995 tnl_wc_init(&ctx->xin->flow, ctx->wc);
4996}
4997
4998static void
4999xlate_wc_finish(struct xlate_ctx *ctx)
5000{
5001 /* Clear the metadata and register wildcard masks, because we won't
5002 * use non-header fields as part of the cache. */
5003 flow_wildcards_clear_non_packet_fields(ctx->wc);
5004
5005 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
5006 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
5007 * represent these fields. The datapath interface, on the other hand,
5008 * represents them with just 8 bits each. This means that if the high
5009 * 8 bits of the masks for these fields somehow become set, then they
5010 * will get chopped off by a round trip through the datapath, and
5011 * revalidation will spot that as an inconsistency and delete the flow.
5012 * Avoid the problem here by making sure that only the low 8 bits of
5013 * either field can be unwildcarded for ICMP.
5014 */
5015 if (is_icmpv4(&ctx->xin->flow) || is_icmpv6(&ctx->xin->flow)) {
5016 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
5017 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
5018 }
5019 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
5020 if (ctx->wc->masks.vlan_tci) {
5021 ctx->wc->masks.vlan_tci |= htons(VLAN_CFI);
5022 }
5023}
5024
e672ff9b
JR
5025/* Translates the flow, actions, or rule in 'xin' into datapath actions in
5026 * 'xout'.
56450a41 5027 * The caller must take responsibility for eventually freeing 'xout', with
fff1b9c0
JR
5028 * xlate_out_uninit().
5029 * Returns 'XLATE_OK' if translation was successful. In case of an error an
5030 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
5031 * so that most callers may ignore the return value and transparently install a
5032 * drop flow when the translation fails. */
5033enum xlate_error
84f0f298 5034xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
9583bc14 5035{
e467ea42
BP
5036 *xout = (struct xlate_out) {
5037 .slow = 0,
fbf5d6ec 5038 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
e467ea42
BP
5039 };
5040
84f0f298 5041 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
bb00fdef
BP
5042 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
5043 if (!xbridge) {
fff1b9c0 5044 return XLATE_BRIDGE_NOT_FOUND;
bb00fdef
BP
5045 }
5046
33bf9176
BP
5047 struct flow *flow = &xin->flow;
5048
bb00fdef
BP
5049 union mf_subvalue stack_stub[1024 / sizeof(union mf_subvalue)];
5050 uint64_t action_set_stub[1024 / 8];
49a73e0c 5051 struct flow_wildcards scratch_wc;
1520ef4f
BP
5052 uint64_t actions_stub[256 / 8];
5053 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
bb00fdef
BP
5054 struct xlate_ctx ctx = {
5055 .xin = xin,
5056 .xout = xout,
5057 .base_flow = *flow,
c2b878e0 5058 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
bb00fdef
BP
5059 .xbridge = xbridge,
5060 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
5061 .rule = xin->rule,
49a73e0c 5062 .wc = xin->wc ? xin->wc : &scratch_wc,
1520ef4f 5063 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
bb00fdef 5064
cdd42eda
JG
5065 .recurse = xin->recurse,
5066 .resubmits = xin->resubmits,
bb00fdef
BP
5067 .in_group = false,
5068 .in_action_set = false,
5069
5070 .table_id = 0,
5071 .rule_cookie = OVS_BE64_MAX,
5072 .orig_skb_priority = flow->skb_priority,
5073 .sflow_n_outputs = 0,
5074 .sflow_odp_port = 0,
2031ef97 5075 .nf_output_iface = NF_OUT_DROP,
bb00fdef 5076 .exit = false,
fff1b9c0 5077 .error = XLATE_OK,
3d6151f3 5078 .mirrors = 0,
bb00fdef
BP
5079
5080 .recirc_action_offset = -1,
5081 .last_unroll_offset = -1,
5082
5083 .was_mpls = false,
07659514 5084 .conntracked = false,
bb00fdef 5085
9ac0aada
JR
5086 .ct_nat_action = NULL,
5087
bb00fdef
BP
5088 .action_set_has_group = false,
5089 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
5090 };
865ca6cf
BP
5091
5092 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
5093 * the packet as the datapath will treat it for output actions:
5094 *
5095 * - Our datapath doesn't retain tunneling information without us
5096 * re-setting it, so clear the tunnel data.
5097 *
5098 * - For VLAN splinters, a higher layer may pretend that the packet
5099 * came in on 'flow->in_port.ofp_port' with 'flow->vlan_tci'
5100 * attached, because that's how we want to treat it from an OpenFlow
5101 * perspective. But from the datapath's perspective it actually came
5102 * in on a VLAN device without any VLAN attached. So here we put the
5103 * datapath's view of the VLAN information in 'base_flow' to ensure
5104 * correct treatment.
5105 */
bb00fdef 5106 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
865ca6cf
BP
5107 if (flow->in_port.ofp_port
5108 != vsp_realdev_to_vlandev(xbridge->ofproto,
5109 flow->in_port.ofp_port,
5110 flow->vlan_tci)) {
5111 ctx.base_flow.vlan_tci = 0;
5112 }
5113
1520ef4f 5114 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
234c3da9
BP
5115 if (xin->wc) {
5116 xlate_wc_init(&ctx);
5117 }
bb00fdef 5118
46c88433 5119 COVERAGE_INC(xlate_actions);
9583bc14 5120
e672ff9b 5121 if (xin->recirc) {
2082425c 5122 const struct recirc_state *state = &xin->recirc->state;
e672ff9b 5123
d6bef3cc
BP
5124 xlate_report(&ctx, "Restoring state post-recirculation:");
5125
e672ff9b
JR
5126 if (xin->ofpacts_len > 0 || ctx.rule) {
5127 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
d6bef3cc 5128 const char *conflict = xin->ofpacts_len ? "actions" : "rule";
e672ff9b 5129
d6bef3cc
BP
5130 VLOG_WARN_RL(&rl, "Recirculation conflict (%s)!", conflict);
5131 xlate_report(&ctx, "- Recirculation conflict (%s)!", conflict);
fff1b9c0 5132 ctx.error = XLATE_RECIRCULATION_CONFLICT;
1520ef4f 5133 goto exit;
e672ff9b
JR
5134 }
5135
5136 /* Set the bridge for post-recirculation processing if needed. */
2082425c 5137 if (ctx.xbridge->ofproto != state->ofproto) {
e672ff9b 5138 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2082425c
BP
5139 const struct xbridge *new_bridge
5140 = xbridge_lookup(xcfg, state->ofproto);
e672ff9b
JR
5141
5142 if (OVS_UNLIKELY(!new_bridge)) {
5143 /* Drop the packet if the bridge cannot be found. */
5144 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5145 VLOG_WARN_RL(&rl, "Recirculation bridge no longer exists.");
d6bef3cc 5146 xlate_report(&ctx, "- Recirculation bridge no longer exists.");
fff1b9c0 5147 ctx.error = XLATE_BRIDGE_NOT_FOUND;
1520ef4f 5148 goto exit;
e672ff9b
JR
5149 }
5150 ctx.xbridge = new_bridge;
5151 }
5152
5153 /* Set the post-recirculation table id. Note: A table lookup is done
5154 * only if there are no post-recirculation actions. */
2082425c 5155 ctx.table_id = state->table_id;
d6bef3cc 5156 xlate_report(&ctx, "- Resuming from table %"PRIu8, ctx.table_id);
e672ff9b 5157
07659514
JS
5158 if (!state->conntracked) {
5159 clear_conntrack(flow);
5160 }
5161
e672ff9b
JR
5162 /* Restore pipeline metadata. May change flow's in_port and other
5163 * metadata to the values that existed when recirculation was
5164 * triggered. */
2082425c 5165 recirc_metadata_to_flow(&state->metadata, flow);
e672ff9b
JR
5166
5167 /* Restore stack, if any. */
2082425c 5168 if (state->stack) {
5c1b2314
BP
5169 ofpbuf_put(&ctx.stack, state->stack,
5170 state->n_stack * sizeof *state->stack);
e672ff9b
JR
5171 }
5172
29bae541
BP
5173 /* Restore mirror state. */
5174 ctx.mirrors = state->mirrors;
5175
e672ff9b 5176 /* Restore action set, if any. */
2082425c 5177 if (state->action_set_len) {
e672ff9b
JR
5178 const struct ofpact *a;
5179
d6bef3cc 5180 xlate_report_actions(&ctx, "- Restoring action set",
417509fa 5181 state->action_set, state->action_set_len);
d6bef3cc 5182
417509fa
BP
5183 ofpbuf_put(&ctx.action_set,
5184 state->action_set, state->action_set_len);
e672ff9b 5185
417509fa 5186 OFPACT_FOR_EACH (a, state->action_set, state->action_set_len) {
e672ff9b
JR
5187 if (a->type == OFPACT_GROUP) {
5188 ctx.action_set_has_group = true;
5189 break;
5190 }
5191 }
5192 }
5193
5194 /* Restore recirculation actions. If there are no actions, processing
5195 * will start with a lookup in the table set above. */
417509fa
BP
5196 xin->ofpacts = state->ofpacts;
5197 xin->ofpacts_len = state->ofpacts_len;
5198 if (state->ofpacts_len) {
d6bef3cc
BP
5199 xlate_report_actions(&ctx, "- Restoring actions",
5200 xin->ofpacts, xin->ofpacts_len);
e672ff9b
JR
5201 }
5202 } else if (OVS_UNLIKELY(flow->recirc_id)) {
5203 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
5204
5205 VLOG_WARN_RL(&rl, "Recirculation context not found for ID %"PRIx32,
5206 flow->recirc_id);
fff1b9c0 5207 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
1520ef4f 5208 goto exit;
e672ff9b 5209 }
621b8064
JR
5210 /* The bridge is now known so obtain its table version. */
5211 ctx.tables_version = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
9583bc14 5212
10c44245 5213 if (!xin->ofpacts && !ctx.rule) {
b2e89cc9
BP
5214 ctx.rule = rule_dpif_lookup_from_table(
5215 ctx.xbridge->ofproto, ctx.tables_version, flow, xin->wc,
1e1e1d19 5216 ctx.xin->resubmit_stats, &ctx.table_id,
b2e89cc9 5217 flow->in_port.ofp_port, true, true);
10c44245 5218 if (ctx.xin->resubmit_stats) {
b2e89cc9 5219 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
10c44245 5220 }
b256dc52
JS
5221 if (ctx.xin->xcache) {
5222 struct xc_entry *entry;
5223
5224 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
b2e89cc9 5225 entry->u.rule = ctx.rule;
1e1e1d19 5226 rule_dpif_ref(ctx.rule);
b256dc52 5227 }
a8c31348
BP
5228
5229 if (OVS_UNLIKELY(ctx.xin->resubmit_hook)) {
b2e89cc9 5230 ctx.xin->resubmit_hook(ctx.xin, ctx.rule, 0);
a8c31348 5231 }
10c44245 5232 }
10c44245 5233
14d2b8b2
BP
5234 /* Get the proximate input port of the packet. (If xin->recirc,
5235 * flow->in_port is the ultimate input port of the packet.) */
5236 struct xport *in_port = get_ofp_port(xbridge,
5237 ctx.base_flow.in_port.ofp_port);
5238
e672ff9b
JR
5239 /* Tunnel stats only for non-recirculated packets. */
5240 if (!xin->recirc && in_port && in_port->is_tunnel) {
b256dc52
JS
5241 if (ctx.xin->resubmit_stats) {
5242 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
5243 if (in_port->bfd) {
5244 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
5245 }
5246 }
5247 if (ctx.xin->xcache) {
5248 struct xc_entry *entry;
5249
5250 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
5251 entry->u.dev.rx = netdev_ref(in_port->netdev);
5252 entry->u.dev.bfd = bfd_ref(in_port->bfd);
d6fc5f57
EJ
5253 }
5254 }
5255
bef1403e
BP
5256 if (!xin->recirc && process_special(&ctx, in_port)) {
5257 /* process_special() did all the processing for this packet.
5258 *
5259 * We do not perform special processing on recirculated packets, as
5260 * recirculated packets are not really received by the bridge.*/
5261 } else if (in_port && in_port->xbundle
5262 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
5263 if (ctx.xin->packet != NULL) {
5264 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
5265 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
5266 "%s, which is reserved exclusively for mirroring",
5267 ctx.xbridge->name, in_port->xbundle->name);
5268 }
5269 } else {
e672ff9b 5270 /* Sampling is done only for packets really received by the bridge. */
a6092018 5271 unsigned int user_cookie_offset = 0;
e672ff9b 5272 if (!xin->recirc) {
a6092018
BP
5273 user_cookie_offset = compose_sflow_action(&ctx);
5274 compose_ipfix_action(&ctx, ODPP_NONE);
e672ff9b 5275 }
0731abc5 5276 size_t sample_actions_len = ctx.odp_actions->size;
9583bc14 5277
234c3da9
BP
5278 if (tnl_process_ecn(flow)
5279 && (!in_port || may_receive(in_port, &ctx))) {
1806291d
BP
5280 const struct ofpact *ofpacts;
5281 size_t ofpacts_len;
5282
5283 if (xin->ofpacts) {
5284 ofpacts = xin->ofpacts;
5285 ofpacts_len = xin->ofpacts_len;
5286 } else if (ctx.rule) {
5287 const struct rule_actions *actions
5288 = rule_dpif_get_actions(ctx.rule);
5289 ofpacts = actions->ofpacts;
5290 ofpacts_len = actions->ofpacts_len;
5291 ctx.rule_cookie = rule_dpif_get_flow_cookie(ctx.rule);
5292 } else {
5293 OVS_NOT_REACHED();
5294 }
5295
7efbc3b7 5296 mirror_ingress_packet(&ctx);
9583bc14 5297 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
fff1b9c0
JR
5298 if (ctx.error) {
5299 goto exit;
5300 }
9583bc14
EJ
5301
5302 /* We've let OFPP_NORMAL and the learning action look at the
5303 * packet, so drop it now if forwarding is disabled. */
9efd308e
DV
5304 if (in_port && (!xport_stp_forward_state(in_port) ||
5305 !xport_rstp_forward_state(in_port))) {
8ec90813 5306 /* Drop all actions added by do_xlate_actions() above. */
1520ef4f 5307 ctx.odp_actions->size = sample_actions_len;
e672ff9b
JR
5308
5309 /* Undo changes that may have been done for recirculation. */
5310 if (exit_recirculates(&ctx)) {
5311 ctx.action_set.size = ctx.recirc_action_offset;
5312 ctx.recirc_action_offset = -1;
5313 ctx.last_unroll_offset = -1;
5314 }
8ec90813 5315 } else if (ctx.action_set.size) {
e672ff9b
JR
5316 /* Translate action set only if not dropping the packet and
5317 * not recirculating. */
5318 if (!exit_recirculates(&ctx)) {
5319 xlate_action_set(&ctx);
5320 }
5321 }
5322 /* Check if need to recirculate. */
5323 if (exit_recirculates(&ctx)) {
5324 compose_recirculate_action(&ctx);
9583bc14
EJ
5325 }
5326 }
5327
e672ff9b
JR
5328 /* Output only fully processed packets. */
5329 if (!exit_recirculates(&ctx)
5330 && xbridge->has_in_band
ce4a6b76
BP
5331 && in_band_must_output_to_local_port(flow)
5332 && !actions_output_to_local_port(&ctx)) {
e93ef1c7 5333 compose_output_action(&ctx, OFPP_LOCAL, NULL);
9583bc14 5334 }
aaa0fbae 5335
a6092018
BP
5336 if (user_cookie_offset) {
5337 fix_sflow_action(&ctx, user_cookie_offset);
e672ff9b 5338 }
9583bc14
EJ
5339 }
5340
1520ef4f 5341 if (nl_attr_oversized(ctx.odp_actions->size)) {
542024c4 5342 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
5343 * can't hand them to the kernel directly. dpif_execute() can execute
5344 * them one by one with help, so just mark the result as SLOW_ACTION to
5345 * prevent the flow from being installed. */
5346 COVERAGE_INC(xlate_actions_oversize);
5347 ctx.xout->slow |= SLOW_ACTION;
1520ef4f 5348 } else if (too_many_output_actions(ctx.odp_actions)) {
7d031d7e
BP
5349 COVERAGE_INC(xlate_actions_too_many_output);
5350 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
5351 }
5352
1806291d
BP
5353 /* Do netflow only for packets really received by the bridge and not sent
5354 * to the controller. We consider packets sent to the controller to be
5355 * part of the control plane rather than the data plane. */
5356 if (!xin->recirc && xbridge->netflow && !(xout->slow & SLOW_CONTROLLER)) {
5357 if (ctx.xin->resubmit_stats) {
5358 netflow_flow_update(xbridge->netflow, flow,
2031ef97 5359 ctx.nf_output_iface,
1806291d
BP
5360 ctx.xin->resubmit_stats);
5361 }
5362 if (ctx.xin->xcache) {
5363 struct xc_entry *entry;
b256dc52 5364
1806291d
BP
5365 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
5366 entry->u.nf.netflow = netflow_ref(xbridge->netflow);
5367 entry->u.nf.flow = xmemdup(flow, sizeof *flow);
2031ef97 5368 entry->u.nf.iface = ctx.nf_output_iface;
d6fc5f57
EJ
5369 }
5370 }
5371
49a73e0c 5372 if (xin->wc) {
234c3da9 5373 xlate_wc_finish(&ctx);
c56fac1b 5374 }
1520ef4f
BP
5375
5376exit:
5377 ofpbuf_uninit(&ctx.stack);
5378 ofpbuf_uninit(&ctx.action_set);
5379 ofpbuf_uninit(&scratch_actions);
fff1b9c0
JR
5380
5381 /* Make sure we return a "drop flow" in case of an error. */
5382 if (ctx.error) {
5383 xout->slow = 0;
5384 if (xin->odp_actions) {
5385 ofpbuf_clear(xin->odp_actions);
5386 }
5387 }
5388 return ctx.error;
91d6cd12
AW
5389}
5390
5391/* Sends 'packet' out 'ofport'.
5392 * May modify 'packet'.
5393 * Returns 0 if successful, otherwise a positive errno value. */
5394int
cf62fa4c 5395xlate_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
91d6cd12 5396{
84f0f298 5397 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
91d6cd12 5398 struct xport *xport;
91d6cd12 5399 struct ofpact_output output;
91d6cd12 5400 struct flow flow;
91d6cd12 5401
91d6cd12
AW
5402 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5403 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
cf62fa4c 5404 flow_extract(packet, &flow);
b5e7e61a 5405 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12 5406
84f0f298 5407 xport = xport_lookup(xcfg, ofport);
91d6cd12 5408 if (!xport) {
02ea2703 5409 return EINVAL;
91d6cd12 5410 }
91d6cd12
AW
5411 output.port = xport->ofp_port;
5412 output.max_len = 0;
e491a67a
YT
5413
5414 return ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
5415 &output.ofpact, sizeof output,
5416 packet);
9583bc14 5417}
b256dc52
JS
5418
5419struct xlate_cache *
5420xlate_cache_new(void)
5421{
5422 struct xlate_cache *xcache = xmalloc(sizeof *xcache);
5423
5424 ofpbuf_init(&xcache->entries, 512);
5425 return xcache;
5426}
5427
5428static struct xc_entry *
5429xlate_cache_add_entry(struct xlate_cache *xcache, enum xc_type type)
5430{
5431 struct xc_entry *entry;
5432
5433 entry = ofpbuf_put_zeros(&xcache->entries, sizeof *entry);
5434 entry->type = type;
5435
5436 return entry;
5437}
5438
5439static void
5440xlate_cache_netdev(struct xc_entry *entry, const struct dpif_flow_stats *stats)
5441{
5442 if (entry->u.dev.tx) {
5443 netdev_vport_inc_tx(entry->u.dev.tx, stats);
5444 }
5445 if (entry->u.dev.rx) {
5446 netdev_vport_inc_rx(entry->u.dev.rx, stats);
5447 }
5448 if (entry->u.dev.bfd) {
5449 bfd_account_rx(entry->u.dev.bfd, stats);
5450 }
5451}
5452
5453static void
5454xlate_cache_normal(struct ofproto_dpif *ofproto, struct flow *flow, int vlan)
5455{
84f0f298 5456 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
b256dc52
JS
5457 struct xbridge *xbridge;
5458 struct xbundle *xbundle;
5459 struct flow_wildcards wc;
5460
84f0f298 5461 xbridge = xbridge_lookup(xcfg, ofproto);
b256dc52
JS
5462 if (!xbridge) {
5463 return;
5464 }
5465
5466 xbundle = lookup_input_bundle(xbridge, flow->in_port.ofp_port, false,
5467 NULL);
5468 if (!xbundle) {
5469 return;
5470 }
5471
5472 update_learning_table(xbridge, flow, &wc, vlan, xbundle);
5473}
5474
5475/* Push stats and perform side effects of flow translation. */
5476void
0725e747 5477xlate_push_stats(struct xlate_cache *xcache,
b256dc52
JS
5478 const struct dpif_flow_stats *stats)
5479{
5480 struct xc_entry *entry;
5481 struct ofpbuf entries = xcache->entries;
74ff3298 5482 struct eth_addr dmac;
b256dc52 5483
bc388583
BP
5484 if (!stats->n_packets) {
5485 return;
5486 }
5487
b256dc52
JS
5488 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5489 switch (entry->type) {
5490 case XC_RULE:
5491 rule_dpif_credit_stats(entry->u.rule, stats);
5492 break;
5493 case XC_BOND:
5494 bond_account(entry->u.bond.bond, entry->u.bond.flow,
5495 entry->u.bond.vid, stats->n_bytes);
5496 break;
5497 case XC_NETDEV:
5498 xlate_cache_netdev(entry, stats);
5499 break;
5500 case XC_NETFLOW:
5501 netflow_flow_update(entry->u.nf.netflow, entry->u.nf.flow,
5502 entry->u.nf.iface, stats);
5503 break;
5504 case XC_MIRROR:
5505 mirror_update_stats(entry->u.mirror.mbridge,
5506 entry->u.mirror.mirrors,
5507 stats->n_packets, stats->n_bytes);
5508 break;
5509 case XC_LEARN:
0725e747 5510 ofproto_dpif_flow_mod(entry->u.learn.ofproto, entry->u.learn.fm);
b256dc52
JS
5511 break;
5512 case XC_NORMAL:
0725e747
BP
5513 xlate_cache_normal(entry->u.normal.ofproto, entry->u.normal.flow,
5514 entry->u.normal.vlan);
b256dc52
JS
5515 break;
5516 case XC_FIN_TIMEOUT:
5517 xlate_fin_timeout__(entry->u.fin.rule, stats->tcp_flags,
5518 entry->u.fin.idle, entry->u.fin.hard);
5519 break;
1e684d7d
RW
5520 case XC_GROUP:
5521 group_dpif_credit_stats(entry->u.group.group, entry->u.group.bucket,
5522 stats);
5523 break;
53902038
TLSC
5524 case XC_TNL_NEIGH:
5525 /* Lookup neighbor to avoid timeout. */
0b411137
TLSC
5526 tnl_neigh_lookup(entry->u.tnl_neigh_cache.br_name,
5527 &entry->u.tnl_neigh_cache.d_ipv6, &dmac);
a36de779 5528 break;
b256dc52
JS
5529 default:
5530 OVS_NOT_REACHED();
5531 }
5532 }
5533}
5534
5535static void
5536xlate_dev_unref(struct xc_entry *entry)
5537{
5538 if (entry->u.dev.tx) {
5539 netdev_close(entry->u.dev.tx);
5540 }
5541 if (entry->u.dev.rx) {
5542 netdev_close(entry->u.dev.rx);
5543 }
5544 if (entry->u.dev.bfd) {
5545 bfd_unref(entry->u.dev.bfd);
5546 }
5547}
5548
5549static void
5550xlate_cache_clear_netflow(struct netflow *netflow, struct flow *flow)
5551{
b256dc52
JS
5552 netflow_flow_clear(netflow, flow);
5553 netflow_unref(netflow);
5554 free(flow);
5555}
5556
5557void
5558xlate_cache_clear(struct xlate_cache *xcache)
5559{
5560 struct xc_entry *entry;
5561 struct ofpbuf entries;
5562
5563 if (!xcache) {
5564 return;
5565 }
5566
5567 XC_ENTRY_FOR_EACH (entry, entries, xcache) {
5568 switch (entry->type) {
5569 case XC_RULE:
5570 rule_dpif_unref(entry->u.rule);
5571 break;
5572 case XC_BOND:
5573 free(entry->u.bond.flow);
5574 bond_unref(entry->u.bond.bond);
5575 break;
5576 case XC_NETDEV:
5577 xlate_dev_unref(entry);
5578 break;
5579 case XC_NETFLOW:
5580 xlate_cache_clear_netflow(entry->u.nf.netflow, entry->u.nf.flow);
5581 break;
5582 case XC_MIRROR:
5583 mbridge_unref(entry->u.mirror.mbridge);
5584 break;
5585 case XC_LEARN:
4165b5e0
JS
5586 free(entry->u.learn.fm);
5587 ofpbuf_delete(entry->u.learn.ofpacts);
b256dc52
JS
5588 break;
5589 case XC_NORMAL:
5590 free(entry->u.normal.flow);
5591 break;
5592 case XC_FIN_TIMEOUT:
83709dfa
JR
5593 /* 'u.fin.rule' is always already held as a XC_RULE, which
5594 * has already released it's reference above. */
b256dc52 5595 break;
1e684d7d
RW
5596 case XC_GROUP:
5597 group_dpif_unref(entry->u.group.group);
5598 break;
53902038 5599 case XC_TNL_NEIGH:
a36de779 5600 break;
b256dc52
JS
5601 default:
5602 OVS_NOT_REACHED();
5603 }
5604 }
5605
5606 ofpbuf_clear(&xcache->entries);
5607}
5608
5609void
5610xlate_cache_delete(struct xlate_cache *xcache)
5611{
5612 xlate_cache_clear(xcache);
5613 ofpbuf_uninit(&xcache->entries);
5614 free(xcache);
5615}