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