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