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