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