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