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