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