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