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