]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-xlate.c
userspace: Add packet_type in dp_packet and flow
[mirror_ovs.git] / ofproto / ofproto-dpif-xlate.c
CommitLineData
b827b231 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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
db7d4e46 25#include "bfd.h"
9583bc14
EJ
26#include "bitmap.h"
27#include "bond.h"
28#include "bundle.h"
29#include "byte-order.h"
db7d4e46 30#include "cfm.h"
9583bc14
EJ
31#include "connmgr.h"
32#include "coverage.h"
46445c63 33#include "csum.h"
e14deea0 34#include "dp-packet.h"
9583bc14 35#include "dpif.h"
f7f1ea29 36#include "in-band.h"
db7d4e46 37#include "lacp.h"
9583bc14
EJ
38#include "learn.h"
39#include "mac-learning.h"
6d95c4e8 40#include "mcast-snooping.h"
9583bc14
EJ
41#include "multipath.h"
42#include "netdev-vport.h"
43#include "netlink.h"
44#include "nx-match.h"
45#include "odp-execute.h"
9583bc14 46#include "ofproto/ofproto-dpif-ipfix.h"
ec7ceaed 47#include "ofproto/ofproto-dpif-mirror.h"
60d02c72 48#include "ofproto/ofproto-dpif-monitor.h"
9583bc14 49#include "ofproto/ofproto-dpif-sflow.h"
2d9b49dd 50#include "ofproto/ofproto-dpif-trace.h"
901a517e 51#include "ofproto/ofproto-dpif-xlate-cache.h"
9583bc14 52#include "ofproto/ofproto-dpif.h"
6f00e29b 53#include "ofproto/ofproto-provider.h"
b598f214
BW
54#include "openvswitch/dynamic-string.h"
55#include "openvswitch/meta-flow.h"
56#include "openvswitch/list.h"
57#include "openvswitch/ofp-actions.h"
58#include "openvswitch/vlog.h"
59#include "ovs-lldp.h"
a36de779 60#include "ovs-router.h"
b598f214
BW
61#include "packets.h"
62#include "tnl-neigh-cache.h"
a36de779 63#include "tnl-ports.h"
9583bc14 64#include "tunnel.h"
ee89ea7b 65#include "util.h"
9583bc14 66
46c88433 67COVERAGE_DEFINE(xlate_actions);
0f032e95 68COVERAGE_DEFINE(xlate_actions_oversize);
7d031d7e 69COVERAGE_DEFINE(xlate_actions_too_many_output);
9583bc14
EJ
70
71VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
72
8a553e9a 73/* Maximum depth of flow table recursion (due to resubmit actions) in a
790c5d26
BP
74 * flow translation.
75 *
76 * The goal of limiting the depth of resubmits is to ensure that flow
77 * translation eventually terminates. Only resubmits to the same table or an
78 * earlier table count against the maximum depth. This is because resubmits to
79 * strictly monotonically increasing table IDs will eventually terminate, since
80 * any OpenFlow switch has a finite number of tables. OpenFlow tables are most
81 * commonly traversed in numerically increasing order, so this limit has little
82 * effect on conventionally designed OpenFlow pipelines.
83 *
84 * Outputs to patch ports and to groups also count against the depth limit. */
85#define MAX_DEPTH 64
8a553e9a 86
98b07853
BP
87/* Maximum number of resubmit actions in a flow translation, whether they are
88 * recursive or not. */
790c5d26 89#define MAX_RESUBMITS (MAX_DEPTH * MAX_DEPTH)
98b07853 90
46c88433
EJ
91struct xbridge {
92 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
93 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
94
ca6ba700 95 struct ovs_list xbundles; /* Owned xbundles. */
46c88433
EJ
96 struct hmap xports; /* Indexed by ofp_port. */
97
98 char *name; /* Name used in log messages. */
89a8a7f0 99 struct dpif *dpif; /* Datapath interface. */
46c88433 100 struct mac_learning *ml; /* Mac learning handle. */
6d95c4e8 101 struct mcast_snooping *ms; /* Multicast Snooping handle. */
46c88433
EJ
102 struct mbridge *mbridge; /* Mirroring. */
103 struct dpif_sflow *sflow; /* SFlow handle, or null. */
104 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
ce3955be 105 struct netflow *netflow; /* Netflow handle, or null. */
9d189a50 106 struct stp *stp; /* STP or null if disabled. */
9efd308e 107 struct rstp *rstp; /* RSTP or null if disabled. */
46c88433 108
46c88433
EJ
109 bool has_in_band; /* Bridge has in band control? */
110 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
4b97b70d 111
b440dd8c
JS
112 /* Datapath feature support. */
113 struct dpif_backer_support support;
46c88433
EJ
114};
115
116struct xbundle {
117 struct hmap_node hmap_node; /* In global 'xbundles' map. */
118 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
119
ca6ba700 120 struct ovs_list list_node; /* In parent 'xbridges' list. */
46c88433
EJ
121 struct xbridge *xbridge; /* Parent xbridge. */
122
ca6ba700 123 struct ovs_list xports; /* Contains "struct xport"s. */
46c88433
EJ
124
125 char *name; /* Name used in log messages. */
126 struct bond *bond; /* Nonnull iff more than one port. */
127 struct lacp *lacp; /* LACP handle or null. */
128
129 enum port_vlan_mode vlan_mode; /* VLAN mode. */
fed8962a
EG
130 uint16_t qinq_ethtype; /* Ethertype of dot1q-tunnel interface
131 * either 0x8100 or 0x88a8. */
46c88433
EJ
132 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
133 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
134 * NULL if all VLANs are trunked. */
fed8962a
EG
135 unsigned long *cvlans; /* Bitmap of allowed customer vlans,
136 * NULL if all VLANs are allowed */
46c88433
EJ
137 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
138 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
c005f976 139 bool protected; /* Protected port mode */
46c88433
EJ
140};
141
142struct xport {
143 struct hmap_node hmap_node; /* Node in global 'xports' map. */
144 struct ofport_dpif *ofport; /* Key in global 'xports map. */
145
146 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
147 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
148
149 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
150
ca6ba700 151 struct ovs_list bundle_node; /* In parent xbundle (if it exists). */
46c88433
EJ
152 struct xbundle *xbundle; /* Parent xbundle or null. */
153
154 struct netdev *netdev; /* 'ofport''s netdev. */
155
156 struct xbridge *xbridge; /* Parent bridge. */
157 struct xport *peer; /* Patch port peer or null. */
158
159 enum ofputil_port_config config; /* OpenFlow port configuration. */
dd8cd4b4 160 enum ofputil_port_state state; /* OpenFlow port state. */
92cf817b 161 int stp_port_no; /* STP port number or -1 if not in use. */
f025bcb7 162 struct rstp_port *rstp_port; /* RSTP port or null. */
46c88433 163
55954f6e
EJ
164 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
165
46c88433
EJ
166 bool may_enable; /* May be enabled in bonds. */
167 bool is_tunnel; /* Is a tunnel port. */
168
169 struct cfm *cfm; /* CFM handle or null. */
170 struct bfd *bfd; /* BFD handle or null. */
0477baa9 171 struct lldp *lldp; /* LLDP handle or null. */
46c88433
EJ
172};
173
4d0acc70
EJ
174struct xlate_ctx {
175 struct xlate_in *xin;
176 struct xlate_out *xout;
177
46c88433 178 const struct xbridge *xbridge;
4d0acc70
EJ
179
180 /* Flow at the last commit. */
181 struct flow base_flow;
182
183 /* Tunnel IP destination address as received. This is stored separately
184 * as the base_flow.tunnel is cleared on init to reflect the datapath
185 * behavior. Used to make sure not to send tunneled output to ourselves,
186 * which might lead to an infinite loop. This could happen easily
187 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
188 * actually set the tun_dst field. */
e4d3706c 189 struct in6_addr orig_tunnel_ipv6_dst;
4d0acc70 190
84cf3c1f
JR
191 /* Stack for the push and pop actions. See comment above nx_stack_push()
192 * in nx-match.c for info on how the stack is stored. */
4d0acc70
EJ
193 struct ofpbuf stack;
194
195 /* The rule that we are currently translating, or NULL. */
196 struct rule_dpif *rule;
197
49a73e0c
BP
198 /* Flow translation populates this with wildcards relevant in translation.
199 * When 'xin->wc' is nonnull, this is the same pointer. When 'xin->wc' is
c0e638aa 200 * null, this is a pointer to a temporary buffer. */
49a73e0c
BP
201 struct flow_wildcards *wc;
202
1520ef4f
BP
203 /* Output buffer for datapath actions. When 'xin->odp_actions' is nonnull,
204 * this is the same pointer. When 'xin->odp_actions' is null, this points
205 * to a scratch ofpbuf. This allows code to add actions to
206 * 'ctx->odp_actions' without worrying about whether the caller really
207 * wants actions. */
208 struct ofpbuf *odp_actions;
209
790c5d26
BP
210 /* Statistics maintained by xlate_table_action().
211 *
2d9b49dd 212 * These statistics limit the amount of work that a single flow
790c5d26
BP
213 * translation can perform. The goal of the first of these, 'depth', is
214 * primarily to prevent translation from performing an infinite amount of
215 * work. It counts the current depth of nested "resubmit"s (and a few
216 * other activities); when a resubmit returns, it decreases. Resubmits to
217 * tables in strictly monotonically increasing order don't contribute to
218 * 'depth' because they cannot cause a flow translation to take an infinite
219 * amount of time (because the number of tables is finite). Translation
220 * aborts when 'depth' exceeds MAX_DEPTH.
221 *
222 * 'resubmits', on the other hand, prevents flow translation from
223 * performing an extraordinarily large while still finite amount of work.
224 * It counts the total number of resubmits (and a few other activities)
225 * that have been executed. Returning from a resubmit does not affect this
226 * counter. Thus, this limits the amount of work that a particular
227 * translation can perform. Translation aborts when 'resubmits' exceeds
228 * MAX_RESUBMITS (which is much larger than MAX_DEPTH).
229 */
790c5d26 230 int depth; /* Current resubmit nesting depth. */
98b07853 231 int resubmits; /* Total number of resubmits. */
5a070238 232 bool in_group; /* Currently translating ofgroup, if true. */
029ca940 233 bool in_action_set; /* Currently translating action_set, if true. */
331c07ac
YHW
234 bool in_packet_out; /* Currently translating a packet_out msg, if
235 * true. */
98b07853 236
4d0acc70 237 uint8_t table_id; /* OpenFlow table ID where flow was found. */
8b1e5560
JR
238 ovs_be64 rule_cookie; /* Cookie of the rule being translated. */
239 uint32_t orig_skb_priority; /* Priority when packet arrived. */
4d0acc70 240 uint32_t sflow_n_outputs; /* Number of output ports. */
4e022ec0 241 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
2031ef97 242 ofp_port_t nf_output_iface; /* Output interface index for NetFlow. */
4d0acc70 243 bool exit; /* No further actions should be processed. */
3d6151f3 244 mirror_mask_t mirrors; /* Bitmap of associated mirrors. */
1356dbd1 245 int mirror_snaplen; /* Max size of a mirror packet in byte. */
7fdb60a7 246
1d361a81
BP
247 /* Freezing Translation
248 * ====================
e672ff9b 249 *
1d361a81
BP
250 * At some point during translation, the code may recognize the need to halt
251 * and checkpoint the translation in a way that it can be restarted again
252 * later. We call the checkpointing process "freezing" and the restarting
253 * process "thawing".
e672ff9b 254 *
1d361a81 255 * The use cases for freezing are:
e672ff9b 256 *
1d361a81
BP
257 * - "Recirculation", where the translation process discovers that it
258 * doesn't have enough information to complete translation without
259 * actually executing the actions that have already been translated,
260 * which provides the additionally needed information. In these
261 * situations, translation freezes translation and assigns the frozen
262 * data a unique "recirculation ID", which it associates with the data
263 * in a table in userspace (see ofproto-dpif-rid.h). It also adds a
264 * OVS_ACTION_ATTR_RECIRC action specifying that ID to the datapath
265 * actions. When a packet hits that action, the datapath looks its
266 * flow up again using the ID. If there's a miss, it comes back to
267 * userspace, which find the recirculation table entry for the ID,
268 * thaws the associated frozen data, and continues translation from
269 * that point given the additional information that is now known.
e672ff9b 270 *
1d361a81
BP
271 * The archetypal example is MPLS. As MPLS is implemented in
272 * OpenFlow, the protocol that follows the last MPLS label becomes
273 * known only when that label is popped by an OpenFlow action. That
274 * means that Open vSwitch can't extract the headers beyond the MPLS
275 * labels until the pop action is executed. Thus, at that point
276 * translation uses the recirculation process to extract the headers
277 * beyond the MPLS labels.
e672ff9b 278 *
1d361a81
BP
279 * (OVS also uses OVS_ACTION_ATTR_RECIRC to implement hashing for
280 * output to bonds. OVS pre-populates all the datapath flows for bond
281 * output in the datapath, though, which means that the elaborate
282 * process of coming back to userspace for a second round of
283 * translation isn't needed, and so bonds don't follow the above
284 * process.)
e672ff9b 285 *
77ab5fd2
BP
286 * - "Continuation". A continuation is a way for an OpenFlow controller
287 * to interpose on a packet's traversal of the OpenFlow tables. When
288 * the translation process encounters a "controller" action with the
289 * "pause" flag, it freezes translation, serializes the frozen data,
290 * and sends it to an OpenFlow controller. The controller then
291 * examines and possibly modifies the frozen data and eventually sends
292 * it back to the switch, which thaws it and continues translation.
e672ff9b 293 *
1d361a81
BP
294 * The main problem of freezing translation is preserving state, so that
295 * when the translation is thawed later it resumes from where it left off,
296 * without disruption. In particular, actions must be preserved as follows:
297 *
298 * - If we're freezing because an action needed more information, the
299 * action that prompted it.
300 *
301 * - Any actions remaining to be translated within the current flow.
302 *
303 * - If translation was frozen within a NXAST_RESUBMIT, then any actions
304 * following the resubmit action. Resubmit actions can be nested, so
305 * this has to go all the way up the control stack.
e672ff9b
JR
306 *
307 * - The OpenFlow 1.1+ action set.
308 *
309 * State that actions and flow table lookups can depend on, such as the
310 * following, must also be preserved:
311 *
312 * - Metadata fields (input port, registers, OF1.1+ metadata, ...).
313 *
1d361a81 314 * - The stack used by NXAST_STACK_PUSH and NXAST_STACK_POP actions.
e672ff9b
JR
315 *
316 * - The table ID and cookie of the flow being translated at each level
1d361a81
BP
317 * of the control stack, because these can become visible through
318 * OFPAT_CONTROLLER actions (and other ways).
e672ff9b
JR
319 *
320 * Translation allows for the control of this state preservation via these
1d361a81
BP
321 * members. When a need to freeze translation is identified, the
322 * translation process:
e672ff9b 323 *
1d361a81 324 * 1. Sets 'freezing' to true.
e672ff9b
JR
325 *
326 * 2. Sets 'exit' to true to tell later steps that we're exiting from the
327 * translation process.
328 *
1d361a81
BP
329 * 3. Adds an OFPACT_UNROLL_XLATE action to 'frozen_actions', and points
330 * frozen_actions.header to the action to make it easy to find it later.
331 * This action holds the current table ID and cookie so that they can be
332 * restored during a post-recirculation upcall translation.
e672ff9b
JR
333 *
334 * 4. Adds the action that prompted recirculation and any actions following
1d361a81 335 * it within the same flow to 'frozen_actions', so that they can be
8a5fb3b4 336 * executed during a post-recirculation upcall translation.
e672ff9b
JR
337 *
338 * 5. Returns.
339 *
340 * 6. The action that prompted recirculation might be nested in a stack of
341 * nested "resubmit"s that have actions remaining. Each of these notices
1d361a81
BP
342 * that we're exiting and freezing and responds by adding more
343 * OFPACT_UNROLL_XLATE actions to 'frozen_actions', as necessary,
344 * followed by any actions that were yet unprocessed.
e672ff9b 345 *
1d361a81
BP
346 * If we're freezing because of recirculation, the caller generates a
347 * recirculation ID and associates all the state produced by this process
348 * with it. For post-recirculation upcall translation, the caller passes it
349 * back in for the new translation to execute. The process yielded a set of
350 * ofpacts that can be translated directly, so it is not much of a special
351 * case at that point.
e672ff9b 352 */
1d361a81 353 bool freezing;
53cc166a
JR
354 bool recirc_update_dp_hash; /* Generated recirculation will be preceded
355 * by datapath HASH action to get an updated
356 * dp_hash after recirculation. */
357 uint32_t dp_hash_alg;
358 uint32_t dp_hash_basis;
1d361a81 359 struct ofpbuf frozen_actions;
77ab5fd2 360 const struct ofpact_controller *pause;
e672ff9b 361
e12ec36b
SH
362 /* True if a packet was but is no longer MPLS (due to an MPLS pop action).
363 * This is a trigger for recirculation in cases where translating an action
364 * or looking up a flow requires access to the fields of the packet after
365 * the MPLS label stack that was originally present. */
366 bool was_mpls;
367
07659514
JS
368 /* True if conntrack has been performed on this packet during processing
369 * on the current bridge. This is used to determine whether conntrack
1d361a81 370 * state from the datapath should be honored after thawing. */
07659514
JS
371 bool conntracked;
372
9ac0aada
JR
373 /* Pointer to an embedded NAT action in a conntrack action, or NULL. */
374 struct ofpact_nat *ct_nat_action;
375
7fdb60a7
SH
376 /* OpenFlow 1.1+ action set.
377 *
378 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
379 * When translation is otherwise complete, ofpacts_execute_action_set()
380 * converts it to a set of "struct ofpact"s that can be translated into
ed9c9e3e 381 * datapath actions. */
c61f3870 382 bool action_set_has_group; /* Action set contains OFPACT_GROUP? */
7fdb60a7 383 struct ofpbuf action_set; /* Action set. */
fff1b9c0
JR
384
385 enum xlate_error error; /* Translation failed. */
4d0acc70
EJ
386};
387
f0fb825a
EG
388/* Structure to track VLAN manipulation */
389struct xvlan_single {
390 uint16_t tpid;
391 uint16_t vid;
392 uint16_t pcp;
393};
394
395struct xvlan {
396 struct xvlan_single v[FLOW_MAX_VLAN_HEADERS];
397};
398
fff1b9c0
JR
399const char *xlate_strerror(enum xlate_error error)
400{
401 switch (error) {
402 case XLATE_OK:
403 return "OK";
404 case XLATE_BRIDGE_NOT_FOUND:
405 return "Bridge not found";
406 case XLATE_RECURSION_TOO_DEEP:
407 return "Recursion too deep";
408 case XLATE_TOO_MANY_RESUBMITS:
409 return "Too many resubmits";
410 case XLATE_STACK_TOO_DEEP:
411 return "Stack too deep";
412 case XLATE_NO_RECIRCULATION_CONTEXT:
413 return "No recirculation context";
414 case XLATE_RECIRCULATION_CONFLICT:
415 return "Recirculation conflict";
416 case XLATE_TOO_MANY_MPLS_LABELS:
417 return "Too many MPLS labels";
8d8ab6c2
JG
418 case XLATE_INVALID_TUNNEL_METADATA:
419 return "Invalid tunnel metadata";
fff1b9c0
JR
420 }
421 return "Unknown error";
422}
423
ed9c9e3e 424static void xlate_action_set(struct xlate_ctx *ctx);
704bb0bf 425static void xlate_commit_actions(struct xlate_ctx *ctx);
ed9c9e3e 426
f1dac512
SC
427static void
428apply_nested_clone_actions(struct xlate_ctx *ctx, const struct xport *in_dev,
429 struct xport *out_dev);
430
1d741d6d 431static void
1d361a81 432ctx_trigger_freeze(struct xlate_ctx *ctx)
1d741d6d
JR
433{
434 ctx->exit = true;
1d361a81 435 ctx->freezing = true;
1d741d6d
JR
436}
437
53cc166a
JR
438static void
439ctx_trigger_recirculate_with_hash(struct xlate_ctx *ctx, uint32_t type,
440 uint32_t basis)
441{
442 ctx->exit = true;
443 ctx->freezing = true;
444 ctx->recirc_update_dp_hash = true;
445 ctx->dp_hash_alg = type;
446 ctx->dp_hash_basis = basis;
447}
448
1d741d6d 449static bool
1d361a81 450ctx_first_frozen_action(const struct xlate_ctx *ctx)
1d741d6d 451{
1d361a81 452 return !ctx->frozen_actions.size;
e672ff9b
JR
453}
454
3293cb85 455static void
1d361a81 456ctx_cancel_freeze(struct xlate_ctx *ctx)
3293cb85 457{
1d361a81
BP
458 if (ctx->freezing) {
459 ctx->freezing = false;
53cc166a 460 ctx->recirc_update_dp_hash = false;
1d361a81
BP
461 ofpbuf_clear(&ctx->frozen_actions);
462 ctx->frozen_actions.header = NULL;
3293cb85
BP
463 }
464}
465
77ab5fd2 466static void finish_freezing(struct xlate_ctx *ctx);
e672ff9b 467
9583bc14
EJ
468/* A controller may use OFPP_NONE as the ingress port to indicate that
469 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
470 * when an input bundle is needed for validation (e.g., mirroring or
471 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
3548d242
BP
472 * any 'port' structs, so care must be taken when dealing with it. */
473static struct xbundle ofpp_none_bundle = {
474 .name = "OFPP_NONE",
475 .vlan_mode = PORT_VLAN_TRUNK
476};
9583bc14 477
55954f6e
EJ
478/* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
479 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
480 * traffic egressing the 'ofport' with that priority should be marked with. */
481struct skb_priority_to_dscp {
482 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
483 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
484
485 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
486};
487
84f0f298
RW
488/* Xlate config contains hash maps of all bridges, bundles and ports.
489 * Xcfgp contains the pointer to the current xlate configuration.
490 * When the main thread needs to change the configuration, it copies xcfgp to
491 * new_xcfg and edits new_xcfg. This enables the use of RCU locking which
492 * does not block handler and revalidator threads. */
493struct xlate_cfg {
494 struct hmap xbridges;
495 struct hmap xbundles;
496 struct hmap xports;
497};
b1b72f2d 498static OVSRCU_TYPE(struct xlate_cfg *) xcfgp = OVSRCU_INITIALIZER(NULL);
f439f23b 499static struct xlate_cfg *new_xcfg = NULL;
46c88433
EJ
500
501static bool may_receive(const struct xport *, struct xlate_ctx *);
9583bc14
EJ
502static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
503 struct xlate_ctx *);
adcf00ba 504static void xlate_normal(struct xlate_ctx *);
6d328fa2
SH
505static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
506 uint8_t table_id, bool may_packet_in,
2cd20955 507 bool honor_table_miss, bool with_ct_orig);
2d9b49dd
BP
508static bool input_vid_is_valid(const struct xlate_ctx *,
509 uint16_t vid, struct xbundle *);
f0fb825a
EG
510static void xvlan_copy(struct xvlan *dst, const struct xvlan *src);
511static void xvlan_pop(struct xvlan *src);
fed8962a 512static void xvlan_push_uninit(struct xvlan *src);
f0fb825a
EG
513static void xvlan_extract(const struct flow *, struct xvlan *);
514static void xvlan_put(struct flow *, const struct xvlan *);
515static void xvlan_input_translate(const struct xbundle *,
516 const struct xvlan *in,
517 struct xvlan *xvlan);
518static void xvlan_output_translate(const struct xbundle *,
519 const struct xvlan *xvlan,
520 struct xvlan *out);
46c88433 521static void output_normal(struct xlate_ctx *, const struct xbundle *,
f0fb825a 522 const struct xvlan *);
e93ef1c7
JR
523
524/* Optional bond recirculation parameter to compose_output_action(). */
525struct xlate_bond_recirc {
526 uint32_t recirc_id; /* !0 Use recirculation instead of output. */
527 uint8_t hash_alg; /* !0 Compute hash for recirc before. */
528 uint32_t hash_basis; /* Compute hash for recirc before. */
529};
530
531static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port,
532 const struct xlate_bond_recirc *xr);
9583bc14 533
84f0f298
RW
534static struct xbridge *xbridge_lookup(struct xlate_cfg *,
535 const struct ofproto_dpif *);
290835f9
BP
536static struct xbridge *xbridge_lookup_by_uuid(struct xlate_cfg *,
537 const struct uuid *);
84f0f298
RW
538static struct xbundle *xbundle_lookup(struct xlate_cfg *,
539 const struct ofbundle *);
540static struct xport *xport_lookup(struct xlate_cfg *,
541 const struct ofport_dpif *);
46c88433 542static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
55954f6e
EJ
543static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
544 uint32_t skb_priority);
545static void clear_skb_priorities(struct xport *);
16194afd 546static size_t count_skb_priorities(const struct xport *);
55954f6e
EJ
547static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
548 uint8_t *dscp);
46c88433 549
84f0f298
RW
550static void xlate_xbridge_init(struct xlate_cfg *, struct xbridge *);
551static void xlate_xbundle_init(struct xlate_cfg *, struct xbundle *);
552static void xlate_xport_init(struct xlate_cfg *, struct xport *);
9efd308e 553static void xlate_xbridge_set(struct xbridge *, struct dpif *,
9efd308e
DV
554 const struct mac_learning *, struct stp *,
555 struct rstp *, const struct mcast_snooping *,
556 const struct mbridge *,
557 const struct dpif_sflow *,
558 const struct dpif_ipfix *,
2f47cdf4 559 const struct netflow *,
84f0f298 560 bool forward_bpdu, bool has_in_band,
b440dd8c 561 const struct dpif_backer_support *);
84f0f298 562static void xlate_xbundle_set(struct xbundle *xbundle,
f0fb825a 563 enum port_vlan_mode vlan_mode,
fed8962a
EG
564 uint16_t qinq_ethtype, int vlan,
565 unsigned long *trunks, unsigned long *cvlans,
f0fb825a 566 bool use_priority_tags,
84f0f298 567 const struct bond *bond, const struct lacp *lacp,
c005f976 568 bool floodable, bool protected);
84f0f298
RW
569static void xlate_xport_set(struct xport *xport, odp_port_t odp_port,
570 const struct netdev *netdev, const struct cfm *cfm,
0477baa9
DF
571 const struct bfd *bfd, const struct lldp *lldp,
572 int stp_port_no, const struct rstp_port *rstp_port,
84f0f298
RW
573 enum ofputil_port_config config,
574 enum ofputil_port_state state, bool is_tunnel,
575 bool may_enable);
576static void xlate_xbridge_remove(struct xlate_cfg *, struct xbridge *);
577static void xlate_xbundle_remove(struct xlate_cfg *, struct xbundle *);
578static void xlate_xport_remove(struct xlate_cfg *, struct xport *);
579static void xlate_xbridge_copy(struct xbridge *);
580static void xlate_xbundle_copy(struct xbridge *, struct xbundle *);
581static void xlate_xport_copy(struct xbridge *, struct xbundle *,
582 struct xport *);
583static void xlate_xcfg_free(struct xlate_cfg *);
2d9b49dd
BP
584\f
585/* Tracing helpers. */
586
587/* If tracing is enabled in 'ctx', creates a new trace node and appends it to
588 * the list of nodes maintained in ctx->xin. The new node has type 'type' and
589 * its text is created from 'format' by treating it as a printf format string.
590 * Returns the list of nodes embedded within the new trace node; ordinarily,
591 * the calleer can ignore this, but it is useful if the caller needs to nest
592 * more trace nodes within the new node.
593 *
594 * If tracing is not enabled, does nothing and returns NULL. */
595static struct ovs_list * OVS_PRINTF_FORMAT(3, 4)
596xlate_report(const struct xlate_ctx *ctx, enum oftrace_node_type type,
597 const char *format, ...)
34dd0d78 598{
2d9b49dd
BP
599 struct ovs_list *subtrace = NULL;
600 if (OVS_UNLIKELY(ctx->xin->trace)) {
c1b3756c 601 va_list args;
c1b3756c 602 va_start(args, format);
2d9b49dd
BP
603 char *text = xvasprintf(format, args);
604 subtrace = &oftrace_report(ctx->xin->trace, type, text)->subs;
c1b3756c 605 va_end(args);
2d9b49dd 606 free(text);
34dd0d78 607 }
2d9b49dd 608 return subtrace;
34dd0d78 609}
84f0f298 610
2d9b49dd
BP
611/* This is like xlate_report() for errors that are serious enough that we
612 * should log them even if we are not tracing. */
613static void OVS_PRINTF_FORMAT(2, 3)
614xlate_report_error(const struct xlate_ctx *ctx, const char *format, ...)
615{
616 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
617 if (!OVS_UNLIKELY(ctx->xin->trace)
618 && (!ctx->xin->packet || VLOG_DROP_WARN(&rl))) {
619 return;
620 }
621
622 struct ds s = DS_EMPTY_INITIALIZER;
623 va_list args;
624 va_start(args, format);
625 ds_put_format_valist(&s, format, args);
626 va_end(args);
627
628 if (ctx->xin->trace) {
629 oftrace_report(ctx->xin->trace, OFT_ERROR, ds_cstr(&s));
630 } else {
631 ds_put_cstr(&s, " while processing ");
632 flow_format(&s, &ctx->base_flow);
633 ds_put_format(&s, " on bridge %s", ctx->xbridge->name);
634 VLOG_WARN("%s", ds_cstr(&s));
635 }
636 ds_destroy(&s);
637}
638
639/* This is like xlate_report() for messages that should be logged at debug
640 * level (even if we are not tracing) because they can be valuable for
641 * debugging. */
642static void OVS_PRINTF_FORMAT(3, 4)
643xlate_report_debug(const struct xlate_ctx *ctx, enum oftrace_node_type type,
644 const char *format, ...)
645{
646 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
647 if (!OVS_UNLIKELY(ctx->xin->trace)
648 && (!ctx->xin->packet || VLOG_DROP_DBG(&rl))) {
649 return;
650 }
651
652 struct ds s = DS_EMPTY_INITIALIZER;
653 va_list args;
654 va_start(args, format);
655 ds_put_format_valist(&s, format, args);
656 va_end(args);
fff1b9c0 657
2d9b49dd
BP
658 if (ctx->xin->trace) {
659 oftrace_report(ctx->xin->trace, type, ds_cstr(&s));
660 } else {
661 VLOG_DBG("bridge %s: %s", ctx->xbridge->name, ds_cstr(&s));
662 }
663 ds_destroy(&s);
664}
fff1b9c0 665
2d9b49dd
BP
666/* If tracing is enabled in 'ctx', appends a node of the given 'type' to the
667 * trace, whose text is 'title' followed by a formatted version of the
668 * 'ofpacts_len' OpenFlow actions in 'ofpacts'.
669 *
670 * If tracing is not enabled, does nothing. */
671static void
672xlate_report_actions(const struct xlate_ctx *ctx, enum oftrace_node_type type,
673 const char *title,
d6bef3cc
BP
674 const struct ofpact *ofpacts, size_t ofpacts_len)
675{
2d9b49dd 676 if (OVS_UNLIKELY(ctx->xin->trace)) {
d6bef3cc 677 struct ds s = DS_EMPTY_INITIALIZER;
2d9b49dd 678 ds_put_format(&s, "%s: ", title);
d6bef3cc 679 ofpacts_format(ofpacts, ofpacts_len, &s);
2d9b49dd 680 oftrace_report(ctx->xin->trace, type, ds_cstr(&s));
d6bef3cc
BP
681 ds_destroy(&s);
682 }
683}
684
2d9b49dd
BP
685/* If tracing is enabled in 'ctx', appends a node of type OFT_DETAIL to the
686 * trace, whose the message is a formatted version of the OpenFlow action set.
687 * 'verb' should be "was" or "is", depending on whether the action set reported
688 * is the new action set or the old one.
689 *
690 * If tracing is not enabled, does nothing. */
691static void
692xlate_report_action_set(const struct xlate_ctx *ctx, const char *verb)
693{
694 if (OVS_UNLIKELY(ctx->xin->trace)) {
695 struct ofpbuf action_list;
696 ofpbuf_init(&action_list, 0);
697 ofpacts_execute_action_set(&action_list, &ctx->action_set);
698 if (action_list.size) {
699 struct ds s = DS_EMPTY_INITIALIZER;
700 ofpacts_format(action_list.data, action_list.size, &s);
701 xlate_report(ctx, OFT_DETAIL, "action set %s: %s",
702 verb, ds_cstr(&s));
703 ds_destroy(&s);
704 } else {
705 xlate_report(ctx, OFT_DETAIL, "action set %s empty", verb);
706 }
707 ofpbuf_uninit(&action_list);
708 }
709}
710
711
712/* If tracing is enabled in 'ctx', appends a node representing 'rule' (in
713 * OpenFlow table 'table_id') to the trace and makes this node the parent for
714 * future trace nodes. The caller should save ctx->xin->trace before calling
715 * this function, then after tracing all of the activities under the table,
716 * restore its previous value.
717 *
718 * If tracing is not enabled, does nothing. */
719static void
720xlate_report_table(const struct xlate_ctx *ctx, struct rule_dpif *rule,
721 uint8_t table_id)
722{
723 if (OVS_LIKELY(!ctx->xin->trace)) {
724 return;
725 }
726
727 struct ds s = DS_EMPTY_INITIALIZER;
728 ds_put_format(&s, "%2d. ", table_id);
729 if (rule == ctx->xin->ofproto->miss_rule) {
730 ds_put_cstr(&s, "No match, and a \"packet-in\" is called for.");
731 } else if (rule == ctx->xin->ofproto->no_packet_in_rule) {
732 ds_put_cstr(&s, "No match.");
733 } else if (rule == ctx->xin->ofproto->drop_frags_rule) {
734 ds_put_cstr(&s, "Packets are IP fragments and "
735 "the fragment handling mode is \"drop\".");
736 } else {
737 minimatch_format(&rule->up.cr.match,
738 ofproto_get_tun_tab(&ctx->xin->ofproto->up),
739 &s, OFP_DEFAULT_PRIORITY);
740 if (ds_last(&s) != ' ') {
741 ds_put_cstr(&s, ", ");
742 }
743 ds_put_format(&s, "priority %d", rule->up.cr.priority);
744 if (rule->up.flow_cookie) {
745 ds_put_format(&s, ", cookie %#"PRIx64,
746 ntohll(rule->up.flow_cookie));
747 }
748 }
749 ctx->xin->trace = &oftrace_report(ctx->xin->trace, OFT_TABLE,
750 ds_cstr(&s))->subs;
751 ds_destroy(&s);
752}
753
754/* If tracing is enabled in 'ctx', adds an OFT_DETAIL trace node to 'ctx'
755 * reporting the value of subfield 'sf'.
756 *
757 * If tracing is not enabled, does nothing. */
758static void
759xlate_report_subfield(const struct xlate_ctx *ctx,
760 const struct mf_subfield *sf)
761{
762 if (OVS_UNLIKELY(ctx->xin->trace)) {
763 struct ds s = DS_EMPTY_INITIALIZER;
764 mf_format_subfield(sf, &s);
765 ds_put_cstr(&s, " is now ");
766
767 if (sf->ofs == 0 && sf->n_bits >= sf->field->n_bits) {
768 union mf_value value;
769 mf_get_value(sf->field, &ctx->xin->flow, &value);
770 mf_format(sf->field, &value, NULL, &s);
771 } else {
772 union mf_subvalue cst;
773 mf_read_subfield(sf, &ctx->xin->flow, &cst);
774 ds_put_hex(&s, &cst, sizeof cst);
775 }
776
777 xlate_report(ctx, OFT_DETAIL, "%s", ds_cstr(&s));
778
779 ds_destroy(&s);
780 }
781}
782\f
84f0f298
RW
783static void
784xlate_xbridge_init(struct xlate_cfg *xcfg, struct xbridge *xbridge)
785{
417e7e66 786 ovs_list_init(&xbridge->xbundles);
84f0f298
RW
787 hmap_init(&xbridge->xports);
788 hmap_insert(&xcfg->xbridges, &xbridge->hmap_node,
789 hash_pointer(xbridge->ofproto, 0));
790}
791
792static void
793xlate_xbundle_init(struct xlate_cfg *xcfg, struct xbundle *xbundle)
794{
417e7e66
BW
795 ovs_list_init(&xbundle->xports);
796 ovs_list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
84f0f298
RW
797 hmap_insert(&xcfg->xbundles, &xbundle->hmap_node,
798 hash_pointer(xbundle->ofbundle, 0));
799}
800
801static void
802xlate_xport_init(struct xlate_cfg *xcfg, struct xport *xport)
803{
804 hmap_init(&xport->skb_priorities);
805 hmap_insert(&xcfg->xports, &xport->hmap_node,
806 hash_pointer(xport->ofport, 0));
807 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
808 hash_ofp_port(xport->ofp_port));
809}
810
811static void
812xlate_xbridge_set(struct xbridge *xbridge,
813 struct dpif *dpif,
ec89fc6f 814 const struct mac_learning *ml, struct stp *stp,
9efd308e 815 struct rstp *rstp, const struct mcast_snooping *ms,
ec89fc6f 816 const struct mbridge *mbridge,
46c88433 817 const struct dpif_sflow *sflow,
ce3955be 818 const struct dpif_ipfix *ipfix,
2f47cdf4 819 const struct netflow *netflow,
4b97b70d 820 bool forward_bpdu, bool has_in_band,
b440dd8c 821 const struct dpif_backer_support *support)
46c88433 822{
46c88433
EJ
823 if (xbridge->ml != ml) {
824 mac_learning_unref(xbridge->ml);
825 xbridge->ml = mac_learning_ref(ml);
826 }
827
6d95c4e8
FL
828 if (xbridge->ms != ms) {
829 mcast_snooping_unref(xbridge->ms);
830 xbridge->ms = mcast_snooping_ref(ms);
831 }
832
46c88433
EJ
833 if (xbridge->mbridge != mbridge) {
834 mbridge_unref(xbridge->mbridge);
835 xbridge->mbridge = mbridge_ref(mbridge);
836 }
837
838 if (xbridge->sflow != sflow) {
839 dpif_sflow_unref(xbridge->sflow);
840 xbridge->sflow = dpif_sflow_ref(sflow);
841 }
842
843 if (xbridge->ipfix != ipfix) {
844 dpif_ipfix_unref(xbridge->ipfix);
845 xbridge->ipfix = dpif_ipfix_ref(ipfix);
846 }
847
9d189a50
EJ
848 if (xbridge->stp != stp) {
849 stp_unref(xbridge->stp);
850 xbridge->stp = stp_ref(stp);
851 }
852
9efd308e
DV
853 if (xbridge->rstp != rstp) {
854 rstp_unref(xbridge->rstp);
855 xbridge->rstp = rstp_ref(rstp);
856 }
857
ce3955be
EJ
858 if (xbridge->netflow != netflow) {
859 netflow_unref(xbridge->netflow);
860 xbridge->netflow = netflow_ref(netflow);
861 }
862
89a8a7f0 863 xbridge->dpif = dpif;
46c88433
EJ
864 xbridge->forward_bpdu = forward_bpdu;
865 xbridge->has_in_band = has_in_band;
b440dd8c 866 xbridge->support = *support;
46c88433
EJ
867}
868
84f0f298
RW
869static void
870xlate_xbundle_set(struct xbundle *xbundle,
fed8962a
EG
871 enum port_vlan_mode vlan_mode, uint16_t qinq_ethtype,
872 int vlan, unsigned long *trunks, unsigned long *cvlans,
f0fb825a 873 bool use_priority_tags,
84f0f298 874 const struct bond *bond, const struct lacp *lacp,
c005f976 875 bool floodable, bool protected)
84f0f298
RW
876{
877 ovs_assert(xbundle->xbridge);
878
879 xbundle->vlan_mode = vlan_mode;
fed8962a 880 xbundle->qinq_ethtype = qinq_ethtype;
84f0f298
RW
881 xbundle->vlan = vlan;
882 xbundle->trunks = trunks;
fed8962a 883 xbundle->cvlans = cvlans;
84f0f298
RW
884 xbundle->use_priority_tags = use_priority_tags;
885 xbundle->floodable = floodable;
c005f976 886 xbundle->protected = protected;
84f0f298
RW
887
888 if (xbundle->bond != bond) {
889 bond_unref(xbundle->bond);
890 xbundle->bond = bond_ref(bond);
891 }
892
893 if (xbundle->lacp != lacp) {
894 lacp_unref(xbundle->lacp);
895 xbundle->lacp = lacp_ref(lacp);
896 }
897}
898
899static void
900xlate_xport_set(struct xport *xport, odp_port_t odp_port,
901 const struct netdev *netdev, const struct cfm *cfm,
0477baa9 902 const struct bfd *bfd, const struct lldp *lldp, int stp_port_no,
f025bcb7 903 const struct rstp_port* rstp_port,
84f0f298
RW
904 enum ofputil_port_config config, enum ofputil_port_state state,
905 bool is_tunnel, bool may_enable)
906{
907 xport->config = config;
908 xport->state = state;
909 xport->stp_port_no = stp_port_no;
910 xport->is_tunnel = is_tunnel;
911 xport->may_enable = may_enable;
912 xport->odp_port = odp_port;
913
f025bcb7
JR
914 if (xport->rstp_port != rstp_port) {
915 rstp_port_unref(xport->rstp_port);
916 xport->rstp_port = rstp_port_ref(rstp_port);
917 }
918
84f0f298
RW
919 if (xport->cfm != cfm) {
920 cfm_unref(xport->cfm);
921 xport->cfm = cfm_ref(cfm);
922 }
923
924 if (xport->bfd != bfd) {
925 bfd_unref(xport->bfd);
926 xport->bfd = bfd_ref(bfd);
927 }
928
0477baa9
DF
929 if (xport->lldp != lldp) {
930 lldp_unref(xport->lldp);
931 xport->lldp = lldp_ref(lldp);
932 }
933
84f0f298
RW
934 if (xport->netdev != netdev) {
935 netdev_close(xport->netdev);
936 xport->netdev = netdev_ref(netdev);
937 }
938}
939
940static void
941xlate_xbridge_copy(struct xbridge *xbridge)
942{
943 struct xbundle *xbundle;
944 struct xport *xport;
945 struct xbridge *new_xbridge = xzalloc(sizeof *xbridge);
946 new_xbridge->ofproto = xbridge->ofproto;
947 new_xbridge->name = xstrdup(xbridge->name);
948 xlate_xbridge_init(new_xcfg, new_xbridge);
949
950 xlate_xbridge_set(new_xbridge,
34dd0d78 951 xbridge->dpif, xbridge->ml, xbridge->stp,
9efd308e
DV
952 xbridge->rstp, xbridge->ms, xbridge->mbridge,
953 xbridge->sflow, xbridge->ipfix, xbridge->netflow,
b440dd8c
JS
954 xbridge->forward_bpdu, xbridge->has_in_band,
955 &xbridge->support);
84f0f298
RW
956 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
957 xlate_xbundle_copy(new_xbridge, xbundle);
958 }
959
960 /* Copy xports which are not part of a xbundle */
961 HMAP_FOR_EACH (xport, ofp_node, &xbridge->xports) {
962 if (!xport->xbundle) {
963 xlate_xport_copy(new_xbridge, NULL, xport);
964 }
965 }
966}
967
968static void
969xlate_xbundle_copy(struct xbridge *xbridge, struct xbundle *xbundle)
970{
971 struct xport *xport;
972 struct xbundle *new_xbundle = xzalloc(sizeof *xbundle);
973 new_xbundle->ofbundle = xbundle->ofbundle;
974 new_xbundle->xbridge = xbridge;
975 new_xbundle->name = xstrdup(xbundle->name);
976 xlate_xbundle_init(new_xcfg, new_xbundle);
977
fed8962a
EG
978 xlate_xbundle_set(new_xbundle, xbundle->vlan_mode, xbundle->qinq_ethtype,
979 xbundle->vlan, xbundle->trunks, xbundle->cvlans,
84f0f298 980 xbundle->use_priority_tags, xbundle->bond, xbundle->lacp,
c005f976 981 xbundle->floodable, xbundle->protected);
84f0f298
RW
982 LIST_FOR_EACH (xport, bundle_node, &xbundle->xports) {
983 xlate_xport_copy(xbridge, new_xbundle, xport);
984 }
985}
986
987static void
988xlate_xport_copy(struct xbridge *xbridge, struct xbundle *xbundle,
989 struct xport *xport)
990{
991 struct skb_priority_to_dscp *pdscp, *new_pdscp;
992 struct xport *new_xport = xzalloc(sizeof *xport);
993 new_xport->ofport = xport->ofport;
994 new_xport->ofp_port = xport->ofp_port;
995 new_xport->xbridge = xbridge;
996 xlate_xport_init(new_xcfg, new_xport);
997
998 xlate_xport_set(new_xport, xport->odp_port, xport->netdev, xport->cfm,
0477baa9
DF
999 xport->bfd, xport->lldp, xport->stp_port_no,
1000 xport->rstp_port, xport->config, xport->state,
1001 xport->is_tunnel, xport->may_enable);
84f0f298
RW
1002
1003 if (xport->peer) {
1004 struct xport *peer = xport_lookup(new_xcfg, xport->peer->ofport);
1005 if (peer) {
1006 new_xport->peer = peer;
1007 new_xport->peer->peer = new_xport;
1008 }
1009 }
1010
1011 if (xbundle) {
1012 new_xport->xbundle = xbundle;
417e7e66 1013 ovs_list_insert(&new_xport->xbundle->xports, &new_xport->bundle_node);
84f0f298
RW
1014 }
1015
1016 HMAP_FOR_EACH (pdscp, hmap_node, &xport->skb_priorities) {
1017 new_pdscp = xmalloc(sizeof *pdscp);
1018 new_pdscp->skb_priority = pdscp->skb_priority;
1019 new_pdscp->dscp = pdscp->dscp;
1020 hmap_insert(&new_xport->skb_priorities, &new_pdscp->hmap_node,
1021 hash_int(new_pdscp->skb_priority, 0));
1022 }
1023}
1024
1025/* Sets the current xlate configuration to new_xcfg and frees the old xlate
1026 * configuration in xcfgp.
1027 *
1028 * This needs to be called after editing the xlate configuration.
1029 *
1030 * Functions that edit the new xlate configuration are
6cd20a22 1031 * xlate_<ofproto/bundle/ofport>_set and xlate_<ofproto/bundle/ofport>_remove.
84f0f298
RW
1032 *
1033 * A sample workflow:
1034 *
1035 * xlate_txn_start();
1036 * ...
1037 * edit_xlate_configuration();
1038 * ...
1039 * xlate_txn_commit(); */
46c88433 1040void
84f0f298
RW
1041xlate_txn_commit(void)
1042{
1043 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1044
1045 ovsrcu_set(&xcfgp, new_xcfg);
40a9c4c2
AW
1046 ovsrcu_synchronize();
1047 xlate_xcfg_free(xcfg);
84f0f298
RW
1048 new_xcfg = NULL;
1049}
1050
1051/* Copies the current xlate configuration in xcfgp to new_xcfg.
1052 *
1053 * This needs to be called prior to editing the xlate configuration. */
1054void
1055xlate_txn_start(void)
1056{
1057 struct xbridge *xbridge;
1058 struct xlate_cfg *xcfg;
1059
1060 ovs_assert(!new_xcfg);
1061
1062 new_xcfg = xmalloc(sizeof *new_xcfg);
1063 hmap_init(&new_xcfg->xbridges);
1064 hmap_init(&new_xcfg->xbundles);
1065 hmap_init(&new_xcfg->xports);
1066
1067 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1068 if (!xcfg) {
1069 return;
1070 }
1071
1072 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
1073 xlate_xbridge_copy(xbridge);
1074 }
1075}
1076
1077
1078static void
1079xlate_xcfg_free(struct xlate_cfg *xcfg)
1080{
1081 struct xbridge *xbridge, *next_xbridge;
1082
1083 if (!xcfg) {
1084 return;
1085 }
1086
1087 HMAP_FOR_EACH_SAFE (xbridge, next_xbridge, hmap_node, &xcfg->xbridges) {
1088 xlate_xbridge_remove(xcfg, xbridge);
1089 }
1090
1091 hmap_destroy(&xcfg->xbridges);
1092 hmap_destroy(&xcfg->xbundles);
1093 hmap_destroy(&xcfg->xports);
1094 free(xcfg);
1095}
1096
1097void
1098xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
34dd0d78 1099 struct dpif *dpif,
84f0f298 1100 const struct mac_learning *ml, struct stp *stp,
9efd308e 1101 struct rstp *rstp, const struct mcast_snooping *ms,
84f0f298
RW
1102 const struct mbridge *mbridge,
1103 const struct dpif_sflow *sflow,
1104 const struct dpif_ipfix *ipfix,
2f47cdf4 1105 const struct netflow *netflow,
b440dd8c
JS
1106 bool forward_bpdu, bool has_in_band,
1107 const struct dpif_backer_support *support)
84f0f298
RW
1108{
1109 struct xbridge *xbridge;
1110
1111 ovs_assert(new_xcfg);
1112
1113 xbridge = xbridge_lookup(new_xcfg, ofproto);
1114 if (!xbridge) {
1115 xbridge = xzalloc(sizeof *xbridge);
1116 xbridge->ofproto = ofproto;
1117
1118 xlate_xbridge_init(new_xcfg, xbridge);
1119 }
1120
1121 free(xbridge->name);
1122 xbridge->name = xstrdup(name);
1123
34dd0d78 1124 xlate_xbridge_set(xbridge, dpif, ml, stp, rstp, ms, mbridge, sflow, ipfix,
b440dd8c 1125 netflow, forward_bpdu, has_in_band, support);
84f0f298
RW
1126}
1127
1128static void
1129xlate_xbridge_remove(struct xlate_cfg *xcfg, struct xbridge *xbridge)
46c88433 1130{
46c88433
EJ
1131 struct xbundle *xbundle, *next_xbundle;
1132 struct xport *xport, *next_xport;
1133
1134 if (!xbridge) {
1135 return;
1136 }
1137
1138 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
84f0f298 1139 xlate_xport_remove(xcfg, xport);
46c88433
EJ
1140 }
1141
1142 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
84f0f298 1143 xlate_xbundle_remove(xcfg, xbundle);
46c88433
EJ
1144 }
1145
84f0f298 1146 hmap_remove(&xcfg->xbridges, &xbridge->hmap_node);
795cc5c1 1147 mac_learning_unref(xbridge->ml);
6d95c4e8 1148 mcast_snooping_unref(xbridge->ms);
795cc5c1
EJ
1149 mbridge_unref(xbridge->mbridge);
1150 dpif_sflow_unref(xbridge->sflow);
1151 dpif_ipfix_unref(xbridge->ipfix);
3570f7e4 1152 netflow_unref(xbridge->netflow);
795cc5c1 1153 stp_unref(xbridge->stp);
9efd308e 1154 rstp_unref(xbridge->rstp);
795cc5c1 1155 hmap_destroy(&xbridge->xports);
46c88433
EJ
1156 free(xbridge->name);
1157 free(xbridge);
1158}
1159
84f0f298
RW
1160void
1161xlate_remove_ofproto(struct ofproto_dpif *ofproto)
1162{
1163 struct xbridge *xbridge;
1164
1165 ovs_assert(new_xcfg);
1166
1167 xbridge = xbridge_lookup(new_xcfg, ofproto);
1168 xlate_xbridge_remove(new_xcfg, xbridge);
1169}
1170
46c88433
EJ
1171void
1172xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
f0fb825a 1173 const char *name, enum port_vlan_mode vlan_mode,
fed8962a
EG
1174 uint16_t qinq_ethtype, int vlan,
1175 unsigned long *trunks, unsigned long *cvlans,
f0fb825a 1176 bool use_priority_tags,
46c88433 1177 const struct bond *bond, const struct lacp *lacp,
c005f976 1178 bool floodable, bool protected)
46c88433 1179{
84f0f298 1180 struct xbundle *xbundle;
46c88433 1181
84f0f298
RW
1182 ovs_assert(new_xcfg);
1183
1184 xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433
EJ
1185 if (!xbundle) {
1186 xbundle = xzalloc(sizeof *xbundle);
1187 xbundle->ofbundle = ofbundle;
84f0f298 1188 xbundle->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433 1189
84f0f298 1190 xlate_xbundle_init(new_xcfg, xbundle);
46c88433
EJ
1191 }
1192
46c88433
EJ
1193 free(xbundle->name);
1194 xbundle->name = xstrdup(name);
1195
fed8962a 1196 xlate_xbundle_set(xbundle, vlan_mode, qinq_ethtype, vlan, trunks, cvlans,
c005f976 1197 use_priority_tags, bond, lacp, floodable, protected);
46c88433
EJ
1198}
1199
84f0f298
RW
1200static void
1201xlate_xbundle_remove(struct xlate_cfg *xcfg, struct xbundle *xbundle)
46c88433 1202{
5f03c983 1203 struct xport *xport;
46c88433
EJ
1204
1205 if (!xbundle) {
1206 return;
1207 }
1208
5f03c983 1209 LIST_FOR_EACH_POP (xport, bundle_node, &xbundle->xports) {
46c88433
EJ
1210 xport->xbundle = NULL;
1211 }
1212
84f0f298 1213 hmap_remove(&xcfg->xbundles, &xbundle->hmap_node);
417e7e66 1214 ovs_list_remove(&xbundle->list_node);
46c88433
EJ
1215 bond_unref(xbundle->bond);
1216 lacp_unref(xbundle->lacp);
1217 free(xbundle->name);
1218 free(xbundle);
1219}
1220
84f0f298
RW
1221void
1222xlate_bundle_remove(struct ofbundle *ofbundle)
1223{
1224 struct xbundle *xbundle;
1225
1226 ovs_assert(new_xcfg);
1227
1228 xbundle = xbundle_lookup(new_xcfg, ofbundle);
1229 xlate_xbundle_remove(new_xcfg, xbundle);
1230}
1231
46c88433
EJ
1232void
1233xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
1234 struct ofport_dpif *ofport, ofp_port_t ofp_port,
1235 odp_port_t odp_port, const struct netdev *netdev,
1236 const struct cfm *cfm, const struct bfd *bfd,
0477baa9
DF
1237 const struct lldp *lldp, struct ofport_dpif *peer,
1238 int stp_port_no, const struct rstp_port *rstp_port,
55954f6e 1239 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
dd8cd4b4
SH
1240 enum ofputil_port_config config,
1241 enum ofputil_port_state state, bool is_tunnel,
9d189a50 1242 bool may_enable)
46c88433 1243{
55954f6e 1244 size_t i;
84f0f298
RW
1245 struct xport *xport;
1246
1247 ovs_assert(new_xcfg);
46c88433 1248
84f0f298 1249 xport = xport_lookup(new_xcfg, ofport);
46c88433
EJ
1250 if (!xport) {
1251 xport = xzalloc(sizeof *xport);
1252 xport->ofport = ofport;
84f0f298 1253 xport->xbridge = xbridge_lookup(new_xcfg, ofproto);
46c88433
EJ
1254 xport->ofp_port = ofp_port;
1255
84f0f298 1256 xlate_xport_init(new_xcfg, xport);
46c88433
EJ
1257 }
1258
1259 ovs_assert(xport->ofp_port == ofp_port);
1260
0477baa9
DF
1261 xlate_xport_set(xport, odp_port, netdev, cfm, bfd, lldp,
1262 stp_port_no, rstp_port, config, state, is_tunnel,
1263 may_enable);
46c88433
EJ
1264
1265 if (xport->peer) {
1266 xport->peer->peer = NULL;
1267 }
84f0f298 1268 xport->peer = xport_lookup(new_xcfg, peer);
46c88433
EJ
1269 if (xport->peer) {
1270 xport->peer->peer = xport;
1271 }
1272
1273 if (xport->xbundle) {
417e7e66 1274 ovs_list_remove(&xport->bundle_node);
46c88433 1275 }
84f0f298 1276 xport->xbundle = xbundle_lookup(new_xcfg, ofbundle);
46c88433 1277 if (xport->xbundle) {
417e7e66 1278 ovs_list_insert(&xport->xbundle->xports, &xport->bundle_node);
46c88433 1279 }
55954f6e
EJ
1280
1281 clear_skb_priorities(xport);
1282 for (i = 0; i < n_qdscp; i++) {
1283 struct skb_priority_to_dscp *pdscp;
1284 uint32_t skb_priority;
1285
89a8a7f0
EJ
1286 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
1287 &skb_priority)) {
55954f6e
EJ
1288 continue;
1289 }
1290
1291 pdscp = xmalloc(sizeof *pdscp);
1292 pdscp->skb_priority = skb_priority;
1293 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
1294 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
1295 hash_int(pdscp->skb_priority, 0));
1296 }
46c88433
EJ
1297}
1298
84f0f298
RW
1299static void
1300xlate_xport_remove(struct xlate_cfg *xcfg, struct xport *xport)
46c88433 1301{
46c88433
EJ
1302 if (!xport) {
1303 return;
1304 }
1305
1306 if (xport->peer) {
1307 xport->peer->peer = NULL;
1308 xport->peer = NULL;
1309 }
1310
e621a12d 1311 if (xport->xbundle) {
417e7e66 1312 ovs_list_remove(&xport->bundle_node);
e621a12d
EJ
1313 }
1314
55954f6e
EJ
1315 clear_skb_priorities(xport);
1316 hmap_destroy(&xport->skb_priorities);
1317
84f0f298 1318 hmap_remove(&xcfg->xports, &xport->hmap_node);
46c88433
EJ
1319 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
1320
1321 netdev_close(xport->netdev);
f025bcb7 1322 rstp_port_unref(xport->rstp_port);
46c88433
EJ
1323 cfm_unref(xport->cfm);
1324 bfd_unref(xport->bfd);
0477baa9 1325 lldp_unref(xport->lldp);
46c88433
EJ
1326 free(xport);
1327}
1328
84f0f298
RW
1329void
1330xlate_ofport_remove(struct ofport_dpif *ofport)
1331{
1332 struct xport *xport;
1333
1334 ovs_assert(new_xcfg);
1335
1336 xport = xport_lookup(new_xcfg, ofport);
1337 xlate_xport_remove(new_xcfg, xport);
1338}
1339
ef377a58
JR
1340static struct ofproto_dpif *
1341xlate_lookup_ofproto_(const struct dpif_backer *backer, const struct flow *flow,
1342 ofp_port_t *ofp_in_port, const struct xport **xportp)
1343{
e672ff9b 1344 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
ef377a58 1345 const struct xport *xport;
f9038ef6 1346
e672ff9b
JR
1347 xport = xport_lookup(xcfg, tnl_port_should_receive(flow)
1348 ? tnl_port_receive(flow)
1349 : odp_port_to_ofport(backer, flow->in_port.odp_port));
1350 if (OVS_UNLIKELY(!xport)) {
1351 return NULL;
ef377a58 1352 }
e672ff9b 1353 *xportp = xport;
f9038ef6 1354 if (ofp_in_port) {
e672ff9b 1355 *ofp_in_port = xport->ofp_port;
f9038ef6 1356 }
e672ff9b 1357 return xport->xbridge->ofproto;
ef377a58
JR
1358}
1359
1360/* Given a datapath and flow metadata ('backer', and 'flow' respectively)
1361 * returns the corresponding struct ofproto_dpif and OpenFlow port number. */
1362struct ofproto_dpif *
1363xlate_lookup_ofproto(const struct dpif_backer *backer, const struct flow *flow,
1364 ofp_port_t *ofp_in_port)
1365{
1366 const struct xport *xport;
1367
1368 return xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
1369}
1370
cc377352 1371/* Given a datapath and flow metadata ('backer', and 'flow' respectively),
ef377a58 1372 * optionally populates 'ofproto' with the ofproto_dpif, 'ofp_in_port' with the
cc377352 1373 * openflow in_port, and 'ipfix', 'sflow', and 'netflow' with the appropriate
dcc2c6cd
JR
1374 * handles for those protocols if they're enabled. Caller may use the returned
1375 * pointers until quiescing, for longer term use additional references must
1376 * be taken.
8449c4d6 1377 *
f9038ef6 1378 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofproto.
ef377a58 1379 */
8449c4d6 1380int
5c476ea3
JR
1381xlate_lookup(const struct dpif_backer *backer, const struct flow *flow,
1382 struct ofproto_dpif **ofprotop, struct dpif_ipfix **ipfix,
1383 struct dpif_sflow **sflow, struct netflow **netflow,
1384 ofp_port_t *ofp_in_port)
8449c4d6 1385{
ef377a58 1386 struct ofproto_dpif *ofproto;
84f0f298 1387 const struct xport *xport;
8449c4d6 1388
ef377a58 1389 ofproto = xlate_lookup_ofproto_(backer, flow, ofp_in_port, &xport);
8449c4d6 1390
f9038ef6 1391 if (!ofproto) {
cc377352 1392 return ENODEV;
8449c4d6 1393 }
8449c4d6 1394
ef377a58
JR
1395 if (ofprotop) {
1396 *ofprotop = ofproto;
8449c4d6
EJ
1397 }
1398
1dfdb9b3 1399 if (ipfix) {
f9038ef6 1400 *ipfix = xport ? xport->xbridge->ipfix : NULL;
1dfdb9b3
EJ
1401 }
1402
1403 if (sflow) {
f9038ef6 1404 *sflow = xport ? xport->xbridge->sflow : NULL;
1dfdb9b3
EJ
1405 }
1406
1407 if (netflow) {
f9038ef6 1408 *netflow = xport ? xport->xbridge->netflow : NULL;
1dfdb9b3 1409 }
f9038ef6 1410
cc377352 1411 return 0;
8449c4d6
EJ
1412}
1413
46c88433 1414static struct xbridge *
84f0f298 1415xbridge_lookup(struct xlate_cfg *xcfg, const struct ofproto_dpif *ofproto)
46c88433 1416{
84f0f298 1417 struct hmap *xbridges;
46c88433
EJ
1418 struct xbridge *xbridge;
1419
84f0f298 1420 if (!ofproto || !xcfg) {
5e6af486
EJ
1421 return NULL;
1422 }
1423
84f0f298
RW
1424 xbridges = &xcfg->xbridges;
1425
46c88433 1426 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
84f0f298 1427 xbridges) {
46c88433
EJ
1428 if (xbridge->ofproto == ofproto) {
1429 return xbridge;
1430 }
1431 }
1432 return NULL;
1433}
1434
290835f9
BP
1435static struct xbridge *
1436xbridge_lookup_by_uuid(struct xlate_cfg *xcfg, const struct uuid *uuid)
1437{
1438 struct xbridge *xbridge;
1439
1440 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
07a3cd5c 1441 if (uuid_equals(&xbridge->ofproto->uuid, uuid)) {
290835f9
BP
1442 return xbridge;
1443 }
1444 }
1445 return NULL;
1446}
1447
46c88433 1448static struct xbundle *
84f0f298 1449xbundle_lookup(struct xlate_cfg *xcfg, const struct ofbundle *ofbundle)
46c88433 1450{
84f0f298 1451 struct hmap *xbundles;
46c88433
EJ
1452 struct xbundle *xbundle;
1453
84f0f298 1454 if (!ofbundle || !xcfg) {
5e6af486
EJ
1455 return NULL;
1456 }
1457
84f0f298
RW
1458 xbundles = &xcfg->xbundles;
1459
46c88433 1460 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
84f0f298 1461 xbundles) {
46c88433
EJ
1462 if (xbundle->ofbundle == ofbundle) {
1463 return xbundle;
1464 }
1465 }
1466 return NULL;
1467}
1468
1469static struct xport *
84f0f298 1470xport_lookup(struct xlate_cfg *xcfg, const struct ofport_dpif *ofport)
46c88433 1471{
84f0f298 1472 struct hmap *xports;
46c88433
EJ
1473 struct xport *xport;
1474
84f0f298 1475 if (!ofport || !xcfg) {
5e6af486
EJ
1476 return NULL;
1477 }
1478
84f0f298
RW
1479 xports = &xcfg->xports;
1480
46c88433 1481 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
84f0f298 1482 xports) {
46c88433
EJ
1483 if (xport->ofport == ofport) {
1484 return xport;
1485 }
1486 }
1487 return NULL;
1488}
1489
40085e56
EJ
1490static struct stp_port *
1491xport_get_stp_port(const struct xport *xport)
1492{
92cf817b 1493 return xport->xbridge->stp && xport->stp_port_no != -1
40085e56
EJ
1494 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
1495 : NULL;
1496}
9d189a50 1497
0d1cee12 1498static bool
9d189a50
EJ
1499xport_stp_learn_state(const struct xport *xport)
1500{
40085e56 1501 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1502 return sp
1503 ? stp_learn_in_state(stp_port_get_state(sp))
1504 : true;
9d189a50
EJ
1505}
1506
1507static bool
1508xport_stp_forward_state(const struct xport *xport)
1509{
40085e56 1510 struct stp_port *sp = xport_get_stp_port(xport);
4b5f1996
DV
1511 return sp
1512 ? stp_forward_in_state(stp_port_get_state(sp))
1513 : true;
9d189a50
EJ
1514}
1515
0d1cee12 1516static bool
bacdb85a 1517xport_stp_should_forward_bpdu(const struct xport *xport)
0d1cee12
K
1518{
1519 struct stp_port *sp = xport_get_stp_port(xport);
bacdb85a 1520 return stp_should_forward_bpdu(sp ? stp_port_get_state(sp) : STP_DISABLED);
0d1cee12
K
1521}
1522
9d189a50
EJ
1523/* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1524 * were used to make the determination.*/
1525static bool
1526stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1527{
bbbca389 1528 /* is_stp() also checks dl_type, but dl_type is always set in 'wc'. */
9d189a50 1529 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
bbbca389 1530 return is_stp(flow);
9d189a50
EJ
1531}
1532
1533static void
cf62fa4c 1534stp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9d189a50 1535{
40085e56 1536 struct stp_port *sp = xport_get_stp_port(xport);
cf62fa4c
PS
1537 struct dp_packet payload = *packet;
1538 struct eth_header *eth = dp_packet_data(&payload);
9d189a50
EJ
1539
1540 /* Sink packets on ports that have STP disabled when the bridge has
1541 * STP enabled. */
1542 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1543 return;
1544 }
1545
1546 /* Trim off padding on payload. */
cf62fa4c
PS
1547 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1548 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9d189a50
EJ
1549 }
1550
cf62fa4c
PS
1551 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1552 stp_received_bpdu(sp, dp_packet_data(&payload), dp_packet_size(&payload));
9d189a50
EJ
1553 }
1554}
1555
f025bcb7
JR
1556static enum rstp_state
1557xport_get_rstp_port_state(const struct xport *xport)
9efd308e 1558{
f025bcb7
JR
1559 return xport->rstp_port
1560 ? rstp_port_get_state(xport->rstp_port)
1561 : RSTP_DISABLED;
9efd308e
DV
1562}
1563
1564static bool
1565xport_rstp_learn_state(const struct xport *xport)
1566{
4b5f1996
DV
1567 return xport->xbridge->rstp && xport->rstp_port
1568 ? rstp_learn_in_state(xport_get_rstp_port_state(xport))
1569 : true;
9efd308e
DV
1570}
1571
1572static bool
1573xport_rstp_forward_state(const struct xport *xport)
1574{
4b5f1996
DV
1575 return xport->xbridge->rstp && xport->rstp_port
1576 ? rstp_forward_in_state(xport_get_rstp_port_state(xport))
1577 : true;
9efd308e
DV
1578}
1579
1580static bool
1581xport_rstp_should_manage_bpdu(const struct xport *xport)
1582{
f025bcb7 1583 return rstp_should_manage_bpdu(xport_get_rstp_port_state(xport));
9efd308e
DV
1584}
1585
1586static void
cf62fa4c 1587rstp_process_packet(const struct xport *xport, const struct dp_packet *packet)
9efd308e 1588{
cf62fa4c
PS
1589 struct dp_packet payload = *packet;
1590 struct eth_header *eth = dp_packet_data(&payload);
9efd308e 1591
f025bcb7
JR
1592 /* Sink packets on ports that have no RSTP. */
1593 if (!xport->rstp_port) {
9efd308e
DV
1594 return;
1595 }
1596
1597 /* Trim off padding on payload. */
cf62fa4c
PS
1598 if (dp_packet_size(&payload) > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1599 dp_packet_set_size(&payload, ntohs(eth->eth_type) + ETH_HEADER_LEN);
9efd308e
DV
1600 }
1601
cf62fa4c
PS
1602 if (dp_packet_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1603 rstp_port_received_bpdu(xport->rstp_port, dp_packet_data(&payload),
1604 dp_packet_size(&payload));
9efd308e
DV
1605 }
1606}
1607
46c88433
EJ
1608static struct xport *
1609get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1610{
1611 struct xport *xport;
1612
1613 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
1614 &xbridge->xports) {
1615 if (xport->ofp_port == ofp_port) {
1616 return xport;
1617 }
1618 }
1619 return NULL;
1620}
1621
1622static odp_port_t
1623ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
1624{
1625 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
1626 return xport ? xport->odp_port : ODPP_NONE;
1627}
1628
dd8cd4b4
SH
1629static bool
1630odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
1631{
086fa873
BP
1632 struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1633 return xport && xport->may_enable;
dd8cd4b4
SH
1634}
1635
1e684d7d 1636static struct ofputil_bucket *
dd8cd4b4
SH
1637group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
1638 int depth);
1639
1640static bool
1641group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
1642{
1643 struct group_dpif *group;
dd8cd4b4 1644
5d08a275 1645 group = group_dpif_lookup(ctx->xbridge->ofproto, group_id,
1f4a8933 1646 ctx->xin->tables_version, false);
db88b35c 1647 if (group) {
76973237 1648 return group_first_live_bucket(ctx, group, depth) != NULL;
dc25893e 1649 }
dd8cd4b4 1650
dc25893e 1651 return false;
dd8cd4b4
SH
1652}
1653
1654#define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
1655
1656static bool
1657bucket_is_alive(const struct xlate_ctx *ctx,
1e684d7d 1658 struct ofputil_bucket *bucket, int depth)
dd8cd4b4
SH
1659{
1660 if (depth >= MAX_LIVENESS_RECURSION) {
2d9b49dd
BP
1661 xlate_report_error(ctx, "bucket chaining exceeded %d links",
1662 MAX_LIVENESS_RECURSION);
dd8cd4b4
SH
1663 return false;
1664 }
1665
fdb1999b
AZ
1666 return (!ofputil_bucket_has_liveness(bucket)
1667 || (bucket->watch_port != OFPP_ANY
1668 && odp_port_is_alive(ctx, bucket->watch_port))
1669 || (bucket->watch_group != OFPG_ANY
1670 && group_is_alive(ctx, bucket->watch_group, depth + 1)));
dd8cd4b4
SH
1671}
1672
1e684d7d 1673static struct ofputil_bucket *
dd8cd4b4
SH
1674group_first_live_bucket(const struct xlate_ctx *ctx,
1675 const struct group_dpif *group, int depth)
1676{
1677 struct ofputil_bucket *bucket;
07a3cd5c 1678 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
dd8cd4b4
SH
1679 if (bucket_is_alive(ctx, bucket, depth)) {
1680 return bucket;
1681 }
1682 }
1683
1684 return NULL;
1685}
1686
1e684d7d 1687static struct ofputil_bucket *
fe7e5749
SH
1688group_best_live_bucket(const struct xlate_ctx *ctx,
1689 const struct group_dpif *group,
1690 uint32_t basis)
1691{
1e684d7d 1692 struct ofputil_bucket *best_bucket = NULL;
fe7e5749 1693 uint32_t best_score = 0;
fe7e5749 1694
1e684d7d 1695 struct ofputil_bucket *bucket;
07a3cd5c 1696 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
fe7e5749 1697 if (bucket_is_alive(ctx, bucket, 0)) {
c09cb861
LS
1698 uint32_t score =
1699 (hash_int(bucket->bucket_id, basis) & 0xffff) * bucket->weight;
fe7e5749
SH
1700 if (score >= best_score) {
1701 best_bucket = bucket;
1702 best_score = score;
1703 }
1704 }
fe7e5749
SH
1705 }
1706
1707 return best_bucket;
1708}
1709
9583bc14 1710static bool
46c88433 1711xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
9583bc14
EJ
1712{
1713 return (bundle->vlan_mode != PORT_VLAN_ACCESS
1714 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
1715}
1716
fed8962a
EG
1717static bool
1718xbundle_allows_cvlan(const struct xbundle *bundle, uint16_t vlan)
1719{
1720 return (!bundle->cvlans || bitmap_is_set(bundle->cvlans, vlan));
1721}
1722
9583bc14 1723static bool
f0fb825a 1724xbundle_includes_vlan(const struct xbundle *xbundle, const struct xvlan *xvlan)
46c88433 1725{
f0fb825a
EG
1726 switch (xbundle->vlan_mode) {
1727 case PORT_VLAN_ACCESS:
1728 return xvlan->v[0].vid == xbundle->vlan && xvlan->v[1].vid == 0;
1729
1730 case PORT_VLAN_TRUNK:
1731 case PORT_VLAN_NATIVE_UNTAGGED:
1732 case PORT_VLAN_NATIVE_TAGGED:
1733 return xbundle_trunks_vlan(xbundle, xvlan->v[0].vid);
1734
fed8962a
EG
1735 case PORT_VLAN_DOT1Q_TUNNEL:
1736 return xvlan->v[0].vid == xbundle->vlan &&
1737 xbundle_allows_cvlan(xbundle, xvlan->v[1].vid);
1738
f0fb825a
EG
1739 default:
1740 OVS_NOT_REACHED();
1741 }
46c88433
EJ
1742}
1743
1744static mirror_mask_t
1745xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
1746{
1747 return xbundle != &ofpp_none_bundle
1748 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
1749 : 0;
1750}
1751
1752static mirror_mask_t
1753xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1754{
46c88433
EJ
1755 return xbundle != &ofpp_none_bundle
1756 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
1757 : 0;
9583bc14
EJ
1758}
1759
46c88433
EJ
1760static mirror_mask_t
1761xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
9583bc14 1762{
46c88433
EJ
1763 return xbundle != &ofpp_none_bundle
1764 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
1765 : 0;
1766}
1767
1768static struct xbundle *
2d9b49dd
BP
1769lookup_input_bundle__(const struct xbridge *xbridge,
1770 ofp_port_t in_port, struct xport **in_xportp)
46c88433
EJ
1771{
1772 struct xport *xport;
9583bc14
EJ
1773
1774 /* Find the port and bundle for the received packet. */
46c88433
EJ
1775 xport = get_ofp_port(xbridge, in_port);
1776 if (in_xportp) {
1777 *in_xportp = xport;
9583bc14 1778 }
46c88433
EJ
1779 if (xport && xport->xbundle) {
1780 return xport->xbundle;
9583bc14
EJ
1781 }
1782
6362203b
YT
1783 /* Special-case OFPP_NONE (OF1.0) and OFPP_CONTROLLER (OF1.1+),
1784 * which a controller may use as the ingress port for traffic that
1785 * it is sourcing. */
1786 if (in_port == OFPP_CONTROLLER || in_port == OFPP_NONE) {
9583bc14
EJ
1787 return &ofpp_none_bundle;
1788 }
2d9b49dd
BP
1789 return NULL;
1790}
9583bc14 1791
2d9b49dd
BP
1792static struct xbundle *
1793lookup_input_bundle(const struct xlate_ctx *ctx,
1794 ofp_port_t in_port, struct xport **in_xportp)
1795{
1796 struct xbundle *xbundle = lookup_input_bundle__(ctx->xbridge,
1797 in_port, in_xportp);
1798 if (!xbundle) {
1799 /* Odd. A few possible reasons here:
1800 *
1801 * - We deleted a port but there are still a few packets queued up
1802 * from it.
1803 *
1804 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
1805 * we don't know about.
1806 *
1807 * - The ofproto client didn't configure the port as part of a bundle.
1808 * This is particularly likely to happen if a packet was received on
1809 * the port after it was created, but before the client had a chance
1810 * to configure its bundle.
1811 */
94783c7c 1812 xlate_report_error(ctx, "received packet on unknown port %"PRIu32,
2d9b49dd 1813 in_port);
9583bc14 1814 }
2d9b49dd 1815 return xbundle;
9583bc14
EJ
1816}
1817
faa624b4
BP
1818/* Mirrors the packet represented by 'ctx' to appropriate mirror destinations,
1819 * given the packet is ingressing or egressing on 'xbundle', which has ingress
1820 * or egress (as appropriate) mirrors 'mirrors'. */
9583bc14 1821static void
7efbc3b7
BP
1822mirror_packet(struct xlate_ctx *ctx, struct xbundle *xbundle,
1823 mirror_mask_t mirrors)
9583bc14 1824{
f0fb825a
EG
1825 struct xvlan in_xvlan;
1826 struct xvlan xvlan;
1827
faa624b4
BP
1828 /* Figure out what VLAN the packet is in (because mirrors can select
1829 * packets on basis of VLAN). */
f0fb825a
EG
1830 xvlan_extract(&ctx->xin->flow, &in_xvlan);
1831 if (!input_vid_is_valid(ctx, in_xvlan.v[0].vid, xbundle)) {
9583bc14
EJ
1832 return;
1833 }
f0fb825a 1834 xvlan_input_translate(xbundle, &in_xvlan, &xvlan);
9583bc14 1835
7efbc3b7 1836 const struct xbridge *xbridge = ctx->xbridge;
9583bc14 1837
7efbc3b7
BP
1838 /* Don't mirror to destinations that we've already mirrored to. */
1839 mirrors &= ~ctx->mirrors;
9583bc14
EJ
1840 if (!mirrors) {
1841 return;
1842 }
1843
7efbc3b7
BP
1844 if (ctx->xin->resubmit_stats) {
1845 mirror_update_stats(xbridge->mbridge, mirrors,
1846 ctx->xin->resubmit_stats->n_packets,
1847 ctx->xin->resubmit_stats->n_bytes);
1848 }
1849 if (ctx->xin->xcache) {
1850 struct xc_entry *entry;
1851
1852 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_MIRROR);
901a517e
JR
1853 entry->mirror.mbridge = mbridge_ref(xbridge->mbridge);
1854 entry->mirror.mirrors = mirrors;
7efbc3b7 1855 }
9583bc14 1856
faa624b4
BP
1857 /* 'mirrors' is a bit-mask of candidates for mirroring. Iterate as long as
1858 * some candidates remain. */
9583bc14 1859 while (mirrors) {
7efbc3b7 1860 const unsigned long *vlans;
ec7ceaed
EJ
1861 mirror_mask_t dup_mirrors;
1862 struct ofbundle *out;
ec7ceaed 1863 int out_vlan;
1356dbd1 1864 int snaplen;
ec7ceaed 1865
faa624b4 1866 /* Get the details of the mirror represented by the rightmost 1-bit. */
7efbc3b7 1867 bool has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
1356dbd1
WT
1868 &vlans, &dup_mirrors,
1869 &out, &snaplen, &out_vlan);
ec7ceaed
EJ
1870 ovs_assert(has_mirror);
1871
1356dbd1 1872
faa624b4
BP
1873 /* If this mirror selects on the basis of VLAN, and it does not select
1874 * 'vlan', then discard this mirror and go on to the next one. */
ec7ceaed 1875 if (vlans) {
f0fb825a 1876 ctx->wc->masks.vlans[0].tci |= htons(VLAN_CFI | VLAN_VID_MASK);
9583bc14 1877 }
f0fb825a 1878 if (vlans && !bitmap_is_set(vlans, xvlan.v[0].vid)) {
9583bc14
EJ
1879 mirrors = zero_rightmost_1bit(mirrors);
1880 continue;
1881 }
1882
faa624b4
BP
1883 /* Record the mirror, and the mirrors that output to the same
1884 * destination, so that we don't mirror to them again. This must be
1885 * done now to ensure that output_normal(), below, doesn't recursively
1886 * output to the same mirrors. */
3d6151f3 1887 ctx->mirrors |= dup_mirrors;
1356dbd1 1888 ctx->mirror_snaplen = snaplen;
faa624b4
BP
1889
1890 /* Send the packet to the mirror. */
ec7ceaed 1891 if (out) {
84f0f298
RW
1892 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
1893 struct xbundle *out_xbundle = xbundle_lookup(xcfg, out);
46c88433 1894 if (out_xbundle) {
f0fb825a 1895 output_normal(ctx, out_xbundle, &xvlan);
46c88433 1896 }
f0fb825a 1897 } else if (xvlan.v[0].vid != out_vlan
7efbc3b7 1898 && !eth_addr_is_reserved(ctx->xin->flow.dl_dst)) {
46c88433 1899 struct xbundle *xbundle;
f0fb825a 1900 uint16_t old_vid = xvlan.v[0].vid;
9583bc14 1901
f0fb825a 1902 xvlan.v[0].vid = out_vlan;
46c88433 1903 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
f0fb825a 1904 if (xbundle_includes_vlan(xbundle, &xvlan)
46c88433 1905 && !xbundle_mirror_out(xbridge, xbundle)) {
f0fb825a 1906 output_normal(ctx, xbundle, &xvlan);
9583bc14
EJ
1907 }
1908 }
f0fb825a 1909 xvlan.v[0].vid = old_vid;
9583bc14 1910 }
faa624b4
BP
1911
1912 /* output_normal() could have recursively output (to different
1913 * mirrors), so make sure that we don't send duplicates. */
1914 mirrors &= ~ctx->mirrors;
1356dbd1 1915 ctx->mirror_snaplen = 0;
9583bc14
EJ
1916 }
1917}
1918
7efbc3b7
BP
1919static void
1920mirror_ingress_packet(struct xlate_ctx *ctx)
1921{
1922 if (mbridge_has_mirrors(ctx->xbridge->mbridge)) {
7efbc3b7 1923 struct xbundle *xbundle = lookup_input_bundle(
2d9b49dd 1924 ctx, ctx->xin->flow.in_port.ofp_port, NULL);
7efbc3b7
BP
1925 if (xbundle) {
1926 mirror_packet(ctx, xbundle,
1927 xbundle_mirror_src(ctx->xbridge, xbundle));
1928 }
1929 }
1930}
1931
46c88433 1932/* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
2d9b49dd 1933 * If so, returns true. Otherwise, returns false.
9583bc14
EJ
1934 *
1935 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1936 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1937 * 0...4095. */
1938static bool
2d9b49dd
BP
1939input_vid_is_valid(const struct xlate_ctx *ctx,
1940 uint16_t vid, struct xbundle *in_xbundle)
9583bc14
EJ
1941{
1942 /* Allow any VID on the OFPP_NONE port. */
46c88433 1943 if (in_xbundle == &ofpp_none_bundle) {
9583bc14
EJ
1944 return true;
1945 }
1946
46c88433 1947 switch (in_xbundle->vlan_mode) {
9583bc14
EJ
1948 case PORT_VLAN_ACCESS:
1949 if (vid) {
2d9b49dd
BP
1950 xlate_report_error(ctx, "dropping VLAN %"PRIu16" tagged "
1951 "packet received on port %s configured as VLAN "
fd13c6b5 1952 "%d access port", vid, in_xbundle->name,
2d9b49dd 1953 in_xbundle->vlan);
9583bc14
EJ
1954 return false;
1955 }
1956 return true;
1957
1958 case PORT_VLAN_NATIVE_UNTAGGED:
1959 case PORT_VLAN_NATIVE_TAGGED:
1960 if (!vid) {
1961 /* Port must always carry its native VLAN. */
1962 return true;
1963 }
1964 /* Fall through. */
1965 case PORT_VLAN_TRUNK:
f0fb825a 1966 if (!xbundle_trunks_vlan(in_xbundle, vid)) {
2d9b49dd
BP
1967 xlate_report_error(ctx, "dropping VLAN %"PRIu16" packet "
1968 "received on port %s not configured for "
1969 "trunking VLAN %"PRIu16,
1970 vid, in_xbundle->name, vid);
9583bc14
EJ
1971 return false;
1972 }
1973 return true;
1974
fed8962a
EG
1975 case PORT_VLAN_DOT1Q_TUNNEL:
1976 if (!xbundle_allows_cvlan(in_xbundle, vid)) {
1977 xlate_report_error(ctx, "dropping VLAN %"PRIu16" packet received "
1978 "on dot1q-tunnel port %s that excludes this "
1979 "VLAN", vid, in_xbundle->name);
1980 return false;
1981 }
1982 return true;
1983
9583bc14 1984 default:
428b2edd 1985 OVS_NOT_REACHED();
9583bc14
EJ
1986 }
1987
1988}
1989
f0fb825a
EG
1990static void
1991xvlan_copy(struct xvlan *dst, const struct xvlan *src)
1992{
1993 *dst = *src;
1994}
1995
1996static void
1997xvlan_pop(struct xvlan *src)
1998{
1999 memmove(&src->v[0], &src->v[1], sizeof(src->v) - sizeof(src->v[0]));
2000 memset(&src->v[FLOW_MAX_VLAN_HEADERS - 1], 0,
2001 sizeof(src->v[FLOW_MAX_VLAN_HEADERS - 1]));
2002}
2003
fed8962a
EG
2004static void
2005xvlan_push_uninit(struct xvlan *src)
2006{
2007 memmove(&src->v[1], &src->v[0], sizeof(src->v) - sizeof(src->v[0]));
2008 memset(&src->v[0], 0, sizeof(src->v[0]));
2009}
2010
f0fb825a
EG
2011/* Extract VLAN information (headers) from flow */
2012static void
2013xvlan_extract(const struct flow *flow, struct xvlan *xvlan)
2014{
2015 int i;
2016 memset(xvlan, 0, sizeof(*xvlan));
2017 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2018 if (!eth_type_vlan(flow->vlans[i].tpid) ||
2019 !(flow->vlans[i].tci & htons(VLAN_CFI))) {
2020 break;
2021 }
2022 xvlan->v[i].tpid = ntohs(flow->vlans[i].tpid);
2023 xvlan->v[i].vid = vlan_tci_to_vid(flow->vlans[i].tci);
2024 xvlan->v[i].pcp = ntohs(flow->vlans[i].tci) & VLAN_PCP_MASK;
2025 }
2026}
2027
2028/* Put VLAN information (headers) to flow */
2029static void
2030xvlan_put(struct flow *flow, const struct xvlan *xvlan)
2031{
2032 ovs_be16 tci;
2033 int i;
2034 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2035 tci = htons(xvlan->v[i].vid | (xvlan->v[i].pcp & VLAN_PCP_MASK));
2036 if (tci) {
2037 tci |= htons(VLAN_CFI);
2038 flow->vlans[i].tpid = xvlan->v[i].tpid ?
2039 htons(xvlan->v[i].tpid) :
2040 htons(ETH_TYPE_VLAN_8021Q);
2041 }
2042 flow->vlans[i].tci = tci;
2043 }
2044}
2045
2046/* Given 'in_xvlan', extracted from the input 802.1Q headers received as part
2047 * of a packet, and 'in_xbundle', the bundle on which the packet was received,
2048 * returns the VLANs of the packet during bridge internal processing. */
2049static void
2050xvlan_input_translate(const struct xbundle *in_xbundle,
2051 const struct xvlan *in_xvlan, struct xvlan *xvlan)
2052{
2053
2054 switch (in_xbundle->vlan_mode) {
2055 case PORT_VLAN_ACCESS:
2056 memset(xvlan, 0, sizeof(*xvlan));
2057 xvlan->v[0].tpid = in_xvlan->v[0].tpid ? in_xvlan->v[0].tpid :
2058 ETH_TYPE_VLAN_8021Q;
2059 xvlan->v[0].vid = in_xbundle->vlan;
2060 xvlan->v[0].pcp = in_xvlan->v[0].pcp;
2061 break;
2062
2063 case PORT_VLAN_TRUNK:
2064 xvlan_copy(xvlan, in_xvlan);
2065 break;
2066
2067 case PORT_VLAN_NATIVE_UNTAGGED:
2068 case PORT_VLAN_NATIVE_TAGGED:
2069 xvlan_copy(xvlan, in_xvlan);
2070 if (!in_xvlan->v[0].vid) {
2071 xvlan->v[0].tpid = in_xvlan->v[0].tpid ? in_xvlan->v[0].tpid :
2072 ETH_TYPE_VLAN_8021Q;
2073 xvlan->v[0].vid = in_xbundle->vlan;
2074 xvlan->v[0].pcp = in_xvlan->v[0].pcp;
2075 }
2076 break;
2077
fed8962a
EG
2078 case PORT_VLAN_DOT1Q_TUNNEL:
2079 xvlan_copy(xvlan, in_xvlan);
2080 xvlan_push_uninit(xvlan);
2081 xvlan->v[0].tpid = in_xbundle->qinq_ethtype;
2082 xvlan->v[0].vid = in_xbundle->vlan;
2083 xvlan->v[0].pcp = 0;
2084 break;
2085
f0fb825a
EG
2086 default:
2087 OVS_NOT_REACHED();
2088 }
2089}
2090
2091/* Given 'xvlan', the VLANs of a packet during internal processing, and
2092 * 'out_xbundle', a bundle on which the packet is to be output, returns the
2093 * VLANs that should be included in output packet. */
2094static void
2095xvlan_output_translate(const struct xbundle *out_xbundle,
2096 const struct xvlan *xvlan, struct xvlan *out_xvlan)
9583bc14 2097{
46c88433 2098 switch (out_xbundle->vlan_mode) {
9583bc14 2099 case PORT_VLAN_ACCESS:
f0fb825a
EG
2100 memset(out_xvlan, 0, sizeof(*out_xvlan));
2101 break;
9583bc14
EJ
2102
2103 case PORT_VLAN_TRUNK:
2104 case PORT_VLAN_NATIVE_TAGGED:
f0fb825a
EG
2105 xvlan_copy(out_xvlan, xvlan);
2106 break;
9583bc14
EJ
2107
2108 case PORT_VLAN_NATIVE_UNTAGGED:
f0fb825a
EG
2109 xvlan_copy(out_xvlan, xvlan);
2110 if (xvlan->v[0].vid == out_xbundle->vlan) {
2111 xvlan_pop(out_xvlan);
2112 }
2113 break;
9583bc14 2114
fed8962a
EG
2115 case PORT_VLAN_DOT1Q_TUNNEL:
2116 xvlan_copy(out_xvlan, xvlan);
2117 xvlan_pop(out_xvlan);
2118 break;
2119
9583bc14 2120 default:
428b2edd 2121 OVS_NOT_REACHED();
9583bc14
EJ
2122 }
2123}
2124
fed8962a
EG
2125/* If output xbundle is dot1q-tunnel, set mask bits of cvlan */
2126static void
2127check_and_set_cvlan_mask(struct flow_wildcards *wc,
2128 const struct xbundle *xbundle)
2129{
2130 if (xbundle->vlan_mode == PORT_VLAN_DOT1Q_TUNNEL && xbundle->cvlans) {
2131 wc->masks.vlans[1].tci = htons(0xffff);
2132 }
2133}
2134
9583bc14 2135static void
46c88433 2136output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
f0fb825a 2137 const struct xvlan *xvlan)
9583bc14 2138{
9583bc14 2139 uint16_t vid;
f0fb825a 2140 union flow_vlan_hdr old_vlans[FLOW_MAX_VLAN_HEADERS];
46c88433 2141 struct xport *xport;
e93ef1c7
JR
2142 struct xlate_bond_recirc xr;
2143 bool use_recirc = false;
f0fb825a 2144 struct xvlan out_xvlan;
9583bc14 2145
fed8962a
EG
2146 check_and_set_cvlan_mask(ctx->wc, out_xbundle);
2147
f0fb825a
EG
2148 xvlan_output_translate(out_xbundle, xvlan, &out_xvlan);
2149 if (out_xbundle->use_priority_tags) {
2150 out_xvlan.v[0].pcp = ntohs(ctx->xin->flow.vlans[0].tci) &
2151 VLAN_PCP_MASK;
2152 }
2153 vid = out_xvlan.v[0].vid;
417e7e66 2154 if (ovs_list_is_empty(&out_xbundle->xports)) {
46c88433
EJ
2155 /* Partially configured bundle with no slaves. Drop the packet. */
2156 return;
2157 } else if (!out_xbundle->bond) {
417e7e66 2158 xport = CONTAINER_OF(ovs_list_front(&out_xbundle->xports), struct xport,
46c88433 2159 bundle_node);
9583bc14 2160 } else {
84f0f298 2161 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
49a73e0c 2162 struct flow_wildcards *wc = ctx->wc;
84f0f298 2163 struct ofport_dpif *ofport;
adcf00ba 2164
a80aba3a
AZ
2165 if (ctx->xbridge->support.odp.recirc) {
2166 /* In case recirculation is not actually in use, 'xr.recirc_id'
2167 * will be set to '0', since a valid 'recirc_id' can
82f9f1f5
AZ
2168 * not be zero. */
2169 bond_update_post_recirc_rules(out_xbundle->bond,
2170 &xr.recirc_id,
2171 &xr.hash_basis);
2172 if (xr.recirc_id) {
2173 /* Use recirculation instead of output. */
2174 use_recirc = true;
e93ef1c7 2175 xr.hash_alg = OVS_HASH_ALG_L4;
54ecb5a2
AZ
2176 /* Recirculation does not require unmasking hash fields. */
2177 wc = NULL;
adcf00ba
AZ
2178 }
2179 }
46c88433 2180
54ecb5a2
AZ
2181 ofport = bond_choose_output_slave(out_xbundle->bond,
2182 &ctx->xin->flow, wc, vid);
84f0f298 2183 xport = xport_lookup(xcfg, ofport);
46c88433
EJ
2184
2185 if (!xport) {
9583bc14
EJ
2186 /* No slaves enabled, so drop packet. */
2187 return;
2188 }
d6fc5f57 2189
e93ef1c7 2190 /* If use_recirc is set, the main thread will handle stats
b256dc52 2191 * accounting for this bond. */
e93ef1c7 2192 if (!use_recirc) {
b256dc52
JS
2193 if (ctx->xin->resubmit_stats) {
2194 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
2195 ctx->xin->resubmit_stats->n_bytes);
2196 }
2197 if (ctx->xin->xcache) {
2198 struct xc_entry *entry;
2199 struct flow *flow;
2200
2201 flow = &ctx->xin->flow;
2202 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_BOND);
901a517e
JR
2203 entry->bond.bond = bond_ref(out_xbundle->bond);
2204 entry->bond.flow = xmemdup(flow, sizeof *flow);
2205 entry->bond.vid = vid;
b256dc52 2206 }
d6fc5f57 2207 }
9583bc14
EJ
2208 }
2209
f0fb825a
EG
2210 memcpy(&old_vlans, &ctx->xin->flow.vlans, sizeof(old_vlans));
2211 xvlan_put(&ctx->xin->flow, &out_xvlan);
9583bc14 2212
e93ef1c7 2213 compose_output_action(ctx, xport->ofp_port, use_recirc ? &xr : NULL);
f0fb825a 2214 memcpy(&ctx->xin->flow.vlans, &old_vlans, sizeof(old_vlans));
9583bc14
EJ
2215}
2216
2217/* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2218 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2219 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2220static bool
2221is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
2222{
2223 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
2224 return false;
2225 }
2226
2227 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2228 if (!eth_addr_is_broadcast(flow->dl_dst)) {
2229 return false;
2230 }
2231
2232 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2233 if (flow->nw_proto == ARP_OP_REPLY) {
2234 return true;
2235 } else if (flow->nw_proto == ARP_OP_REQUEST) {
2236 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2237 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2238
2239 return flow->nw_src == flow->nw_dst;
2240 } else {
2241 return false;
2242 }
2243}
2244
ff69c24a
FL
2245/* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
2246 * dropped. Returns true if they may be forwarded, false if they should be
2247 * dropped.
2248 *
2249 * 'in_port' must be the xport that corresponds to flow->in_port.
2250 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
2251 *
2252 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
2253 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
2254 * checked by input_vid_is_valid().
2255 *
2256 * May also add tags to '*tags', although the current implementation only does
2257 * so in one special case.
2258 */
2259static bool
2260is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
2261 uint16_t vlan)
2262{
2263 struct xbundle *in_xbundle = in_port->xbundle;
2264 const struct xbridge *xbridge = ctx->xbridge;
2265 struct flow *flow = &ctx->xin->flow;
2266
2267 /* Drop frames for reserved multicast addresses
2268 * only if forward_bpdu option is absent. */
2269 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
2d9b49dd
BP
2270 xlate_report(ctx, OFT_DETAIL,
2271 "packet has reserved destination MAC, dropping");
ff69c24a
FL
2272 return false;
2273 }
2274
2275 if (in_xbundle->bond) {
2276 struct mac_entry *mac;
2277
2278 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
2279 flow->dl_dst)) {
2280 case BV_ACCEPT:
2281 break;
2282
2283 case BV_DROP:
2d9b49dd
BP
2284 xlate_report(ctx, OFT_DETAIL,
2285 "bonding refused admissibility, dropping");
ff69c24a
FL
2286 return false;
2287
2288 case BV_DROP_IF_MOVED:
2289 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
2290 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
9d078ec2
BP
2291 if (mac
2292 && mac_entry_get_port(xbridge->ml, mac) != in_xbundle->ofbundle
49a73e0c 2293 && (!is_gratuitous_arp(flow, ctx->wc)
9d078ec2 2294 || mac_entry_is_grat_arp_locked(mac))) {
ff69c24a 2295 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2d9b49dd
BP
2296 xlate_report(ctx, OFT_DETAIL,
2297 "SLB bond thinks this packet looped back, "
ff69c24a
FL
2298 "dropping");
2299 return false;
2300 }
2301 ovs_rwlock_unlock(&xbridge->ml->rwlock);
2302 break;
2303 }
2304 }
2305
2306 return true;
2307}
2308
2d9b49dd
BP
2309static bool
2310update_learning_table__(const struct xbridge *xbridge,
2311 struct xbundle *in_xbundle, struct eth_addr dl_src,
2312 int vlan, bool is_grat_arp)
2313{
2314 return (in_xbundle == &ofpp_none_bundle
2315 || !mac_learning_update(xbridge->ml, dl_src, vlan,
2316 is_grat_arp,
2317 in_xbundle->bond != NULL,
2318 in_xbundle->ofbundle));
2319}
2320
ee047520 2321static void
2d9b49dd 2322update_learning_table(const struct xlate_ctx *ctx,
064799a1
JR
2323 struct xbundle *in_xbundle, struct eth_addr dl_src,
2324 int vlan, bool is_grat_arp)
ee047520 2325{
2d9b49dd
BP
2326 if (!update_learning_table__(ctx->xbridge, in_xbundle, dl_src, vlan,
2327 is_grat_arp)) {
2328 xlate_report_debug(ctx, OFT_DETAIL, "learned that "ETH_ADDR_FMT" is "
2329 "on port %s in VLAN %d",
2330 ETH_ADDR_ARGS(dl_src), in_xbundle->name, vlan);
ee047520 2331 }
9583bc14
EJ
2332}
2333
86e2dcdd
FL
2334/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2335 * was received on 'in_xbundle' in 'vlan' and is either Report or Query. */
2336static void
2d9b49dd 2337update_mcast_snooping_table4__(const struct xlate_ctx *ctx,
06994f87
TLSC
2338 const struct flow *flow,
2339 struct mcast_snooping *ms, int vlan,
2340 struct xbundle *in_xbundle,
2341 const struct dp_packet *packet)
86e2dcdd
FL
2342 OVS_REQ_WRLOCK(ms->rwlock)
2343{
46445c63 2344 const struct igmp_header *igmp;
e3102e42 2345 int count;
46445c63 2346 size_t offset;
06994f87 2347 ovs_be32 ip4 = flow->igmp_group_ip4;
86e2dcdd 2348
46445c63
EC
2349 offset = (char *) dp_packet_l4(packet) - (char *) dp_packet_data(packet);
2350 igmp = dp_packet_at(packet, offset, IGMP_HEADER_LEN);
2351 if (!igmp || csum(igmp, dp_packet_l4_size(packet)) != 0) {
2d9b49dd
BP
2352 xlate_report_debug(ctx, OFT_DETAIL,
2353 "multicast snooping received bad IGMP "
2354 "checksum on port %s in VLAN %d",
2355 in_xbundle->name, vlan);
46445c63
EC
2356 return;
2357 }
2358
86e2dcdd
FL
2359 switch (ntohs(flow->tp_src)) {
2360 case IGMP_HOST_MEMBERSHIP_REPORT:
2361 case IGMPV2_HOST_MEMBERSHIP_REPORT:
964a4d5f 2362 if (mcast_snooping_add_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2d9b49dd
BP
2363 xlate_report_debug(ctx, OFT_DETAIL,
2364 "multicast snooping learned that "
2365 IP_FMT" is on port %s in VLAN %d",
2366 IP_ARGS(ip4), in_xbundle->name, vlan);
86e2dcdd
FL
2367 }
2368 break;
2369 case IGMP_HOST_LEAVE_MESSAGE:
964a4d5f 2370 if (mcast_snooping_leave_group4(ms, ip4, vlan, in_xbundle->ofbundle)) {
2d9b49dd
BP
2371 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping leaving "
2372 IP_FMT" is on port %s in VLAN %d",
2373 IP_ARGS(ip4), in_xbundle->name, vlan);
86e2dcdd
FL
2374 }
2375 break;
2376 case IGMP_HOST_MEMBERSHIP_QUERY:
2377 if (flow->nw_src && mcast_snooping_add_mrouter(ms, vlan,
2d9b49dd
BP
2378 in_xbundle->ofbundle)) {
2379 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping query "
2380 "from "IP_FMT" is on port %s in VLAN %d",
2381 IP_ARGS(flow->nw_src), in_xbundle->name, vlan);
86e2dcdd
FL
2382 }
2383 break;
e3102e42 2384 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2d9b49dd
BP
2385 count = mcast_snooping_add_report(ms, packet, vlan,
2386 in_xbundle->ofbundle);
2387 if (count) {
2388 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping processed "
2389 "%d addresses on port %s in VLAN %d",
2390 count, in_xbundle->name, vlan);
e3102e42
TLSC
2391 }
2392 break;
86e2dcdd
FL
2393 }
2394}
2395
06994f87 2396static void
2d9b49dd 2397update_mcast_snooping_table6__(const struct xlate_ctx *ctx,
06994f87
TLSC
2398 const struct flow *flow,
2399 struct mcast_snooping *ms, int vlan,
2400 struct xbundle *in_xbundle,
2401 const struct dp_packet *packet)
2402 OVS_REQ_WRLOCK(ms->rwlock)
2403{
46445c63 2404 const struct mld_header *mld;
06994f87 2405 int count;
46445c63
EC
2406 size_t offset;
2407
2408 offset = (char *) dp_packet_l4(packet) - (char *) dp_packet_data(packet);
2409 mld = dp_packet_at(packet, offset, MLD_HEADER_LEN);
2410
2411 if (!mld ||
2412 packet_csum_upperlayer6(dp_packet_l3(packet),
2413 mld, IPPROTO_ICMPV6,
2414 dp_packet_l4_size(packet)) != 0) {
2d9b49dd
BP
2415 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping received "
2416 "bad MLD checksum on port %s in VLAN %d",
2417 in_xbundle->name, vlan);
46445c63
EC
2418 return;
2419 }
06994f87
TLSC
2420
2421 switch (ntohs(flow->tp_src)) {
2422 case MLD_QUERY:
2423 if (!ipv6_addr_equals(&flow->ipv6_src, &in6addr_any)
2424 && mcast_snooping_add_mrouter(ms, vlan, in_xbundle->ofbundle)) {
2d9b49dd
BP
2425 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping query on "
2426 "port %s in VLAN %d", in_xbundle->name, vlan);
06994f87
TLSC
2427 }
2428 break;
2429 case MLD_REPORT:
2430 case MLD_DONE:
2431 case MLD2_REPORT:
2432 count = mcast_snooping_add_mld(ms, packet, vlan, in_xbundle->ofbundle);
2433 if (count) {
2d9b49dd
BP
2434 xlate_report_debug(ctx, OFT_DETAIL, "multicast snooping processed "
2435 "%d addresses on port %s in VLAN %d",
2436 count, in_xbundle->name, vlan);
06994f87
TLSC
2437 }
2438 break;
2439 }
2440}
2441
86e2dcdd
FL
2442/* Updates multicast snooping table 'ms' given that a packet matching 'flow'
2443 * was received on 'in_xbundle' in 'vlan'. */
2444static void
2d9b49dd 2445update_mcast_snooping_table(const struct xlate_ctx *ctx,
86e2dcdd 2446 const struct flow *flow, int vlan,
e3102e42
TLSC
2447 struct xbundle *in_xbundle,
2448 const struct dp_packet *packet)
86e2dcdd 2449{
2d9b49dd 2450 struct mcast_snooping *ms = ctx->xbridge->ms;
86e2dcdd
FL
2451 struct xlate_cfg *xcfg;
2452 struct xbundle *mcast_xbundle;
f4ae6e23 2453 struct mcast_port_bundle *fport;
86e2dcdd
FL
2454
2455 /* Don't learn the OFPP_NONE port. */
2456 if (in_xbundle == &ofpp_none_bundle) {
2457 return;
2458 }
2459
2460 /* Don't learn from flood ports */
2461 mcast_xbundle = NULL;
2462 ovs_rwlock_wrlock(&ms->rwlock);
2463 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2464 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2465 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2466 if (mcast_xbundle == in_xbundle) {
2467 break;
2468 }
2469 }
2470
2471 if (!mcast_xbundle || mcast_xbundle != in_xbundle) {
06994f87 2472 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2d9b49dd 2473 update_mcast_snooping_table4__(ctx, flow, ms, vlan,
06994f87
TLSC
2474 in_xbundle, packet);
2475 } else {
2d9b49dd 2476 update_mcast_snooping_table6__(ctx, flow, ms, vlan,
06994f87
TLSC
2477 in_xbundle, packet);
2478 }
86e2dcdd
FL
2479 }
2480 ovs_rwlock_unlock(&ms->rwlock);
2481}
2482
2483/* send the packet to ports having the multicast group learned */
2484static void
2485xlate_normal_mcast_send_group(struct xlate_ctx *ctx,
2486 struct mcast_snooping *ms OVS_UNUSED,
2487 struct mcast_group *grp,
f0fb825a
EG
2488 struct xbundle *in_xbundle,
2489 const struct xvlan *xvlan)
86e2dcdd
FL
2490 OVS_REQ_RDLOCK(ms->rwlock)
2491{
2492 struct xlate_cfg *xcfg;
2493 struct mcast_group_bundle *b;
2494 struct xbundle *mcast_xbundle;
2495
2496 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2497 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
2498 mcast_xbundle = xbundle_lookup(xcfg, b->port);
2499 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2d9b49dd 2500 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast group port");
f0fb825a 2501 output_normal(ctx, mcast_xbundle, xvlan);
86e2dcdd 2502 } else if (!mcast_xbundle) {
2d9b49dd
BP
2503 xlate_report(ctx, OFT_WARN,
2504 "mcast group port is unknown, dropping");
86e2dcdd 2505 } else {
2d9b49dd
BP
2506 xlate_report(ctx, OFT_DETAIL,
2507 "mcast group port is input port, dropping");
86e2dcdd
FL
2508 }
2509 }
2510}
2511
2512/* send the packet to ports connected to multicast routers */
2513static void
2514xlate_normal_mcast_send_mrouters(struct xlate_ctx *ctx,
2515 struct mcast_snooping *ms,
f0fb825a
EG
2516 struct xbundle *in_xbundle,
2517 const struct xvlan *xvlan)
86e2dcdd
FL
2518 OVS_REQ_RDLOCK(ms->rwlock)
2519{
2520 struct xlate_cfg *xcfg;
2521 struct mcast_mrouter_bundle *mrouter;
2522 struct xbundle *mcast_xbundle;
2523
2524 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2525 LIST_FOR_EACH(mrouter, mrouter_node, &ms->mrouter_lru) {
2526 mcast_xbundle = xbundle_lookup(xcfg, mrouter->port);
94a881c1 2527 if (mcast_xbundle && mcast_xbundle != in_xbundle
f0fb825a 2528 && mrouter->vlan == xvlan->v[0].vid) {
2d9b49dd 2529 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast router port");
f0fb825a 2530 output_normal(ctx, mcast_xbundle, xvlan);
86e2dcdd 2531 } else if (!mcast_xbundle) {
2d9b49dd
BP
2532 xlate_report(ctx, OFT_WARN,
2533 "mcast router port is unknown, dropping");
f0fb825a 2534 } else if (mrouter->vlan != xvlan->v[0].vid) {
2d9b49dd
BP
2535 xlate_report(ctx, OFT_DETAIL,
2536 "mcast router is on another vlan, dropping");
86e2dcdd 2537 } else {
2d9b49dd
BP
2538 xlate_report(ctx, OFT_DETAIL,
2539 "mcast router port is input port, dropping");
86e2dcdd
FL
2540 }
2541 }
2542}
2543
2544/* send the packet to ports flagged to be flooded */
2545static void
2546xlate_normal_mcast_send_fports(struct xlate_ctx *ctx,
2547 struct mcast_snooping *ms,
f0fb825a
EG
2548 struct xbundle *in_xbundle,
2549 const struct xvlan *xvlan)
86e2dcdd
FL
2550 OVS_REQ_RDLOCK(ms->rwlock)
2551{
2552 struct xlate_cfg *xcfg;
f4ae6e23 2553 struct mcast_port_bundle *fport;
86e2dcdd
FL
2554 struct xbundle *mcast_xbundle;
2555
2556 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
f4ae6e23 2557 LIST_FOR_EACH(fport, node, &ms->fport_list) {
86e2dcdd
FL
2558 mcast_xbundle = xbundle_lookup(xcfg, fport->port);
2559 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2d9b49dd 2560 xlate_report(ctx, OFT_DETAIL, "forwarding to mcast flood port");
f0fb825a 2561 output_normal(ctx, mcast_xbundle, xvlan);
86e2dcdd 2562 } else if (!mcast_xbundle) {
2d9b49dd
BP
2563 xlate_report(ctx, OFT_WARN,
2564 "mcast flood port is unknown, dropping");
86e2dcdd 2565 } else {
2d9b49dd
BP
2566 xlate_report(ctx, OFT_DETAIL,
2567 "mcast flood port is input port, dropping");
86e2dcdd
FL
2568 }
2569 }
2570}
2571
8e04a33f
FL
2572/* forward the Reports to configured ports */
2573static void
2574xlate_normal_mcast_send_rports(struct xlate_ctx *ctx,
2575 struct mcast_snooping *ms,
f0fb825a
EG
2576 struct xbundle *in_xbundle,
2577 const struct xvlan *xvlan)
8e04a33f
FL
2578 OVS_REQ_RDLOCK(ms->rwlock)
2579{
2580 struct xlate_cfg *xcfg;
2581 struct mcast_port_bundle *rport;
2582 struct xbundle *mcast_xbundle;
2583
2584 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2585 LIST_FOR_EACH(rport, node, &ms->rport_list) {
2586 mcast_xbundle = xbundle_lookup(xcfg, rport->port);
2587 if (mcast_xbundle && mcast_xbundle != in_xbundle) {
2d9b49dd
BP
2588 xlate_report(ctx, OFT_DETAIL,
2589 "forwarding report to mcast flagged port");
f0fb825a 2590 output_normal(ctx, mcast_xbundle, xvlan);
8e04a33f 2591 } else if (!mcast_xbundle) {
2d9b49dd
BP
2592 xlate_report(ctx, OFT_WARN,
2593 "mcast port is unknown, dropping the report");
8e04a33f 2594 } else {
2d9b49dd
BP
2595 xlate_report(ctx, OFT_DETAIL,
2596 "mcast port is input port, dropping the Report");
8e04a33f
FL
2597 }
2598 }
2599}
2600
682800a4
FL
2601static void
2602xlate_normal_flood(struct xlate_ctx *ctx, struct xbundle *in_xbundle,
f0fb825a 2603 struct xvlan *xvlan)
682800a4
FL
2604{
2605 struct xbundle *xbundle;
2606
2607 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
2608 if (xbundle != in_xbundle
f0fb825a 2609 && xbundle_includes_vlan(xbundle, xvlan)
682800a4
FL
2610 && xbundle->floodable
2611 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
f0fb825a 2612 output_normal(ctx, xbundle, xvlan);
682800a4
FL
2613 }
2614 }
2031ef97 2615 ctx->nf_output_iface = NF_OUT_FLOOD;
682800a4
FL
2616}
2617
a75636c8
BP
2618static bool
2619is_ip_local_multicast(const struct flow *flow, struct flow_wildcards *wc)
2620{
2621 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2622 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2623 return ip_is_local_multicast(flow->nw_dst);
2624 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2625 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2626 return ipv6_is_all_hosts(&flow->ipv6_dst);
2627 } else {
2628 return false;
2629 }
2630}
2631
9583bc14
EJ
2632static void
2633xlate_normal(struct xlate_ctx *ctx)
2634{
49a73e0c 2635 struct flow_wildcards *wc = ctx->wc;
33bf9176 2636 struct flow *flow = &ctx->xin->flow;
46c88433
EJ
2637 struct xbundle *in_xbundle;
2638 struct xport *in_port;
9583bc14 2639 struct mac_entry *mac;
d6d5bbc9 2640 void *mac_port;
f0fb825a
EG
2641 struct xvlan in_xvlan;
2642 struct xvlan xvlan;
9583bc14 2643 uint16_t vlan;
9583bc14 2644
33bf9176
BP
2645 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2646 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
f0fb825a 2647 wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
9583bc14 2648
2d9b49dd 2649 in_xbundle = lookup_input_bundle(ctx, flow->in_port.ofp_port, &in_port);
46c88433 2650 if (!in_xbundle) {
2d9b49dd 2651 xlate_report(ctx, OFT_WARN, "no input bundle, dropping");
9583bc14
EJ
2652 return;
2653 }
2654
2655 /* Drop malformed frames. */
f0fb825a
EG
2656 if (eth_type_vlan(flow->dl_type) &&
2657 !(flow->vlans[0].tci & htons(VLAN_CFI))) {
9583bc14 2658 if (ctx->xin->packet != NULL) {
2d9b49dd
BP
2659 xlate_report_error(ctx, "dropping packet with partial "
2660 "VLAN tag received on port %s",
2661 in_xbundle->name);
9583bc14 2662 }
2d9b49dd 2663 xlate_report(ctx, OFT_WARN, "partial VLAN tag, dropping");
9583bc14
EJ
2664 return;
2665 }
2666
2667 /* Drop frames on bundles reserved for mirroring. */
46c88433 2668 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
9583bc14 2669 if (ctx->xin->packet != NULL) {
2d9b49dd
BP
2670 xlate_report_error(ctx, "dropping packet received on port %s, "
2671 "which is reserved exclusively for mirroring",
2672 in_xbundle->name);
9583bc14 2673 }
2d9b49dd
BP
2674 xlate_report(ctx, OFT_WARN,
2675 "input port is mirror output port, dropping");
9583bc14
EJ
2676 return;
2677 }
2678
2679 /* Check VLAN. */
f0fb825a
EG
2680 xvlan_extract(flow, &in_xvlan);
2681 if (!input_vid_is_valid(ctx, in_xvlan.v[0].vid, in_xbundle)) {
2d9b49dd
BP
2682 xlate_report(ctx, OFT_WARN,
2683 "disallowed VLAN VID for this input port, dropping");
9583bc14
EJ
2684 return;
2685 }
f0fb825a
EG
2686 xvlan_input_translate(in_xbundle, &in_xvlan, &xvlan);
2687 vlan = xvlan.v[0].vid;
9583bc14
EJ
2688
2689 /* Check other admissibility requirements. */
2690 if (in_port && !is_admissible(ctx, in_port, vlan)) {
2691 return;
2692 }
2693
2694 /* Learn source MAC. */
064799a1 2695 bool is_grat_arp = is_gratuitous_arp(flow, wc);
df70a773 2696 if (ctx->xin->allow_side_effects) {
2d9b49dd 2697 update_learning_table(ctx, in_xbundle, flow->dl_src, vlan,
064799a1 2698 is_grat_arp);
9583bc14 2699 }
064799a1 2700 if (ctx->xin->xcache && in_xbundle != &ofpp_none_bundle) {
b256dc52
JS
2701 struct xc_entry *entry;
2702
064799a1 2703 /* Save just enough info to update mac learning table later. */
b256dc52 2704 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NORMAL);
901a517e
JR
2705 entry->normal.ofproto = ctx->xbridge->ofproto;
2706 entry->normal.in_port = flow->in_port.ofp_port;
2707 entry->normal.dl_src = flow->dl_src;
2708 entry->normal.vlan = vlan;
2709 entry->normal.is_gratuitous_arp = is_grat_arp;
b256dc52 2710 }
9583bc14
EJ
2711
2712 /* Determine output bundle. */
86e2dcdd
FL
2713 if (mcast_snooping_enabled(ctx->xbridge->ms)
2714 && !eth_addr_is_broadcast(flow->dl_dst)
2715 && eth_addr_is_multicast(flow->dl_dst)
06994f87 2716 && is_ip_any(flow)) {
86e2dcdd 2717 struct mcast_snooping *ms = ctx->xbridge->ms;
06994f87 2718 struct mcast_group *grp = NULL;
86e2dcdd 2719
a75636c8
BP
2720 if (is_igmp(flow, wc)) {
2721 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
d29f137b
TLSC
2722 if (mcast_snooping_is_membership(flow->tp_src) ||
2723 mcast_snooping_is_query(flow->tp_src)) {
df70a773 2724 if (ctx->xin->allow_side_effects && ctx->xin->packet) {
2d9b49dd 2725 update_mcast_snooping_table(ctx, flow, vlan,
e3102e42 2726 in_xbundle, ctx->xin->packet);
d29f137b
TLSC
2727 }
2728 /*
2729 * IGMP packets need to take the slow path, in order to be
2730 * processed for mdb updates. That will prevent expires
2731 * firing off even after hosts have sent reports.
2732 */
2733 ctx->xout->slow |= SLOW_ACTION;
86e2dcdd 2734 }
d6d5bbc9 2735
86e2dcdd
FL
2736 if (mcast_snooping_is_membership(flow->tp_src)) {
2737 ovs_rwlock_rdlock(&ms->rwlock);
f0fb825a 2738 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
8e04a33f
FL
2739 /* RFC4541: section 2.1.1, item 1: A snooping switch should
2740 * forward IGMP Membership Reports only to those ports where
2741 * multicast routers are attached. Alternatively stated: a
2742 * snooping switch should not forward IGMP Membership Reports
2743 * to ports on which only hosts are attached.
2744 * An administrative control may be provided to override this
2745 * restriction, allowing the report messages to be flooded to
2746 * other ports. */
f0fb825a 2747 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, &xvlan);
86e2dcdd
FL
2748 ovs_rwlock_unlock(&ms->rwlock);
2749 } else {
2d9b49dd 2750 xlate_report(ctx, OFT_DETAIL, "multicast traffic, flooding");
f0fb825a 2751 xlate_normal_flood(ctx, in_xbundle, &xvlan);
86e2dcdd
FL
2752 }
2753 return;
a75636c8 2754 } else if (is_mld(flow, wc)) {
06994f87 2755 ctx->xout->slow |= SLOW_ACTION;
df70a773 2756 if (ctx->xin->allow_side_effects && ctx->xin->packet) {
2d9b49dd 2757 update_mcast_snooping_table(ctx, flow, vlan,
06994f87
TLSC
2758 in_xbundle, ctx->xin->packet);
2759 }
a75636c8 2760 if (is_mld_report(flow, wc)) {
06994f87 2761 ovs_rwlock_rdlock(&ms->rwlock);
f0fb825a
EG
2762 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2763 xlate_normal_mcast_send_rports(ctx, ms, in_xbundle, &xvlan);
06994f87
TLSC
2764 ovs_rwlock_unlock(&ms->rwlock);
2765 } else {
2d9b49dd 2766 xlate_report(ctx, OFT_DETAIL, "MLD query, flooding");
f0fb825a 2767 xlate_normal_flood(ctx, in_xbundle, &xvlan);
06994f87 2768 }
86e2dcdd 2769 } else {
a75636c8 2770 if (is_ip_local_multicast(flow, wc)) {
86e2dcdd
FL
2771 /* RFC4541: section 2.1.2, item 2: Packets with a dst IP
2772 * address in the 224.0.0.x range which are not IGMP must
2773 * be forwarded on all ports */
2d9b49dd
BP
2774 xlate_report(ctx, OFT_DETAIL,
2775 "RFC4541: section 2.1.2, item 2, flooding");
f0fb825a 2776 xlate_normal_flood(ctx, in_xbundle, &xvlan);
86e2dcdd
FL
2777 return;
2778 }
2779 }
2780
2781 /* forwarding to group base ports */
2782 ovs_rwlock_rdlock(&ms->rwlock);
06994f87
TLSC
2783 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2784 grp = mcast_snooping_lookup4(ms, flow->nw_dst, vlan);
2785 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2786 grp = mcast_snooping_lookup(ms, &flow->ipv6_dst, vlan);
2787 }
86e2dcdd 2788 if (grp) {
f0fb825a
EG
2789 xlate_normal_mcast_send_group(ctx, ms, grp, in_xbundle, &xvlan);
2790 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, &xvlan);
2791 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
9583bc14 2792 } else {
86e2dcdd 2793 if (mcast_snooping_flood_unreg(ms)) {
2d9b49dd
BP
2794 xlate_report(ctx, OFT_DETAIL,
2795 "unregistered multicast, flooding");
f0fb825a 2796 xlate_normal_flood(ctx, in_xbundle, &xvlan);
86e2dcdd 2797 } else {
f0fb825a
EG
2798 xlate_normal_mcast_send_mrouters(ctx, ms, in_xbundle, &xvlan);
2799 xlate_normal_mcast_send_fports(ctx, ms, in_xbundle, &xvlan);
86e2dcdd 2800 }
9583bc14 2801 }
86e2dcdd 2802 ovs_rwlock_unlock(&ms->rwlock);
9583bc14 2803 } else {
86e2dcdd
FL
2804 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
2805 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
9d078ec2 2806 mac_port = mac ? mac_entry_get_port(ctx->xbridge->ml, mac) : NULL;
86e2dcdd
FL
2807 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
2808
2809 if (mac_port) {
2810 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2811 struct xbundle *mac_xbundle = xbundle_lookup(xcfg, mac_port);
2812 if (mac_xbundle && mac_xbundle != in_xbundle) {
2d9b49dd 2813 xlate_report(ctx, OFT_DETAIL, "forwarding to learned port");
f0fb825a 2814 output_normal(ctx, mac_xbundle, &xvlan);
86e2dcdd 2815 } else if (!mac_xbundle) {
2d9b49dd
BP
2816 xlate_report(ctx, OFT_WARN,
2817 "learned port is unknown, dropping");
86e2dcdd 2818 } else {
2d9b49dd
BP
2819 xlate_report(ctx, OFT_DETAIL,
2820 "learned port is input port, dropping");
86e2dcdd
FL
2821 }
2822 } else {
2d9b49dd
BP
2823 xlate_report(ctx, OFT_DETAIL,
2824 "no learned MAC for destination, flooding");
f0fb825a 2825 xlate_normal_flood(ctx, in_xbundle, &xvlan);
86e2dcdd 2826 }
9583bc14
EJ
2827 }
2828}
2829
a6092018
BP
2830/* Appends a "sample" action for sFlow or IPFIX to 'ctx->odp_actions'. The
2831 * 'probability' is the number of packets out of UINT32_MAX to sample. The
2832 * 'cookie' (of length 'cookie_size' bytes) is passed back in the callback for
2833 * each sampled packet. 'tunnel_out_port', if not ODPP_NONE, is added as the
2834 * OVS_USERSPACE_ATTR_EGRESS_TUN_PORT attribute. If 'include_actions', an
f69f713b
BY
2835 * OVS_USERSPACE_ATTR_ACTIONS attribute is added. If 'emit_set_tunnel',
2836 * sample(sampling_port=1) would translate into datapath sample action
2837 * set(tunnel(...)), sample(...) and it is used for sampling egress tunnel
2838 * information.
9583bc14
EJ
2839 */
2840static size_t
a6092018 2841compose_sample_action(struct xlate_ctx *ctx,
9583bc14
EJ
2842 const uint32_t probability,
2843 const union user_action_cookie *cookie,
8b7ea2d4 2844 const size_t cookie_size,
7321bda3
NM
2845 const odp_port_t tunnel_out_port,
2846 bool include_actions)
9583bc14 2847{
b97f2c3a
BY
2848 if (probability == 0) {
2849 /* No need to generate sampling or the inner action. */
2850 return 0;
2851 }
2852
31b29c2e
AZ
2853 /* If the slow path meter is configured by the controller,
2854 * insert a meter action before the user space action. */
2855 struct ofproto *ofproto = &ctx->xin->ofproto->up;
2856 uint32_t meter_id = ofproto->slowpath_meter_id;
2857
2858 /* When meter action is not required, avoid generate sample action
2859 * for 100% sampling rate. */
2860 bool is_sample = probability < UINT32_MAX || meter_id != UINT32_MAX;
72471622
BY
2861 size_t sample_offset, actions_offset;
2862 if (is_sample) {
2863 sample_offset = nl_msg_start_nested(ctx->odp_actions,
2864 OVS_ACTION_ATTR_SAMPLE);
2865 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY,
2866 probability);
2867 actions_offset = nl_msg_start_nested(ctx->odp_actions,
2868 OVS_SAMPLE_ATTR_ACTIONS);
2869 }
9583bc14 2870
31b29c2e
AZ
2871 if (meter_id != UINT32_MAX) {
2872 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_METER, meter_id);
2873 }
2874
a6092018
BP
2875 odp_port_t odp_port = ofp_port_to_odp_port(
2876 ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
2877 uint32_t pid = dpif_port_get_pid(ctx->xbridge->dpif, odp_port,
2878 flow_hash_5tuple(&ctx->xin->flow, 0));
2879 int cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size,
2880 tunnel_out_port,
2881 include_actions,
2882 ctx->odp_actions);
89a8a7f0 2883
72471622
BY
2884 if (is_sample) {
2885 nl_msg_end_nested(ctx->odp_actions, actions_offset);
2886 nl_msg_end_nested(ctx->odp_actions, sample_offset);
2887 }
9583bc14 2888
9583bc14
EJ
2889 return cookie_offset;
2890}
2891
a6092018
BP
2892/* If sFLow is not enabled, returns 0 without doing anything.
2893 *
2894 * If sFlow is enabled, appends a template "sample" action to the ODP actions
2895 * in 'ctx'. This action is a template because some of the information needed
2896 * to fill it out is not available until flow translation is complete. In this
2897 * case, this functions returns an offset, which is always nonzero, to pass
2898 * later to fix_sflow_action() to fill in the rest of the template. */
9583bc14 2899static size_t
a6092018 2900compose_sflow_action(struct xlate_ctx *ctx)
9583bc14 2901{
a6092018
BP
2902 struct dpif_sflow *sflow = ctx->xbridge->sflow;
2903 if (!sflow || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2904 return 0;
2905 }
2906
a6092018
BP
2907 union user_action_cookie cookie = { .type = USER_ACTION_COOKIE_SFLOW };
2908 return compose_sample_action(ctx, dpif_sflow_get_probability(sflow),
7321bda3
NM
2909 &cookie, sizeof cookie.sflow, ODPP_NONE,
2910 true);
9583bc14
EJ
2911}
2912
f69f713b
BY
2913/* If flow IPFIX is enabled, make sure IPFIX flow sample action
2914 * at egress point of tunnel port is just in front of corresponding
2915 * output action. If bridge IPFIX is enabled, this appends an IPFIX
2916 * sample action to 'ctx->odp_actions'. */
9583bc14 2917static void
a6092018 2918compose_ipfix_action(struct xlate_ctx *ctx, odp_port_t output_odp_port)
9583bc14 2919{
a6092018 2920 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
8b7ea2d4 2921 odp_port_t tunnel_out_port = ODPP_NONE;
9583bc14 2922
a6092018 2923 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
9583bc14
EJ
2924 return;
2925 }
2926
8b7ea2d4
WZ
2927 /* For input case, output_odp_port is ODPP_NONE, which is an invalid port
2928 * number. */
2929 if (output_odp_port == ODPP_NONE &&
a6092018 2930 !dpif_ipfix_get_bridge_exporter_input_sampling(ipfix)) {
8b7ea2d4
WZ
2931 return;
2932 }
2933
f69f713b 2934 /* For output case, output_odp_port is valid. */
8b7ea2d4 2935 if (output_odp_port != ODPP_NONE) {
a6092018 2936 if (!dpif_ipfix_get_bridge_exporter_output_sampling(ipfix)) {
8b7ea2d4
WZ
2937 return;
2938 }
2939 /* If tunnel sampling is enabled, put an additional option attribute:
2940 * OVS_USERSPACE_ATTR_TUNNEL_OUT_PORT
2941 */
a6092018
BP
2942 if (dpif_ipfix_get_bridge_exporter_tunnel_sampling(ipfix) &&
2943 dpif_ipfix_get_tunnel_port(ipfix, output_odp_port) ) {
8b7ea2d4
WZ
2944 tunnel_out_port = output_odp_port;
2945 }
2946 }
2947
a6092018
BP
2948 union user_action_cookie cookie = {
2949 .ipfix = {
2950 .type = USER_ACTION_COOKIE_IPFIX,
2951 .output_odp_port = output_odp_port,
2952 }
2953 };
2954 compose_sample_action(ctx,
2955 dpif_ipfix_get_bridge_exporter_probability(ipfix),
7321bda3
NM
2956 &cookie, sizeof cookie.ipfix, tunnel_out_port,
2957 false);
9583bc14
EJ
2958}
2959
a6092018
BP
2960/* Fix "sample" action according to data collected while composing ODP actions,
2961 * as described in compose_sflow_action().
2962 *
2963 * 'user_cookie_offset' must be the offset returned by add_sflow_action(). */
9583bc14 2964static void
a6092018 2965fix_sflow_action(struct xlate_ctx *ctx, unsigned int user_cookie_offset)
9583bc14
EJ
2966{
2967 const struct flow *base = &ctx->base_flow;
2968 union user_action_cookie *cookie;
2969
a6092018 2970 cookie = ofpbuf_at(ctx->odp_actions, user_cookie_offset,
9583bc14
EJ
2971 sizeof cookie->sflow);
2972 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
2973
a6092018 2974 cookie->type = USER_ACTION_COOKIE_SFLOW;
f0fb825a 2975 cookie->sflow.vlan_tci = base->vlans[0].tci;
a6092018
BP
2976
2977 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
2978 * port information") for the interpretation of cookie->output. */
2979 switch (ctx->sflow_n_outputs) {
2980 case 0:
2981 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
2982 cookie->sflow.output = 0x40000000 | 256;
2983 break;
2984
2985 case 1:
2986 cookie->sflow.output = dpif_sflow_odp_port_to_ifindex(
2987 ctx->xbridge->sflow, ctx->sflow_odp_port);
2988 if (cookie->sflow.output) {
2989 break;
2990 }
2991 /* Fall through. */
2992 default:
2993 /* 0x80000000 means "multiple output ports. */
2994 cookie->sflow.output = 0x80000000 | ctx->sflow_n_outputs;
2995 break;
2996 }
9583bc14
EJ
2997}
2998
515793d5
BP
2999static bool
3000process_special(struct xlate_ctx *ctx, const struct xport *xport)
db7d4e46 3001{
515793d5 3002 const struct flow *flow = &ctx->xin->flow;
49a73e0c 3003 struct flow_wildcards *wc = ctx->wc;
46c88433 3004 const struct xbridge *xbridge = ctx->xbridge;
515793d5
BP
3005 const struct dp_packet *packet = ctx->xin->packet;
3006 enum slow_path_reason slow;
642dc74d 3007
46c88433 3008 if (!xport) {
515793d5 3009 slow = 0;
46c88433 3010 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
db7d4e46 3011 if (packet) {
46c88433 3012 cfm_process_heartbeat(xport->cfm, packet);
db7d4e46 3013 }
515793d5 3014 slow = SLOW_CFM;
fab52e16 3015 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
db7d4e46 3016 if (packet) {
46c88433 3017 bfd_process_packet(xport->bfd, flow, packet);
60d02c72
AW
3018 /* If POLL received, immediately sends FINAL back. */
3019 if (bfd_should_send_packet(xport->bfd)) {
6d308b28 3020 ofproto_dpif_monitor_port_send_soon(xport->ofport);
60d02c72 3021 }
db7d4e46 3022 }
515793d5 3023 slow = SLOW_BFD;
46c88433 3024 } else if (xport->xbundle && xport->xbundle->lacp
db7d4e46
JP
3025 && flow->dl_type == htons(ETH_TYPE_LACP)) {
3026 if (packet) {
46c88433 3027 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
db7d4e46 3028 }
515793d5 3029 slow = SLOW_LACP;
9efd308e
DV
3030 } else if ((xbridge->stp || xbridge->rstp) &&
3031 stp_should_process_flow(flow, wc)) {
db7d4e46 3032 if (packet) {
f025bcb7
JR
3033 xbridge->stp
3034 ? stp_process_packet(xport, packet)
3035 : rstp_process_packet(xport, packet);
db7d4e46 3036 }
515793d5 3037 slow = SLOW_STP;
19aef6ef 3038 } else if (xport->lldp && lldp_should_process_flow(xport->lldp, flow)) {
0477baa9
DF
3039 if (packet) {
3040 lldp_process_packet(xport->lldp, packet);
3041 }
515793d5 3042 slow = SLOW_LLDP;
db7d4e46 3043 } else {
515793d5
BP
3044 slow = 0;
3045 }
3046
3047 if (slow) {
3048 ctx->xout->slow |= slow;
3049 return true;
3050 } else {
3051 return false;
db7d4e46
JP
3052 }
3053}
3054
a36de779
PS
3055static int
3056tnl_route_lookup_flow(const struct flow *oflow,
a8704b50
PS
3057 struct in6_addr *ip, struct in6_addr *src,
3058 struct xport **out_port)
a36de779
PS
3059{
3060 char out_dev[IFNAMSIZ];
3061 struct xbridge *xbridge;
3062 struct xlate_cfg *xcfg;
c2b878e0
TLSC
3063 struct in6_addr gw;
3064 struct in6_addr dst;
a36de779 3065
c2b878e0 3066 dst = flow_tnl_dst(&oflow->tunnel);
ed52ca57 3067 if (!ovs_router_lookup(oflow->pkt_mark, &dst, out_dev, src, &gw)) {
a36de779
PS
3068 return -ENOENT;
3069 }
3070
c2b878e0
TLSC
3071 if (ipv6_addr_is_set(&gw) &&
3072 (!IN6_IS_ADDR_V4MAPPED(&gw) || in6_addr_get_mapped_ipv4(&gw))) {
a36de779
PS
3073 *ip = gw;
3074 } else {
c2b878e0 3075 *ip = dst;
a36de779
PS
3076 }
3077
3078 xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
3079 ovs_assert(xcfg);
3080
3081 HMAP_FOR_EACH (xbridge, hmap_node, &xcfg->xbridges) {
3082 if (!strncmp(xbridge->name, out_dev, IFNAMSIZ)) {
3083 struct xport *port;
3084
3085 HMAP_FOR_EACH (port, ofp_node, &xbridge->xports) {
3086 if (!strncmp(netdev_get_name(port->netdev), out_dev, IFNAMSIZ)) {
3087 *out_port = port;
3088 return 0;
3089 }
3090 }
3091 }
3092 }
3093 return -ENOENT;
3094}
3095
3096static int
cdd42eda
JG
3097compose_table_xlate(struct xlate_ctx *ctx, const struct xport *out_dev,
3098 struct dp_packet *packet)
a36de779 3099{
cdd42eda 3100 struct xbridge *xbridge = out_dev->xbridge;
a36de779
PS
3101 struct ofpact_output output;
3102 struct flow flow;
3103
3104 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
cf62fa4c 3105 flow_extract(packet, &flow);
cdd42eda
JG
3106 flow.in_port.ofp_port = out_dev->ofp_port;
3107 output.port = OFPP_TABLE;
a36de779
PS
3108 output.max_len = 0;
3109
1f4a8933
JR
3110 return ofproto_dpif_execute_actions__(xbridge->ofproto,
3111 ctx->xin->tables_version, &flow,
3112 NULL, &output.ofpact, sizeof output,
2d9b49dd 3113 ctx->depth, ctx->resubmits, packet);
a36de779
PS
3114}
3115
c2b878e0
TLSC
3116static void
3117tnl_send_nd_request(struct xlate_ctx *ctx, const struct xport *out_dev,
3118 const struct eth_addr eth_src,
3119 struct in6_addr * ipv6_src, struct in6_addr * ipv6_dst)
3120{
3121 struct dp_packet packet;
3122
3123 dp_packet_init(&packet, 0);
16187903 3124 compose_nd_ns(&packet, eth_src, ipv6_src, ipv6_dst);
c2b878e0
TLSC
3125 compose_table_xlate(ctx, out_dev, &packet);
3126 dp_packet_uninit(&packet);
3127}
3128
a36de779 3129static void
cdd42eda 3130tnl_send_arp_request(struct xlate_ctx *ctx, const struct xport *out_dev,
74ff3298 3131 const struct eth_addr eth_src,
a36de779
PS
3132 ovs_be32 ip_src, ovs_be32 ip_dst)
3133{
cf62fa4c 3134 struct dp_packet packet;
a36de779 3135
cf62fa4c 3136 dp_packet_init(&packet, 0);
eb0b295e
BP
3137 compose_arp(&packet, ARP_OP_REQUEST,
3138 eth_src, eth_addr_zero, true, ip_src, ip_dst);
a36de779 3139
cdd42eda 3140 compose_table_xlate(ctx, out_dev, &packet);
cf62fa4c 3141 dp_packet_uninit(&packet);
a36de779
PS
3142}
3143
3144static int
81de18ec 3145build_tunnel_send(struct xlate_ctx *ctx, const struct xport *xport,
a36de779
PS
3146 const struct flow *flow, odp_port_t tunnel_odp_port)
3147{
4975aa3e 3148 struct netdev_tnl_build_header_params tnl_params;
a36de779
PS
3149 struct ovs_action_push_tnl tnl_push_data;
3150 struct xport *out_dev = NULL;
c2b878e0
TLSC
3151 ovs_be32 s_ip = 0, d_ip = 0;
3152 struct in6_addr s_ip6 = in6addr_any;
3153 struct in6_addr d_ip6 = in6addr_any;
74ff3298
JR
3154 struct eth_addr smac;
3155 struct eth_addr dmac;
a36de779 3156 int err;
c2b878e0
TLSC
3157 char buf_sip6[INET6_ADDRSTRLEN];
3158 char buf_dip6[INET6_ADDRSTRLEN];
a36de779 3159
a8704b50 3160 err = tnl_route_lookup_flow(flow, &d_ip6, &s_ip6, &out_dev);
a36de779 3161 if (err) {
2d9b49dd 3162 xlate_report(ctx, OFT_WARN, "native tunnel routing failed");
a36de779
PS
3163 return err;
3164 }
c2b878e0 3165
2d9b49dd 3166 xlate_report(ctx, OFT_DETAIL, "tunneling to %s via %s",
c2b878e0
TLSC
3167 ipv6_string_mapped(buf_dip6, &d_ip6),
3168 netdev_get_name(out_dev->netdev));
a36de779
PS
3169
3170 /* Use mac addr of bridge port of the peer. */
74ff3298 3171 err = netdev_get_etheraddr(out_dev->netdev, &smac);
a36de779 3172 if (err) {
2d9b49dd
BP
3173 xlate_report(ctx, OFT_WARN,
3174 "tunnel output device lacks Ethernet address");
a36de779
PS
3175 return err;
3176 }
3177
c2b878e0
TLSC
3178 d_ip = in6_addr_get_mapped_ipv4(&d_ip6);
3179 if (d_ip) {
a8704b50 3180 s_ip = in6_addr_get_mapped_ipv4(&s_ip6);
a36de779
PS
3181 }
3182
c2b878e0 3183 err = tnl_neigh_lookup(out_dev->xbridge->name, &d_ip6, &dmac);
a36de779 3184 if (err) {
2d9b49dd
BP
3185 xlate_report(ctx, OFT_DETAIL,
3186 "neighbor cache miss for %s on bridge %s, "
c2b878e0
TLSC
3187 "sending %s request",
3188 buf_dip6, out_dev->xbridge->name, d_ip ? "ARP" : "ND");
3189 if (d_ip) {
3190 tnl_send_arp_request(ctx, out_dev, smac, s_ip, d_ip);
3191 } else {
3192 tnl_send_nd_request(ctx, out_dev, smac, &s_ip6, &d_ip6);
3193 }
a36de779
PS
3194 return err;
3195 }
c2b878e0 3196
a36de779
PS
3197 if (ctx->xin->xcache) {
3198 struct xc_entry *entry;
3199
53902038 3200 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_TNL_NEIGH);
901a517e
JR
3201 ovs_strlcpy(entry->tnl_neigh_cache.br_name, out_dev->xbridge->name,
3202 sizeof entry->tnl_neigh_cache.br_name);
3203 entry->tnl_neigh_cache.d_ipv6 = d_ip6;
a36de779 3204 }
81de18ec 3205
2d9b49dd 3206 xlate_report(ctx, OFT_DETAIL, "tunneling from "ETH_ADDR_FMT" %s"
c2b878e0
TLSC
3207 " to "ETH_ADDR_FMT" %s",
3208 ETH_ADDR_ARGS(smac), ipv6_string_mapped(buf_sip6, &s_ip6),
3209 ETH_ADDR_ARGS(dmac), buf_dip6);
3210
4975aa3e
PS
3211 netdev_init_tnl_build_header_params(&tnl_params, flow, &s_ip6, dmac, smac);
3212 err = tnl_port_build_header(xport->ofport, &tnl_push_data, &tnl_params);
a36de779
PS
3213 if (err) {
3214 return err;
3215 }
3216 tnl_push_data.tnl_port = odp_to_u32(tunnel_odp_port);
3217 tnl_push_data.out_port = odp_to_u32(out_dev->odp_port);
f1dac512
SC
3218
3219 size_t push_action_size = 0;
3220 size_t clone_ofs = nl_msg_start_nested(ctx->odp_actions,
3221 OVS_ACTION_ATTR_CLONE);
1520ef4f 3222 odp_put_tnl_push_action(ctx->odp_actions, &tnl_push_data);
f1dac512
SC
3223 push_action_size = ctx->odp_actions->size;
3224 apply_nested_clone_actions(ctx, xport, out_dev);
3225 if (ctx->odp_actions->size > push_action_size) {
3226 /* Update the CLONE action only when combined */
3227 nl_msg_end_nested(ctx->odp_actions, clone_ofs);
3228 }
a36de779
PS
3229 return 0;
3230}
3231
704bb0bf
JS
3232static void
3233xlate_commit_actions(struct xlate_ctx *ctx)
3234{
3235 bool use_masked = ctx->xbridge->support.masked_set_action;
3236
3237 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
3238 ctx->odp_actions, ctx->wc,
3239 use_masked);
3240}
3241
07659514 3242static void
72fe7578 3243clear_conntrack(struct xlate_ctx *ctx)
07659514 3244{
72fe7578 3245 ctx->conntracked = false;
6846e91e 3246 flow_clear_conntrack(&ctx->xin->flow);
07659514
JS
3247}
3248
58d636ee
BK
3249static bool
3250xlate_flow_is_protected(const struct xlate_ctx *ctx, const struct flow *flow, const struct xport *xport_out)
3251{
3252 const struct xport *xport_in;
3253
3254 if (!xport_out) {
3255 return false;
3256 }
3257
3258 xport_in = get_ofp_port(ctx->xbridge, flow->in_port.ofp_port);
3259
3260 return (xport_in && xport_in->xbundle && xport_out->xbundle &&
3261 xport_in->xbundle->protected && xport_out->xbundle->protected);
3262}
3263
f1dac512
SC
3264/* Populate and apply nested actions on 'out_dev'.
3265 * The nested actions are applied on cloned packets in dp while outputting to
3266 * either patch or tunnel ports.
3267 * On output to a patch port, the output action will be replaced with set of
3268 * nested actions on the peer patch port.
3269 * Similarly on output to a tunnel port, the post nested actions on
3270 * tunnel are chained up with the tunnel-push action.
3271 */
3272static void
3273apply_nested_clone_actions(struct xlate_ctx *ctx, const struct xport *in_dev,
3274 struct xport *out_dev)
3275{
3276 struct flow *flow = &ctx->xin->flow;
3277 struct flow old_flow = ctx->xin->flow;
3278 struct flow_tnl old_flow_tnl_wc = ctx->wc->masks.tunnel;
3279 bool old_conntrack = ctx->conntracked;
3280 bool old_was_mpls = ctx->was_mpls;
3281 ovs_version_t old_version = ctx->xin->tables_version;
3282 struct ofpbuf old_stack = ctx->stack;
3283 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
3284 struct ofpbuf old_action_set = ctx->action_set;
3285 struct ovs_list *old_trace = ctx->xin->trace;
3286 uint64_t actset_stub[1024 / 8];
3287
3288 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
3289 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
3290 flow->in_port.ofp_port = out_dev->ofp_port;
3291 flow->metadata = htonll(0);
3292 memset(&flow->tunnel, 0, sizeof flow->tunnel);
3293 memset(&ctx->wc->masks.tunnel, 0, sizeof ctx->wc->masks.tunnel);
3294 flow->tunnel.metadata.tab =
3295 ofproto_get_tun_tab(&out_dev->xbridge->ofproto->up);
3296 ctx->wc->masks.tunnel.metadata.tab = flow->tunnel.metadata.tab;
3297 memset(flow->regs, 0, sizeof flow->regs);
3298 flow->actset_output = OFPP_UNSET;
3299 ctx->conntracked = false;
3300 clear_conntrack(ctx);
3301 ctx->xin->trace = xlate_report(ctx, OFT_BRIDGE,
3302 "bridge(\"%s\")",
3303 out_dev->xbridge->name);
3304 mirror_mask_t old_mirrors = ctx->mirrors;
3305 bool independent_mirrors = out_dev->xbridge != ctx->xbridge;
3306 if (independent_mirrors) {
3307 ctx->mirrors = 0;
3308 }
3309 ctx->xbridge = out_dev->xbridge;
3310
3311 /* The bridge is now known so obtain its table version. */
3312 ctx->xin->tables_version
3313 = ofproto_dpif_get_tables_version(ctx->xbridge->ofproto);
3314
3315 if (!process_special(ctx, out_dev) && may_receive(out_dev, ctx)) {
3316 if (xport_stp_forward_state(out_dev) &&
3317 xport_rstp_forward_state(out_dev)) {
3318 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true,
3319 false);
3320 if (!ctx->freezing) {
3321 xlate_action_set(ctx);
3322 }
3323 if (ctx->freezing) {
3324 finish_freezing(ctx);
3325 }
3326 } else {
3327 /* Forwarding is disabled by STP and RSTP. Let OFPP_NORMAL and
3328 * the learning action look at the packet, then drop it. */
3329 struct flow old_base_flow = ctx->base_flow;
3330 size_t old_size = ctx->odp_actions->size;
3331 mirror_mask_t old_mirrors2 = ctx->mirrors;
3332
3333 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true, true,
3334 false);
3335 ctx->mirrors = old_mirrors2;
3336 ctx->base_flow = old_base_flow;
3337 ctx->odp_actions->size = old_size;
3338
3339 /* Undo changes that may have been done for freezing. */
3340 ctx_cancel_freeze(ctx);
3341 }
3342 }
3343
3344 ctx->xin->trace = old_trace;
3345 if (independent_mirrors) {
3346 ctx->mirrors = old_mirrors;
3347 }
3348 ctx->xin->flow = old_flow;
3349 ctx->xbridge = in_dev->xbridge;
3350 ofpbuf_uninit(&ctx->action_set);
3351 ctx->action_set = old_action_set;
3352 ofpbuf_uninit(&ctx->stack);
3353 ctx->stack = old_stack;
3354
3355 /* Restore calling bridge's lookup version. */
3356 ctx->xin->tables_version = old_version;
3357
3358 /* Restore to calling bridge tunneling information */
3359 ctx->wc->masks.tunnel = old_flow_tnl_wc;
3360
3361 /* The out bridge popping MPLS should have no effect on the original
3362 * bridge. */
3363 ctx->was_mpls = old_was_mpls;
3364
3365 /* The out bridge's conntrack execution should have no effect on the
3366 * original bridge. */
3367 ctx->conntracked = old_conntrack;
3368
3369 /* The fact that the out bridge exits (for any reason) does not mean
3370 * that the original bridge should exit. Specifically, if the out
3371 * bridge freezes translation, the original bridge must continue
3372 * processing with the original, not the frozen packet! */
3373 ctx->exit = false;
3374
3375 /* Out bridge errors do not propagate back. */
3376 ctx->error = XLATE_OK;
3377
3378 if (ctx->xin->resubmit_stats) {
3379 netdev_vport_inc_tx(in_dev->netdev, ctx->xin->resubmit_stats);
3380 netdev_vport_inc_rx(out_dev->netdev, ctx->xin->resubmit_stats);
3381 if (out_dev->bfd) {
3382 bfd_account_rx(out_dev->bfd, ctx->xin->resubmit_stats);
3383 }
3384 }
3385 if (ctx->xin->xcache) {
3386 struct xc_entry *entry;
3387 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
3388 entry->dev.tx = netdev_ref(in_dev->netdev);
3389 entry->dev.rx = netdev_ref(out_dev->netdev);
3390 entry->dev.bfd = bfd_ref(out_dev->bfd);
3391 }
3392}
3393
9583bc14 3394static void
4e022ec0 3395compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
e93ef1c7 3396 const struct xlate_bond_recirc *xr, bool check_stp)
9583bc14 3397{
46c88433 3398 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
49a73e0c 3399 struct flow_wildcards *wc = ctx->wc;
33bf9176 3400 struct flow *flow = &ctx->xin->flow;
a36de779 3401 struct flow_tnl flow_tnl;
f0fb825a 3402 union flow_vlan_hdr flow_vlans[FLOW_MAX_VLAN_HEADERS];
9583bc14 3403 uint8_t flow_nw_tos;
4e022ec0 3404 odp_port_t out_port, odp_port;
a36de779 3405 bool tnl_push_pop_send = false;
ca077186 3406 uint8_t dscp;
9583bc14
EJ
3407
3408 /* If 'struct flow' gets additional metadata, we'll need to zero it out
3409 * before traversing a patch port. */
2482b0b0 3410 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
a36de779 3411 memset(&flow_tnl, 0, sizeof flow_tnl);
9583bc14 3412
46c88433 3413 if (!xport) {
2d9b49dd 3414 xlate_report(ctx, OFT_WARN, "Nonexistent output port");
9583bc14 3415 return;
46c88433 3416 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
2d9b49dd 3417 xlate_report(ctx, OFT_DETAIL, "OFPPC_NO_FWD set, skipping output");
9583bc14 3418 return;
1356dbd1 3419 } else if (ctx->mirror_snaplen != 0 && xport->odp_port == ODPP_NONE) {
2d9b49dd
BP
3420 xlate_report(ctx, OFT_WARN,
3421 "Mirror truncate to ODPP_NONE, skipping output");
1356dbd1 3422 return;
58d636ee 3423 } else if (xlate_flow_is_protected(ctx, flow, xport)) {
2d9b49dd
BP
3424 xlate_report(ctx, OFT_WARN,
3425 "Flow is between protected ports, skipping output.");
58d636ee 3426 return;
0d1cee12 3427 } else if (check_stp) {
bbbca389 3428 if (is_stp(&ctx->base_flow)) {
9efd308e
DV
3429 if (!xport_stp_should_forward_bpdu(xport) &&
3430 !xport_rstp_should_manage_bpdu(xport)) {
3431 if (ctx->xbridge->stp != NULL) {
2d9b49dd
BP
3432 xlate_report(ctx, OFT_WARN,
3433 "STP not in listening state, "
3434 "skipping bpdu output");
9efd308e 3435 } else if (ctx->xbridge->rstp != NULL) {
2d9b49dd
BP
3436 xlate_report(ctx, OFT_WARN,
3437 "RSTP not managing BPDU in this state, "
3438 "skipping bpdu output");
9efd308e 3439 }
0d1cee12
K
3440 return;
3441 }
67818616
MV
3442 } else if ((xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc))
3443 || (xport->bfd && bfd_should_process_flow(xport->bfd, flow,
3444 wc))) {
3445 /* Pass; STP should not block link health detection. */
9efd308e
DV
3446 } else if (!xport_stp_forward_state(xport) ||
3447 !xport_rstp_forward_state(xport)) {
3448 if (ctx->xbridge->stp != NULL) {
2d9b49dd
BP
3449 xlate_report(ctx, OFT_WARN,
3450 "STP not in forwarding state, skipping output");
9efd308e 3451 } else if (ctx->xbridge->rstp != NULL) {
2d9b49dd
BP
3452 xlate_report(ctx, OFT_WARN,
3453 "RSTP not in forwarding state, skipping output");
9efd308e 3454 }
0d1cee12
K
3455 return;
3456 }
9583bc14
EJ
3457 }
3458
46c88433 3459 if (xport->peer) {
f1dac512
SC
3460 apply_nested_clone_actions(ctx, xport, xport->peer);
3461 return;
9583bc14
EJ
3462 }
3463
f0fb825a 3464 memcpy(flow_vlans, flow->vlans, sizeof flow_vlans);
33bf9176 3465 flow_nw_tos = flow->nw_tos;
9583bc14 3466
16194afd
DDP
3467 if (count_skb_priorities(xport)) {
3468 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3469 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
3470 wc->masks.nw_tos |= IP_DSCP_MASK;
3471 flow->nw_tos &= ~IP_DSCP_MASK;
3472 flow->nw_tos |= dscp;
3473 }
9583bc14
EJ
3474 }
3475
46c88433 3476 if (xport->is_tunnel) {
c2b878e0 3477 struct in6_addr dst;
9583bc14
EJ
3478 /* Save tunnel metadata so that changes made due to
3479 * the Logical (tunnel) Port are not visible for any further
3480 * matches, while explicit set actions on tunnel metadata are.
3481 */
a36de779 3482 flow_tnl = flow->tunnel;
49a73e0c 3483 odp_port = tnl_port_send(xport->ofport, flow, ctx->wc);
4e022ec0 3484 if (odp_port == ODPP_NONE) {
2d9b49dd 3485 xlate_report(ctx, OFT_WARN, "Tunneling decided against output");
9583bc14
EJ
3486 goto out; /* restore flow_nw_tos */
3487 }
c2b878e0
TLSC
3488 dst = flow_tnl_dst(&flow->tunnel);
3489 if (ipv6_addr_equals(&dst, &ctx->orig_tunnel_ipv6_dst)) {
2d9b49dd 3490 xlate_report(ctx, OFT_WARN, "Not tunneling to our own address");
9583bc14
EJ
3491 goto out; /* restore flow_nw_tos */
3492 }
3493 if (ctx->xin->resubmit_stats) {
46c88433 3494 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
9583bc14 3495 }
b256dc52
JS
3496 if (ctx->xin->xcache) {
3497 struct xc_entry *entry;
3498
3499 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_NETDEV);
901a517e 3500 entry->dev.tx = netdev_ref(xport->netdev);
b256dc52 3501 }
9583bc14 3502 out_port = odp_port;
a36de779 3503 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
2d9b49dd 3504 xlate_report(ctx, OFT_DETAIL, "output to native tunnel");
a36de779
PS
3505 tnl_push_pop_send = true;
3506 } else {
2d9b49dd 3507 xlate_report(ctx, OFT_DETAIL, "output to kernel tunnel");
1520ef4f 3508 commit_odp_tunnel_action(flow, &ctx->base_flow, ctx->odp_actions);
a36de779
PS
3509 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3510 }
9583bc14 3511 } else {
46c88433 3512 odp_port = xport->odp_port;
7614e5d0 3513 out_port = odp_port;
9583bc14 3514 }
9583bc14 3515
4e022ec0 3516 if (out_port != ODPP_NONE) {
704bb0bf 3517 xlate_commit_actions(ctx);
adcf00ba 3518
e93ef1c7 3519 if (xr) {
347bf289 3520 struct ovs_action_hash *act_hash;
adcf00ba 3521
347bf289 3522 /* Hash action. */
1520ef4f 3523 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
347bf289
AZ
3524 OVS_ACTION_ATTR_HASH,
3525 sizeof *act_hash);
3526 act_hash->hash_alg = xr->hash_alg;
62ac1f20 3527 act_hash->hash_basis = xr->hash_basis;
347bf289
AZ
3528
3529 /* Recirc action. */
1520ef4f 3530 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC,
347bf289 3531 xr->recirc_id);
adcf00ba 3532 } else {
a36de779
PS
3533
3534 if (tnl_push_pop_send) {
3535 build_tunnel_send(ctx, xport, flow, odp_port);
3536 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
3537 } else {
3538 odp_port_t odp_tnl_port = ODPP_NONE;
3539
3540 /* XXX: Write better Filter for tunnel port. We can use inport
3541 * int tunnel-port flow to avoid these checks completely. */
3542 if (ofp_port == OFPP_LOCAL &&
3543 ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
3544
3545 odp_tnl_port = tnl_port_map_lookup(flow, wc);
3546 }
3547
3548 if (odp_tnl_port != ODPP_NONE) {
1520ef4f 3549 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3550 OVS_ACTION_ATTR_TUNNEL_POP,
3551 odp_tnl_port);
3552 } else {
3553 /* Tunnel push-pop action is not compatible with
3554 * IPFIX action. */
a6092018 3555 compose_ipfix_action(ctx, out_port);
1356dbd1
WT
3556
3557 /* Handle truncation of the mirrored packet. */
3558 if (ctx->mirror_snaplen > 0 &&
3559 ctx->mirror_snaplen < UINT16_MAX) {
3560 struct ovs_action_trunc *trunc;
3561
3562 trunc = nl_msg_put_unspec_uninit(ctx->odp_actions,
3563 OVS_ACTION_ATTR_TRUNC,
3564 sizeof *trunc);
3565 trunc->max_len = ctx->mirror_snaplen;
3566 if (!ctx->xbridge->support.trunc) {
3567 ctx->xout->slow |= SLOW_ACTION;
3568 }
3569 }
3570
1520ef4f 3571 nl_msg_put_odp_port(ctx->odp_actions,
a36de779
PS
3572 OVS_ACTION_ATTR_OUTPUT,
3573 out_port);
1356dbd1
WT
3574 }
3575 }
adcf00ba 3576 }
9583bc14 3577
6cbbf4fa
EJ
3578 ctx->sflow_odp_port = odp_port;
3579 ctx->sflow_n_outputs++;
2031ef97 3580 ctx->nf_output_iface = ofp_port;
6cbbf4fa
EJ
3581 }
3582
7efbc3b7
BP
3583 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
3584 mirror_packet(ctx, xport->xbundle,
3585 xbundle_mirror_dst(xport->xbundle->xbridge,
3586 xport->xbundle));
3587 }
3588
6cbbf4fa 3589 out:
9583bc14 3590 /* Restore flow */
f0fb825a 3591 memcpy(flow->vlans, flow_vlans, sizeof flow->vlans);
33bf9176 3592 flow->nw_tos = flow_nw_tos;
9583bc14
EJ
3593}
3594
3595static void
e93ef1c7
JR
3596compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port,
3597 const struct xlate_bond_recirc *xr)
9583bc14 3598{
e93ef1c7 3599 compose_output_action__(ctx, ofp_port, xr, true);
9583bc14
EJ
3600}
3601
bb61b33d 3602static void
790c5d26 3603xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule, bool deepens)
bb61b33d
BP
3604{
3605 struct rule_dpif *old_rule = ctx->rule;
8b1e5560 3606 ovs_be64 old_cookie = ctx->rule_cookie;
dc723c44 3607 const struct rule_actions *actions;
bb61b33d
BP
3608
3609 if (ctx->xin->resubmit_stats) {
70742c7f 3610 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
bb61b33d
BP
3611 }
3612
98b07853 3613 ctx->resubmits++;
790c5d26 3614
790c5d26 3615 ctx->depth += deepens;
bb61b33d 3616 ctx->rule = rule;
07a3cd5c
BP
3617 ctx->rule_cookie = rule->up.flow_cookie;
3618 actions = rule_get_actions(&rule->up);
6f00e29b 3619 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
8b1e5560 3620 ctx->rule_cookie = old_cookie;
bb61b33d 3621 ctx->rule = old_rule;
790c5d26 3622 ctx->depth -= deepens;
bb61b33d
BP
3623}
3624
bd3240ba
SH
3625static bool
3626xlate_resubmit_resource_check(struct xlate_ctx *ctx)
9583bc14 3627{
790c5d26 3628 if (ctx->depth >= MAX_DEPTH) {
2d9b49dd 3629 xlate_report_error(ctx, "over max translation depth %d", MAX_DEPTH);
fff1b9c0 3630 ctx->error = XLATE_RECURSION_TOO_DEEP;
790c5d26 3631 } else if (ctx->resubmits >= MAX_RESUBMITS) {
2d9b49dd 3632 xlate_report_error(ctx, "over %d resubmit actions", MAX_RESUBMITS);
fff1b9c0 3633 ctx->error = XLATE_TOO_MANY_RESUBMITS;
1520ef4f 3634 } else if (ctx->odp_actions->size > UINT16_MAX) {
2d9b49dd 3635 xlate_report_error(ctx, "resubmits yielded over 64 kB of actions");
fff1b9c0
JR
3636 /* NOT an error, as we'll be slow-pathing the flow in this case? */
3637 ctx->exit = true; /* XXX: translation still terminated! */
6fd6ed71 3638 } else if (ctx->stack.size >= 65536) {
2d9b49dd 3639 xlate_report_error(ctx, "resubmits yielded over 64 kB of stack");
fff1b9c0 3640 ctx->error = XLATE_STACK_TOO_DEEP;
98b07853 3641 } else {
bd3240ba
SH
3642 return true;
3643 }
3644
3645 return false;
3646}
3647
2cd20955
JR
3648static void
3649tuple_swap_flow(struct flow *flow, bool ipv4)
3650{
3651 uint8_t nw_proto = flow->nw_proto;
3652 flow->nw_proto = flow->ct_nw_proto;
3653 flow->ct_nw_proto = nw_proto;
3654
3655 if (ipv4) {
3656 ovs_be32 nw_src = flow->nw_src;
3657 flow->nw_src = flow->ct_nw_src;
3658 flow->ct_nw_src = nw_src;
3659
3660 ovs_be32 nw_dst = flow->nw_dst;
3661 flow->nw_dst = flow->ct_nw_dst;
3662 flow->ct_nw_dst = nw_dst;
3663 } else {
3664 struct in6_addr ipv6_src = flow->ipv6_src;
3665 flow->ipv6_src = flow->ct_ipv6_src;
3666 flow->ct_ipv6_src = ipv6_src;
3667
3668 struct in6_addr ipv6_dst = flow->ipv6_dst;
3669 flow->ipv6_dst = flow->ct_ipv6_dst;
3670 flow->ct_ipv6_dst = ipv6_dst;
3671 }
3672
3673 ovs_be16 tp_src = flow->tp_src;
3674 flow->tp_src = flow->ct_tp_src;
3675 flow->ct_tp_src = tp_src;
3676
3677 ovs_be16 tp_dst = flow->tp_dst;
3678 flow->tp_dst = flow->ct_tp_dst;
3679 flow->ct_tp_dst = tp_dst;
3680}
3681
3682static void
3683tuple_swap(struct flow *flow, struct flow_wildcards *wc)
3684{
3685 bool ipv4 = (flow->dl_type == htons(ETH_TYPE_IP));
3686
3687 tuple_swap_flow(flow, ipv4);
3688 tuple_swap_flow(&wc->masks, ipv4);
3689}
3690
bd3240ba 3691static void
6d328fa2 3692xlate_table_action(struct xlate_ctx *ctx, ofp_port_t in_port, uint8_t table_id,
2cd20955
JR
3693 bool may_packet_in, bool honor_table_miss,
3694 bool with_ct_orig)
bd3240ba 3695{
e12ec36b
SH
3696 /* Check if we need to recirculate before matching in a table. */
3697 if (ctx->was_mpls) {
3698 ctx_trigger_freeze(ctx);
3699 return;
3700 }
bd3240ba 3701 if (xlate_resubmit_resource_check(ctx)) {
9583bc14 3702 uint8_t old_table_id = ctx->table_id;
3f207910 3703 struct rule_dpif *rule;
9583bc14
EJ
3704
3705 ctx->table_id = table_id;
3706
2cd20955
JR
3707 /* Swap packet fields with CT 5-tuple if requested. */
3708 if (with_ct_orig) {
3709 /* Do not swap if there is no CT tuple, or if key is not IP. */
3710 if (ctx->xin->flow.ct_nw_proto == 0 ||
3711 !is_ip_any(&ctx->xin->flow)) {
3712 xlate_report_error(ctx,
3713 "resubmit(ct) with non-tracked or non-IP packet!");
3714 return;
3715 }
3716 tuple_swap(&ctx->xin->flow, ctx->wc);
3717 }
34dd0d78 3718 rule = rule_dpif_lookup_from_table(ctx->xbridge->ofproto,
1f4a8933 3719 ctx->xin->tables_version,
c0e638aa 3720 &ctx->xin->flow, ctx->wc,
34dd0d78
JR
3721 ctx->xin->resubmit_stats,
3722 &ctx->table_id, in_port,
a027899e
JR
3723 may_packet_in, honor_table_miss,
3724 ctx->xin->xcache);
2cd20955
JR
3725 /* Swap back. */
3726 if (with_ct_orig) {
3727 tuple_swap(&ctx->xin->flow, ctx->wc);
3728 }
ad3efdcb 3729
a2143702 3730 if (rule) {
83709dfa
JR
3731 /* Fill in the cache entry here instead of xlate_recursively
3732 * to make the reference counting more explicit. We take a
3733 * reference in the lookups above if we are going to cache the
3734 * rule. */
3735 if (ctx->xin->xcache) {
3736 struct xc_entry *entry;
3737
3738 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_RULE);
901a517e 3739 entry->rule = rule;
07a3cd5c 3740 ofproto_rule_ref(&rule->up);
83709dfa 3741 }
2d9b49dd
BP
3742
3743 struct ovs_list *old_trace = ctx->xin->trace;
3744 xlate_report_table(ctx, rule, table_id);
790c5d26 3745 xlate_recursively(ctx, rule, table_id <= old_table_id);
2d9b49dd 3746 ctx->xin->trace = old_trace;
ad3efdcb
EJ
3747 }
3748
9583bc14 3749 ctx->table_id = old_table_id;
98b07853 3750 return;
9583bc14
EJ
3751 }
3752}
3753
76973237 3754/* Consumes the group reference, which is only taken if xcache exists. */
f4fb341b 3755static void
1e684d7d
RW
3756xlate_group_stats(struct xlate_ctx *ctx, struct group_dpif *group,
3757 struct ofputil_bucket *bucket)
3758{
3759 if (ctx->xin->resubmit_stats) {
3760 group_dpif_credit_stats(group, bucket, ctx->xin->resubmit_stats);
3761 }
3762 if (ctx->xin->xcache) {
3763 struct xc_entry *entry;
3764
3765 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_GROUP);
901a517e
JR
3766 entry->group.group = group;
3767 entry->group.bucket = bucket;
1e684d7d
RW
3768 }
3769}
3770
3771static void
3772xlate_group_bucket(struct xlate_ctx *ctx, struct ofputil_bucket *bucket)
f4fb341b
SH
3773{
3774 uint64_t action_list_stub[1024 / 8];
0a2869d5
BP
3775 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
3776 struct ofpbuf action_set = ofpbuf_const_initializer(bucket->ofpacts,
3777 bucket->ofpacts_len);
5b09e569 3778 struct flow old_flow = ctx->xin->flow;
e12ec36b 3779 bool old_was_mpls = ctx->was_mpls;
f4fb341b 3780
f4fb341b 3781 ofpacts_execute_action_set(&action_list, &action_set);
790c5d26 3782 ctx->depth++;
6fd6ed71 3783 do_xlate_actions(action_list.data, action_list.size, ctx);
790c5d26 3784 ctx->depth--;
f4fb341b 3785
f4fb341b 3786 ofpbuf_uninit(&action_list);
5b09e569 3787
77ab5fd2 3788 /* Check if need to freeze. */
1d361a81 3789 if (ctx->freezing) {
77ab5fd2 3790 finish_freezing(ctx);
e672ff9b
JR
3791 }
3792
5b09e569
JR
3793 /* Roll back flow to previous state.
3794 * This is equivalent to cloning the packet for each bucket.
3795 *
3796 * As a side effect any subsequently applied actions will
3797 * also effectively be applied to a clone of the packet taken
3798 * just before applying the all or indirect group.
3799 *
3800 * Note that group buckets are action sets, hence they cannot modify the
3801 * main action set. Also any stack actions are ignored when executing an
3802 * action set, so group buckets cannot change the stack either.
3803 * However, we do allow resubmit actions in group buckets, which could
3804 * break the above assumptions. It is up to the controller to not mess up
3805 * with the action_set and stack in the tables resubmitted to from
3806 * group buckets. */
3807 ctx->xin->flow = old_flow;
3808
e12ec36b
SH
3809 /* The group bucket popping MPLS should have no effect after bucket
3810 * execution. */
3811 ctx->was_mpls = old_was_mpls;
3812
5b09e569
JR
3813 /* The fact that the group bucket exits (for any reason) does not mean that
3814 * the translation after the group action should exit. Specifically, if
1d361a81
BP
3815 * the group bucket freezes translation, the actions after the group action
3816 * must continue processing with the original, not the frozen packet! */
5b09e569 3817 ctx->exit = false;
f4fb341b
SH
3818}
3819
3820static void
3821xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
3822{
1e684d7d 3823 struct ofputil_bucket *bucket;
07a3cd5c 3824 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
f4fb341b 3825 xlate_group_bucket(ctx, bucket);
f4fb341b 3826 }
1e684d7d 3827 xlate_group_stats(ctx, group, NULL);
f4fb341b
SH
3828}
3829
dd8cd4b4
SH
3830static void
3831xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
3832{
1e684d7d 3833 struct ofputil_bucket *bucket;
dd8cd4b4
SH
3834
3835 bucket = group_first_live_bucket(ctx, group, 0);
3836 if (bucket) {
3837 xlate_group_bucket(ctx, bucket);
1e684d7d 3838 xlate_group_stats(ctx, group, bucket);
76973237 3839 } else if (ctx->xin->xcache) {
07a3cd5c 3840 ofproto_group_unref(&group->up);
dd8cd4b4
SH
3841 }
3842}
3843
fe7e5749 3844static void
7565c3e4 3845xlate_default_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
fe7e5749 3846{
49a73e0c 3847 struct flow_wildcards *wc = ctx->wc;
1e684d7d 3848 struct ofputil_bucket *bucket;
fe7e5749
SH
3849 uint32_t basis;
3850
1d1aae0b 3851 basis = flow_hash_symmetric_l4(&ctx->xin->flow, 0);
80e3509d 3852 flow_mask_hash_fields(&ctx->xin->flow, wc, NX_HASH_FIELDS_SYMMETRIC_L4);
fe7e5749
SH
3853 bucket = group_best_live_bucket(ctx, group, basis);
3854 if (bucket) {
fe7e5749 3855 xlate_group_bucket(ctx, bucket);
1e684d7d 3856 xlate_group_stats(ctx, group, bucket);
76973237 3857 } else if (ctx->xin->xcache) {
07a3cd5c 3858 ofproto_group_unref(&group->up);
fe7e5749
SH
3859 }
3860}
3861
0c4b9393
SH
3862static void
3863xlate_hash_fields_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3864{
07a3cd5c
BP
3865 const struct field_array *fields = &group->up.props.fields;
3866 const uint8_t *mask_values = fields->values;
3867 uint32_t basis = hash_uint64(group->up.props.selection_method_param);
0c4b9393 3868
07a3cd5c 3869 size_t i;
e8dba719
JR
3870 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, fields->used.bm) {
3871 const struct mf_field *mf = mf_from_id(i);
0c4b9393 3872
e8dba719
JR
3873 /* Skip fields for which prerequisities are not met. */
3874 if (!mf_are_prereqs_ok(mf, &ctx->xin->flow, ctx->wc)) {
3875 /* Skip the mask bytes for this field. */
3876 mask_values += mf->n_bytes;
3877 continue;
3878 }
0c4b9393 3879
e8dba719
JR
3880 union mf_value value;
3881 union mf_value mask;
0c4b9393 3882
e8dba719
JR
3883 mf_get_value(mf, &ctx->xin->flow, &value);
3884 /* Mask the value. */
3885 for (int j = 0; j < mf->n_bytes; j++) {
3886 mask.b[j] = *mask_values++;
3887 value.b[j] &= mask.b[j];
3888 }
3889 basis = hash_bytes(&value, mf->n_bytes, basis);
1cb20095 3890
e8dba719
JR
3891 /* For tunnels, hash in whether the field is present. */
3892 if (mf_is_tun_metadata(mf)) {
3893 basis = hash_boolean(mf_is_set(mf, &ctx->xin->flow), basis);
0c4b9393 3894 }
e8dba719
JR
3895
3896 mf_mask_field_masked(mf, &mask, ctx->wc);
0c4b9393
SH
3897 }
3898
07a3cd5c 3899 struct ofputil_bucket *bucket = group_best_live_bucket(ctx, group, basis);
0c4b9393
SH
3900 if (bucket) {
3901 xlate_group_bucket(ctx, bucket);
3902 xlate_group_stats(ctx, group, bucket);
76973237 3903 } else if (ctx->xin->xcache) {
07a3cd5c 3904 ofproto_group_unref(&group->up);
0c4b9393
SH
3905 }
3906}
3907
53cc166a
JR
3908static void
3909xlate_dp_hash_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3910{
3911 struct ofputil_bucket *bucket;
3912
3913 /* dp_hash value 0 is special since it means that the dp_hash has not been
3914 * computed, as all computed dp_hash values are non-zero. Therefore
3915 * compare to zero can be used to decide if the dp_hash value is valid
3916 * without masking the dp_hash field. */
3917 if (!ctx->xin->flow.dp_hash) {
07a3cd5c 3918 uint64_t param = group->up.props.selection_method_param;
53cc166a
JR
3919
3920 ctx_trigger_recirculate_with_hash(ctx, param >> 32, (uint32_t)param);
3921 } else {
07a3cd5c 3922 uint32_t n_buckets = group->up.n_buckets;
53cc166a
JR
3923 if (n_buckets) {
3924 /* Minimal mask to cover the number of buckets. */
3925 uint32_t mask = (1 << log_2_ceil(n_buckets)) - 1;
3926 /* Multiplier chosen to make the trivial 1 bit case to
3927 * actually distribute amongst two equal weight buckets. */
3928 uint32_t basis = 0xc2b73583 * (ctx->xin->flow.dp_hash & mask);
3929
3930 ctx->wc->masks.dp_hash |= mask;
3931 bucket = group_best_live_bucket(ctx, group, basis);
3932 if (bucket) {
3933 xlate_group_bucket(ctx, bucket);
3934 xlate_group_stats(ctx, group, bucket);
3935 }
3936 }
3937 }
3938}
3939
7565c3e4
SH
3940static void
3941xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
3942{
07a3cd5c 3943 const char *selection_method = group->up.props.selection_method;
7565c3e4 3944
e12ec36b
SH
3945 /* Select groups may access flow keys beyond L2 in order to
3946 * select a bucket. Recirculate as appropriate to make this possible.
3947 */
3948 if (ctx->was_mpls) {
3949 ctx_trigger_freeze(ctx);
3950 }
3951
7565c3e4
SH
3952 if (selection_method[0] == '\0') {
3953 xlate_default_select_group(ctx, group);
0c4b9393
SH
3954 } else if (!strcasecmp("hash", selection_method)) {
3955 xlate_hash_fields_select_group(ctx, group);
53cc166a
JR
3956 } else if (!strcasecmp("dp_hash", selection_method)) {
3957 xlate_dp_hash_select_group(ctx, group);
7565c3e4
SH
3958 } else {
3959 /* Parsing of groups should ensure this never happens */
3960 OVS_NOT_REACHED();
3961 }
3962}
3963
f4fb341b
SH
3964static void
3965xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
3966{
0eb48fe1 3967 bool was_in_group = ctx->in_group;
5a070238
BP
3968 ctx->in_group = true;
3969
07a3cd5c 3970 switch (group->up.type) {
f4fb341b
SH
3971 case OFPGT11_ALL:
3972 case OFPGT11_INDIRECT:
3973 xlate_all_group(ctx, group);
3974 break;
3975 case OFPGT11_SELECT:
fe7e5749 3976 xlate_select_group(ctx, group);
f4fb341b 3977 break;
dd8cd4b4
SH
3978 case OFPGT11_FF:
3979 xlate_ff_group(ctx, group);
3980 break;
f4fb341b 3981 default:
428b2edd 3982 OVS_NOT_REACHED();
f4fb341b 3983 }
5a070238 3984
0eb48fe1 3985 ctx->in_group = was_in_group;
f4fb341b
SH
3986}
3987
3988static bool
3989xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
3990{
0eb48fe1 3991 if (xlate_resubmit_resource_check(ctx)) {
f4fb341b 3992 struct group_dpif *group;
f4fb341b 3993
76973237
JR
3994 /* Take ref only if xcache exists. */
3995 group = group_dpif_lookup(ctx->xbridge->ofproto, group_id,
1f4a8933 3996 ctx->xin->tables_version, ctx->xin->xcache);
db88b35c
JR
3997 if (!group) {
3998 /* XXX: Should set ctx->error ? */
2d9b49dd
BP
3999 xlate_report(ctx, OFT_WARN, "output to nonexistent group %"PRIu32,
4000 group_id);
f4fb341b
SH
4001 return true;
4002 }
db88b35c 4003 xlate_group_action__(ctx, group);
f4fb341b
SH
4004 }
4005
4006 return false;
4007}
4008
9583bc14
EJ
4009static void
4010xlate_ofpact_resubmit(struct xlate_ctx *ctx,
4011 const struct ofpact_resubmit *resubmit)
4012{
4e022ec0 4013 ofp_port_t in_port;
9583bc14 4014 uint8_t table_id;
adcf00ba
AZ
4015 bool may_packet_in = false;
4016 bool honor_table_miss = false;
4017
4018 if (ctx->rule && rule_dpif_is_internal(ctx->rule)) {
4019 /* Still allow missed packets to be sent to the controller
4020 * if resubmitting from an internal table. */
4021 may_packet_in = true;
4022 honor_table_miss = true;
4023 }
9583bc14
EJ
4024
4025 in_port = resubmit->in_port;
4026 if (in_port == OFPP_IN_PORT) {
4e022ec0 4027 in_port = ctx->xin->flow.in_port.ofp_port;
9583bc14
EJ
4028 }
4029
4030 table_id = resubmit->table_id;
4031 if (table_id == 255) {
4032 table_id = ctx->table_id;
4033 }
4034
adcf00ba 4035 xlate_table_action(ctx, in_port, table_id, may_packet_in,
2cd20955 4036 honor_table_miss, resubmit->with_ct_orig);
9583bc14
EJ
4037}
4038
4039static void
4040flood_packets(struct xlate_ctx *ctx, bool all)
4041{
46c88433 4042 const struct xport *xport;
9583bc14 4043
46c88433
EJ
4044 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
4045 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
4046 continue;
4047 }
4048
4049 if (all) {
e93ef1c7 4050 compose_output_action__(ctx, xport->ofp_port, NULL, false);
46c88433 4051 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
e93ef1c7 4052 compose_output_action(ctx, xport->ofp_port, NULL);
9583bc14
EJ
4053 }
4054 }
4055
2031ef97 4056 ctx->nf_output_iface = NF_OUT_FLOOD;
9583bc14
EJ
4057}
4058
27d931da
AZ
4059/* Copy and reformat a partially xlated odp actions to a new
4060 * odp actions list in 'b', so that the new actions list
4061 * can be executed by odp_execute_actions.
4062 *
4063 * When xlate using nested odp actions, such as sample and clone,
4064 * the nested action created by nl_msg_start_nested() may not
4065 * have been properly closed yet, thus can not be executed
4066 * directly.
4067 *
4068 * Since unclosed nested action has to be last action, it can be
4069 * fixed by skipping the outer header, and treating the actions within
4070 * as if they are outside the nested attribute since the effect
4071 * of executing them on packet is the same.
4072 *
4073 * As an optimization, a fully closed 'sample' or 'clone' action
4074 * is skipped since their execution has no effect to the packet.
4075 *
4076 * Returns true if success. 'b' contains the new actions list.
4077 * The caller is responsible for disposing 'b'.
4078 *
4079 * Returns false if error, 'b' has been freed already. */
4080static bool
4081xlate_fixup_actions(struct ofpbuf *b, const struct nlattr *actions,
4082 size_t actions_len)
4083{
4084 const struct nlattr *a;
4085 unsigned int left;
4086
4087 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
4088 int type = nl_attr_type(a);
4089
4090 switch ((enum ovs_action_attr) type) {
4091 case OVS_ACTION_ATTR_HASH:
4092 case OVS_ACTION_ATTR_PUSH_VLAN:
4093 case OVS_ACTION_ATTR_POP_VLAN:
4094 case OVS_ACTION_ATTR_PUSH_MPLS:
4095 case OVS_ACTION_ATTR_POP_MPLS:
4096 case OVS_ACTION_ATTR_SET:
4097 case OVS_ACTION_ATTR_SET_MASKED:
4098 case OVS_ACTION_ATTR_TRUNC:
4099 case OVS_ACTION_ATTR_OUTPUT:
4100 case OVS_ACTION_ATTR_TUNNEL_PUSH:
4101 case OVS_ACTION_ATTR_TUNNEL_POP:
4102 case OVS_ACTION_ATTR_USERSPACE:
4103 case OVS_ACTION_ATTR_RECIRC:
4104 case OVS_ACTION_ATTR_CT:
0d11fc52
JR
4105 case OVS_ACTION_ATTR_PUSH_ETH:
4106 case OVS_ACTION_ATTR_POP_ETH:
5dddf960 4107 case OVS_ACTION_ATTR_METER:
27d931da
AZ
4108 ofpbuf_put(b, a, nl_attr_len_pad(a, left));
4109 break;
4110
4111 case OVS_ACTION_ATTR_CLONE:
4112 /* If the clone action has been fully xlated, it can
4113 * be skipped, since any actions executed within clone
4114 * do not affect the current packet.
4115 *
4116 * When xlating actions within clone, the clone action,
4117 * because it is an nested netlink attribute, do not have
4118 * a valid 'nla_len'; it will be zero instead. Skip
4119 * the clone header to find the start of the actions
4120 * enclosed. Treat those actions as if they are written
4121 * outside of clone. */
4122 if (!a->nla_len) {
4123 bool ok;
4124 if (left < NLA_HDRLEN) {
4125 goto error;
4126 }
4127
4128 ok = xlate_fixup_actions(b, nl_attr_get_unspec(a, 0),
4129 left - NLA_HDRLEN);
4130 if (!ok) {
4131 goto error;
4132 }
4133 }
4134 break;
4135
4136 case OVS_ACTION_ATTR_SAMPLE:
4137 if (!a->nla_len) {
4138 bool ok;
4139 if (left < NLA_HDRLEN) {
4140 goto error;
4141 }
4142 const struct nlattr *attr = nl_attr_get_unspec(a, 0);
4143 left -= NLA_HDRLEN;
4144
4145 while (left > 0 &&
4146 nl_attr_type(attr) != OVS_SAMPLE_ATTR_ACTIONS) {
4147 /* Only OVS_SAMPLE_ATTR_ACTIONS can have unclosed
4148 * nested netlink attribute. */
4149 if (!attr->nla_len) {
4150 goto error;
4151 }
4152
4153 left -= NLA_ALIGN(attr->nla_len);
4154 attr = nl_attr_next(attr);
4155 }
4156
4157 if (left < NLA_HDRLEN) {
4158 goto error;
4159 }
4160
4161 ok = xlate_fixup_actions(b, nl_attr_get_unspec(attr, 0),
4162 left - NLA_HDRLEN);
4163 if (!ok) {
4164 goto error;
4165 }
4166 }
4167 break;
4168
4169 case OVS_ACTION_ATTR_UNSPEC:
4170 case __OVS_ACTION_ATTR_MAX:
4171 OVS_NOT_REACHED();
4172 }
4173 }
4174
4175 return true;
4176
4177error:
4178 ofpbuf_delete(b);
4179 return false;
4180}
4181
4182static bool
4183xlate_execute_odp_actions(struct dp_packet *packet,
4184 const struct nlattr *actions, int actions_len)
4185{
4186 struct dp_packet_batch batch;
4187 struct ofpbuf *b = ofpbuf_new(actions_len);
4188
4189 if (!xlate_fixup_actions(b, actions, actions_len)) {
4190 return false;
4191 }
4192
4193 dp_packet_batch_init_packet(&batch, packet);
4194 odp_execute_actions(NULL, &batch, false, b->data, b->size, NULL);
4195 ofpbuf_delete(b);
4196
4197 return true;
4198}
4199
9583bc14
EJ
4200static void
4201execute_controller_action(struct xlate_ctx *ctx, int len,
4202 enum ofp_packet_in_reason reason,
bdcad671
BP
4203 uint16_t controller_id,
4204 const uint8_t *userdata, size_t userdata_len)
9583bc14 4205{
e14deea0 4206 struct dp_packet *packet;
9583bc14 4207
04594cd5 4208 ctx->xout->slow |= SLOW_CONTROLLER;
b476e2f2 4209 xlate_commit_actions(ctx);
9583bc14
EJ
4210 if (!ctx->xin->packet) {
4211 return;
4212 }
4213
df70a773
JR
4214 if (!ctx->xin->allow_side_effects && !ctx->xin->xcache) {
4215 return;
4216 }
4217
cf62fa4c 4218 packet = dp_packet_clone(ctx->xin->packet);
27d931da
AZ
4219 if (!xlate_execute_odp_actions(packet, ctx->odp_actions->data,
4220 ctx->odp_actions->size)) {
4221 xlate_report_error(ctx, "Failed to execute controller action");
4222 dp_packet_delete(packet);
4223 return;
4224 }
9bfe9334
BP
4225 /* A packet sent by an action in a table-miss rule is considered an
4226 * explicit table miss. OpenFlow before 1.3 doesn't have that concept so
4227 * it will get translated back to OFPR_ACTION for those versions. */
4228 if (reason == OFPR_ACTION
07a3cd5c 4229 && ctx->rule && rule_is_table_miss(&ctx->rule->up)) {
9bfe9334
BP
4230 reason = OFPR_EXPLICIT_MISS;
4231 }
4232
4233 size_t packet_len = dp_packet_size(packet);
0fb7792a 4234
a2b53dec
BP
4235 struct ofproto_async_msg *am = xmalloc(sizeof *am);
4236 *am = (struct ofproto_async_msg) {
9bfe9334 4237 .controller_id = controller_id,
a2b53dec
BP
4238 .oam = OAM_PACKET_IN,
4239 .pin = {
4240 .up = {
77ab5fd2
BP
4241 .public = {
4242 .packet = dp_packet_steal_data(packet),
4243 .packet_len = packet_len,
4244 .reason = reason,
4245 .table_id = ctx->table_id,
4246 .cookie = ctx->rule_cookie,
4247 .userdata = (userdata_len
4248 ? xmemdup(userdata, userdata_len)
4249 : NULL),
4250 .userdata_len = userdata_len,
4251 }
a2b53dec
BP
4252 },
4253 .max_len = len,
9bfe9334 4254 },
9bfe9334 4255 };
77ab5fd2 4256 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
9583bc14 4257
df70a773
JR
4258 /* Async messages are only sent once, so if we send one now, no
4259 * xlate cache entry is created. */
4260 if (ctx->xin->allow_side_effects) {
4261 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
4262 } else /* xcache */ {
4263 struct xc_entry *entry;
4264
4265 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_CONTROLLER);
4266 entry->controller.ofproto = ctx->xbridge->ofproto;
4267 entry->controller.am = am;
4268 }
3b4fff43
RM
4269
4270 dp_packet_delete(packet);
9583bc14
EJ
4271}
4272
7bbdd84f 4273static void
77ab5fd2 4274emit_continuation(struct xlate_ctx *ctx, const struct frozen_state *state)
7bbdd84f 4275{
df70a773
JR
4276 if (!ctx->xin->allow_side_effects && !ctx->xin->xcache) {
4277 return;
4278 }
4279
77ab5fd2
BP
4280 struct ofproto_async_msg *am = xmalloc(sizeof *am);
4281 *am = (struct ofproto_async_msg) {
4282 .controller_id = ctx->pause->controller_id,
4283 .oam = OAM_PACKET_IN,
4284 .pin = {
4285 .up = {
4286 .public = {
4287 .userdata = xmemdup(ctx->pause->userdata,
4288 ctx->pause->userdata_len),
4289 .userdata_len = ctx->pause->userdata_len,
4290 .packet = xmemdup(dp_packet_data(ctx->xin->packet),
4291 dp_packet_size(ctx->xin->packet)),
4292 .packet_len = dp_packet_size(ctx->xin->packet),
0b024e49 4293 .reason = ctx->pause->reason,
77ab5fd2 4294 },
07a3cd5c 4295 .bridge = ctx->xbridge->ofproto->uuid,
84cf3c1f
JR
4296 .stack = xmemdup(state->stack, state->stack_size),
4297 .stack_size = state->stack_size,
77ab5fd2
BP
4298 .mirrors = state->mirrors,
4299 .conntracked = state->conntracked,
4300 .actions = xmemdup(state->ofpacts, state->ofpacts_len),
4301 .actions_len = state->ofpacts_len,
4302 .action_set = xmemdup(state->action_set,
4303 state->action_set_len),
4304 .action_set_len = state->action_set_len,
4305 },
4306 .max_len = UINT16_MAX,
4307 },
4308 };
4309 flow_get_metadata(&ctx->xin->flow, &am->pin.up.public.flow_metadata);
df70a773
JR
4310
4311 /* Async messages are only sent once, so if we send one now, no
4312 * xlate cache entry is created. */
4313 if (ctx->xin->allow_side_effects) {
4314 ofproto_dpif_send_async_msg(ctx->xbridge->ofproto, am);
4315 } else /* xcache */ {
4316 struct xc_entry *entry;
4317
4318 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_CONTROLLER);
4319 entry->controller.ofproto = ctx->xbridge->ofproto;
4320 entry->controller.am = am;
4321 }
77ab5fd2 4322}
7bbdd84f 4323
77ab5fd2
BP
4324static void
4325finish_freezing__(struct xlate_ctx *ctx, uint8_t table)
4326{
1d361a81 4327 ovs_assert(ctx->freezing);
7bbdd84f 4328
1d361a81 4329 struct frozen_state state = {
07659514 4330 .table_id = table,
07a3cd5c 4331 .ofproto_uuid = ctx->xbridge->ofproto->uuid,
5c1b2314 4332 .stack = ctx->stack.data,
84cf3c1f 4333 .stack_size = ctx->stack.size,
29bae541 4334 .mirrors = ctx->mirrors,
07659514 4335 .conntracked = ctx->conntracked,
1d361a81
BP
4336 .ofpacts = ctx->frozen_actions.data,
4337 .ofpacts_len = ctx->frozen_actions.size,
417509fa 4338 .action_set = ctx->action_set.data,
8a5fb3b4 4339 .action_set_len = ctx->action_set.size,
2082425c 4340 };
77ab5fd2 4341 frozen_metadata_from_flow(&state.metadata, &ctx->xin->flow);
2082425c 4342
77ab5fd2
BP
4343 if (ctx->pause) {
4344 if (ctx->xin->packet) {
4345 emit_continuation(ctx, &state);
4346 }
4347 } else {
4348 /* Allocate a unique recirc id for the given metadata state in the
4349 * flow. An existing id, with a new reference to the corresponding
4350 * recirculation context, will be returned if possible.
4351 * The life-cycle of this recirc id is managed by associating it
4352 * with the udpif key ('ukey') created for each new datapath flow. */
4353 uint32_t id = recirc_alloc_id_ctx(&state);
4354 if (!id) {
2d9b49dd 4355 xlate_report_error(ctx, "Failed to allocate recirculation id");
77ab5fd2
BP
4356 ctx->error = XLATE_NO_RECIRCULATION_CONTEXT;
4357 return;
4358 }
4359 recirc_refs_add(&ctx->xout->recircs, id);
7bbdd84f 4360
53cc166a
JR
4361 if (ctx->recirc_update_dp_hash) {
4362 struct ovs_action_hash *act_hash;
4363
4364 /* Hash action. */
4365 act_hash = nl_msg_put_unspec_uninit(ctx->odp_actions,
4366 OVS_ACTION_ATTR_HASH,
4367 sizeof *act_hash);
4368 act_hash->hash_alg = OVS_HASH_ALG_L4; /* Make configurable. */
4369 act_hash->hash_basis = 0; /* Make configurable. */
4370 }
77ab5fd2
BP
4371 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_RECIRC, id);
4372 }
e672ff9b 4373
1d361a81
BP
4374 /* Undo changes done by freezing. */
4375 ctx_cancel_freeze(ctx);
7bbdd84f
SH
4376}
4377
1d361a81 4378/* Called only when we're freezing. */
07659514 4379static void
77ab5fd2 4380finish_freezing(struct xlate_ctx *ctx)
07659514
JS
4381{
4382 xlate_commit_actions(ctx);
77ab5fd2 4383 finish_freezing__(ctx, 0);
07659514
JS
4384}
4385
e37b8437
JS
4386/* Fork the pipeline here. The current packet will continue processing the
4387 * current action list. A clone of the current packet will recirculate, skip
4388 * the remainder of the current action list and asynchronously resume pipeline
4389 * processing in 'table' with the current metadata and action set. */
4390static void
4391compose_recirculate_and_fork(struct xlate_ctx *ctx, uint8_t table)
4392{
1d361a81 4393 ctx->freezing = true;
77ab5fd2 4394 finish_freezing__(ctx, table);
e37b8437
JS
4395}
4396
8bfd0fda
BP
4397static void
4398compose_mpls_push_action(struct xlate_ctx *ctx, struct ofpact_push_mpls *mpls)
9583bc14 4399{
33bf9176 4400 struct flow *flow = &ctx->xin->flow;
8bfd0fda 4401 int n;
33bf9176 4402
8bfd0fda 4403 ovs_assert(eth_type_mpls(mpls->ethertype));
b0a17866 4404
49a73e0c 4405 n = flow_count_mpls_labels(flow, ctx->wc);
8bfd0fda 4406 if (!n) {
704bb0bf 4407 xlate_commit_actions(ctx);
8bfd0fda
BP
4408 } else if (n >= FLOW_MAX_MPLS_LABELS) {
4409 if (ctx->xin->packet != NULL) {
2d9b49dd
BP
4410 xlate_report_error(ctx, "dropping packet on which an MPLS push "
4411 "action can't be performed as it would have "
4412 "more MPLS LSEs than the %d supported.",
4413 FLOW_MAX_MPLS_LABELS);
9583bc14 4414 }
fff1b9c0 4415 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
8bfd0fda 4416 return;
9583bc14 4417 }
b0a17866 4418
742c0ac3
JR
4419 /* Update flow's MPLS stack, and clear L3/4 fields to mark them invalid. */
4420 flow_push_mpls(flow, n, mpls->ethertype, ctx->wc, true);
9583bc14
EJ
4421}
4422
8bfd0fda 4423static void
9cfef3d0 4424compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
9583bc14 4425{
8bfd0fda 4426 struct flow *flow = &ctx->xin->flow;
49a73e0c 4427 int n = flow_count_mpls_labels(flow, ctx->wc);
33bf9176 4428
49a73e0c 4429 if (flow_pop_mpls(flow, n, eth_type, ctx->wc)) {
8bf009bf 4430 if (!eth_type_mpls(eth_type) && ctx->xbridge->support.odp.recirc) {
e12ec36b 4431 ctx->was_mpls = true;
7bbdd84f
SH
4432 }
4433 } else if (n >= FLOW_MAX_MPLS_LABELS) {
8bfd0fda 4434 if (ctx->xin->packet != NULL) {
2d9b49dd
BP
4435 xlate_report_error(ctx, "dropping packet on which an "
4436 "MPLS pop action can't be performed as it has "
4437 "more MPLS LSEs than the %d supported.",
4438 FLOW_MAX_MPLS_LABELS);
8bfd0fda 4439 }
fff1b9c0 4440 ctx->error = XLATE_TOO_MANY_MPLS_LABELS;
1520ef4f 4441 ofpbuf_clear(ctx->odp_actions);
9583bc14
EJ
4442 }
4443}
4444
4445static bool
4446compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
4447{
33bf9176
BP
4448 struct flow *flow = &ctx->xin->flow;
4449
4450 if (!is_ip_any(flow)) {
9583bc14
EJ
4451 return false;
4452 }
4453
49a73e0c 4454 ctx->wc->masks.nw_ttl = 0xff;
33bf9176
BP
4455 if (flow->nw_ttl > 1) {
4456 flow->nw_ttl--;
9583bc14
EJ
4457 return false;
4458 } else {
4459 size_t i;
4460
4461 for (i = 0; i < ids->n_controllers; i++) {
4462 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
bdcad671 4463 ids->cnt_ids[i], NULL, 0);
9583bc14
EJ
4464 }
4465
4466 /* Stop processing for current table. */
2d9b49dd
BP
4467 xlate_report(ctx, OFT_WARN, "IPv%d decrement TTL exception",
4468 flow->dl_type == htons(ETH_TYPE_IP) ? 4 : 6);
9583bc14
EJ
4469 return true;
4470 }
4471}
4472
8bfd0fda 4473static void
097d4939
JR
4474compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
4475{
8bfd0fda 4476 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 4477 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_LABEL_MASK);
8bfd0fda 4478 set_mpls_lse_label(&ctx->xin->flow.mpls_lse[0], label);
097d4939 4479 }
097d4939
JR
4480}
4481
8bfd0fda 4482static void
097d4939
JR
4483compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
4484{
8bfd0fda 4485 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 4486 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TC_MASK);
8bfd0fda 4487 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse[0], tc);
097d4939 4488 }
097d4939
JR
4489}
4490
8bfd0fda 4491static void
9cfef3d0 4492compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
9583bc14 4493{
8bfd0fda 4494 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
49a73e0c 4495 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda 4496 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse[0], ttl);
b0a17866 4497 }
9583bc14
EJ
4498}
4499
4500static bool
9cfef3d0 4501compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
9583bc14 4502{
33bf9176 4503 struct flow *flow = &ctx->xin->flow;
1dd35f8a 4504
8bfd0fda 4505 if (eth_type_mpls(flow->dl_type)) {
22d38fca
JR
4506 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse[0]);
4507
49a73e0c 4508 ctx->wc->masks.mpls_lse[0] |= htonl(MPLS_TTL_MASK);
8bfd0fda
BP
4509 if (ttl > 1) {
4510 ttl--;
4511 set_mpls_lse_ttl(&flow->mpls_lse[0], ttl);
4512 return false;
4513 } else {
bdcad671
BP
4514 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0,
4515 NULL, 0);
8bfd0fda 4516 }
9583bc14 4517 }
22d38fca
JR
4518
4519 /* Stop processing for current table. */
2d9b49dd 4520 xlate_report(ctx, OFT_WARN, "MPLS decrement TTL exception");
22d38fca 4521 return true;
9583bc14
EJ
4522}
4523
4524static void
4525xlate_output_action(struct xlate_ctx *ctx,
4e022ec0 4526 ofp_port_t port, uint16_t max_len, bool may_packet_in)
9583bc14 4527{
2031ef97 4528 ofp_port_t prev_nf_output_iface = ctx->nf_output_iface;
9583bc14 4529
2031ef97 4530 ctx->nf_output_iface = NF_OUT_DROP;
9583bc14
EJ
4531
4532 switch (port) {
4533 case OFPP_IN_PORT:
e93ef1c7 4534 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port, NULL);
9583bc14
EJ
4535 break;
4536 case OFPP_TABLE:
4e022ec0 4537 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2cd20955 4538 0, may_packet_in, true, false);
9583bc14
EJ
4539 break;
4540 case OFPP_NORMAL:
4541 xlate_normal(ctx);
4542 break;
4543 case OFPP_FLOOD:
4544 flood_packets(ctx, false);
4545 break;
4546 case OFPP_ALL:
4547 flood_packets(ctx, true);
4548 break;
4549 case OFPP_CONTROLLER:
3a11fd5b 4550 execute_controller_action(ctx, max_len,
331c07ac
YHW
4551 (ctx->in_packet_out ? OFPR_PACKET_OUT
4552 : ctx->in_group ? OFPR_GROUP
029ca940
SS
4553 : ctx->in_action_set ? OFPR_ACTION_SET
4554 : OFPR_ACTION),
bdcad671 4555 0, NULL, 0);
9583bc14
EJ
4556 break;
4557 case OFPP_NONE:
4558 break;
4559 case OFPP_LOCAL:
4560 default:
4e022ec0 4561 if (port != ctx->xin->flow.in_port.ofp_port) {
e93ef1c7 4562 compose_output_action(ctx, port, NULL);
9583bc14 4563 } else {
2d9b49dd 4564 xlate_report(ctx, OFT_WARN, "skipping output to input port");
9583bc14
EJ
4565 }
4566 break;
4567 }
4568
4569 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2031ef97
BP
4570 ctx->nf_output_iface = NF_OUT_FLOOD;
4571 } else if (ctx->nf_output_iface == NF_OUT_DROP) {
4572 ctx->nf_output_iface = prev_nf_output_iface;
9583bc14 4573 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2031ef97
BP
4574 ctx->nf_output_iface != NF_OUT_FLOOD) {
4575 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
4576 }
4577}
4578
4579static void
4580xlate_output_reg_action(struct xlate_ctx *ctx,
4581 const struct ofpact_output_reg *or)
4582{
4583 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
4584 if (port <= UINT16_MAX) {
2d9b49dd
BP
4585 xlate_report(ctx, OFT_DETAIL, "output port is %"PRIu64, port);
4586
9583bc14
EJ
4587 union mf_subvalue value;
4588
4589 memset(&value, 0xff, sizeof value);
49a73e0c 4590 mf_write_subfield_flow(&or->src, &value, &ctx->wc->masks);
2d9b49dd
BP
4591 xlate_output_action(ctx, u16_to_ofp(port), or->max_len, false);
4592 } else {
4593 xlate_report(ctx, OFT_WARN, "output port %"PRIu64" is out of range",
4594 port);
9583bc14
EJ
4595 }
4596}
4597
aaca4fe0
WT
4598static void
4599xlate_output_trunc_action(struct xlate_ctx *ctx,
4600 ofp_port_t port, uint32_t max_len)
4601{
4602 bool support_trunc = ctx->xbridge->support.trunc;
4603 struct ovs_action_trunc *trunc;
2f2b904f 4604 char name[OFP10_MAX_PORT_NAME_LEN];
aaca4fe0
WT
4605
4606 switch (port) {
4607 case OFPP_TABLE:
4608 case OFPP_NORMAL:
4609 case OFPP_FLOOD:
4610 case OFPP_ALL:
4611 case OFPP_CONTROLLER:
4612 case OFPP_NONE:
4613 ofputil_port_to_string(port, name, sizeof name);
2d9b49dd
BP
4614 xlate_report(ctx, OFT_WARN,
4615 "output_trunc does not support port: %s", name);
aaca4fe0
WT
4616 break;
4617 case OFPP_LOCAL:
4618 case OFPP_IN_PORT:
4619 default:
4620 if (port != ctx->xin->flow.in_port.ofp_port) {
4621 const struct xport *xport = get_ofp_port(ctx->xbridge, port);
4622
4623 if (xport == NULL || xport->odp_port == ODPP_NONE) {
4624 /* Since truncate happens at its following output action, if
4625 * the output port is a patch port, the behavior is somehow
4626 * unpredicable. For simpilicity, disallow this case. */
4627 ofputil_port_to_string(port, name, sizeof name);
2d9b49dd
BP
4628 xlate_report_error(ctx, "output_trunc does not support "
4629 "patch port %s", name);
aaca4fe0
WT
4630 break;
4631 }
4632
4633 trunc = nl_msg_put_unspec_uninit(ctx->odp_actions,
4634 OVS_ACTION_ATTR_TRUNC,
4635 sizeof *trunc);
4636 trunc->max_len = max_len;
4637 xlate_output_action(ctx, port, max_len, false);
4638 if (!support_trunc) {
4639 ctx->xout->slow |= SLOW_ACTION;
4640 }
4641 } else {
2d9b49dd 4642 xlate_report(ctx, OFT_WARN, "skipping output to input port");
aaca4fe0
WT
4643 }
4644 break;
4645 }
4646}
4647
9583bc14
EJ
4648static void
4649xlate_enqueue_action(struct xlate_ctx *ctx,
4650 const struct ofpact_enqueue *enqueue)
4651{
4e022ec0 4652 ofp_port_t ofp_port = enqueue->port;
9583bc14
EJ
4653 uint32_t queue_id = enqueue->queue;
4654 uint32_t flow_priority, priority;
4655 int error;
4656
4657 /* Translate queue to priority. */
89a8a7f0 4658 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
9583bc14
EJ
4659 if (error) {
4660 /* Fall back to ordinary output action. */
4661 xlate_output_action(ctx, enqueue->port, 0, false);
4662 return;
4663 }
4664
4665 /* Check output port. */
4666 if (ofp_port == OFPP_IN_PORT) {
4e022ec0
AW
4667 ofp_port = ctx->xin->flow.in_port.ofp_port;
4668 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
9583bc14
EJ
4669 return;
4670 }
4671
4672 /* Add datapath actions. */
4673 flow_priority = ctx->xin->flow.skb_priority;
4674 ctx->xin->flow.skb_priority = priority;
e93ef1c7 4675 compose_output_action(ctx, ofp_port, NULL);
9583bc14
EJ
4676 ctx->xin->flow.skb_priority = flow_priority;
4677
4678 /* Update NetFlow output port. */
2031ef97
BP
4679 if (ctx->nf_output_iface == NF_OUT_DROP) {
4680 ctx->nf_output_iface = ofp_port;
4681 } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
4682 ctx->nf_output_iface = NF_OUT_MULTI;
9583bc14
EJ
4683 }
4684}
4685
4686static void
4687xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
4688{
4689 uint32_t skb_priority;
4690
89a8a7f0 4691 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
9583bc14
EJ
4692 ctx->xin->flow.skb_priority = skb_priority;
4693 } else {
4694 /* Couldn't translate queue to a priority. Nothing to do. A warning
4695 * has already been logged. */
4696 }
4697}
4698
4699static bool
46c88433 4700slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
9583bc14 4701{
46c88433
EJ
4702 const struct xbridge *xbridge = xbridge_;
4703 struct xport *port;
9583bc14
EJ
4704
4705 switch (ofp_port) {
4706 case OFPP_IN_PORT:
4707 case OFPP_TABLE:
4708 case OFPP_NORMAL:
4709 case OFPP_FLOOD:
4710 case OFPP_ALL:
4711 case OFPP_NONE:
4712 return true;
4713 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
4714 return false;
4715 default:
46c88433 4716 port = get_ofp_port(xbridge, ofp_port);
9583bc14
EJ
4717 return port ? port->may_enable : false;
4718 }
4719}
4720
4721static void
4722xlate_bundle_action(struct xlate_ctx *ctx,
4723 const struct ofpact_bundle *bundle)
4724{
4e022ec0 4725 ofp_port_t port;
9583bc14 4726
49a73e0c 4727 port = bundle_execute(bundle, &ctx->xin->flow, ctx->wc, slave_enabled_cb,
46c88433 4728 CONST_CAST(struct xbridge *, ctx->xbridge));
9583bc14 4729 if (bundle->dst.field) {
49a73e0c 4730 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow, ctx->wc);
2d9b49dd 4731 xlate_report_subfield(ctx, &bundle->dst);
9583bc14
EJ
4732 } else {
4733 xlate_output_action(ctx, port, 0, false);
4734 }
4735}
4736
4165b5e0
JS
4737static void
4738xlate_learn_action(struct xlate_ctx *ctx, const struct ofpact_learn *learn)
4739{
49a73e0c 4740 learn_mask(learn, ctx->wc);
9583bc14 4741
df70a773 4742 if (ctx->xin->xcache || ctx->xin->allow_side_effects) {
4165b5e0
JS
4743 uint64_t ofpacts_stub[1024 / 8];
4744 struct ofputil_flow_mod fm;
2c7ee524 4745 struct ofproto_flow_mod ofm__, *ofm;
4165b5e0 4746 struct ofpbuf ofpacts;
2c7ee524
JR
4747 enum ofperr error;
4748
4749 if (ctx->xin->xcache) {
3f3b97b0 4750 ofm = xmalloc(sizeof *ofm);
2c7ee524
JR
4751 } else {
4752 ofm = &ofm__;
4753 }
4165b5e0
JS
4754
4755 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2c7ee524 4756 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2d9b49dd
BP
4757 if (OVS_UNLIKELY(ctx->xin->trace)) {
4758 struct ds s = DS_EMPTY_INITIALIZER;
4759 ds_put_format(&s, "table=%"PRIu8" ", fm.table_id);
4760 match_format(&fm.match, &s, OFP_DEFAULT_PRIORITY);
4761 ds_chomp(&s, ' ');
4762 ds_put_format(&s, " priority=%d", fm.priority);
4763 if (fm.new_cookie) {
4764 ds_put_format(&s, " cookie=%#"PRIx64, ntohll(fm.new_cookie));
4765 }
4766 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
4767 ds_put_format(&s, " idle=%"PRIu16, fm.idle_timeout);
4768 }
4769 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
4770 ds_put_format(&s, " hard=%"PRIu16, fm.hard_timeout);
4771 }
4772 if (fm.flags & NX_LEARN_F_SEND_FLOW_REM) {
4773 ds_put_cstr(&s, " send_flow_rem");
4774 }
4775 ds_put_cstr(&s, " actions=");
4776 ofpacts_format(fm.ofpacts, fm.ofpacts_len, &s);
4777 xlate_report(ctx, OFT_DETAIL, "%s", ds_cstr(&s));
4778 ds_destroy(&s);
4779 }
2c7ee524
JR
4780 error = ofproto_dpif_flow_mod_init_for_learn(ctx->xbridge->ofproto,
4781 &fm, ofm);
4165b5e0 4782 ofpbuf_uninit(&ofpacts);
2c7ee524 4783
3f3b97b0 4784 if (!error) {
4c71600d 4785 bool success = true;
3f3b97b0 4786 if (ctx->xin->allow_side_effects) {
4c71600d
DDP
4787 error = ofproto_flow_mod_learn(ofm, ctx->xin->xcache != NULL,
4788 learn->limit, &success);
4789 } else if (learn->limit) {
4790 if (!ofm->temp_rule
4791 || ofm->temp_rule->state != RULE_INSERTED) {
4792 /* The learned rule expired and there are no packets, so
4793 * we cannot learn again. Since the translated actions
4794 * depend on the result of learning, we tell the caller
4795 * that there's no point in caching this result. */
4796 ctx->xout->avoid_caching = true;
4797 }
3f3b97b0
DDP
4798 }
4799
4c71600d
DDP
4800 if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
4801 nxm_reg_load(&learn->result_dst, success ? 1 : 0,
4802 &ctx->xin->flow, ctx->wc);
4803 xlate_report_subfield(ctx, &learn->result_dst);
4804 }
4805
4806 if (success && ctx->xin->xcache) {
3f3b97b0
DDP
4807 struct xc_entry *entry;
4808
4809 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_LEARN);
4810 entry->learn.ofm = ofm;
4c71600d 4811 entry->learn.limit = learn->limit;
3f3b97b0
DDP
4812 ofm = NULL;
4813 }
4c71600d
DDP
4814
4815 if (OVS_UNLIKELY(ctx->xin->trace && !success)) {
4816 xlate_report(ctx, OFT_DETAIL, "Limit exceeded, learn failed");
4817 }
3f3b97b0
DDP
4818 }
4819
4820 if (ctx->xin->xcache) {
4821 free(ofm);
2c7ee524
JR
4822 }
4823
4824 if (error) {
2d9b49dd
BP
4825 xlate_report_error(ctx, "LEARN action execution failed (%s).",
4826 ofperr_to_string(error));
2c7ee524 4827 }
2d9b49dd
BP
4828 } else {
4829 xlate_report(ctx, OFT_WARN,
4830 "suppressing side effects, so learn action ignored");
b256dc52
JS
4831 }
4832}
4833
4834static void
4835xlate_fin_timeout__(struct rule_dpif *rule, uint16_t tcp_flags,
4836 uint16_t idle_timeout, uint16_t hard_timeout)
4837{
4838 if (tcp_flags & (TCP_FIN | TCP_RST)) {
07a3cd5c 4839 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
b256dc52 4840 }
9583bc14
EJ
4841}
4842
9583bc14
EJ
4843static void
4844xlate_fin_timeout(struct xlate_ctx *ctx,
4845 const struct ofpact_fin_timeout *oft)
4846{
b256dc52 4847 if (ctx->rule) {
df70a773
JR
4848 if (ctx->xin->allow_side_effects) {
4849 xlate_fin_timeout__(ctx->rule, ctx->xin->tcp_flags,
4850 oft->fin_idle_timeout, oft->fin_hard_timeout);
4851 }
b256dc52
JS
4852 if (ctx->xin->xcache) {
4853 struct xc_entry *entry;
4854
4855 entry = xlate_cache_add_entry(ctx->xin->xcache, XC_FIN_TIMEOUT);
83709dfa
JR
4856 /* XC_RULE already holds a reference on the rule, none is taken
4857 * here. */
901a517e
JR
4858 entry->fin.rule = ctx->rule;
4859 entry->fin.idle = oft->fin_idle_timeout;
4860 entry->fin.hard = oft->fin_hard_timeout;
b256dc52 4861 }
9583bc14
EJ
4862 }
4863}
4864
4865static void
4866xlate_sample_action(struct xlate_ctx *ctx,
4867 const struct ofpact_sample *os)
4868{
f69f713b
BY
4869 odp_port_t output_odp_port = ODPP_NONE;
4870 odp_port_t tunnel_out_port = ODPP_NONE;
4871 struct dpif_ipfix *ipfix = ctx->xbridge->ipfix;
4872 bool emit_set_tunnel = false;
4873
4874 if (!ipfix || ctx->xin->flow.in_port.ofp_port == OFPP_NONE) {
4875 return;
4876 }
4877
e824d78d
JR
4878 /* Scale the probability from 16-bit to 32-bit while representing
4879 * the same percentage. */
4880 uint32_t probability = (os->probability << 16) | os->probability;
4881
b440dd8c 4882 if (!ctx->xbridge->support.variable_length_userdata) {
2d9b49dd
BP
4883 xlate_report_error(ctx, "ignoring NXAST_SAMPLE action because "
4884 "datapath lacks support (needs Linux 3.10+ or "
4885 "kernel module from OVS 1.11+)");
e824d78d
JR
4886 return;
4887 }
4888
f69f713b
BY
4889 /* If ofp_port in flow sample action is equel to ofp_port,
4890 * this sample action is a input port action. */
4891 if (os->sampling_port != OFPP_NONE &&
4892 os->sampling_port != ctx->xin->flow.in_port.ofp_port) {
4893 output_odp_port = ofp_port_to_odp_port(ctx->xbridge,
4894 os->sampling_port);
4895 if (output_odp_port == ODPP_NONE) {
2d9b49dd
BP
4896 xlate_report_error(ctx, "can't use unknown port %d in flow sample "
4897 "action", os->sampling_port);
f69f713b
BY
4898 return;
4899 }
4900
4901 if (dpif_ipfix_get_flow_exporter_tunnel_sampling(ipfix,
4902 os->collector_set_id)
4903 && dpif_ipfix_get_tunnel_port(ipfix, output_odp_port)) {
4904 tunnel_out_port = output_odp_port;
4905 emit_set_tunnel = true;
4906 }
4907 }
4908
4909 xlate_commit_actions(ctx);
4910 /* If 'emit_set_tunnel', sample(sampling_port=1) would translate
4911 * into datapath sample action set(tunnel(...)), sample(...) and
4912 * it is used for sampling egress tunnel information. */
4913 if (emit_set_tunnel) {
4914 const struct xport *xport = get_ofp_port(ctx->xbridge,
4915 os->sampling_port);
4916
4917 if (xport && xport->is_tunnel) {
4918 struct flow *flow = &ctx->xin->flow;
4919 tnl_port_send(xport->ofport, flow, ctx->wc);
4920 if (!ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
4921 struct flow_tnl flow_tnl = flow->tunnel;
4922
4923 commit_odp_tunnel_action(flow, &ctx->base_flow,
4924 ctx->odp_actions);
4925 flow->tunnel = flow_tnl;
4926 }
4927 } else {
2d9b49dd
BP
4928 xlate_report_error(ctx,
4929 "sampling_port:%d should be a tunnel port.",
4930 os->sampling_port);
f69f713b
BY
4931 }
4932 }
e824d78d 4933
a6092018
BP
4934 union user_action_cookie cookie = {
4935 .flow_sample = {
4936 .type = USER_ACTION_COOKIE_FLOW_SAMPLE,
4937 .probability = os->probability,
4938 .collector_set_id = os->collector_set_id,
4939 .obs_domain_id = os->obs_domain_id,
4940 .obs_point_id = os->obs_point_id,
f69f713b 4941 .output_odp_port = output_odp_port,
4930ea56 4942 .direction = os->direction,
a6092018
BP
4943 }
4944 };
4945 compose_sample_action(ctx, probability, &cookie, sizeof cookie.flow_sample,
f69f713b 4946 tunnel_out_port, false);
9583bc14
EJ
4947}
4948
456024cb 4949/* Use datapath 'clone' or sample to enclose the translation of 'oc'. */
7ae62a67
WT
4950static void
4951compose_clone_action(struct xlate_ctx *ctx, const struct ofpact_nest *oc)
bef503e8
AZ
4952{
4953 size_t clone_offset = nl_msg_start_nested(ctx->odp_actions,
4954 OVS_ACTION_ATTR_CLONE);
456024cb
AZ
4955 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
4956 nl_msg_end_non_empty_nested(ctx->odp_actions, clone_offset);
4957}
4958
4959/* Use datapath 'sample' action to translate clone. */
4960static void
4961compose_clone_action_using_sample(struct xlate_ctx *ctx,
4962 const struct ofpact_nest *oc)
4963{
4964 size_t offset = nl_msg_start_nested(ctx->odp_actions,
4965 OVS_ACTION_ATTR_SAMPLE);
4966
4967 size_t ac_offset = nl_msg_start_nested(ctx->odp_actions,
4968 OVS_SAMPLE_ATTR_ACTIONS);
bef503e8
AZ
4969
4970 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
4971
456024cb
AZ
4972 if (nl_msg_end_non_empty_nested(ctx->odp_actions, ac_offset)) {
4973 nl_msg_cancel_nested(ctx->odp_actions, offset);
4974 } else {
4975 nl_msg_put_u32(ctx->odp_actions, OVS_SAMPLE_ATTR_PROBABILITY,
4976 UINT32_MAX); /* 100% probability. */
4977 nl_msg_end_nested(ctx->odp_actions, offset);
4978 }
bef503e8
AZ
4979}
4980
4981static void
4982xlate_clone(struct xlate_ctx *ctx, const struct ofpact_nest *oc)
7ae62a67 4983{
bd3c2df3 4984 bool old_was_mpls = ctx->was_mpls;
ba653d2a 4985 bool old_conntracked = ctx->conntracked;
7ae62a67 4986 struct flow old_flow = ctx->xin->flow;
ba653d2a 4987
b827b231
BP
4988 struct ofpbuf old_stack = ctx->stack;
4989 union mf_subvalue new_stack[1024 / sizeof(union mf_subvalue)];
4990 ofpbuf_use_stub(&ctx->stack, new_stack, sizeof new_stack);
4991 ofpbuf_put(&ctx->stack, old_stack.data, old_stack.size);
4992
4993 struct ofpbuf old_action_set = ctx->action_set;
4994 uint64_t actset_stub[1024 / 8];
4995 ofpbuf_use_stub(&ctx->action_set, actset_stub, sizeof actset_stub);
4996 ofpbuf_put(&ctx->action_set, old_action_set.data, old_action_set.size);
4997
456024cb
AZ
4998 /* Datapath clone action will make sure the pre clone packets
4999 * are used for actions after clone. Save and restore
5000 * ctx->base_flow to reflect this for the openflow pipeline. */
bef503e8 5001 if (ctx->xbridge->support.clone) {
889ee380 5002 struct flow old_base_flow = ctx->base_flow;
bef503e8 5003 compose_clone_action(ctx, oc);
889ee380
AZ
5004 ctx->base_flow = old_base_flow;
5005 } else if (ctx->xbridge->support.sample_nesting > 3) {
5006 /* Avoid generate sample action if datapath
5007 * only allow small number of nesting. Deeper nesting
5008 * can cause the datapath to reject the generated flow. */
5009 struct flow old_base_flow = ctx->base_flow;
456024cb 5010 compose_clone_action_using_sample(ctx, oc);
889ee380
AZ
5011 ctx->base_flow = old_base_flow;
5012 } else {
5013 do_xlate_actions(oc->actions, ofpact_nest_get_action_len(oc), ctx);
bef503e8 5014 }
ba653d2a 5015
b827b231
BP
5016 ofpbuf_uninit(&ctx->action_set);
5017 ctx->action_set = old_action_set;
5018
5019 ofpbuf_uninit(&ctx->stack);
5020 ctx->stack = old_stack;
5021
7ae62a67 5022 ctx->xin->flow = old_flow;
ba653d2a
BP
5023
5024 /* The clone's conntrack execution should have no effect on the original
5025 * packet. */
5026 ctx->conntracked = old_conntracked;
bd3c2df3
BP
5027
5028 /* Popping MPLS from the clone should have no effect on the original
5029 * packet. */
5030 ctx->was_mpls = old_was_mpls;
7ae62a67
WT
5031}
5032
076caa2f
JR
5033static void
5034xlate_meter_action(struct xlate_ctx *ctx, const struct ofpact_meter *meter)
5035{
5036 if (meter->provider_meter_id != UINT32_MAX) {
5037 nl_msg_put_u32(ctx->odp_actions, OVS_ACTION_ATTR_METER,
5038 meter->provider_meter_id);
5039 }
5040}
5041
9583bc14 5042static bool
46c88433 5043may_receive(const struct xport *xport, struct xlate_ctx *ctx)
9583bc14 5044{
bbbca389 5045 if (xport->config & (is_stp(&ctx->xin->flow)
46c88433
EJ
5046 ? OFPUTIL_PC_NO_RECV_STP
5047 : OFPUTIL_PC_NO_RECV)) {
9583bc14
EJ
5048 return false;
5049 }
5050
5051 /* Only drop packets here if both forwarding and learning are
5052 * disabled. If just learning is enabled, we need to have
5053 * OFPP_NORMAL and the learning action have a look at the packet
5054 * before we can drop it. */
9efd308e
DV
5055 if ((!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) ||
5056 (!xport_rstp_forward_state(xport) && !xport_rstp_learn_state(xport))) {
9583bc14
EJ
5057 return false;
5058 }
5059
5060 return true;
5061}
5062
7fdb60a7 5063static void
7e7e8dbb
BP
5064xlate_write_actions__(struct xlate_ctx *ctx,
5065 const struct ofpact *ofpacts, size_t ofpacts_len)
7fdb60a7 5066{
c61f3870
BP
5067 /* Maintain actset_output depending on the contents of the action set:
5068 *
5069 * - OFPP_UNSET, if there is no "output" action.
5070 *
5071 * - The output port, if there is an "output" action and no "group"
5072 * action.
5073 *
5074 * - OFPP_UNSET, if there is a "group" action.
5075 */
5076 if (!ctx->action_set_has_group) {
7e7e8dbb
BP
5077 const struct ofpact *a;
5078 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5079 if (a->type == OFPACT_OUTPUT) {
5080 ctx->xin->flow.actset_output = ofpact_get_OUTPUT(a)->port;
5081 } else if (a->type == OFPACT_GROUP) {
c61f3870
BP
5082 ctx->xin->flow.actset_output = OFPP_UNSET;
5083 ctx->action_set_has_group = true;
9055ca9a 5084 break;
c61f3870
BP
5085 }
5086 }
5087 }
5088
7e7e8dbb
BP
5089 ofpbuf_put(&ctx->action_set, ofpacts, ofpacts_len);
5090}
5091
5092static void
5093xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact_nest *a)
5094{
5095 xlate_write_actions__(ctx, a->actions, ofpact_nest_get_action_len(a));
7fdb60a7
SH
5096}
5097
5098static void
5099xlate_action_set(struct xlate_ctx *ctx)
5100{
2d9b49dd
BP
5101 uint64_t action_list_stub[1024 / 8];
5102 struct ofpbuf action_list = OFPBUF_STUB_INITIALIZER(action_list_stub);
7fdb60a7 5103 ofpacts_execute_action_set(&action_list, &ctx->action_set);
ed9c9e3e
JR
5104 /* Clear the action set, as it is not needed any more. */
5105 ofpbuf_clear(&ctx->action_set);
2d9b49dd
BP
5106 if (action_list.size) {
5107 ctx->in_action_set = true;
5108
5109 struct ovs_list *old_trace = ctx->xin->trace;
5110 ctx->xin->trace = xlate_report(ctx, OFT_TABLE,
5111 "--. Executing action set:");
5112 do_xlate_actions(action_list.data, action_list.size, ctx);
5113 ctx->xin->trace = old_trace;
5114
5115 ctx->in_action_set = false;
5116 }
7fdb60a7
SH
5117 ofpbuf_uninit(&action_list);
5118}
5119
e672ff9b 5120static void
1d361a81 5121freeze_put_unroll_xlate(struct xlate_ctx *ctx)
e672ff9b 5122{
1d361a81 5123 struct ofpact_unroll_xlate *unroll = ctx->frozen_actions.header;
e672ff9b
JR
5124
5125 /* Restore the table_id and rule cookie for a potential PACKET
5126 * IN if needed. */
5127 if (!unroll ||
5128 (ctx->table_id != unroll->rule_table_id
5129 || ctx->rule_cookie != unroll->rule_cookie)) {
1d361a81 5130 unroll = ofpact_put_UNROLL_XLATE(&ctx->frozen_actions);
e672ff9b
JR
5131 unroll->rule_table_id = ctx->table_id;
5132 unroll->rule_cookie = ctx->rule_cookie;
1d361a81 5133 ctx->frozen_actions.header = unroll;
e672ff9b
JR
5134 }
5135}
5136
5137
1d361a81
BP
5138/* Copy actions 'a' through 'end' to ctx->frozen_actions, which will be
5139 * executed after thawing. Inserts an UNROLL_XLATE action, if none is already
5140 * present, before any action that may depend on the current table ID or flow
5141 * cookie. */
e672ff9b 5142static void
1d361a81 5143freeze_unroll_actions(const struct ofpact *a, const struct ofpact *end,
e672ff9b
JR
5144 struct xlate_ctx *ctx)
5145{
c2b283b7 5146 for (; a < end; a = ofpact_next(a)) {
e672ff9b 5147 switch (a->type) {
e672ff9b 5148 case OFPACT_OUTPUT_REG:
aaca4fe0 5149 case OFPACT_OUTPUT_TRUNC:
e672ff9b
JR
5150 case OFPACT_GROUP:
5151 case OFPACT_OUTPUT:
5152 case OFPACT_CONTROLLER:
5153 case OFPACT_DEC_MPLS_TTL:
5154 case OFPACT_DEC_TTL:
83a31283
BP
5155 /* These actions may generate asynchronous messages, which include
5156 * table ID and flow cookie information. */
1d361a81 5157 freeze_put_unroll_xlate(ctx);
e672ff9b
JR
5158 break;
5159
83a31283
BP
5160 case OFPACT_RESUBMIT:
5161 if (ofpact_get_RESUBMIT(a)->table_id == 0xff) {
5162 /* This resubmit action is relative to the current table, so we
5163 * need to track what table that is.*/
1d361a81 5164 freeze_put_unroll_xlate(ctx);
83a31283
BP
5165 }
5166 break;
5167
e672ff9b
JR
5168 case OFPACT_SET_TUNNEL:
5169 case OFPACT_REG_MOVE:
5170 case OFPACT_SET_FIELD:
5171 case OFPACT_STACK_PUSH:
5172 case OFPACT_STACK_POP:
5173 case OFPACT_LEARN:
5174 case OFPACT_WRITE_METADATA:
83a31283 5175 case OFPACT_GOTO_TABLE:
e672ff9b
JR
5176 case OFPACT_ENQUEUE:
5177 case OFPACT_SET_VLAN_VID:
5178 case OFPACT_SET_VLAN_PCP:
5179 case OFPACT_STRIP_VLAN:
5180 case OFPACT_PUSH_VLAN:
5181 case OFPACT_SET_ETH_SRC:
5182 case OFPACT_SET_ETH_DST:
5183 case OFPACT_SET_IPV4_SRC:
5184 case OFPACT_SET_IPV4_DST:
5185 case OFPACT_SET_IP_DSCP:
5186 case OFPACT_SET_IP_ECN:
5187 case OFPACT_SET_IP_TTL:
5188 case OFPACT_SET_L4_SRC_PORT:
5189 case OFPACT_SET_L4_DST_PORT:
5190 case OFPACT_SET_QUEUE:
5191 case OFPACT_POP_QUEUE:
5192 case OFPACT_PUSH_MPLS:
5193 case OFPACT_POP_MPLS:
5194 case OFPACT_SET_MPLS_LABEL:
5195 case OFPACT_SET_MPLS_TC:
5196 case OFPACT_SET_MPLS_TTL:
5197 case OFPACT_MULTIPATH:
5198 case OFPACT_BUNDLE:
5199 case OFPACT_EXIT:
5200 case OFPACT_UNROLL_XLATE:
5201 case OFPACT_FIN_TIMEOUT:
5202 case OFPACT_CLEAR_ACTIONS:
5203 case OFPACT_WRITE_ACTIONS:
5204 case OFPACT_METER:
5205 case OFPACT_SAMPLE:
7ae62a67 5206 case OFPACT_CLONE:
d4abaff5 5207 case OFPACT_DEBUG_RECIRC:
07659514 5208 case OFPACT_CT:
72fe7578 5209 case OFPACT_CT_CLEAR:
9ac0aada 5210 case OFPACT_NAT:
83a31283 5211 /* These may not generate PACKET INs. */
e672ff9b
JR
5212 break;
5213
e672ff9b
JR
5214 case OFPACT_NOTE:
5215 case OFPACT_CONJUNCTION:
83a31283 5216 /* These need not be copied for restoration. */
e672ff9b
JR
5217 continue;
5218 }
5219 /* Copy the action over. */
1d361a81 5220 ofpbuf_put(&ctx->frozen_actions, a, OFPACT_ALIGN(a->len));
e672ff9b
JR
5221 }
5222}
5223
8e53fe8c 5224static void
f2d105b5
JS
5225put_ct_mark(const struct flow *flow, struct ofpbuf *odp_actions,
5226 struct flow_wildcards *wc)
8e53fe8c 5227{
2a754f4a
JS
5228 if (wc->masks.ct_mark) {
5229 struct {
5230 uint32_t key;
5231 uint32_t mask;
5232 } *odp_ct_mark;
5233
5234 odp_ct_mark = nl_msg_put_unspec_uninit(odp_actions, OVS_CT_ATTR_MARK,
5235 sizeof(*odp_ct_mark));
5236 odp_ct_mark->key = flow->ct_mark & wc->masks.ct_mark;
5237 odp_ct_mark->mask = wc->masks.ct_mark;
8e53fe8c
JS
5238 }
5239}
5240
9daf2348 5241static void
f2d105b5
JS
5242put_ct_label(const struct flow *flow, struct ofpbuf *odp_actions,
5243 struct flow_wildcards *wc)
9daf2348 5244{
2ff8484b 5245 if (!ovs_u128_is_zero(wc->masks.ct_label)) {
9daf2348
JS
5246 struct {
5247 ovs_u128 key;
5248 ovs_u128 mask;
5249 } *odp_ct_label;
5250
5251 odp_ct_label = nl_msg_put_unspec_uninit(odp_actions,
5252 OVS_CT_ATTR_LABELS,
5253 sizeof(*odp_ct_label));
f2d105b5 5254 odp_ct_label->key = ovs_u128_and(flow->ct_label, wc->masks.ct_label);
9daf2348
JS
5255 odp_ct_label->mask = wc->masks.ct_label;
5256 }
5257}
5258
d787ad39 5259static void
2d9b49dd
BP
5260put_ct_helper(struct xlate_ctx *ctx,
5261 struct ofpbuf *odp_actions, struct ofpact_conntrack *ofc)
d787ad39
JS
5262{
5263 if (ofc->alg) {
40c7b2fc
JS
5264 switch(ofc->alg) {
5265 case IPPORT_FTP:
d787ad39 5266 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "ftp");
40c7b2fc
JS
5267 break;
5268 case IPPORT_TFTP:
5269 nl_msg_put_string(odp_actions, OVS_CT_ATTR_HELPER, "tftp");
5270 break;
5271 default:
2d9b49dd 5272 xlate_report_error(ctx, "cannot serialize ct_helper %d", ofc->alg);
40c7b2fc 5273 break;
d787ad39
JS
5274 }
5275 }
5276}
5277
9ac0aada
JR
5278static void
5279put_ct_nat(struct xlate_ctx *ctx)
5280{
5281 struct ofpact_nat *ofn = ctx->ct_nat_action;
5282 size_t nat_offset;
5283
5284 if (!ofn) {
5285 return;
5286 }
5287
5288 nat_offset = nl_msg_start_nested(ctx->odp_actions, OVS_CT_ATTR_NAT);
5289 if (ofn->flags & NX_NAT_F_SRC || ofn->flags & NX_NAT_F_DST) {
5290 nl_msg_put_flag(ctx->odp_actions, ofn->flags & NX_NAT_F_SRC
5291 ? OVS_NAT_ATTR_SRC : OVS_NAT_ATTR_DST);
5292 if (ofn->flags & NX_NAT_F_PERSISTENT) {
5293 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PERSISTENT);
5294 }
5295 if (ofn->flags & NX_NAT_F_PROTO_HASH) {
5296 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_HASH);
5297 } else if (ofn->flags & NX_NAT_F_PROTO_RANDOM) {
5298 nl_msg_put_flag(ctx->odp_actions, OVS_NAT_ATTR_PROTO_RANDOM);
5299 }
5300 if (ofn->range_af == AF_INET) {
73e8bc23 5301 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
9ac0aada
JR
5302 ofn->range.addr.ipv4.min);
5303 if (ofn->range.addr.ipv4.max &&
73e8bc23
BP
5304 (ntohl(ofn->range.addr.ipv4.max)
5305 > ntohl(ofn->range.addr.ipv4.min))) {
5306 nl_msg_put_be32(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
5307 ofn->range.addr.ipv4.max);
9ac0aada
JR
5308 }
5309 } else if (ofn->range_af == AF_INET6) {
5310 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MIN,
5311 &ofn->range.addr.ipv6.min,
5312 sizeof ofn->range.addr.ipv6.min);
5313 if (!ipv6_mask_is_any(&ofn->range.addr.ipv6.max) &&
5314 memcmp(&ofn->range.addr.ipv6.max, &ofn->range.addr.ipv6.min,
5315 sizeof ofn->range.addr.ipv6.max) > 0) {
5316 nl_msg_put_unspec(ctx->odp_actions, OVS_NAT_ATTR_IP_MAX,
5317 &ofn->range.addr.ipv6.max,
5318 sizeof ofn->range.addr.ipv6.max);
5319 }
5320 }
5321 if (ofn->range_af != AF_UNSPEC && ofn->range.proto.min) {
5322 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MIN,
5323 ofn->range.proto.min);
5324 if (ofn->range.proto.max &&
5325 ofn->range.proto.max > ofn->range.proto.min) {
5326 nl_msg_put_u16(ctx->odp_actions, OVS_NAT_ATTR_PROTO_MAX,
5327 ofn->range.proto.max);
5328 }
5329 }
5330 }
5331 nl_msg_end_nested(ctx->odp_actions, nat_offset);
5332}
5333
07659514
JS
5334static void
5335compose_conntrack_action(struct xlate_ctx *ctx, struct ofpact_conntrack *ofc)
5336{
9daf2348 5337 ovs_u128 old_ct_label = ctx->base_flow.ct_label;
f2d105b5 5338 ovs_u128 old_ct_label_mask = ctx->wc->masks.ct_label;
8e53fe8c 5339 uint32_t old_ct_mark = ctx->base_flow.ct_mark;
f2d105b5 5340 uint32_t old_ct_mark_mask = ctx->wc->masks.ct_mark;
07659514
JS
5341 size_t ct_offset;
5342 uint16_t zone;
5343
5344 /* Ensure that any prior actions are applied before composing the new
5345 * conntrack action. */
5346 xlate_commit_actions(ctx);
5347
8e53fe8c 5348 /* Process nested actions first, to populate the key. */
9ac0aada 5349 ctx->ct_nat_action = NULL;
f2d105b5
JS
5350 ctx->wc->masks.ct_mark = 0;
5351 ctx->wc->masks.ct_label.u64.hi = ctx->wc->masks.ct_label.u64.lo = 0;
8e53fe8c
JS
5352 do_xlate_actions(ofc->actions, ofpact_ct_get_action_len(ofc), ctx);
5353
07659514
JS
5354 if (ofc->zone_src.field) {
5355 zone = mf_get_subfield(&ofc->zone_src, &ctx->xin->flow);
5356 } else {
5357 zone = ofc->zone_imm;
5358 }
5359
5360 ct_offset = nl_msg_start_nested(ctx->odp_actions, OVS_ACTION_ATTR_CT);
5361 if (ofc->flags & NX_CT_F_COMMIT) {
a76a37ef
JR
5362 nl_msg_put_flag(ctx->odp_actions, ofc->flags & NX_CT_F_FORCE ?
5363 OVS_CT_ATTR_FORCE_COMMIT : OVS_CT_ATTR_COMMIT);
adfe7a0b
JR
5364 if (ctx->xbridge->support.ct_eventmask) {
5365 nl_msg_put_u32(ctx->odp_actions, OVS_CT_ATTR_EVENTMASK,
975954af 5366 OVS_CT_EVENTMASK_DEFAULT);
adfe7a0b 5367 }
07659514
JS
5368 }
5369 nl_msg_put_u16(ctx->odp_actions, OVS_CT_ATTR_ZONE, zone);
f2d105b5
JS
5370 put_ct_mark(&ctx->xin->flow, ctx->odp_actions, ctx->wc);
5371 put_ct_label(&ctx->xin->flow, ctx->odp_actions, ctx->wc);
2d9b49dd 5372 put_ct_helper(ctx, ctx->odp_actions, ofc);
9ac0aada
JR
5373 put_ct_nat(ctx);
5374 ctx->ct_nat_action = NULL;
07659514
JS
5375 nl_msg_end_nested(ctx->odp_actions, ct_offset);
5376
8e53fe8c
JS
5377 /* Restore the original ct fields in the key. These should only be exposed
5378 * after recirculation to another table. */
5379 ctx->base_flow.ct_mark = old_ct_mark;
f2d105b5 5380 ctx->wc->masks.ct_mark = old_ct_mark_mask;
9daf2348 5381 ctx->base_flow.ct_label = old_ct_label;
f2d105b5 5382 ctx->wc->masks.ct_label = old_ct_label_mask;
8e53fe8c 5383
07659514
JS
5384 if (ofc->recirc_table == NX_CT_RECIRC_NONE) {
5385 /* If we do not recirculate as part of this action, hide the results of
5386 * connection tracking from subsequent recirculations. */
5387 ctx->conntracked = false;
5388 } else {
5389 /* Use ct_* fields from datapath during recirculation upcall. */
5390 ctx->conntracked = true;
e37b8437 5391 compose_recirculate_and_fork(ctx, ofc->recirc_table);
07659514
JS
5392 }
5393}
5394
e12ec36b
SH
5395static void
5396recirc_for_mpls(const struct ofpact *a, struct xlate_ctx *ctx)
5397{
5398 /* No need to recirculate if already exiting. */
5399 if (ctx->exit) {
5400 return;
5401 }
5402
5403 /* Do not consider recirculating unless the packet was previously MPLS. */
5404 if (!ctx->was_mpls) {
5405 return;
5406 }
5407
5408 /* Special case these actions, only recirculating if necessary.
5409 * This avoids the overhead of recirculation in common use-cases.
5410 */
5411 switch (a->type) {
5412
5413 /* Output actions do not require recirculation. */
5414 case OFPACT_OUTPUT:
aaca4fe0 5415 case OFPACT_OUTPUT_TRUNC:
e12ec36b
SH
5416 case OFPACT_ENQUEUE:
5417 case OFPACT_OUTPUT_REG:
5418 /* Set actions that don't touch L3+ fields do not require recirculation. */
5419 case OFPACT_SET_VLAN_VID:
5420 case OFPACT_SET_VLAN_PCP:
5421 case OFPACT_SET_ETH_SRC:
5422 case OFPACT_SET_ETH_DST:
5423 case OFPACT_SET_TUNNEL:
5424 case OFPACT_SET_QUEUE:
5425 /* If actions of a group require recirculation that can be detected
5426 * when translating them. */
5427 case OFPACT_GROUP:
5428 return;
5429
5430 /* Set field that don't touch L3+ fields don't require recirculation. */
5431 case OFPACT_SET_FIELD:
5432 if (mf_is_l3_or_higher(ofpact_get_SET_FIELD(a)->field)) {
5433 break;
5434 }
5435 return;
5436
5437 /* For simplicity, recirculate in all other cases. */
5438 case OFPACT_CONTROLLER:
5439 case OFPACT_BUNDLE:
5440 case OFPACT_STRIP_VLAN:
5441 case OFPACT_PUSH_VLAN:
5442 case OFPACT_SET_IPV4_SRC:
5443 case OFPACT_SET_IPV4_DST:
5444 case OFPACT_SET_IP_DSCP:
5445 case OFPACT_SET_IP_ECN:
5446 case OFPACT_SET_IP_TTL:
5447 case OFPACT_SET_L4_SRC_PORT:
5448 case OFPACT_SET_L4_DST_PORT:
5449 case OFPACT_REG_MOVE:
5450 case OFPACT_STACK_PUSH:
5451 case OFPACT_STACK_POP:
5452 case OFPACT_DEC_TTL:
5453 case OFPACT_SET_MPLS_LABEL:
5454 case OFPACT_SET_MPLS_TC:
5455 case OFPACT_SET_MPLS_TTL:
5456 case OFPACT_DEC_MPLS_TTL:
5457 case OFPACT_PUSH_MPLS:
5458 case OFPACT_POP_MPLS:
5459 case OFPACT_POP_QUEUE:
5460 case OFPACT_FIN_TIMEOUT:
5461 case OFPACT_RESUBMIT:
5462 case OFPACT_LEARN:
5463 case OFPACT_CONJUNCTION:
5464 case OFPACT_MULTIPATH:
5465 case OFPACT_NOTE:
5466 case OFPACT_EXIT:
5467 case OFPACT_SAMPLE:
7ae62a67 5468 case OFPACT_CLONE:
e12ec36b
SH
5469 case OFPACT_UNROLL_XLATE:
5470 case OFPACT_CT:
72fe7578 5471 case OFPACT_CT_CLEAR:
e12ec36b
SH
5472 case OFPACT_NAT:
5473 case OFPACT_DEBUG_RECIRC:
5474 case OFPACT_METER:
5475 case OFPACT_CLEAR_ACTIONS:
5476 case OFPACT_WRITE_ACTIONS:
5477 case OFPACT_WRITE_METADATA:
5478 case OFPACT_GOTO_TABLE:
5479 default:
5480 break;
5481 }
5482
5483 /* Recirculate */
5484 ctx_trigger_freeze(ctx);
5485}
5486
2d9b49dd
BP
5487static void
5488xlate_ofpact_reg_move(struct xlate_ctx *ctx, const struct ofpact_reg_move *a)
5489{
5490 mf_subfield_copy(&a->src, &a->dst, &ctx->xin->flow, ctx->wc);
5491 xlate_report_subfield(ctx, &a->dst);
5492}
5493
5494static void
5495xlate_ofpact_stack_pop(struct xlate_ctx *ctx, const struct ofpact_stack *a)
5496{
5497 if (nxm_execute_stack_pop(a, &ctx->xin->flow, ctx->wc, &ctx->stack)) {
5498 xlate_report_subfield(ctx, &a->subfield);
5499 } else {
5500 xlate_report_error(ctx, "stack underflow");
5501 }
5502}
5503
5504/* Restore translation context data that was stored earlier. */
5505static void
5506xlate_ofpact_unroll_xlate(struct xlate_ctx *ctx,
5507 const struct ofpact_unroll_xlate *a)
5508{
5509 ctx->table_id = a->rule_table_id;
5510 ctx->rule_cookie = a->rule_cookie;
5511 xlate_report(ctx, OFT_THAW, "restored state: table=%"PRIu8", "
5512 "cookie=%#"PRIx64, a->rule_table_id, a->rule_cookie);
5513}
5514
9583bc14
EJ
5515static void
5516do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
5517 struct xlate_ctx *ctx)
5518{
49a73e0c 5519 struct flow_wildcards *wc = ctx->wc;
33bf9176 5520 struct flow *flow = &ctx->xin->flow;
9583bc14
EJ
5521 const struct ofpact *a;
5522
a36de779 5523 if (ovs_native_tunneling_is_on(ctx->xbridge->ofproto)) {
53902038 5524 tnl_neigh_snoop(flow, wc, ctx->xbridge->name);
a36de779 5525 }
f47ea021
JR
5526 /* dl_type already in the mask, not set below. */
5527
2d9b49dd
BP
5528 if (!ofpacts_len) {
5529 xlate_report(ctx, OFT_ACTION, "drop");
5530 return;
5531 }
5532
9583bc14
EJ
5533 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
5534 struct ofpact_controller *controller;
5535 const struct ofpact_metadata *metadata;
b2dd70be
JR
5536 const struct ofpact_set_field *set_field;
5537 const struct mf_field *mf;
9583bc14 5538
fff1b9c0
JR
5539 if (ctx->error) {
5540 break;
5541 }
5542
e12ec36b
SH
5543 recirc_for_mpls(a, ctx);
5544
e672ff9b
JR
5545 if (ctx->exit) {
5546 /* Check if need to store the remaining actions for later
5547 * execution. */
1d361a81
BP
5548 if (ctx->freezing) {
5549 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len),
e672ff9b
JR
5550 ctx);
5551 }
5552 break;
7bbdd84f
SH
5553 }
5554
2d9b49dd
BP
5555 if (OVS_UNLIKELY(ctx->xin->trace)) {
5556 struct ds s = DS_EMPTY_INITIALIZER;
5557 ofpacts_format(a, OFPACT_ALIGN(a->len), &s);
5558 xlate_report(ctx, OFT_ACTION, "%s", ds_cstr(&s));
5559 ds_destroy(&s);
5560 }
5561
9583bc14
EJ
5562 switch (a->type) {
5563 case OFPACT_OUTPUT:
5564 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
5565 ofpact_get_OUTPUT(a)->max_len, true);
5566 break;
5567
7395c052 5568 case OFPACT_GROUP:
f4fb341b 5569 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
1d741d6d 5570 /* Group could not be found. */
db88b35c
JR
5571
5572 /* XXX: Terminates action list translation, but does not
5573 * terminate the pipeline. */
f4fb341b
SH
5574 return;
5575 }
7395c052
NZ
5576 break;
5577
9583bc14
EJ
5578 case OFPACT_CONTROLLER:
5579 controller = ofpact_get_CONTROLLER(a);
77ab5fd2
BP
5580 if (controller->pause) {
5581 ctx->pause = controller;
5582 ctx->xout->slow |= SLOW_CONTROLLER;
5583 ctx_trigger_freeze(ctx);
5584 a = ofpact_next(a);
5585 } else {
5586 execute_controller_action(ctx, controller->max_len,
5587 controller->reason,
5588 controller->controller_id,
5589 controller->userdata,
5590 controller->userdata_len);
5591 }
9583bc14
EJ
5592 break;
5593
5594 case OFPACT_ENQUEUE:
16194afd
DDP
5595 memset(&wc->masks.skb_priority, 0xff,
5596 sizeof wc->masks.skb_priority);
9583bc14
EJ
5597 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
5598 break;
5599
5600 case OFPACT_SET_VLAN_VID:
f0fb825a
EG
5601 wc->masks.vlans[0].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
5602 if (flow->vlans[0].tci & htons(VLAN_CFI) ||
ca287d20 5603 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
f0fb825a
EG
5604 if (!flow->vlans[0].tpid) {
5605 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN);
5606 }
5607 flow->vlans[0].tci &= ~htons(VLAN_VID_MASK);
5608 flow->vlans[0].tci |=
5609 (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid) |
5610 htons(VLAN_CFI));
ca287d20 5611 }
9583bc14
EJ
5612 break;
5613
5614 case OFPACT_SET_VLAN_PCP:
f0fb825a
EG
5615 wc->masks.vlans[0].tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
5616 if (flow->vlans[0].tci & htons(VLAN_CFI) ||
ca287d20 5617 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
f0fb825a
EG
5618 if (!flow->vlans[0].tpid) {
5619 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN);
5620 }
5621 flow->vlans[0].tci &= ~htons(VLAN_PCP_MASK);
5622 flow->vlans[0].tci |=
5623 htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
5624 << VLAN_PCP_SHIFT) | VLAN_CFI);
ca287d20 5625 }
9583bc14
EJ
5626 break;
5627
5628 case OFPACT_STRIP_VLAN:
f0fb825a 5629 flow_pop_vlan(flow, wc);
9583bc14
EJ
5630 break;
5631
5632 case OFPACT_PUSH_VLAN:
f0fb825a
EG
5633 flow_push_vlan_uninit(flow, wc);
5634 flow->vlans[0].tpid = ofpact_get_PUSH_VLAN(a)->ethertype;
5635 flow->vlans[0].tci = htons(VLAN_CFI);
9583bc14
EJ
5636 break;
5637
5638 case OFPACT_SET_ETH_SRC:
74ff3298
JR
5639 WC_MASK_FIELD(wc, dl_src);
5640 flow->dl_src = ofpact_get_SET_ETH_SRC(a)->mac;
9583bc14
EJ
5641 break;
5642
5643 case OFPACT_SET_ETH_DST:
74ff3298
JR
5644 WC_MASK_FIELD(wc, dl_dst);
5645 flow->dl_dst = ofpact_get_SET_ETH_DST(a)->mac;
9583bc14
EJ
5646 break;
5647
5648 case OFPACT_SET_IPV4_SRC:
33bf9176 5649 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 5650 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
33bf9176 5651 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
9583bc14
EJ
5652 }
5653 break;
5654
5655 case OFPACT_SET_IPV4_DST:
33bf9176 5656 if (flow->dl_type == htons(ETH_TYPE_IP)) {
f47ea021 5657 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
33bf9176 5658 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
9583bc14
EJ
5659 }
5660 break;
5661
04f01c24
BP
5662 case OFPACT_SET_IP_DSCP:
5663 if (is_ip_any(flow)) {
f47ea021 5664 wc->masks.nw_tos |= IP_DSCP_MASK;
33bf9176 5665 flow->nw_tos &= ~IP_DSCP_MASK;
04f01c24 5666 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
9583bc14
EJ
5667 }
5668 break;
5669
ff14eb7a
JR
5670 case OFPACT_SET_IP_ECN:
5671 if (is_ip_any(flow)) {
5672 wc->masks.nw_tos |= IP_ECN_MASK;
5673 flow->nw_tos &= ~IP_ECN_MASK;
5674 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
5675 }
5676 break;
5677
0c20dbe4
JR
5678 case OFPACT_SET_IP_TTL:
5679 if (is_ip_any(flow)) {
5680 wc->masks.nw_ttl = 0xff;
5681 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
5682 }
5683 break;
5684
9583bc14 5685 case OFPACT_SET_L4_SRC_PORT:
b8778a0d 5686 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
5687 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
5688 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
33bf9176 5689 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
9583bc14
EJ
5690 }
5691 break;
5692
5693 case OFPACT_SET_L4_DST_PORT:
b8778a0d 5694 if (is_ip_any(flow) && !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
f47ea021
JR
5695 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
5696 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
33bf9176 5697 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
9583bc14
EJ
5698 }
5699 break;
5700
5701 case OFPACT_RESUBMIT:
8bf009bf
JR
5702 /* Freezing complicates resubmit. Some action in the flow
5703 * entry found by resubmit might trigger freezing. If that
5704 * happens, then we do not want to execute the resubmit again after
5705 * during thawing, so we want to skip back to the head of the loop
5706 * to avoid that, only adding any actions that follow the resubmit
5707 * to the frozen actions.
6b1c5734 5708 */
9583bc14 5709 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
6b1c5734 5710 continue;
9583bc14
EJ
5711
5712 case OFPACT_SET_TUNNEL:
33bf9176 5713 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
9583bc14
EJ
5714 break;
5715
5716 case OFPACT_SET_QUEUE:
16194afd
DDP
5717 memset(&wc->masks.skb_priority, 0xff,
5718 sizeof wc->masks.skb_priority);
9583bc14
EJ
5719 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
5720 break;
5721
5722 case OFPACT_POP_QUEUE:
16194afd
DDP
5723 memset(&wc->masks.skb_priority, 0xff,
5724 sizeof wc->masks.skb_priority);
2d9b49dd
BP
5725 if (flow->skb_priority != ctx->orig_skb_priority) {
5726 flow->skb_priority = ctx->orig_skb_priority;
5727 xlate_report(ctx, OFT_DETAIL, "queue = %#"PRIx32,
5728 flow->skb_priority);
5729 }
9583bc14
EJ
5730 break;
5731
5732 case OFPACT_REG_MOVE:
2d9b49dd 5733 xlate_ofpact_reg_move(ctx, ofpact_get_REG_MOVE(a));
9583bc14
EJ
5734 break;
5735
b2dd70be
JR
5736 case OFPACT_SET_FIELD:
5737 set_field = ofpact_get_SET_FIELD(a);
5738 mf = set_field->field;
b2dd70be 5739
aff49b8c
JR
5740 /* Set the field only if the packet actually has it. */
5741 if (mf_are_prereqs_ok(mf, flow, wc)) {
128684a6
JR
5742 mf_mask_field_masked(mf, ofpact_set_field_mask(set_field), wc);
5743 mf_set_flow_value_masked(mf, set_field->value,
5744 ofpact_set_field_mask(set_field),
5745 flow);
2d9b49dd
BP
5746 } else {
5747 xlate_report(ctx, OFT_WARN,
5748 "unmet prerequisites for %s, set_field ignored",
5749 mf->name);
5750
b8778a0d 5751 }
b2dd70be
JR
5752 break;
5753
9583bc14 5754 case OFPACT_STACK_PUSH:
33bf9176
BP
5755 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
5756 &ctx->stack);
9583bc14
EJ
5757 break;
5758
5759 case OFPACT_STACK_POP:
2d9b49dd 5760 xlate_ofpact_stack_pop(ctx, ofpact_get_STACK_POP(a));
9583bc14
EJ
5761 break;
5762
5763 case OFPACT_PUSH_MPLS:
8bfd0fda 5764 compose_mpls_push_action(ctx, ofpact_get_PUSH_MPLS(a));
9583bc14
EJ
5765 break;
5766
5767 case OFPACT_POP_MPLS:
8bfd0fda 5768 compose_mpls_pop_action(ctx, ofpact_get_POP_MPLS(a)->ethertype);
9583bc14
EJ
5769 break;
5770
097d4939 5771 case OFPACT_SET_MPLS_LABEL:
8bfd0fda
BP
5772 compose_set_mpls_label_action(
5773 ctx, ofpact_get_SET_MPLS_LABEL(a)->label);
1d741d6d 5774 break;
097d4939
JR
5775
5776 case OFPACT_SET_MPLS_TC:
8bfd0fda 5777 compose_set_mpls_tc_action(ctx, ofpact_get_SET_MPLS_TC(a)->tc);
097d4939
JR
5778 break;
5779
9583bc14 5780 case OFPACT_SET_MPLS_TTL:
8bfd0fda 5781 compose_set_mpls_ttl_action(ctx, ofpact_get_SET_MPLS_TTL(a)->ttl);
9583bc14
EJ
5782 break;
5783
5784 case OFPACT_DEC_MPLS_TTL:
9cfef3d0 5785 if (compose_dec_mpls_ttl_action(ctx)) {
ad3efdcb 5786 return;
9583bc14
EJ
5787 }
5788 break;
5789
5790 case OFPACT_DEC_TTL:
f74e7df7 5791 wc->masks.nw_ttl = 0xff;
9583bc14 5792 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
ad3efdcb 5793 return;
9583bc14
EJ
5794 }
5795 break;
5796
5797 case OFPACT_NOTE:
5798 /* Nothing to do. */
5799 break;
5800
5801 case OFPACT_MULTIPATH:
33bf9176 5802 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2d9b49dd 5803 xlate_report_subfield(ctx, &ofpact_get_MULTIPATH(a)->dst);
9583bc14
EJ
5804 break;
5805
5806 case OFPACT_BUNDLE:
9583bc14
EJ
5807 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
5808 break;
5809
5810 case OFPACT_OUTPUT_REG:
5811 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
5812 break;
5813
aaca4fe0
WT
5814 case OFPACT_OUTPUT_TRUNC:
5815 xlate_output_trunc_action(ctx, ofpact_get_OUTPUT_TRUNC(a)->port,
5816 ofpact_get_OUTPUT_TRUNC(a)->max_len);
5817 break;
5818
9583bc14
EJ
5819 case OFPACT_LEARN:
5820 xlate_learn_action(ctx, ofpact_get_LEARN(a));
5821 break;
5822
2d9b49dd 5823 case OFPACT_CONJUNCTION:
afc3987b
BP
5824 /* A flow with a "conjunction" action represents part of a special
5825 * kind of "set membership match". Such a flow should not actually
5826 * get executed, but it could via, say, a "packet-out", even though
5827 * that wouldn't be useful. Log it to help debugging. */
2d9b49dd 5828 xlate_report_error(ctx, "executing no-op conjunction action");
18080541
BP
5829 break;
5830
9583bc14
EJ
5831 case OFPACT_EXIT:
5832 ctx->exit = true;
5833 break;
5834
2d9b49dd
BP
5835 case OFPACT_UNROLL_XLATE:
5836 xlate_ofpact_unroll_xlate(ctx, ofpact_get_UNROLL_XLATE(a));
e672ff9b 5837 break;
2d9b49dd 5838
9583bc14 5839 case OFPACT_FIN_TIMEOUT:
33bf9176 5840 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
9583bc14
EJ
5841 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
5842 break;
5843
5844 case OFPACT_CLEAR_ACTIONS:
2d9b49dd 5845 xlate_report_action_set(ctx, "was");
7fdb60a7 5846 ofpbuf_clear(&ctx->action_set);
c61f3870
BP
5847 ctx->xin->flow.actset_output = OFPP_UNSET;
5848 ctx->action_set_has_group = false;
7fdb60a7
SH
5849 break;
5850
5851 case OFPACT_WRITE_ACTIONS:
7e7e8dbb 5852 xlate_write_actions(ctx, ofpact_get_WRITE_ACTIONS(a));
2d9b49dd 5853 xlate_report_action_set(ctx, "is");
9583bc14
EJ
5854 break;
5855
5856 case OFPACT_WRITE_METADATA:
5857 metadata = ofpact_get_WRITE_METADATA(a);
33bf9176
BP
5858 flow->metadata &= ~metadata->mask;
5859 flow->metadata |= metadata->metadata & metadata->mask;
9583bc14
EJ
5860 break;
5861
638a19b0 5862 case OFPACT_METER:
076caa2f 5863 xlate_meter_action(ctx, ofpact_get_METER(a));
638a19b0
JR
5864 break;
5865
9583bc14 5866 case OFPACT_GOTO_TABLE: {
9583bc14 5867 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
9583bc14 5868
9167fc1a
JR
5869 ovs_assert(ctx->table_id < ogt->table_id);
5870
4468099e 5871 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2cd20955 5872 ogt->table_id, true, true, false);
9583bc14
EJ
5873 break;
5874 }
5875
5876 case OFPACT_SAMPLE:
5877 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
5878 break;
d4abaff5 5879
7ae62a67 5880 case OFPACT_CLONE:
bef503e8 5881 xlate_clone(ctx, ofpact_get_CLONE(a));
7ae62a67
WT
5882 break;
5883
07659514 5884 case OFPACT_CT:
07659514
JS
5885 compose_conntrack_action(ctx, ofpact_get_CT(a));
5886 break;
5887
72fe7578
BP
5888 case OFPACT_CT_CLEAR:
5889 clear_conntrack(ctx);
5890 break;
5891
9ac0aada
JR
5892 case OFPACT_NAT:
5893 /* This will be processed by compose_conntrack_action(). */
5894 ctx->ct_nat_action = ofpact_get_NAT(a);
5895 break;
5896
d4abaff5 5897 case OFPACT_DEBUG_RECIRC:
1d361a81 5898 ctx_trigger_freeze(ctx);
d4abaff5
BP
5899 a = ofpact_next(a);
5900 break;
9583bc14 5901 }
1d741d6d
JR
5902
5903 /* Check if need to store this and the remaining actions for later
5904 * execution. */
1d361a81
BP
5905 if (!ctx->error && ctx->exit && ctx_first_frozen_action(ctx)) {
5906 freeze_unroll_actions(a, ofpact_end(ofpacts, ofpacts_len), ctx);
1d741d6d
JR
5907 break;
5908 }
9583bc14 5909 }
9583bc14
EJ
5910}
5911
5912void
5913xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
1f4a8933
JR
5914 ovs_version_t version, const struct flow *flow,
5915 ofp_port_t in_port, struct rule_dpif *rule, uint16_t tcp_flags,
1520ef4f
BP
5916 const struct dp_packet *packet, struct flow_wildcards *wc,
5917 struct ofpbuf *odp_actions)
9583bc14
EJ
5918{
5919 xin->ofproto = ofproto;
1f4a8933 5920 xin->tables_version = version;
9583bc14 5921 xin->flow = *flow;
8d8ab6c2 5922 xin->upcall_flow = flow;
cc377352 5923 xin->flow.in_port.ofp_port = in_port;
c61f3870 5924 xin->flow.actset_output = OFPP_UNSET;
9583bc14 5925 xin->packet = packet;
df70a773 5926 xin->allow_side_effects = packet != NULL;
9583bc14 5927 xin->rule = rule;
b256dc52 5928 xin->xcache = NULL;
9583bc14
EJ
5929 xin->ofpacts = NULL;
5930 xin->ofpacts_len = 0;
5931 xin->tcp_flags = tcp_flags;
2d9b49dd 5932 xin->trace = NULL;
9583bc14 5933 xin->resubmit_stats = NULL;
790c5d26 5934 xin->depth = 0;
cdd42eda 5935 xin->resubmits = 0;
49a73e0c 5936 xin->wc = wc;
1520ef4f 5937 xin->odp_actions = odp_actions;
331c07ac 5938 xin->in_packet_out = false;
e672ff9b
JR
5939
5940 /* Do recirc lookup. */
1d361a81 5941 xin->frozen_state = NULL;
29b1ea3f
BP
5942 if (flow->recirc_id) {
5943 const struct recirc_id_node *node
5944 = recirc_id_node_find(flow->recirc_id);
5945 if (node) {
1d361a81 5946 xin->frozen_state = &node->state;
29b1ea3f
BP
5947 }
5948 }
9583bc14
EJ
5949}
5950
5951void
5952xlate_out_uninit(struct xlate_out *xout)
5953{
e672ff9b 5954 if (xout) {
fbf5d6ec 5955 recirc_refs_unref(&xout->recircs);
9583bc14
EJ
5956 }
5957}
9583bc14 5958\f
55954f6e
EJ
5959static struct skb_priority_to_dscp *
5960get_skb_priority(const struct xport *xport, uint32_t skb_priority)
5961{
5962 struct skb_priority_to_dscp *pdscp;
5963 uint32_t hash;
5964
5965 hash = hash_int(skb_priority, 0);
5966 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
5967 if (pdscp->skb_priority == skb_priority) {
5968 return pdscp;
5969 }
5970 }
5971 return NULL;
5972}
5973
5974static bool
5975dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
5976 uint8_t *dscp)
5977{
5978 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
5979 *dscp = pdscp ? pdscp->dscp : 0;
5980 return pdscp != NULL;
5981}
5982
16194afd
DDP
5983static size_t
5984count_skb_priorities(const struct xport *xport)
5985{
5986 return hmap_count(&xport->skb_priorities);
5987}
5988
55954f6e
EJ
5989static void
5990clear_skb_priorities(struct xport *xport)
5991{
4ec3d7c7 5992 struct skb_priority_to_dscp *pdscp;
55954f6e 5993
4ec3d7c7 5994 HMAP_FOR_EACH_POP (pdscp, hmap_node, &xport->skb_priorities) {
55954f6e
EJ
5995 free(pdscp);
5996 }
5997}
5998
ce4a6b76
BP
5999static bool
6000actions_output_to_local_port(const struct xlate_ctx *ctx)
6001{
46c88433 6002 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
ce4a6b76
BP
6003 const struct nlattr *a;
6004 unsigned int left;
6005
1520ef4f
BP
6006 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->odp_actions->data,
6007 ctx->odp_actions->size) {
ce4a6b76
BP
6008 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
6009 && nl_attr_get_odp_port(a) == local_odp_port) {
6010 return true;
6011 }
6012 }
6013 return false;
6014}
9583bc14 6015
5e2a6702 6016#if defined(__linux__)
7d031d7e
BP
6017/* Returns the maximum number of packets that the Linux kernel is willing to
6018 * queue up internally to certain kinds of software-implemented ports, or the
6019 * default (and rarely modified) value if it cannot be determined. */
6020static int
6021netdev_max_backlog(void)
6022{
6023 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
6024 static int max_backlog = 1000; /* The normal default value. */
6025
6026 if (ovsthread_once_start(&once)) {
6027 static const char filename[] = "/proc/sys/net/core/netdev_max_backlog";
6028 FILE *stream;
6029 int n;
6030
6031 stream = fopen(filename, "r");
6032 if (!stream) {
120c348f 6033 VLOG_INFO("%s: open failed (%s)", filename, ovs_strerror(errno));
7d031d7e
BP
6034 } else {
6035 if (fscanf(stream, "%d", &n) != 1) {
6036 VLOG_WARN("%s: read error", filename);
6037 } else if (n <= 100) {
6038 VLOG_WARN("%s: unexpectedly small value %d", filename, n);
6039 } else {
6040 max_backlog = n;
6041 }
6042 fclose(stream);
6043 }
6044 ovsthread_once_done(&once);
6045
6046 VLOG_DBG("%s: using %d max_backlog", filename, max_backlog);
6047 }
6048
6049 return max_backlog;
6050}
6051
6052/* Counts and returns the number of OVS_ACTION_ATTR_OUTPUT actions in
6053 * 'odp_actions'. */
6054static int
6055count_output_actions(const struct ofpbuf *odp_actions)
6056{
6057 const struct nlattr *a;
6058 size_t left;
6059 int n = 0;
6060
6fd6ed71 6061 NL_ATTR_FOR_EACH_UNSAFE (a, left, odp_actions->data, odp_actions->size) {
7d031d7e
BP
6062 if (a->nla_type == OVS_ACTION_ATTR_OUTPUT) {
6063 n++;
6064 }
6065 }
6066 return n;
6067}
5e2a6702 6068#endif /* defined(__linux__) */
7d031d7e
BP
6069
6070/* Returns true if 'odp_actions' contains more output actions than the datapath
6071 * can reliably handle in one go. On Linux, this is the value of the
6072 * net.core.netdev_max_backlog sysctl, which limits the maximum number of
6073 * packets that the kernel is willing to queue up for processing while the
6074 * datapath is processing a set of actions. */
6075static bool
5e2a6702 6076too_many_output_actions(const struct ofpbuf *odp_actions OVS_UNUSED)
7d031d7e
BP
6077{
6078#ifdef __linux__
6fd6ed71 6079 return (odp_actions->size / NL_A_U32_SIZE > netdev_max_backlog()
7d031d7e
BP
6080 && count_output_actions(odp_actions) > netdev_max_backlog());
6081#else
6082 /* OSes other than Linux might have similar limits, but we don't know how
6083 * to determine them.*/
6084 return false;
6085#endif
6086}
6087
234c3da9
BP
6088static void
6089xlate_wc_init(struct xlate_ctx *ctx)
6090{
6091 flow_wildcards_init_catchall(ctx->wc);
6092
6093 /* Some fields we consider to always be examined. */
5e2e998a
JR
6094 WC_MASK_FIELD(ctx->wc, in_port);
6095 WC_MASK_FIELD(ctx->wc, dl_type);
234c3da9 6096 if (is_ip_any(&ctx->xin->flow)) {
5e2e998a 6097 WC_MASK_FIELD_MASK(ctx->wc, nw_frag, FLOW_NW_FRAG_MASK);
234c3da9
BP
6098 }
6099
6100 if (ctx->xbridge->support.odp.recirc) {
6101 /* Always exactly match recirc_id when datapath supports
6102 * recirculation. */
5e2e998a 6103 WC_MASK_FIELD(ctx->wc, recirc_id);
234c3da9
BP
6104 }
6105
6106 if (ctx->xbridge->netflow) {
6107 netflow_mask_wc(&ctx->xin->flow, ctx->wc);
6108 }
6109
6110 tnl_wc_init(&ctx->xin->flow, ctx->wc);
6111}
6112
6113static void
6114xlate_wc_finish(struct xlate_ctx *ctx)
6115{
f0fb825a
EG
6116 int i;
6117
234c3da9
BP
6118 /* Clear the metadata and register wildcard masks, because we won't
6119 * use non-header fields as part of the cache. */
6120 flow_wildcards_clear_non_packet_fields(ctx->wc);
6121
6122 /* ICMPv4 and ICMPv6 have 8-bit "type" and "code" fields. struct flow
6123 * uses the low 8 bits of the 16-bit tp_src and tp_dst members to
6124 * represent these fields. The datapath interface, on the other hand,
6125 * represents them with just 8 bits each. This means that if the high
6126 * 8 bits of the masks for these fields somehow become set, then they
6127 * will get chopped off by a round trip through the datapath, and
6128 * revalidation will spot that as an inconsistency and delete the flow.
6129 * Avoid the problem here by making sure that only the low 8 bits of
6130 * either field can be unwildcarded for ICMP.
6131 */
a75636c8 6132 if (is_icmpv4(&ctx->xin->flow, NULL) || is_icmpv6(&ctx->xin->flow, NULL)) {
234c3da9
BP
6133 ctx->wc->masks.tp_src &= htons(UINT8_MAX);
6134 ctx->wc->masks.tp_dst &= htons(UINT8_MAX);
6135 }
6136 /* VLAN_TCI CFI bit must be matched if any of the TCI is matched. */
f0fb825a
EG
6137 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
6138 if (ctx->wc->masks.vlans[i].tci) {
6139 ctx->wc->masks.vlans[i].tci |= htons(VLAN_CFI);
6140 }
234c3da9 6141 }
4a7ab326
DDP
6142
6143 /* The classifier might return masks that match on tp_src and tp_dst even
6144 * for later fragments. This happens because there might be flows that
6145 * match on tp_src or tp_dst without matching on the frag bits, because
6146 * it is not a prerequisite for OpenFlow. Since it is a prerequisite for
6147 * datapath flows and since tp_src and tp_dst are always going to be 0,
6148 * wildcard the fields here. */
6149 if (ctx->xin->flow.nw_frag & FLOW_NW_FRAG_LATER) {
6150 ctx->wc->masks.tp_src = 0;
6151 ctx->wc->masks.tp_dst = 0;
6152 }
234c3da9
BP
6153}
6154
e672ff9b
JR
6155/* Translates the flow, actions, or rule in 'xin' into datapath actions in
6156 * 'xout'.
56450a41 6157 * The caller must take responsibility for eventually freeing 'xout', with
fff1b9c0
JR
6158 * xlate_out_uninit().
6159 * Returns 'XLATE_OK' if translation was successful. In case of an error an
6160 * empty set of actions will be returned in 'xin->odp_actions' (if non-NULL),
6161 * so that most callers may ignore the return value and transparently install a
6162 * drop flow when the translation fails. */
6163enum xlate_error
84f0f298 6164xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
9583bc14 6165{
e467ea42
BP
6166 *xout = (struct xlate_out) {
6167 .slow = 0,
fbf5d6ec 6168 .recircs = RECIRC_REFS_EMPTY_INITIALIZER,
e467ea42
BP
6169 };
6170
84f0f298 6171 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
bb00fdef
BP
6172 struct xbridge *xbridge = xbridge_lookup(xcfg, xin->ofproto);
6173 if (!xbridge) {
fff1b9c0 6174 return XLATE_BRIDGE_NOT_FOUND;
bb00fdef
BP
6175 }
6176
33bf9176
BP
6177 struct flow *flow = &xin->flow;
6178
84cf3c1f 6179 uint8_t stack_stub[1024];
bb00fdef 6180 uint64_t action_set_stub[1024 / 8];
1d361a81 6181 uint64_t frozen_actions_stub[1024 / 8];
1520ef4f
BP
6182 uint64_t actions_stub[256 / 8];
6183 struct ofpbuf scratch_actions = OFPBUF_STUB_INITIALIZER(actions_stub);
bb00fdef
BP
6184 struct xlate_ctx ctx = {
6185 .xin = xin,
6186 .xout = xout,
6187 .base_flow = *flow,
c2b878e0 6188 .orig_tunnel_ipv6_dst = flow_tnl_dst(&flow->tunnel),
bb00fdef
BP
6189 .xbridge = xbridge,
6190 .stack = OFPBUF_STUB_INITIALIZER(stack_stub),
6191 .rule = xin->rule,
c0e638aa
BP
6192 .wc = (xin->wc
6193 ? xin->wc
f36efd90 6194 : &(struct flow_wildcards) { .masks = { .dl_type = 0 } }),
1520ef4f 6195 .odp_actions = xin->odp_actions ? xin->odp_actions : &scratch_actions,
bb00fdef 6196
790c5d26 6197 .depth = xin->depth,
cdd42eda 6198 .resubmits = xin->resubmits,
bb00fdef
BP
6199 .in_group = false,
6200 .in_action_set = false,
331c07ac 6201 .in_packet_out = xin->in_packet_out,
bb00fdef
BP
6202
6203 .table_id = 0,
6204 .rule_cookie = OVS_BE64_MAX,
6205 .orig_skb_priority = flow->skb_priority,
6206 .sflow_n_outputs = 0,
6207 .sflow_odp_port = 0,
2031ef97 6208 .nf_output_iface = NF_OUT_DROP,
bb00fdef 6209 .exit = false,
fff1b9c0 6210 .error = XLATE_OK,
3d6151f3 6211 .mirrors = 0,
bb00fdef 6212
1d361a81 6213 .freezing = false,
53cc166a 6214 .recirc_update_dp_hash = false,
1d361a81 6215 .frozen_actions = OFPBUF_STUB_INITIALIZER(frozen_actions_stub),
77ab5fd2 6216 .pause = NULL,
bb00fdef 6217
e12ec36b 6218 .was_mpls = false,
07659514 6219 .conntracked = false,
bb00fdef 6220
9ac0aada
JR
6221 .ct_nat_action = NULL,
6222
bb00fdef
BP
6223 .action_set_has_group = false,
6224 .action_set = OFPBUF_STUB_INITIALIZER(action_set_stub),
6225 };
865ca6cf
BP
6226
6227 /* 'base_flow' reflects the packet as it came in, but we need it to reflect
42deb67d
PS
6228 * the packet as the datapath will treat it for output actions. Our
6229 * datapath doesn't retain tunneling information without us re-setting
6230 * it, so clear the tunnel data.
865ca6cf 6231 */
42deb67d 6232
bb00fdef 6233 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
865ca6cf 6234
1520ef4f 6235 ofpbuf_reserve(ctx.odp_actions, NL_A_U32_SIZE);
c0e638aa 6236 xlate_wc_init(&ctx);
bb00fdef 6237
46c88433 6238 COVERAGE_INC(xlate_actions);
9583bc14 6239
2d9b49dd
BP
6240 xin->trace = xlate_report(&ctx, OFT_BRIDGE, "bridge(\"%s\")",
6241 xbridge->name);
1d361a81
BP
6242 if (xin->frozen_state) {
6243 const struct frozen_state *state = xin->frozen_state;
e672ff9b 6244
2d9b49dd
BP
6245 struct ovs_list *old_trace = xin->trace;
6246 xin->trace = xlate_report(&ctx, OFT_THAW, "thaw");
d6bef3cc 6247
e672ff9b 6248 if (xin->ofpacts_len > 0 || ctx.rule) {
2d9b49dd
BP
6249 xlate_report_error(&ctx, "Recirculation conflict (%s)!",
6250 xin->ofpacts_len ? "actions" : "rule");
fff1b9c0 6251 ctx.error = XLATE_RECIRCULATION_CONFLICT;
1520ef4f 6252 goto exit;
e672ff9b
JR
6253 }
6254
6255 /* Set the bridge for post-recirculation processing if needed. */
07a3cd5c 6256 if (!uuid_equals(&ctx.xbridge->ofproto->uuid, &state->ofproto_uuid)) {
e672ff9b 6257 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
2082425c 6258 const struct xbridge *new_bridge
290835f9 6259 = xbridge_lookup_by_uuid(xcfg, &state->ofproto_uuid);
e672ff9b
JR
6260
6261 if (OVS_UNLIKELY(!new_bridge)) {
6262 /* Drop the packet if the bridge cannot be found. */
2d9b49dd 6263 xlate_report_error(&ctx, "Frozen bridge no longer exists.");
fff1b9c0 6264 ctx.error = XLATE_BRIDGE_NOT_FOUND;
2d9b49dd 6265 xin->trace = old_trace;
1520ef4f 6266 goto exit;
e672ff9b
JR
6267 }
6268 ctx.xbridge = new_bridge;
1f4a8933
JR
6269 /* The bridge is now known so obtain its table version. */
6270 ctx.xin->tables_version
6271 = ofproto_dpif_get_tables_version(ctx.xbridge->ofproto);
e672ff9b
JR
6272 }
6273
1d361a81
BP
6274 /* Set the thawed table id. Note: A table lookup is done only if there
6275 * are no frozen actions. */
2082425c 6276 ctx.table_id = state->table_id;
2d9b49dd
BP
6277 xlate_report(&ctx, OFT_THAW,
6278 "Resuming from table %"PRIu8, ctx.table_id);
e672ff9b 6279
07659514 6280 if (!state->conntracked) {
72fe7578 6281 clear_conntrack(&ctx);
07659514
JS
6282 }
6283
e672ff9b 6284 /* Restore pipeline metadata. May change flow's in_port and other
1d361a81
BP
6285 * metadata to the values that existed when freezing was triggered. */
6286 frozen_metadata_to_flow(&state->metadata, flow);
e672ff9b
JR
6287
6288 /* Restore stack, if any. */
2082425c 6289 if (state->stack) {
84cf3c1f 6290 ofpbuf_put(&ctx.stack, state->stack, state->stack_size);
e672ff9b
JR
6291 }
6292
29bae541
BP
6293 /* Restore mirror state. */
6294 ctx.mirrors = state->mirrors;
6295
e672ff9b 6296 /* Restore action set, if any. */
2082425c 6297 if (state->action_set_len) {
2d9b49dd 6298 xlate_report_actions(&ctx, OFT_THAW, "Restoring action set",
417509fa 6299 state->action_set, state->action_set_len);
d6bef3cc 6300
7e7e8dbb
BP
6301 flow->actset_output = OFPP_UNSET;
6302 xlate_write_actions__(&ctx, state->action_set,
6303 state->action_set_len);
e672ff9b
JR
6304 }
6305
1d361a81
BP
6306 /* Restore frozen actions. If there are no actions, processing will
6307 * start with a lookup in the table set above. */
417509fa
BP
6308 xin->ofpacts = state->ofpacts;
6309 xin->ofpacts_len = state->ofpacts_len;
6310 if (state->ofpacts_len) {
2d9b49dd 6311 xlate_report_actions(&ctx, OFT_THAW, "Restoring actions",
d6bef3cc 6312 xin->ofpacts, xin->ofpacts_len);
e672ff9b 6313 }
e672ff9b 6314
2d9b49dd
BP
6315 xin->trace = old_trace;
6316 } else if (OVS_UNLIKELY(flow->recirc_id)) {
6317 xlate_report_error(&ctx,
6318 "Recirculation context not found for ID %"PRIx32,
6319 flow->recirc_id);
fff1b9c0 6320 ctx.error = XLATE_NO_RECIRCULATION_CONTEXT;
1520ef4f 6321 goto exit;
e672ff9b 6322 }
9583bc14 6323
8d8ab6c2
JG
6324 /* Tunnel metadata in udpif format must be normalized before translation. */
6325 if (flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
5b09d9f7
MS
6326 const struct tun_table *tun_tab = ofproto_get_tun_tab(
6327 &ctx.xbridge->ofproto->up);
8d8ab6c2
JG
6328 int err;
6329
6330 err = tun_metadata_from_geneve_udpif(tun_tab, &xin->upcall_flow->tunnel,
6331 &xin->upcall_flow->tunnel,
6332 &flow->tunnel);
6333 if (err) {
2d9b49dd 6334 xlate_report_error(&ctx, "Invalid Geneve tunnel metadata");
8d8ab6c2
JG
6335 ctx.error = XLATE_INVALID_TUNNEL_METADATA;
6336 goto exit;
6337 }
6338 } else if (!flow->tunnel.metadata.tab) {
6339 /* If the original flow did not come in on a tunnel, then it won't have
6340 * FLOW_TNL_F_UDPIF set. However, we still need to have a metadata
6341 * table in case we generate tunnel actions. */
5b09d9f7
MS
6342 flow->tunnel.metadata.tab = ofproto_get_tun_tab(
6343 &ctx.xbridge->ofproto->up);
8d8ab6c2
JG
6344 }
6345 ctx.wc->masks.tunnel.metadata.tab = flow->tunnel.metadata.tab;
6346
10c44245 6347 if (!xin->ofpacts && !ctx.rule) {
b2e89cc9 6348 ctx.rule = rule_dpif_lookup_from_table(
1f4a8933 6349 ctx.xbridge->ofproto, ctx.xin->tables_version, flow, ctx.wc,
1e1e1d19 6350 ctx.xin->resubmit_stats, &ctx.table_id,
a027899e 6351 flow->in_port.ofp_port, true, true, ctx.xin->xcache);
10c44245 6352 if (ctx.xin->resubmit_stats) {
b2e89cc9 6353 rule_dpif_credit_stats(ctx.rule, ctx.xin->resubmit_stats);
10c44245 6354 }
b256dc52
JS
6355 if (ctx.xin->xcache) {
6356 struct xc_entry *entry;
6357
6358 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_RULE);
901a517e 6359 entry->rule = ctx.rule;
07a3cd5c 6360 ofproto_rule_ref(&ctx.rule->up);
b256dc52 6361 }
a8c31348 6362
2d9b49dd 6363 xlate_report_table(&ctx, ctx.rule, ctx.table_id);
10c44245 6364 }
10c44245 6365
1d361a81 6366 /* Get the proximate input port of the packet. (If xin->frozen_state,
14d2b8b2
BP
6367 * flow->in_port is the ultimate input port of the packet.) */
6368 struct xport *in_port = get_ofp_port(xbridge,
6369 ctx.base_flow.in_port.ofp_port);
6370
1d361a81
BP
6371 /* Tunnel stats only for not-thawed packets. */
6372 if (!xin->frozen_state && in_port && in_port->is_tunnel) {
b256dc52
JS
6373 if (ctx.xin->resubmit_stats) {
6374 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
6375 if (in_port->bfd) {
6376 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
6377 }
6378 }
6379 if (ctx.xin->xcache) {
6380 struct xc_entry *entry;
6381
6382 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETDEV);
901a517e
JR
6383 entry->dev.rx = netdev_ref(in_port->netdev);
6384 entry->dev.bfd = bfd_ref(in_port->bfd);
d6fc5f57
EJ
6385 }
6386 }
6387
1d361a81 6388 if (!xin->frozen_state && process_special(&ctx, in_port)) {
bef1403e
BP
6389 /* process_special() did all the processing for this packet.
6390 *
1d361a81
BP
6391 * We do not perform special processing on thawed packets, since that
6392 * was done before they were frozen and should not be redone. */
bef1403e
BP
6393 } else if (in_port && in_port->xbundle
6394 && xbundle_mirror_out(xbridge, in_port->xbundle)) {
2d9b49dd
BP
6395 xlate_report_error(&ctx, "dropping packet received on port "
6396 "%s, which is reserved exclusively for mirroring",
6397 in_port->xbundle->name);
bef1403e 6398 } else {
1d361a81 6399 /* Sampling is done on initial reception; don't redo after thawing. */
a6092018 6400 unsigned int user_cookie_offset = 0;
1d361a81 6401 if (!xin->frozen_state) {
a6092018
BP
6402 user_cookie_offset = compose_sflow_action(&ctx);
6403 compose_ipfix_action(&ctx, ODPP_NONE);
e672ff9b 6404 }
0731abc5 6405 size_t sample_actions_len = ctx.odp_actions->size;
9583bc14 6406
234c3da9
BP
6407 if (tnl_process_ecn(flow)
6408 && (!in_port || may_receive(in_port, &ctx))) {
1806291d
BP
6409 const struct ofpact *ofpacts;
6410 size_t ofpacts_len;
6411
6412 if (xin->ofpacts) {
6413 ofpacts = xin->ofpacts;
6414 ofpacts_len = xin->ofpacts_len;
6415 } else if (ctx.rule) {
6416 const struct rule_actions *actions
07a3cd5c 6417 = rule_get_actions(&ctx.rule->up);
1806291d
BP
6418 ofpacts = actions->ofpacts;
6419 ofpacts_len = actions->ofpacts_len;
07a3cd5c 6420 ctx.rule_cookie = ctx.rule->up.flow_cookie;
1806291d
BP
6421 } else {
6422 OVS_NOT_REACHED();
6423 }
6424
7efbc3b7 6425 mirror_ingress_packet(&ctx);
9583bc14 6426 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
fff1b9c0
JR
6427 if (ctx.error) {
6428 goto exit;
6429 }
9583bc14
EJ
6430
6431 /* We've let OFPP_NORMAL and the learning action look at the
1d361a81 6432 * packet, so cancel all actions and freezing if forwarding is
8a5fb3b4 6433 * disabled. */
9efd308e
DV
6434 if (in_port && (!xport_stp_forward_state(in_port) ||
6435 !xport_rstp_forward_state(in_port))) {
1520ef4f 6436 ctx.odp_actions->size = sample_actions_len;
1d361a81 6437 ctx_cancel_freeze(&ctx);
8a5fb3b4
BP
6438 ofpbuf_clear(&ctx.action_set);
6439 }
6440
1d361a81 6441 if (!ctx.freezing) {
8a5fb3b4 6442 xlate_action_set(&ctx);
e672ff9b 6443 }
1d361a81 6444 if (ctx.freezing) {
77ab5fd2 6445 finish_freezing(&ctx);
9583bc14
EJ
6446 }
6447 }
6448
e672ff9b 6449 /* Output only fully processed packets. */
1d361a81 6450 if (!ctx.freezing
e672ff9b 6451 && xbridge->has_in_band
ce4a6b76
BP
6452 && in_band_must_output_to_local_port(flow)
6453 && !actions_output_to_local_port(&ctx)) {
e93ef1c7 6454 compose_output_action(&ctx, OFPP_LOCAL, NULL);
9583bc14 6455 }
aaa0fbae 6456
a6092018
BP
6457 if (user_cookie_offset) {
6458 fix_sflow_action(&ctx, user_cookie_offset);
e672ff9b 6459 }
9583bc14
EJ
6460 }
6461
1520ef4f 6462 if (nl_attr_oversized(ctx.odp_actions->size)) {
542024c4 6463 /* These datapath actions are too big for a Netlink attribute, so we
0f032e95
BP
6464 * can't hand them to the kernel directly. dpif_execute() can execute
6465 * them one by one with help, so just mark the result as SLOW_ACTION to
6466 * prevent the flow from being installed. */
6467 COVERAGE_INC(xlate_actions_oversize);
6468 ctx.xout->slow |= SLOW_ACTION;
1520ef4f 6469 } else if (too_many_output_actions(ctx.odp_actions)) {
7d031d7e
BP
6470 COVERAGE_INC(xlate_actions_too_many_output);
6471 ctx.xout->slow |= SLOW_ACTION;
542024c4
BP
6472 }
6473
64fb5f82
JP
6474 /* Update NetFlow for non-frozen traffic. */
6475 if (xbridge->netflow && !xin->frozen_state) {
1806291d
BP
6476 if (ctx.xin->resubmit_stats) {
6477 netflow_flow_update(xbridge->netflow, flow,
2031ef97 6478 ctx.nf_output_iface,
1806291d
BP
6479 ctx.xin->resubmit_stats);
6480 }
6481 if (ctx.xin->xcache) {
6482 struct xc_entry *entry;
b256dc52 6483
1806291d 6484 entry = xlate_cache_add_entry(ctx.xin->xcache, XC_NETFLOW);
901a517e
JR
6485 entry->nf.netflow = netflow_ref(xbridge->netflow);
6486 entry->nf.flow = xmemdup(flow, sizeof *flow);
6487 entry->nf.iface = ctx.nf_output_iface;
d6fc5f57
EJ
6488 }
6489 }
6490
8d8ab6c2
JG
6491 /* Translate tunnel metadata masks to udpif format if necessary. */
6492 if (xin->upcall_flow->tunnel.flags & FLOW_TNL_F_UDPIF) {
6493 if (ctx.wc->masks.tunnel.metadata.present.map) {
6494 const struct flow_tnl *upcall_tnl = &xin->upcall_flow->tunnel;
6495 struct geneve_opt opts[TLV_TOT_OPT_SIZE /
6496 sizeof(struct geneve_opt)];
6497
6498 tun_metadata_to_geneve_udpif_mask(&flow->tunnel,
6499 &ctx.wc->masks.tunnel,
6500 upcall_tnl->metadata.opts.gnv,
6501 upcall_tnl->metadata.present.len,
6502 opts);
6503 memset(&ctx.wc->masks.tunnel.metadata, 0,
6504 sizeof ctx.wc->masks.tunnel.metadata);
6505 memcpy(&ctx.wc->masks.tunnel.metadata.opts.gnv, opts,
6506 upcall_tnl->metadata.present.len);
6507 }
6508 ctx.wc->masks.tunnel.metadata.present.len = 0xff;
6509 ctx.wc->masks.tunnel.metadata.tab = NULL;
6510 ctx.wc->masks.tunnel.flags |= FLOW_TNL_F_UDPIF;
6511 } else if (!xin->upcall_flow->tunnel.metadata.tab) {
6512 /* If we didn't have options in UDPIF format and didn't have an existing
6513 * metadata table, then it means that there were no options at all when
6514 * we started processing and any wildcards we picked up were from
6515 * action generation. Without options on the incoming packet, wildcards
6516 * aren't meaningful. To avoid them possibly getting misinterpreted,
6517 * just clear everything. */
6518 if (ctx.wc->masks.tunnel.metadata.present.map) {
6519 memset(&ctx.wc->masks.tunnel.metadata, 0,
6520 sizeof ctx.wc->masks.tunnel.metadata);
6521 } else {
6522 ctx.wc->masks.tunnel.metadata.tab = NULL;
6523 }
6524 }
6525
c0e638aa 6526 xlate_wc_finish(&ctx);
1520ef4f
BP
6527
6528exit:
8d8ab6c2
JG
6529 /* Reset the table to what it was when we came in. If we only fetched
6530 * it locally, then it has no meaning outside of flow translation. */
6531 flow->tunnel.metadata.tab = xin->upcall_flow->tunnel.metadata.tab;
6532
1520ef4f
BP
6533 ofpbuf_uninit(&ctx.stack);
6534 ofpbuf_uninit(&ctx.action_set);
1d361a81 6535 ofpbuf_uninit(&ctx.frozen_actions);
1520ef4f 6536 ofpbuf_uninit(&scratch_actions);
fff1b9c0
JR
6537
6538 /* Make sure we return a "drop flow" in case of an error. */
6539 if (ctx.error) {
6540 xout->slow = 0;
6541 if (xin->odp_actions) {
6542 ofpbuf_clear(xin->odp_actions);
6543 }
6544 }
6545 return ctx.error;
91d6cd12
AW
6546}
6547
77ab5fd2
BP
6548enum ofperr
6549xlate_resume(struct ofproto_dpif *ofproto,
6550 const struct ofputil_packet_in_private *pin,
6551 struct ofpbuf *odp_actions,
6552 enum slow_path_reason *slow)
6553{
6554 struct dp_packet packet;
6555 dp_packet_use_const(&packet, pin->public.packet,
6556 pin->public.packet_len);
6557
6558 struct flow flow;
6559 flow_extract(&packet, &flow);
6560
6561 struct xlate_in xin;
1f4a8933
JR
6562 xlate_in_init(&xin, ofproto, ofproto_dpif_get_tables_version(ofproto),
6563 &flow, 0, NULL, ntohs(flow.tcp_flags),
77ab5fd2
BP
6564 &packet, NULL, odp_actions);
6565
6566 struct ofpact_note noop;
6567 ofpact_init_NOTE(&noop);
6568 noop.length = 0;
6569
6570 bool any_actions = pin->actions_len > 0;
6571 struct frozen_state state = {
6572 .table_id = 0, /* Not the table where NXAST_PAUSE was executed. */
6573 .ofproto_uuid = pin->bridge,
6574 .stack = pin->stack,
84cf3c1f 6575 .stack_size = pin->stack_size,
77ab5fd2
BP
6576 .mirrors = pin->mirrors,
6577 .conntracked = pin->conntracked,
6578
6579 /* When there are no actions, xlate_actions() will search the flow
6580 * table. We don't want it to do that (we want it to resume), so
6581 * supply a no-op action if there aren't any.
6582 *
6583 * (We can't necessarily avoid translating actions entirely if there
6584 * aren't any actions, because there might be some finishing-up to do
6585 * at the end of the pipeline, and we don't check for those
6586 * conditions.) */
6587 .ofpacts = any_actions ? pin->actions : &noop.ofpact,
6588 .ofpacts_len = any_actions ? pin->actions_len : sizeof noop,
6589
6590 .action_set = pin->action_set,
6591 .action_set_len = pin->action_set_len,
6592 };
6593 frozen_metadata_from_flow(&state.metadata,
6594 &pin->public.flow_metadata.flow);
6595 xin.frozen_state = &state;
6596
6597 struct xlate_out xout;
6598 enum xlate_error error = xlate_actions(&xin, &xout);
6599 *slow = xout.slow;
6600 xlate_out_uninit(&xout);
6601
6602 /* xlate_actions() can generate a number of errors, but only
6603 * XLATE_BRIDGE_NOT_FOUND really stands out to me as one that we should be
6604 * sure to report over OpenFlow. The others could come up in packet-outs
6605 * or regular flow translation and I don't think that it's going to be too
6606 * useful to report them to the controller. */
6607 return error == XLATE_BRIDGE_NOT_FOUND ? OFPERR_NXR_STALE : 0;
6608}
6609
2eb79142
JG
6610/* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
6611 * supports a notion of an OAM flag, sets it if 'oam' is true.
91d6cd12
AW
6612 * May modify 'packet'.
6613 * Returns 0 if successful, otherwise a positive errno value. */
6614int
2eb79142
JG
6615xlate_send_packet(const struct ofport_dpif *ofport, bool oam,
6616 struct dp_packet *packet)
91d6cd12 6617{
84f0f298 6618 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
91d6cd12 6619 struct xport *xport;
2eb79142
JG
6620 uint64_t ofpacts_stub[1024 / 8];
6621 struct ofpbuf ofpacts;
91d6cd12 6622 struct flow flow;
91d6cd12 6623
2eb79142 6624 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
91d6cd12 6625 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
cf62fa4c 6626 flow_extract(packet, &flow);
b5e7e61a 6627 flow.in_port.ofp_port = OFPP_NONE;
91d6cd12 6628
84f0f298 6629 xport = xport_lookup(xcfg, ofport);
91d6cd12 6630 if (!xport) {
02ea2703 6631 return EINVAL;
91d6cd12 6632 }
2eb79142
JG
6633
6634 if (oam) {
128684a6
JR
6635 const ovs_be16 oam = htons(NX_TUN_FLAG_OAM);
6636 ofpact_put_set_field(&ofpacts, mf_from_id(MFF_TUN_FLAGS), &oam, &oam);
2eb79142
JG
6637 }
6638
6639 ofpact_put_OUTPUT(&ofpacts)->port = xport->ofp_port;
e491a67a 6640
1f4a8933
JR
6641 /* Actions here are not referring to anything versionable (flow tables or
6642 * groups) so we don't need to worry about the version here. */
6643 return ofproto_dpif_execute_actions(xport->xbridge->ofproto,
6644 OVS_VERSION_MAX, &flow, NULL,
2eb79142 6645 ofpacts.data, ofpacts.size, packet);
9583bc14 6646}
b256dc52 6647
901a517e 6648void
064799a1
JR
6649xlate_mac_learning_update(const struct ofproto_dpif *ofproto,
6650 ofp_port_t in_port, struct eth_addr dl_src,
6651 int vlan, bool is_grat_arp)
b256dc52 6652{
84f0f298 6653 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
b256dc52
JS
6654 struct xbridge *xbridge;
6655 struct xbundle *xbundle;
b256dc52 6656
84f0f298 6657 xbridge = xbridge_lookup(xcfg, ofproto);
b256dc52
JS
6658 if (!xbridge) {
6659 return;
6660 }
6661
2d9b49dd 6662 xbundle = lookup_input_bundle__(xbridge, in_port, NULL);
b256dc52
JS
6663 if (!xbundle) {
6664 return;
6665 }
6666
2d9b49dd 6667 update_learning_table__(xbridge, xbundle, dl_src, vlan, is_grat_arp);
b256dc52 6668}
bef503e8
AZ
6669
6670void
6671xlate_disable_dp_clone(const struct ofproto_dpif *ofproto)
6672{
6673 struct xlate_cfg *xcfg = ovsrcu_get(struct xlate_cfg *, &xcfgp);
6674 struct xbridge *xbridge = xbridge_lookup(xcfg, ofproto);
6675
6676 if (xbridge) {
6677 xbridge->support.clone = false;
6678 }
6679}