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