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