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