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