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