]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-xlate.c
bfd: Send FINAL immediately after receiving POLL.
[mirror_ovs.git] / ofproto / ofproto-dpif-xlate.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "meta-flow.h"
37 #include "multipath.h"
38 #include "netdev-vport.h"
39 #include "netlink.h"
40 #include "nx-match.h"
41 #include "odp-execute.h"
42 #include "ofp-actions.h"
43 #include "ofproto/ofproto-dpif-ipfix.h"
44 #include "ofproto/ofproto-dpif-mirror.h"
45 #include "ofproto/ofproto-dpif-monitor.h"
46 #include "ofproto/ofproto-dpif-sflow.h"
47 #include "ofproto/ofproto-dpif.h"
48 #include "ofproto/ofproto-provider.h"
49 #include "tunnel.h"
50 #include "vlog.h"
51
52 COVERAGE_DEFINE(xlate_actions);
53 COVERAGE_DEFINE(xlate_actions_oversize);
54
55 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_xlate);
56
57 /* Maximum depth of flow table recursion (due to resubmit actions) in a
58 * flow translation. */
59 #define MAX_RESUBMIT_RECURSION 64
60
61 /* Maximum number of resubmit actions in a flow translation, whether they are
62 * recursive or not. */
63 #define MAX_RESUBMITS (MAX_RESUBMIT_RECURSION * MAX_RESUBMIT_RECURSION)
64
65 struct ovs_rwlock xlate_rwlock = OVS_RWLOCK_INITIALIZER;
66
67 struct xbridge {
68 struct hmap_node hmap_node; /* Node in global 'xbridges' map. */
69 struct ofproto_dpif *ofproto; /* Key in global 'xbridges' map. */
70
71 struct list xbundles; /* Owned xbundles. */
72 struct hmap xports; /* Indexed by ofp_port. */
73
74 char *name; /* Name used in log messages. */
75 struct dpif *dpif; /* Datapath interface. */
76 struct mac_learning *ml; /* Mac learning handle. */
77 struct mbridge *mbridge; /* Mirroring. */
78 struct dpif_sflow *sflow; /* SFlow handle, or null. */
79 struct dpif_ipfix *ipfix; /* Ipfix handle, or null. */
80 struct netflow *netflow; /* Netflow handle, or null. */
81 struct stp *stp; /* STP or null if disabled. */
82
83 /* Special rules installed by ofproto-dpif. */
84 struct rule_dpif *miss_rule;
85 struct rule_dpif *no_packet_in_rule;
86
87 enum ofp_config_flags frag; /* Fragmentation handling. */
88 bool has_in_band; /* Bridge has in band control? */
89 bool forward_bpdu; /* Bridge forwards STP BPDUs? */
90 };
91
92 struct xbundle {
93 struct hmap_node hmap_node; /* In global 'xbundles' map. */
94 struct ofbundle *ofbundle; /* Key in global 'xbundles' map. */
95
96 struct list list_node; /* In parent 'xbridges' list. */
97 struct xbridge *xbridge; /* Parent xbridge. */
98
99 struct list xports; /* Contains "struct xport"s. */
100
101 char *name; /* Name used in log messages. */
102 struct bond *bond; /* Nonnull iff more than one port. */
103 struct lacp *lacp; /* LACP handle or null. */
104
105 enum port_vlan_mode vlan_mode; /* VLAN mode. */
106 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
107 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
108 * NULL if all VLANs are trunked. */
109 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
110 bool floodable; /* No port has OFPUTIL_PC_NO_FLOOD set? */
111 };
112
113 struct xport {
114 struct hmap_node hmap_node; /* Node in global 'xports' map. */
115 struct ofport_dpif *ofport; /* Key in global 'xports map. */
116
117 struct hmap_node ofp_node; /* Node in parent xbridge 'xports' map. */
118 ofp_port_t ofp_port; /* Key in parent xbridge 'xports' map. */
119
120 odp_port_t odp_port; /* Datapath port number or ODPP_NONE. */
121
122 struct list bundle_node; /* In parent xbundle (if it exists). */
123 struct xbundle *xbundle; /* Parent xbundle or null. */
124
125 struct netdev *netdev; /* 'ofport''s netdev. */
126
127 struct xbridge *xbridge; /* Parent bridge. */
128 struct xport *peer; /* Patch port peer or null. */
129
130 enum ofputil_port_config config; /* OpenFlow port configuration. */
131 enum ofputil_port_state state; /* OpenFlow port state. */
132 int stp_port_no; /* STP port number or -1 if not in use. */
133
134 struct hmap skb_priorities; /* Map of 'skb_priority_to_dscp's. */
135
136 bool may_enable; /* May be enabled in bonds. */
137 bool is_tunnel; /* Is a tunnel port. */
138
139 struct cfm *cfm; /* CFM handle or null. */
140 struct bfd *bfd; /* BFD handle or null. */
141 };
142
143 struct xlate_ctx {
144 struct xlate_in *xin;
145 struct xlate_out *xout;
146
147 const struct xbridge *xbridge;
148
149 /* Flow at the last commit. */
150 struct flow base_flow;
151
152 /* Tunnel IP destination address as received. This is stored separately
153 * as the base_flow.tunnel is cleared on init to reflect the datapath
154 * behavior. Used to make sure not to send tunneled output to ourselves,
155 * which might lead to an infinite loop. This could happen easily
156 * if a tunnel is marked as 'ip_remote=flow', and the flow does not
157 * actually set the tun_dst field. */
158 ovs_be32 orig_tunnel_ip_dst;
159
160 /* Stack for the push and pop actions. Each stack element is of type
161 * "union mf_subvalue". */
162 union mf_subvalue init_stack[1024 / sizeof(union mf_subvalue)];
163 struct ofpbuf stack;
164
165 /* The rule that we are currently translating, or NULL. */
166 struct rule_dpif *rule;
167
168 int mpls_depth_delta; /* Delta of the mpls stack depth since
169 * actions were last committed.
170 * Must be between -1 and 1 inclusive. */
171 ovs_be32 pre_push_mpls_lse; /* Used to record the top-most MPLS LSE
172 * prior to an mpls_push so that it may be
173 * used for a subsequent mpls_pop. */
174
175 /* Resubmit statistics, via xlate_table_action(). */
176 int recurse; /* Current resubmit nesting depth. */
177 int resubmits; /* Total number of resubmits. */
178
179 uint32_t orig_skb_priority; /* Priority when packet arrived. */
180 uint8_t table_id; /* OpenFlow table ID where flow was found. */
181 uint32_t sflow_n_outputs; /* Number of output ports. */
182 odp_port_t sflow_odp_port; /* Output port for composing sFlow action. */
183 uint16_t user_cookie_offset;/* Used for user_action_cookie fixup. */
184 bool exit; /* No further actions should be processed. */
185
186 /* OpenFlow 1.1+ action set.
187 *
188 * 'action_set' accumulates "struct ofpact"s added by OFPACT_WRITE_ACTIONS.
189 * When translation is otherwise complete, ofpacts_execute_action_set()
190 * converts it to a set of "struct ofpact"s that can be translated into
191 * datapath actions. */
192 struct ofpbuf action_set; /* Action set. */
193 uint64_t action_set_stub[1024 / 8];
194 };
195
196 /* A controller may use OFPP_NONE as the ingress port to indicate that
197 * it did not arrive on a "real" port. 'ofpp_none_bundle' exists for
198 * when an input bundle is needed for validation (e.g., mirroring or
199 * OFPP_NORMAL processing). It is not connected to an 'ofproto' or have
200 * any 'port' structs, so care must be taken when dealing with it.
201 * The bundle's name and vlan mode are initialized in lookup_input_bundle() */
202 static struct xbundle ofpp_none_bundle;
203
204 /* Node in 'xport''s 'skb_priorities' map. Used to maintain a map from
205 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
206 * traffic egressing the 'ofport' with that priority should be marked with. */
207 struct skb_priority_to_dscp {
208 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'skb_priorities'. */
209 uint32_t skb_priority; /* Priority of this queue (see struct flow). */
210
211 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
212 };
213
214 static struct hmap xbridges = HMAP_INITIALIZER(&xbridges);
215 static struct hmap xbundles = HMAP_INITIALIZER(&xbundles);
216 static struct hmap xports = HMAP_INITIALIZER(&xports);
217
218 static bool may_receive(const struct xport *, struct xlate_ctx *);
219 static void do_xlate_actions(const struct ofpact *, size_t ofpacts_len,
220 struct xlate_ctx *);
221 static void xlate_actions__(struct xlate_in *, struct xlate_out *)
222 OVS_REQ_RDLOCK(xlate_rwlock);
223 static void xlate_normal(struct xlate_ctx *);
224 static void xlate_report(struct xlate_ctx *, const char *);
225 static void xlate_table_action(struct xlate_ctx *, ofp_port_t in_port,
226 uint8_t table_id, bool may_packet_in);
227 static bool input_vid_is_valid(uint16_t vid, struct xbundle *, bool warn);
228 static uint16_t input_vid_to_vlan(const struct xbundle *, uint16_t vid);
229 static void output_normal(struct xlate_ctx *, const struct xbundle *,
230 uint16_t vlan);
231 static void compose_output_action(struct xlate_ctx *, ofp_port_t ofp_port);
232
233 static struct xbridge *xbridge_lookup(const struct ofproto_dpif *);
234 static struct xbundle *xbundle_lookup(const struct ofbundle *);
235 static struct xport *xport_lookup(const struct ofport_dpif *);
236 static struct xport *get_ofp_port(const struct xbridge *, ofp_port_t ofp_port);
237 static struct skb_priority_to_dscp *get_skb_priority(const struct xport *,
238 uint32_t skb_priority);
239 static void clear_skb_priorities(struct xport *);
240 static bool dscp_from_skb_priority(const struct xport *, uint32_t skb_priority,
241 uint8_t *dscp);
242
243 void
244 xlate_ofproto_set(struct ofproto_dpif *ofproto, const char *name,
245 struct dpif *dpif, struct rule_dpif *miss_rule,
246 struct rule_dpif *no_packet_in_rule,
247 const struct mac_learning *ml, struct stp *stp,
248 const struct mbridge *mbridge,
249 const struct dpif_sflow *sflow,
250 const struct dpif_ipfix *ipfix,
251 const struct netflow *netflow, enum ofp_config_flags frag,
252 bool forward_bpdu, bool has_in_band)
253 {
254 struct xbridge *xbridge = xbridge_lookup(ofproto);
255
256 if (!xbridge) {
257 xbridge = xzalloc(sizeof *xbridge);
258 xbridge->ofproto = ofproto;
259
260 hmap_insert(&xbridges, &xbridge->hmap_node, hash_pointer(ofproto, 0));
261 hmap_init(&xbridge->xports);
262 list_init(&xbridge->xbundles);
263 }
264
265 if (xbridge->ml != ml) {
266 mac_learning_unref(xbridge->ml);
267 xbridge->ml = mac_learning_ref(ml);
268 }
269
270 if (xbridge->mbridge != mbridge) {
271 mbridge_unref(xbridge->mbridge);
272 xbridge->mbridge = mbridge_ref(mbridge);
273 }
274
275 if (xbridge->sflow != sflow) {
276 dpif_sflow_unref(xbridge->sflow);
277 xbridge->sflow = dpif_sflow_ref(sflow);
278 }
279
280 if (xbridge->ipfix != ipfix) {
281 dpif_ipfix_unref(xbridge->ipfix);
282 xbridge->ipfix = dpif_ipfix_ref(ipfix);
283 }
284
285 if (xbridge->stp != stp) {
286 stp_unref(xbridge->stp);
287 xbridge->stp = stp_ref(stp);
288 }
289
290 if (xbridge->netflow != netflow) {
291 netflow_unref(xbridge->netflow);
292 xbridge->netflow = netflow_ref(netflow);
293 }
294
295 free(xbridge->name);
296 xbridge->name = xstrdup(name);
297
298 xbridge->dpif = dpif;
299 xbridge->forward_bpdu = forward_bpdu;
300 xbridge->has_in_band = has_in_band;
301 xbridge->frag = frag;
302 xbridge->miss_rule = miss_rule;
303 xbridge->no_packet_in_rule = no_packet_in_rule;
304 }
305
306 void
307 xlate_remove_ofproto(struct ofproto_dpif *ofproto)
308 {
309 struct xbridge *xbridge = xbridge_lookup(ofproto);
310 struct xbundle *xbundle, *next_xbundle;
311 struct xport *xport, *next_xport;
312
313 if (!xbridge) {
314 return;
315 }
316
317 HMAP_FOR_EACH_SAFE (xport, next_xport, ofp_node, &xbridge->xports) {
318 xlate_ofport_remove(xport->ofport);
319 }
320
321 LIST_FOR_EACH_SAFE (xbundle, next_xbundle, list_node, &xbridge->xbundles) {
322 xlate_bundle_remove(xbundle->ofbundle);
323 }
324
325 hmap_remove(&xbridges, &xbridge->hmap_node);
326 mac_learning_unref(xbridge->ml);
327 mbridge_unref(xbridge->mbridge);
328 dpif_sflow_unref(xbridge->sflow);
329 dpif_ipfix_unref(xbridge->ipfix);
330 stp_unref(xbridge->stp);
331 hmap_destroy(&xbridge->xports);
332 free(xbridge->name);
333 free(xbridge);
334 }
335
336 void
337 xlate_bundle_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
338 const char *name, enum port_vlan_mode vlan_mode, int vlan,
339 unsigned long *trunks, bool use_priority_tags,
340 const struct bond *bond, const struct lacp *lacp,
341 bool floodable)
342 {
343 struct xbundle *xbundle = xbundle_lookup(ofbundle);
344
345 if (!xbundle) {
346 xbundle = xzalloc(sizeof *xbundle);
347 xbundle->ofbundle = ofbundle;
348 xbundle->xbridge = xbridge_lookup(ofproto);
349
350 hmap_insert(&xbundles, &xbundle->hmap_node, hash_pointer(ofbundle, 0));
351 list_insert(&xbundle->xbridge->xbundles, &xbundle->list_node);
352 list_init(&xbundle->xports);
353 }
354
355 ovs_assert(xbundle->xbridge);
356
357 free(xbundle->name);
358 xbundle->name = xstrdup(name);
359
360 xbundle->vlan_mode = vlan_mode;
361 xbundle->vlan = vlan;
362 xbundle->trunks = trunks;
363 xbundle->use_priority_tags = use_priority_tags;
364 xbundle->floodable = floodable;
365
366 if (xbundle->bond != bond) {
367 bond_unref(xbundle->bond);
368 xbundle->bond = bond_ref(bond);
369 }
370
371 if (xbundle->lacp != lacp) {
372 lacp_unref(xbundle->lacp);
373 xbundle->lacp = lacp_ref(lacp);
374 }
375 }
376
377 void
378 xlate_bundle_remove(struct ofbundle *ofbundle)
379 {
380 struct xbundle *xbundle = xbundle_lookup(ofbundle);
381 struct xport *xport, *next;
382
383 if (!xbundle) {
384 return;
385 }
386
387 LIST_FOR_EACH_SAFE (xport, next, bundle_node, &xbundle->xports) {
388 list_remove(&xport->bundle_node);
389 xport->xbundle = NULL;
390 }
391
392 hmap_remove(&xbundles, &xbundle->hmap_node);
393 list_remove(&xbundle->list_node);
394 bond_unref(xbundle->bond);
395 lacp_unref(xbundle->lacp);
396 free(xbundle->name);
397 free(xbundle);
398 }
399
400 void
401 xlate_ofport_set(struct ofproto_dpif *ofproto, struct ofbundle *ofbundle,
402 struct ofport_dpif *ofport, ofp_port_t ofp_port,
403 odp_port_t odp_port, const struct netdev *netdev,
404 const struct cfm *cfm, const struct bfd *bfd,
405 struct ofport_dpif *peer, int stp_port_no,
406 const struct ofproto_port_queue *qdscp_list, size_t n_qdscp,
407 enum ofputil_port_config config,
408 enum ofputil_port_state state, bool is_tunnel,
409 bool may_enable)
410 {
411 struct xport *xport = xport_lookup(ofport);
412 size_t i;
413
414 if (!xport) {
415 xport = xzalloc(sizeof *xport);
416 xport->ofport = ofport;
417 xport->xbridge = xbridge_lookup(ofproto);
418 xport->ofp_port = ofp_port;
419
420 hmap_init(&xport->skb_priorities);
421 hmap_insert(&xports, &xport->hmap_node, hash_pointer(ofport, 0));
422 hmap_insert(&xport->xbridge->xports, &xport->ofp_node,
423 hash_ofp_port(xport->ofp_port));
424 }
425
426 ovs_assert(xport->ofp_port == ofp_port);
427
428 xport->config = config;
429 xport->state = state;
430 xport->stp_port_no = stp_port_no;
431 xport->is_tunnel = is_tunnel;
432 xport->may_enable = may_enable;
433 xport->odp_port = odp_port;
434
435 if (xport->netdev != netdev) {
436 netdev_close(xport->netdev);
437 xport->netdev = netdev_ref(netdev);
438 }
439
440 if (xport->cfm != cfm) {
441 cfm_unref(xport->cfm);
442 xport->cfm = cfm_ref(cfm);
443 }
444
445 if (xport->bfd != bfd) {
446 bfd_unref(xport->bfd);
447 xport->bfd = bfd_ref(bfd);
448 }
449
450 if (xport->peer) {
451 xport->peer->peer = NULL;
452 }
453 xport->peer = xport_lookup(peer);
454 if (xport->peer) {
455 xport->peer->peer = xport;
456 }
457
458 if (xport->xbundle) {
459 list_remove(&xport->bundle_node);
460 }
461 xport->xbundle = xbundle_lookup(ofbundle);
462 if (xport->xbundle) {
463 list_insert(&xport->xbundle->xports, &xport->bundle_node);
464 }
465
466 clear_skb_priorities(xport);
467 for (i = 0; i < n_qdscp; i++) {
468 struct skb_priority_to_dscp *pdscp;
469 uint32_t skb_priority;
470
471 if (dpif_queue_to_priority(xport->xbridge->dpif, qdscp_list[i].queue,
472 &skb_priority)) {
473 continue;
474 }
475
476 pdscp = xmalloc(sizeof *pdscp);
477 pdscp->skb_priority = skb_priority;
478 pdscp->dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
479 hmap_insert(&xport->skb_priorities, &pdscp->hmap_node,
480 hash_int(pdscp->skb_priority, 0));
481 }
482 }
483
484 void
485 xlate_ofport_remove(struct ofport_dpif *ofport)
486 {
487 struct xport *xport = xport_lookup(ofport);
488
489 if (!xport) {
490 return;
491 }
492
493 if (xport->peer) {
494 xport->peer->peer = NULL;
495 xport->peer = NULL;
496 }
497
498 if (xport->xbundle) {
499 list_remove(&xport->bundle_node);
500 }
501
502 clear_skb_priorities(xport);
503 hmap_destroy(&xport->skb_priorities);
504
505 hmap_remove(&xports, &xport->hmap_node);
506 hmap_remove(&xport->xbridge->xports, &xport->ofp_node);
507
508 netdev_close(xport->netdev);
509 cfm_unref(xport->cfm);
510 bfd_unref(xport->bfd);
511 free(xport);
512 }
513
514 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
515 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
516 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
517 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
518 * with the ofproto_dpif, 'odp_in_port' with the datapath in_port, that
519 * 'packet' ingressed, and 'ipfix', 'sflow', and 'netflow' with the appropriate
520 * handles for those protocols if they're enabled. Caller is responsible for
521 * unrefing them.
522 *
523 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
524 * 'flow''s in_port to OFPP_NONE.
525 *
526 * This function does post-processing on data returned from
527 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
528 * of the upcall processing logic. In particular, if the extracted in_port is
529 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
530 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
531 * a VLAN header onto 'packet' (if it is nonnull).
532 *
533 * Similarly, this function also includes some logic to help with tunnels. It
534 * may modify 'flow' as necessary to make the tunneling implementation
535 * transparent to the upcall processing logic.
536 *
537 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
538 * or some other positive errno if there are other problems. */
539 int
540 xlate_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
541 const struct nlattr *key, size_t key_len,
542 struct flow *flow, enum odp_key_fitness *fitnessp,
543 struct ofproto_dpif **ofproto, struct dpif_ipfix **ipfix,
544 struct dpif_sflow **sflow, struct netflow **netflow,
545 odp_port_t *odp_in_port)
546 {
547 enum odp_key_fitness fitness;
548 const struct xport *xport;
549 int error = ENODEV;
550
551 ovs_rwlock_rdlock(&xlate_rwlock);
552 fitness = odp_flow_key_to_flow(key, key_len, flow);
553 if (fitness == ODP_FIT_ERROR) {
554 error = EINVAL;
555 goto exit;
556 }
557
558 if (odp_in_port) {
559 *odp_in_port = flow->in_port.odp_port;
560 }
561
562 xport = xport_lookup(tnl_port_should_receive(flow)
563 ? tnl_port_receive(flow)
564 : odp_port_to_ofport(backer, flow->in_port.odp_port));
565
566 flow->in_port.ofp_port = xport ? xport->ofp_port : OFPP_NONE;
567 if (!xport) {
568 goto exit;
569 }
570
571 if (vsp_adjust_flow(xport->xbridge->ofproto, flow)) {
572 if (packet) {
573 /* Make the packet resemble the flow, so that it gets sent to
574 * an OpenFlow controller properly, so that it looks correct
575 * for sFlow, and so that flow_extract() will get the correct
576 * vlan_tci if it is called on 'packet'. */
577 eth_push_vlan(packet, flow->vlan_tci);
578 }
579 /* We can't reproduce 'key' from 'flow'. */
580 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
581 }
582 error = 0;
583
584 if (ofproto) {
585 *ofproto = xport->xbridge->ofproto;
586 }
587
588 if (ipfix) {
589 *ipfix = dpif_ipfix_ref(xport->xbridge->ipfix);
590 }
591
592 if (sflow) {
593 *sflow = dpif_sflow_ref(xport->xbridge->sflow);
594 }
595
596 if (netflow) {
597 *netflow = netflow_ref(xport->xbridge->netflow);
598 }
599
600 exit:
601 if (fitnessp) {
602 *fitnessp = fitness;
603 }
604 ovs_rwlock_unlock(&xlate_rwlock);
605 return error;
606 }
607
608 static struct xbridge *
609 xbridge_lookup(const struct ofproto_dpif *ofproto)
610 {
611 struct xbridge *xbridge;
612
613 if (!ofproto) {
614 return NULL;
615 }
616
617 HMAP_FOR_EACH_IN_BUCKET (xbridge, hmap_node, hash_pointer(ofproto, 0),
618 &xbridges) {
619 if (xbridge->ofproto == ofproto) {
620 return xbridge;
621 }
622 }
623 return NULL;
624 }
625
626 static struct xbundle *
627 xbundle_lookup(const struct ofbundle *ofbundle)
628 {
629 struct xbundle *xbundle;
630
631 if (!ofbundle) {
632 return NULL;
633 }
634
635 HMAP_FOR_EACH_IN_BUCKET (xbundle, hmap_node, hash_pointer(ofbundle, 0),
636 &xbundles) {
637 if (xbundle->ofbundle == ofbundle) {
638 return xbundle;
639 }
640 }
641 return NULL;
642 }
643
644 static struct xport *
645 xport_lookup(const struct ofport_dpif *ofport)
646 {
647 struct xport *xport;
648
649 if (!ofport) {
650 return NULL;
651 }
652
653 HMAP_FOR_EACH_IN_BUCKET (xport, hmap_node, hash_pointer(ofport, 0),
654 &xports) {
655 if (xport->ofport == ofport) {
656 return xport;
657 }
658 }
659 return NULL;
660 }
661
662 static struct stp_port *
663 xport_get_stp_port(const struct xport *xport)
664 {
665 return xport->xbridge->stp && xport->stp_port_no != -1
666 ? stp_get_port(xport->xbridge->stp, xport->stp_port_no)
667 : NULL;
668 }
669
670 static enum stp_state
671 xport_stp_learn_state(const struct xport *xport)
672 {
673 struct stp_port *sp = xport_get_stp_port(xport);
674 return stp_learn_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
675 }
676
677 static bool
678 xport_stp_forward_state(const struct xport *xport)
679 {
680 struct stp_port *sp = xport_get_stp_port(xport);
681 return stp_forward_in_state(sp ? stp_port_get_state(sp) : STP_DISABLED);
682 }
683
684 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
685 * were used to make the determination.*/
686 static bool
687 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
688 {
689 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
690 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
691 }
692
693 static void
694 stp_process_packet(const struct xport *xport, const struct ofpbuf *packet)
695 {
696 struct stp_port *sp = xport_get_stp_port(xport);
697 struct ofpbuf payload = *packet;
698 struct eth_header *eth = payload.data;
699
700 /* Sink packets on ports that have STP disabled when the bridge has
701 * STP enabled. */
702 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
703 return;
704 }
705
706 /* Trim off padding on payload. */
707 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
708 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
709 }
710
711 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
712 stp_received_bpdu(sp, payload.data, payload.size);
713 }
714 }
715
716 static struct xport *
717 get_ofp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
718 {
719 struct xport *xport;
720
721 HMAP_FOR_EACH_IN_BUCKET (xport, ofp_node, hash_ofp_port(ofp_port),
722 &xbridge->xports) {
723 if (xport->ofp_port == ofp_port) {
724 return xport;
725 }
726 }
727 return NULL;
728 }
729
730 static odp_port_t
731 ofp_port_to_odp_port(const struct xbridge *xbridge, ofp_port_t ofp_port)
732 {
733 const struct xport *xport = get_ofp_port(xbridge, ofp_port);
734 return xport ? xport->odp_port : ODPP_NONE;
735 }
736
737 static bool
738 odp_port_is_alive(const struct xlate_ctx *ctx, ofp_port_t ofp_port)
739 {
740 struct xport *xport;
741
742 xport = get_ofp_port(ctx->xbridge, ofp_port);
743 if (!xport || xport->config & OFPUTIL_PC_PORT_DOWN ||
744 xport->state & OFPUTIL_PS_LINK_DOWN) {
745 return false;
746 }
747
748 return true;
749 }
750
751 static const struct ofputil_bucket *
752 group_first_live_bucket(const struct xlate_ctx *, const struct group_dpif *,
753 int depth);
754
755 static bool
756 group_is_alive(const struct xlate_ctx *ctx, uint32_t group_id, int depth)
757 {
758 struct group_dpif *group;
759 bool hit;
760
761 hit = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
762 if (!hit) {
763 return false;
764 }
765
766 hit = group_first_live_bucket(ctx, group, depth) != NULL;
767
768 group_dpif_release(group);
769 return hit;
770 }
771
772 #define MAX_LIVENESS_RECURSION 128 /* Arbitrary limit */
773
774 static bool
775 bucket_is_alive(const struct xlate_ctx *ctx,
776 const struct ofputil_bucket *bucket, int depth)
777 {
778 if (depth >= MAX_LIVENESS_RECURSION) {
779 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
780
781 VLOG_WARN_RL(&rl, "bucket chaining exceeded %d links",
782 MAX_LIVENESS_RECURSION);
783 return false;
784 }
785
786 return !ofputil_bucket_has_liveness(bucket) ||
787 (bucket->watch_port != OFPP_ANY &&
788 odp_port_is_alive(ctx, bucket->watch_port)) ||
789 (bucket->watch_group != OFPG_ANY &&
790 group_is_alive(ctx, bucket->watch_group, depth + 1));
791 }
792
793 static const struct ofputil_bucket *
794 group_first_live_bucket(const struct xlate_ctx *ctx,
795 const struct group_dpif *group, int depth)
796 {
797 struct ofputil_bucket *bucket;
798 const struct list *buckets;
799
800 group_dpif_get_buckets(group, &buckets);
801 LIST_FOR_EACH (bucket, list_node, buckets) {
802 if (bucket_is_alive(ctx, bucket, depth)) {
803 return bucket;
804 }
805 }
806
807 return NULL;
808 }
809
810 static const struct ofputil_bucket *
811 group_best_live_bucket(const struct xlate_ctx *ctx,
812 const struct group_dpif *group,
813 uint32_t basis)
814 {
815 const struct ofputil_bucket *best_bucket = NULL;
816 uint32_t best_score = 0;
817 int i = 0;
818
819 const struct ofputil_bucket *bucket;
820 const struct list *buckets;
821
822 group_dpif_get_buckets(group, &buckets);
823 LIST_FOR_EACH (bucket, list_node, buckets) {
824 if (bucket_is_alive(ctx, bucket, 0)) {
825 uint32_t score = (hash_int(i, basis) & 0xffff) * bucket->weight;
826 if (score >= best_score) {
827 best_bucket = bucket;
828 best_score = score;
829 }
830 }
831 i++;
832 }
833
834 return best_bucket;
835 }
836
837 static bool
838 xbundle_trunks_vlan(const struct xbundle *bundle, uint16_t vlan)
839 {
840 return (bundle->vlan_mode != PORT_VLAN_ACCESS
841 && (!bundle->trunks || bitmap_is_set(bundle->trunks, vlan)));
842 }
843
844 static bool
845 xbundle_includes_vlan(const struct xbundle *xbundle, uint16_t vlan)
846 {
847 return vlan == xbundle->vlan || xbundle_trunks_vlan(xbundle, vlan);
848 }
849
850 static mirror_mask_t
851 xbundle_mirror_out(const struct xbridge *xbridge, struct xbundle *xbundle)
852 {
853 return xbundle != &ofpp_none_bundle
854 ? mirror_bundle_out(xbridge->mbridge, xbundle->ofbundle)
855 : 0;
856 }
857
858 static mirror_mask_t
859 xbundle_mirror_src(const struct xbridge *xbridge, struct xbundle *xbundle)
860 {
861 return xbundle != &ofpp_none_bundle
862 ? mirror_bundle_src(xbridge->mbridge, xbundle->ofbundle)
863 : 0;
864 }
865
866 static mirror_mask_t
867 xbundle_mirror_dst(const struct xbridge *xbridge, struct xbundle *xbundle)
868 {
869 return xbundle != &ofpp_none_bundle
870 ? mirror_bundle_dst(xbridge->mbridge, xbundle->ofbundle)
871 : 0;
872 }
873
874 static struct xbundle *
875 lookup_input_bundle(const struct xbridge *xbridge, ofp_port_t in_port,
876 bool warn, struct xport **in_xportp)
877 {
878 struct xport *xport;
879
880 /* Find the port and bundle for the received packet. */
881 xport = get_ofp_port(xbridge, in_port);
882 if (in_xportp) {
883 *in_xportp = xport;
884 }
885 if (xport && xport->xbundle) {
886 return xport->xbundle;
887 }
888
889 /* Special-case OFPP_NONE, which a controller may use as the ingress
890 * port for traffic that it is sourcing. */
891 if (in_port == OFPP_NONE) {
892 ofpp_none_bundle.name = "OFPP_NONE";
893 ofpp_none_bundle.vlan_mode = PORT_VLAN_TRUNK;
894 return &ofpp_none_bundle;
895 }
896
897 /* Odd. A few possible reasons here:
898 *
899 * - We deleted a port but there are still a few packets queued up
900 * from it.
901 *
902 * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
903 * we don't know about.
904 *
905 * - The ofproto client didn't configure the port as part of a bundle.
906 * This is particularly likely to happen if a packet was received on the
907 * port after it was created, but before the client had a chance to
908 * configure its bundle.
909 */
910 if (warn) {
911 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
912
913 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
914 "port %"PRIu16, xbridge->name, in_port);
915 }
916 return NULL;
917 }
918
919 static void
920 add_mirror_actions(struct xlate_ctx *ctx, const struct flow *orig_flow)
921 {
922 const struct xbridge *xbridge = ctx->xbridge;
923 mirror_mask_t mirrors;
924 struct xbundle *in_xbundle;
925 uint16_t vlan;
926 uint16_t vid;
927
928 mirrors = ctx->xout->mirrors;
929 ctx->xout->mirrors = 0;
930
931 in_xbundle = lookup_input_bundle(xbridge, orig_flow->in_port.ofp_port,
932 ctx->xin->packet != NULL, NULL);
933 if (!in_xbundle) {
934 return;
935 }
936 mirrors |= xbundle_mirror_src(xbridge, in_xbundle);
937
938 /* Drop frames on bundles reserved for mirroring. */
939 if (xbundle_mirror_out(xbridge, in_xbundle)) {
940 if (ctx->xin->packet != NULL) {
941 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
942 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
943 "%s, which is reserved exclusively for mirroring",
944 ctx->xbridge->name, in_xbundle->name);
945 }
946 ofpbuf_clear(&ctx->xout->odp_actions);
947 return;
948 }
949
950 /* Check VLAN. */
951 vid = vlan_tci_to_vid(orig_flow->vlan_tci);
952 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
953 return;
954 }
955 vlan = input_vid_to_vlan(in_xbundle, vid);
956
957 if (!mirrors) {
958 return;
959 }
960
961 /* Restore the original packet before adding the mirror actions. */
962 ctx->xin->flow = *orig_flow;
963
964 while (mirrors) {
965 mirror_mask_t dup_mirrors;
966 struct ofbundle *out;
967 unsigned long *vlans;
968 bool vlan_mirrored;
969 bool has_mirror;
970 int out_vlan;
971
972 has_mirror = mirror_get(xbridge->mbridge, raw_ctz(mirrors),
973 &vlans, &dup_mirrors, &out, &out_vlan);
974 ovs_assert(has_mirror);
975
976 if (vlans) {
977 ctx->xout->wc.masks.vlan_tci |= htons(VLAN_CFI | VLAN_VID_MASK);
978 }
979 vlan_mirrored = !vlans || bitmap_is_set(vlans, vlan);
980 free(vlans);
981
982 if (!vlan_mirrored) {
983 mirrors = zero_rightmost_1bit(mirrors);
984 continue;
985 }
986
987 mirrors &= ~dup_mirrors;
988 ctx->xout->mirrors |= dup_mirrors;
989 if (out) {
990 struct xbundle *out_xbundle = xbundle_lookup(out);
991 if (out_xbundle) {
992 output_normal(ctx, out_xbundle, vlan);
993 }
994 } else if (vlan != out_vlan
995 && !eth_addr_is_reserved(orig_flow->dl_dst)) {
996 struct xbundle *xbundle;
997
998 LIST_FOR_EACH (xbundle, list_node, &xbridge->xbundles) {
999 if (xbundle_includes_vlan(xbundle, out_vlan)
1000 && !xbundle_mirror_out(xbridge, xbundle)) {
1001 output_normal(ctx, xbundle, out_vlan);
1002 }
1003 }
1004 }
1005 }
1006 }
1007
1008 /* Given 'vid', the VID obtained from the 802.1Q header that was received as
1009 * part of a packet (specify 0 if there was no 802.1Q header), and 'in_xbundle',
1010 * the bundle on which the packet was received, returns the VLAN to which the
1011 * packet belongs.
1012 *
1013 * Both 'vid' and the return value are in the range 0...4095. */
1014 static uint16_t
1015 input_vid_to_vlan(const struct xbundle *in_xbundle, uint16_t vid)
1016 {
1017 switch (in_xbundle->vlan_mode) {
1018 case PORT_VLAN_ACCESS:
1019 return in_xbundle->vlan;
1020 break;
1021
1022 case PORT_VLAN_TRUNK:
1023 return vid;
1024
1025 case PORT_VLAN_NATIVE_UNTAGGED:
1026 case PORT_VLAN_NATIVE_TAGGED:
1027 return vid ? vid : in_xbundle->vlan;
1028
1029 default:
1030 OVS_NOT_REACHED();
1031 }
1032 }
1033
1034 /* Checks whether a packet with the given 'vid' may ingress on 'in_xbundle'.
1035 * If so, returns true. Otherwise, returns false and, if 'warn' is true, logs
1036 * a warning.
1037 *
1038 * 'vid' should be the VID obtained from the 802.1Q header that was received as
1039 * part of a packet (specify 0 if there was no 802.1Q header), in the range
1040 * 0...4095. */
1041 static bool
1042 input_vid_is_valid(uint16_t vid, struct xbundle *in_xbundle, bool warn)
1043 {
1044 /* Allow any VID on the OFPP_NONE port. */
1045 if (in_xbundle == &ofpp_none_bundle) {
1046 return true;
1047 }
1048
1049 switch (in_xbundle->vlan_mode) {
1050 case PORT_VLAN_ACCESS:
1051 if (vid) {
1052 if (warn) {
1053 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1054 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" tagged "
1055 "packet received on port %s configured as VLAN "
1056 "%"PRIu16" access port", vid, in_xbundle->name,
1057 in_xbundle->vlan);
1058 }
1059 return false;
1060 }
1061 return true;
1062
1063 case PORT_VLAN_NATIVE_UNTAGGED:
1064 case PORT_VLAN_NATIVE_TAGGED:
1065 if (!vid) {
1066 /* Port must always carry its native VLAN. */
1067 return true;
1068 }
1069 /* Fall through. */
1070 case PORT_VLAN_TRUNK:
1071 if (!xbundle_includes_vlan(in_xbundle, vid)) {
1072 if (warn) {
1073 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1074 VLOG_WARN_RL(&rl, "dropping VLAN %"PRIu16" packet "
1075 "received on port %s not configured for trunking "
1076 "VLAN %"PRIu16, vid, in_xbundle->name, vid);
1077 }
1078 return false;
1079 }
1080 return true;
1081
1082 default:
1083 OVS_NOT_REACHED();
1084 }
1085
1086 }
1087
1088 /* Given 'vlan', the VLAN that a packet belongs to, and
1089 * 'out_xbundle', a bundle on which the packet is to be output, returns the VID
1090 * that should be included in the 802.1Q header. (If the return value is 0,
1091 * then the 802.1Q header should only be included in the packet if there is a
1092 * nonzero PCP.)
1093 *
1094 * Both 'vlan' and the return value are in the range 0...4095. */
1095 static uint16_t
1096 output_vlan_to_vid(const struct xbundle *out_xbundle, uint16_t vlan)
1097 {
1098 switch (out_xbundle->vlan_mode) {
1099 case PORT_VLAN_ACCESS:
1100 return 0;
1101
1102 case PORT_VLAN_TRUNK:
1103 case PORT_VLAN_NATIVE_TAGGED:
1104 return vlan;
1105
1106 case PORT_VLAN_NATIVE_UNTAGGED:
1107 return vlan == out_xbundle->vlan ? 0 : vlan;
1108
1109 default:
1110 OVS_NOT_REACHED();
1111 }
1112 }
1113
1114 static void
1115 output_normal(struct xlate_ctx *ctx, const struct xbundle *out_xbundle,
1116 uint16_t vlan)
1117 {
1118 ovs_be16 *flow_tci = &ctx->xin->flow.vlan_tci;
1119 uint16_t vid;
1120 ovs_be16 tci, old_tci;
1121 struct xport *xport;
1122
1123 vid = output_vlan_to_vid(out_xbundle, vlan);
1124 if (list_is_empty(&out_xbundle->xports)) {
1125 /* Partially configured bundle with no slaves. Drop the packet. */
1126 return;
1127 } else if (!out_xbundle->bond) {
1128 xport = CONTAINER_OF(list_front(&out_xbundle->xports), struct xport,
1129 bundle_node);
1130 } else {
1131 struct ofport_dpif *ofport;
1132
1133 ofport = bond_choose_output_slave(out_xbundle->bond, &ctx->xin->flow,
1134 &ctx->xout->wc, vid);
1135 xport = xport_lookup(ofport);
1136
1137 if (!xport) {
1138 /* No slaves enabled, so drop packet. */
1139 return;
1140 }
1141
1142 if (ctx->xin->resubmit_stats) {
1143 bond_account(out_xbundle->bond, &ctx->xin->flow, vid,
1144 ctx->xin->resubmit_stats->n_bytes);
1145 }
1146 }
1147
1148 old_tci = *flow_tci;
1149 tci = htons(vid);
1150 if (tci || out_xbundle->use_priority_tags) {
1151 tci |= *flow_tci & htons(VLAN_PCP_MASK);
1152 if (tci) {
1153 tci |= htons(VLAN_CFI);
1154 }
1155 }
1156 *flow_tci = tci;
1157
1158 compose_output_action(ctx, xport->ofp_port);
1159 *flow_tci = old_tci;
1160 }
1161
1162 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
1163 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
1164 * indicate this; newer upstream kernels use gratuitous ARP requests. */
1165 static bool
1166 is_gratuitous_arp(const struct flow *flow, struct flow_wildcards *wc)
1167 {
1168 if (flow->dl_type != htons(ETH_TYPE_ARP)) {
1169 return false;
1170 }
1171
1172 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1173 if (!eth_addr_is_broadcast(flow->dl_dst)) {
1174 return false;
1175 }
1176
1177 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1178 if (flow->nw_proto == ARP_OP_REPLY) {
1179 return true;
1180 } else if (flow->nw_proto == ARP_OP_REQUEST) {
1181 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1182 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1183
1184 return flow->nw_src == flow->nw_dst;
1185 } else {
1186 return false;
1187 }
1188 }
1189
1190 /* Checks whether a MAC learning update is necessary for MAC learning table
1191 * 'ml' given that a packet matching 'flow' was received on 'in_xbundle' in
1192 * 'vlan'.
1193 *
1194 * Most packets processed through the MAC learning table do not actually
1195 * change it in any way. This function requires only a read lock on the MAC
1196 * learning table, so it is much cheaper in this common case.
1197 *
1198 * Keep the code here synchronized with that in update_learning_table__()
1199 * below. */
1200 static bool
1201 is_mac_learning_update_needed(const struct mac_learning *ml,
1202 const struct flow *flow,
1203 struct flow_wildcards *wc,
1204 int vlan, struct xbundle *in_xbundle)
1205 OVS_REQ_RDLOCK(ml->rwlock)
1206 {
1207 struct mac_entry *mac;
1208
1209 if (!mac_learning_may_learn(ml, flow->dl_src, vlan)) {
1210 return false;
1211 }
1212
1213 mac = mac_learning_lookup(ml, flow->dl_src, vlan);
1214 if (!mac || mac_entry_age(ml, mac)) {
1215 return true;
1216 }
1217
1218 if (is_gratuitous_arp(flow, wc)) {
1219 /* We don't want to learn from gratuitous ARP packets that are
1220 * reflected back over bond slaves so we lock the learning table. */
1221 if (!in_xbundle->bond) {
1222 return true;
1223 } else if (mac_entry_is_grat_arp_locked(mac)) {
1224 return false;
1225 }
1226 }
1227
1228 return mac->port.p != in_xbundle->ofbundle;
1229 }
1230
1231
1232 /* Updates MAC learning table 'ml' given that a packet matching 'flow' was
1233 * received on 'in_xbundle' in 'vlan'.
1234 *
1235 * This code repeats all the checks in is_mac_learning_update_needed() because
1236 * the lock was released between there and here and thus the MAC learning state
1237 * could have changed.
1238 *
1239 * Keep the code here synchronized with that in is_mac_learning_update_needed()
1240 * above. */
1241 static void
1242 update_learning_table__(const struct xbridge *xbridge,
1243 const struct flow *flow, struct flow_wildcards *wc,
1244 int vlan, struct xbundle *in_xbundle)
1245 OVS_REQ_WRLOCK(xbridge->ml->rwlock)
1246 {
1247 struct mac_entry *mac;
1248
1249 if (!mac_learning_may_learn(xbridge->ml, flow->dl_src, vlan)) {
1250 return;
1251 }
1252
1253 mac = mac_learning_insert(xbridge->ml, flow->dl_src, vlan);
1254 if (is_gratuitous_arp(flow, wc)) {
1255 /* We don't want to learn from gratuitous ARP packets that are
1256 * reflected back over bond slaves so we lock the learning table. */
1257 if (!in_xbundle->bond) {
1258 mac_entry_set_grat_arp_lock(mac);
1259 } else if (mac_entry_is_grat_arp_locked(mac)) {
1260 return;
1261 }
1262 }
1263
1264 if (mac->port.p != in_xbundle->ofbundle) {
1265 /* The log messages here could actually be useful in debugging,
1266 * so keep the rate limit relatively high. */
1267 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
1268
1269 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
1270 "on port %s in VLAN %d",
1271 xbridge->name, ETH_ADDR_ARGS(flow->dl_src),
1272 in_xbundle->name, vlan);
1273
1274 mac->port.p = in_xbundle->ofbundle;
1275 mac_learning_changed(xbridge->ml);
1276 }
1277 }
1278
1279 static void
1280 update_learning_table(const struct xbridge *xbridge,
1281 const struct flow *flow, struct flow_wildcards *wc,
1282 int vlan, struct xbundle *in_xbundle)
1283 {
1284 bool need_update;
1285
1286 /* Don't learn the OFPP_NONE port. */
1287 if (in_xbundle == &ofpp_none_bundle) {
1288 return;
1289 }
1290
1291 /* First try the common case: no change to MAC learning table. */
1292 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1293 need_update = is_mac_learning_update_needed(xbridge->ml, flow, wc, vlan,
1294 in_xbundle);
1295 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1296
1297 if (need_update) {
1298 /* Slow path: MAC learning table might need an update. */
1299 ovs_rwlock_wrlock(&xbridge->ml->rwlock);
1300 update_learning_table__(xbridge, flow, wc, vlan, in_xbundle);
1301 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1302 }
1303 }
1304
1305 /* Determines whether packets in 'flow' within 'xbridge' should be forwarded or
1306 * dropped. Returns true if they may be forwarded, false if they should be
1307 * dropped.
1308 *
1309 * 'in_port' must be the xport that corresponds to flow->in_port.
1310 * 'in_port' must be part of a bundle (e.g. in_port->bundle must be nonnull).
1311 *
1312 * 'vlan' must be the VLAN that corresponds to flow->vlan_tci on 'in_port', as
1313 * returned by input_vid_to_vlan(). It must be a valid VLAN for 'in_port', as
1314 * checked by input_vid_is_valid().
1315 *
1316 * May also add tags to '*tags', although the current implementation only does
1317 * so in one special case.
1318 */
1319 static bool
1320 is_admissible(struct xlate_ctx *ctx, struct xport *in_port,
1321 uint16_t vlan)
1322 {
1323 struct xbundle *in_xbundle = in_port->xbundle;
1324 const struct xbridge *xbridge = ctx->xbridge;
1325 struct flow *flow = &ctx->xin->flow;
1326
1327 /* Drop frames for reserved multicast addresses
1328 * only if forward_bpdu option is absent. */
1329 if (!xbridge->forward_bpdu && eth_addr_is_reserved(flow->dl_dst)) {
1330 xlate_report(ctx, "packet has reserved destination MAC, dropping");
1331 return false;
1332 }
1333
1334 if (in_xbundle->bond) {
1335 struct mac_entry *mac;
1336
1337 switch (bond_check_admissibility(in_xbundle->bond, in_port->ofport,
1338 flow->dl_dst)) {
1339 case BV_ACCEPT:
1340 break;
1341
1342 case BV_DROP:
1343 xlate_report(ctx, "bonding refused admissibility, dropping");
1344 return false;
1345
1346 case BV_DROP_IF_MOVED:
1347 ovs_rwlock_rdlock(&xbridge->ml->rwlock);
1348 mac = mac_learning_lookup(xbridge->ml, flow->dl_src, vlan);
1349 if (mac && mac->port.p != in_xbundle->ofbundle &&
1350 (!is_gratuitous_arp(flow, &ctx->xout->wc)
1351 || mac_entry_is_grat_arp_locked(mac))) {
1352 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1353 xlate_report(ctx, "SLB bond thinks this packet looped back, "
1354 "dropping");
1355 return false;
1356 }
1357 ovs_rwlock_unlock(&xbridge->ml->rwlock);
1358 break;
1359 }
1360 }
1361
1362 return true;
1363 }
1364
1365 static void
1366 xlate_normal(struct xlate_ctx *ctx)
1367 {
1368 struct flow_wildcards *wc = &ctx->xout->wc;
1369 struct flow *flow = &ctx->xin->flow;
1370 struct xbundle *in_xbundle;
1371 struct xport *in_port;
1372 struct mac_entry *mac;
1373 void *mac_port;
1374 uint16_t vlan;
1375 uint16_t vid;
1376
1377 ctx->xout->has_normal = true;
1378
1379 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1380 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1381 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1382
1383 in_xbundle = lookup_input_bundle(ctx->xbridge, flow->in_port.ofp_port,
1384 ctx->xin->packet != NULL, &in_port);
1385 if (!in_xbundle) {
1386 xlate_report(ctx, "no input bundle, dropping");
1387 return;
1388 }
1389
1390 /* Drop malformed frames. */
1391 if (flow->dl_type == htons(ETH_TYPE_VLAN) &&
1392 !(flow->vlan_tci & htons(VLAN_CFI))) {
1393 if (ctx->xin->packet != NULL) {
1394 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1395 VLOG_WARN_RL(&rl, "bridge %s: dropping packet with partial "
1396 "VLAN tag received on port %s",
1397 ctx->xbridge->name, in_xbundle->name);
1398 }
1399 xlate_report(ctx, "partial VLAN tag, dropping");
1400 return;
1401 }
1402
1403 /* Drop frames on bundles reserved for mirroring. */
1404 if (xbundle_mirror_out(ctx->xbridge, in_xbundle)) {
1405 if (ctx->xin->packet != NULL) {
1406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1407 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
1408 "%s, which is reserved exclusively for mirroring",
1409 ctx->xbridge->name, in_xbundle->name);
1410 }
1411 xlate_report(ctx, "input port is mirror output port, dropping");
1412 return;
1413 }
1414
1415 /* Check VLAN. */
1416 vid = vlan_tci_to_vid(flow->vlan_tci);
1417 if (!input_vid_is_valid(vid, in_xbundle, ctx->xin->packet != NULL)) {
1418 xlate_report(ctx, "disallowed VLAN VID for this input port, dropping");
1419 return;
1420 }
1421 vlan = input_vid_to_vlan(in_xbundle, vid);
1422
1423 /* Check other admissibility requirements. */
1424 if (in_port && !is_admissible(ctx, in_port, vlan)) {
1425 return;
1426 }
1427
1428 /* Learn source MAC. */
1429 if (ctx->xin->may_learn) {
1430 update_learning_table(ctx->xbridge, flow, wc, vlan, in_xbundle);
1431 }
1432
1433 /* Determine output bundle. */
1434 ovs_rwlock_rdlock(&ctx->xbridge->ml->rwlock);
1435 mac = mac_learning_lookup(ctx->xbridge->ml, flow->dl_dst, vlan);
1436 mac_port = mac ? mac->port.p : NULL;
1437 ovs_rwlock_unlock(&ctx->xbridge->ml->rwlock);
1438
1439 if (mac_port) {
1440 struct xbundle *mac_xbundle = xbundle_lookup(mac_port);
1441 if (mac_xbundle && mac_xbundle != in_xbundle) {
1442 xlate_report(ctx, "forwarding to learned port");
1443 output_normal(ctx, mac_xbundle, vlan);
1444 } else if (!mac_xbundle) {
1445 xlate_report(ctx, "learned port is unknown, dropping");
1446 } else {
1447 xlate_report(ctx, "learned port is input port, dropping");
1448 }
1449 } else {
1450 struct xbundle *xbundle;
1451
1452 xlate_report(ctx, "no learned MAC for destination, flooding");
1453 LIST_FOR_EACH (xbundle, list_node, &ctx->xbridge->xbundles) {
1454 if (xbundle != in_xbundle
1455 && xbundle_includes_vlan(xbundle, vlan)
1456 && xbundle->floodable
1457 && !xbundle_mirror_out(ctx->xbridge, xbundle)) {
1458 output_normal(ctx, xbundle, vlan);
1459 }
1460 }
1461 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
1462 }
1463 }
1464
1465 /* Compose SAMPLE action for sFlow or IPFIX. The given probability is
1466 * the number of packets out of UINT32_MAX to sample. The given
1467 * cookie is passed back in the callback for each sampled packet.
1468 */
1469 static size_t
1470 compose_sample_action(const struct xbridge *xbridge,
1471 struct ofpbuf *odp_actions,
1472 const struct flow *flow,
1473 const uint32_t probability,
1474 const union user_action_cookie *cookie,
1475 const size_t cookie_size)
1476 {
1477 size_t sample_offset, actions_offset;
1478 odp_port_t odp_port;
1479 int cookie_offset;
1480 uint32_t pid;
1481
1482 sample_offset = nl_msg_start_nested(odp_actions, OVS_ACTION_ATTR_SAMPLE);
1483
1484 nl_msg_put_u32(odp_actions, OVS_SAMPLE_ATTR_PROBABILITY, probability);
1485
1486 actions_offset = nl_msg_start_nested(odp_actions, OVS_SAMPLE_ATTR_ACTIONS);
1487
1488 odp_port = ofp_port_to_odp_port(xbridge, flow->in_port.ofp_port);
1489 pid = dpif_port_get_pid(xbridge->dpif, odp_port);
1490 cookie_offset = odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
1491
1492 nl_msg_end_nested(odp_actions, actions_offset);
1493 nl_msg_end_nested(odp_actions, sample_offset);
1494 return cookie_offset;
1495 }
1496
1497 static void
1498 compose_sflow_cookie(const struct xbridge *xbridge, ovs_be16 vlan_tci,
1499 odp_port_t odp_port, unsigned int n_outputs,
1500 union user_action_cookie *cookie)
1501 {
1502 int ifindex;
1503
1504 cookie->type = USER_ACTION_COOKIE_SFLOW;
1505 cookie->sflow.vlan_tci = vlan_tci;
1506
1507 /* See http://www.sflow.org/sflow_version_5.txt (search for "Input/output
1508 * port information") for the interpretation of cookie->output. */
1509 switch (n_outputs) {
1510 case 0:
1511 /* 0x40000000 | 256 means "packet dropped for unknown reason". */
1512 cookie->sflow.output = 0x40000000 | 256;
1513 break;
1514
1515 case 1:
1516 ifindex = dpif_sflow_odp_port_to_ifindex(xbridge->sflow, odp_port);
1517 if (ifindex) {
1518 cookie->sflow.output = ifindex;
1519 break;
1520 }
1521 /* Fall through. */
1522 default:
1523 /* 0x80000000 means "multiple output ports. */
1524 cookie->sflow.output = 0x80000000 | n_outputs;
1525 break;
1526 }
1527 }
1528
1529 /* Compose SAMPLE action for sFlow bridge sampling. */
1530 static size_t
1531 compose_sflow_action(const struct xbridge *xbridge,
1532 struct ofpbuf *odp_actions,
1533 const struct flow *flow,
1534 odp_port_t odp_port)
1535 {
1536 uint32_t probability;
1537 union user_action_cookie cookie;
1538
1539 if (!xbridge->sflow || flow->in_port.ofp_port == OFPP_NONE) {
1540 return 0;
1541 }
1542
1543 probability = dpif_sflow_get_probability(xbridge->sflow);
1544 compose_sflow_cookie(xbridge, htons(0), odp_port,
1545 odp_port == ODPP_NONE ? 0 : 1, &cookie);
1546
1547 return compose_sample_action(xbridge, odp_actions, flow, probability,
1548 &cookie, sizeof cookie.sflow);
1549 }
1550
1551 static void
1552 compose_flow_sample_cookie(uint16_t probability, uint32_t collector_set_id,
1553 uint32_t obs_domain_id, uint32_t obs_point_id,
1554 union user_action_cookie *cookie)
1555 {
1556 cookie->type = USER_ACTION_COOKIE_FLOW_SAMPLE;
1557 cookie->flow_sample.probability = probability;
1558 cookie->flow_sample.collector_set_id = collector_set_id;
1559 cookie->flow_sample.obs_domain_id = obs_domain_id;
1560 cookie->flow_sample.obs_point_id = obs_point_id;
1561 }
1562
1563 static void
1564 compose_ipfix_cookie(union user_action_cookie *cookie)
1565 {
1566 cookie->type = USER_ACTION_COOKIE_IPFIX;
1567 }
1568
1569 /* Compose SAMPLE action for IPFIX bridge sampling. */
1570 static void
1571 compose_ipfix_action(const struct xbridge *xbridge,
1572 struct ofpbuf *odp_actions,
1573 const struct flow *flow)
1574 {
1575 uint32_t probability;
1576 union user_action_cookie cookie;
1577
1578 if (!xbridge->ipfix || flow->in_port.ofp_port == OFPP_NONE) {
1579 return;
1580 }
1581
1582 probability = dpif_ipfix_get_bridge_exporter_probability(xbridge->ipfix);
1583 compose_ipfix_cookie(&cookie);
1584
1585 compose_sample_action(xbridge, odp_actions, flow, probability,
1586 &cookie, sizeof cookie.ipfix);
1587 }
1588
1589 /* SAMPLE action for sFlow must be first action in any given list of
1590 * actions. At this point we do not have all information required to
1591 * build it. So try to build sample action as complete as possible. */
1592 static void
1593 add_sflow_action(struct xlate_ctx *ctx)
1594 {
1595 ctx->user_cookie_offset = compose_sflow_action(ctx->xbridge,
1596 &ctx->xout->odp_actions,
1597 &ctx->xin->flow, ODPP_NONE);
1598 ctx->sflow_odp_port = 0;
1599 ctx->sflow_n_outputs = 0;
1600 }
1601
1602 /* SAMPLE action for IPFIX must be 1st or 2nd action in any given list
1603 * of actions, eventually after the SAMPLE action for sFlow. */
1604 static void
1605 add_ipfix_action(struct xlate_ctx *ctx)
1606 {
1607 compose_ipfix_action(ctx->xbridge, &ctx->xout->odp_actions,
1608 &ctx->xin->flow);
1609 }
1610
1611 /* Fix SAMPLE action according to data collected while composing ODP actions.
1612 * We need to fix SAMPLE actions OVS_SAMPLE_ATTR_ACTIONS attribute, i.e. nested
1613 * USERSPACE action's user-cookie which is required for sflow. */
1614 static void
1615 fix_sflow_action(struct xlate_ctx *ctx)
1616 {
1617 const struct flow *base = &ctx->base_flow;
1618 union user_action_cookie *cookie;
1619
1620 if (!ctx->user_cookie_offset) {
1621 return;
1622 }
1623
1624 cookie = ofpbuf_at(&ctx->xout->odp_actions, ctx->user_cookie_offset,
1625 sizeof cookie->sflow);
1626 ovs_assert(cookie->type == USER_ACTION_COOKIE_SFLOW);
1627
1628 compose_sflow_cookie(ctx->xbridge, base->vlan_tci,
1629 ctx->sflow_odp_port, ctx->sflow_n_outputs, cookie);
1630 }
1631
1632 static enum slow_path_reason
1633 process_special(struct xlate_ctx *ctx, const struct flow *flow,
1634 const struct xport *xport, const struct ofpbuf *packet)
1635 {
1636 struct flow_wildcards *wc = &ctx->xout->wc;
1637 const struct xbridge *xbridge = ctx->xbridge;
1638
1639 if (!xport) {
1640 return 0;
1641 } else if (xport->cfm && cfm_should_process_flow(xport->cfm, flow, wc)) {
1642 if (packet) {
1643 cfm_process_heartbeat(xport->cfm, packet);
1644 }
1645 return SLOW_CFM;
1646 } else if (xport->bfd && bfd_should_process_flow(xport->bfd, flow, wc)) {
1647 if (packet) {
1648 bfd_process_packet(xport->bfd, flow, packet);
1649 /* If POLL received, immediately sends FINAL back. */
1650 if (bfd_should_send_packet(xport->bfd)) {
1651 if (xport->peer) {
1652 ofproto_dpif_monitor_port_send_soon(xport->ofport);
1653 } else {
1654 ofproto_dpif_monitor_port_send_soon_safe(xport->ofport);
1655 }
1656 }
1657 }
1658 return SLOW_BFD;
1659 } else if (xport->xbundle && xport->xbundle->lacp
1660 && flow->dl_type == htons(ETH_TYPE_LACP)) {
1661 if (packet) {
1662 lacp_process_packet(xport->xbundle->lacp, xport->ofport, packet);
1663 }
1664 return SLOW_LACP;
1665 } else if (xbridge->stp && stp_should_process_flow(flow, wc)) {
1666 if (packet) {
1667 stp_process_packet(xport, packet);
1668 }
1669 return SLOW_STP;
1670 } else {
1671 return 0;
1672 }
1673 }
1674
1675 static void
1676 compose_output_action__(struct xlate_ctx *ctx, ofp_port_t ofp_port,
1677 bool check_stp)
1678 {
1679 const struct xport *xport = get_ofp_port(ctx->xbridge, ofp_port);
1680 struct flow_wildcards *wc = &ctx->xout->wc;
1681 struct flow *flow = &ctx->xin->flow;
1682 ovs_be16 flow_vlan_tci;
1683 uint32_t flow_pkt_mark;
1684 uint8_t flow_nw_tos;
1685 odp_port_t out_port, odp_port;
1686 uint8_t dscp;
1687
1688 /* If 'struct flow' gets additional metadata, we'll need to zero it out
1689 * before traversing a patch port. */
1690 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 23);
1691
1692 if (!xport) {
1693 xlate_report(ctx, "Nonexistent output port");
1694 return;
1695 } else if (xport->config & OFPUTIL_PC_NO_FWD) {
1696 xlate_report(ctx, "OFPPC_NO_FWD set, skipping output");
1697 return;
1698 } else if (check_stp && !xport_stp_forward_state(xport)) {
1699 xlate_report(ctx, "STP not in forwarding state, skipping output");
1700 return;
1701 }
1702
1703 if (mbridge_has_mirrors(ctx->xbridge->mbridge) && xport->xbundle) {
1704 ctx->xout->mirrors |= xbundle_mirror_dst(xport->xbundle->xbridge,
1705 xport->xbundle);
1706 }
1707
1708 if (xport->peer) {
1709 const struct xport *peer = xport->peer;
1710 struct flow old_flow = ctx->xin->flow;
1711 enum slow_path_reason special;
1712
1713 ctx->xbridge = peer->xbridge;
1714 flow->in_port.ofp_port = peer->ofp_port;
1715 flow->metadata = htonll(0);
1716 memset(&flow->tunnel, 0, sizeof flow->tunnel);
1717 memset(flow->regs, 0, sizeof flow->regs);
1718
1719 special = process_special(ctx, &ctx->xin->flow, peer,
1720 ctx->xin->packet);
1721 if (special) {
1722 ctx->xout->slow |= special;
1723 } else if (may_receive(peer, ctx)) {
1724 if (xport_stp_forward_state(peer)) {
1725 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1726 } else {
1727 /* Forwarding is disabled by STP. Let OFPP_NORMAL and the
1728 * learning action look at the packet, then drop it. */
1729 struct flow old_base_flow = ctx->base_flow;
1730 size_t old_size = ctx->xout->odp_actions.size;
1731 mirror_mask_t old_mirrors = ctx->xout->mirrors;
1732 xlate_table_action(ctx, flow->in_port.ofp_port, 0, true);
1733 ctx->xout->mirrors = old_mirrors;
1734 ctx->base_flow = old_base_flow;
1735 ctx->xout->odp_actions.size = old_size;
1736 }
1737 }
1738
1739 ctx->xin->flow = old_flow;
1740 ctx->xbridge = xport->xbridge;
1741
1742 if (ctx->xin->resubmit_stats) {
1743 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1744 netdev_vport_inc_rx(peer->netdev, ctx->xin->resubmit_stats);
1745 if (peer->bfd) {
1746 bfd_account_rx(peer->bfd, ctx->xin->resubmit_stats);
1747 }
1748 }
1749
1750 return;
1751 }
1752
1753 flow_vlan_tci = flow->vlan_tci;
1754 flow_pkt_mark = flow->pkt_mark;
1755 flow_nw_tos = flow->nw_tos;
1756
1757 if (dscp_from_skb_priority(xport, flow->skb_priority, &dscp)) {
1758 wc->masks.nw_tos |= IP_ECN_MASK;
1759 flow->nw_tos &= ~IP_DSCP_MASK;
1760 flow->nw_tos |= dscp;
1761 }
1762
1763 if (xport->is_tunnel) {
1764 /* Save tunnel metadata so that changes made due to
1765 * the Logical (tunnel) Port are not visible for any further
1766 * matches, while explicit set actions on tunnel metadata are.
1767 */
1768 struct flow_tnl flow_tnl = flow->tunnel;
1769 odp_port = tnl_port_send(xport->ofport, flow, &ctx->xout->wc);
1770 if (odp_port == ODPP_NONE) {
1771 xlate_report(ctx, "Tunneling decided against output");
1772 goto out; /* restore flow_nw_tos */
1773 }
1774 if (flow->tunnel.ip_dst == ctx->orig_tunnel_ip_dst) {
1775 xlate_report(ctx, "Not tunneling to our own address");
1776 goto out; /* restore flow_nw_tos */
1777 }
1778 if (ctx->xin->resubmit_stats) {
1779 netdev_vport_inc_tx(xport->netdev, ctx->xin->resubmit_stats);
1780 }
1781 out_port = odp_port;
1782 commit_odp_tunnel_action(flow, &ctx->base_flow,
1783 &ctx->xout->odp_actions);
1784 flow->tunnel = flow_tnl; /* Restore tunnel metadata */
1785 } else {
1786 ofp_port_t vlandev_port;
1787
1788 odp_port = xport->odp_port;
1789 if (ofproto_has_vlan_splinters(ctx->xbridge->ofproto)) {
1790 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1791 }
1792 vlandev_port = vsp_realdev_to_vlandev(ctx->xbridge->ofproto, ofp_port,
1793 flow->vlan_tci);
1794 if (vlandev_port == ofp_port) {
1795 out_port = odp_port;
1796 } else {
1797 out_port = ofp_port_to_odp_port(ctx->xbridge, vlandev_port);
1798 flow->vlan_tci = htons(0);
1799 }
1800 }
1801
1802 if (out_port != ODPP_NONE) {
1803 ctx->xout->slow |= commit_odp_actions(flow, &ctx->base_flow,
1804 &ctx->xout->odp_actions,
1805 &ctx->xout->wc,
1806 &ctx->mpls_depth_delta);
1807 nl_msg_put_odp_port(&ctx->xout->odp_actions, OVS_ACTION_ATTR_OUTPUT,
1808 out_port);
1809
1810 ctx->sflow_odp_port = odp_port;
1811 ctx->sflow_n_outputs++;
1812 ctx->xout->nf_output_iface = ofp_port;
1813 }
1814
1815 out:
1816 /* Restore flow */
1817 flow->vlan_tci = flow_vlan_tci;
1818 flow->pkt_mark = flow_pkt_mark;
1819 flow->nw_tos = flow_nw_tos;
1820 }
1821
1822 static void
1823 compose_output_action(struct xlate_ctx *ctx, ofp_port_t ofp_port)
1824 {
1825 compose_output_action__(ctx, ofp_port, true);
1826 }
1827
1828 static void
1829 xlate_recursively(struct xlate_ctx *ctx, struct rule_dpif *rule)
1830 {
1831 struct rule_dpif *old_rule = ctx->rule;
1832 struct rule_actions *actions;
1833
1834 if (ctx->xin->resubmit_stats) {
1835 rule_dpif_credit_stats(rule, ctx->xin->resubmit_stats);
1836 }
1837
1838 ctx->resubmits++;
1839 ctx->recurse++;
1840 ctx->rule = rule;
1841 actions = rule_dpif_get_actions(rule);
1842 do_xlate_actions(actions->ofpacts, actions->ofpacts_len, ctx);
1843 rule_actions_unref(actions);
1844 ctx->rule = old_rule;
1845 ctx->recurse--;
1846 }
1847
1848 static bool
1849 xlate_resubmit_resource_check(struct xlate_ctx *ctx)
1850 {
1851 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1852
1853 if (ctx->recurse >= MAX_RESUBMIT_RECURSION) {
1854 VLOG_ERR_RL(&rl, "resubmit actions recursed over %d times",
1855 MAX_RESUBMIT_RECURSION);
1856 } else if (ctx->resubmits >= MAX_RESUBMITS) {
1857 VLOG_ERR_RL(&rl, "over %d resubmit actions", MAX_RESUBMITS);
1858 } else if (ctx->xout->odp_actions.size > UINT16_MAX) {
1859 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of actions");
1860 } else if (ctx->stack.size >= 65536) {
1861 VLOG_ERR_RL(&rl, "resubmits yielded over 64 kB of stack");
1862 } else {
1863 return true;
1864 }
1865
1866 return false;
1867 }
1868
1869 static void
1870 xlate_table_action(struct xlate_ctx *ctx,
1871 ofp_port_t in_port, uint8_t table_id, bool may_packet_in)
1872 {
1873 if (xlate_resubmit_resource_check(ctx)) {
1874 ofp_port_t old_in_port = ctx->xin->flow.in_port.ofp_port;
1875 bool skip_wildcards = ctx->xin->skip_wildcards;
1876 uint8_t old_table_id = ctx->table_id;
1877 struct rule_dpif *rule;
1878
1879 ctx->table_id = table_id;
1880
1881 /* Look up a flow with 'in_port' as the input port. Then restore the
1882 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
1883 * have surprising behavior). */
1884 ctx->xin->flow.in_port.ofp_port = in_port;
1885 rule_dpif_lookup_in_table(ctx->xbridge->ofproto, &ctx->xin->flow,
1886 !skip_wildcards ? &ctx->xout->wc : NULL,
1887 table_id, &rule);
1888 ctx->xin->flow.in_port.ofp_port = old_in_port;
1889
1890 if (ctx->xin->resubmit_hook) {
1891 ctx->xin->resubmit_hook(ctx->xin, rule, ctx->recurse);
1892 }
1893
1894 if (!rule && may_packet_in) {
1895 struct xport *xport;
1896
1897 /* XXX
1898 * check if table configuration flags
1899 * OFPTC11_TABLE_MISS_CONTROLLER, default.
1900 * OFPTC11_TABLE_MISS_CONTINUE,
1901 * OFPTC11_TABLE_MISS_DROP
1902 * When OF1.0, OFPTC11_TABLE_MISS_CONTINUE is used. What to do? */
1903 xport = get_ofp_port(ctx->xbridge, ctx->xin->flow.in_port.ofp_port);
1904 choose_miss_rule(xport ? xport->config : 0,
1905 ctx->xbridge->miss_rule,
1906 ctx->xbridge->no_packet_in_rule, &rule);
1907 }
1908 if (rule) {
1909 xlate_recursively(ctx, rule);
1910 rule_dpif_unref(rule);
1911 }
1912
1913 ctx->table_id = old_table_id;
1914 return;
1915 }
1916
1917 ctx->exit = true;
1918 }
1919
1920 static void
1921 xlate_group_bucket(struct xlate_ctx *ctx, const struct ofputil_bucket *bucket)
1922 {
1923 uint64_t action_list_stub[1024 / 8];
1924 struct ofpbuf action_list, action_set;
1925
1926 ofpbuf_use_const(&action_set, bucket->ofpacts, bucket->ofpacts_len);
1927 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
1928
1929 ofpacts_execute_action_set(&action_list, &action_set);
1930 ctx->recurse++;
1931 do_xlate_actions(action_list.data, action_list.size, ctx);
1932 ctx->recurse--;
1933
1934 ofpbuf_uninit(&action_set);
1935 ofpbuf_uninit(&action_list);
1936 }
1937
1938 static void
1939 xlate_all_group(struct xlate_ctx *ctx, struct group_dpif *group)
1940 {
1941 const struct ofputil_bucket *bucket;
1942 const struct list *buckets;
1943 struct flow old_flow = ctx->xin->flow;
1944
1945 group_dpif_get_buckets(group, &buckets);
1946
1947 LIST_FOR_EACH (bucket, list_node, buckets) {
1948 xlate_group_bucket(ctx, bucket);
1949 /* Roll back flow to previous state.
1950 * This is equivalent to cloning the packet for each bucket.
1951 *
1952 * As a side effect any subsequently applied actions will
1953 * also effectively be applied to a clone of the packet taken
1954 * just before applying the all or indirect group. */
1955 ctx->xin->flow = old_flow;
1956 }
1957 }
1958
1959 static void
1960 xlate_ff_group(struct xlate_ctx *ctx, struct group_dpif *group)
1961 {
1962 const struct ofputil_bucket *bucket;
1963
1964 bucket = group_first_live_bucket(ctx, group, 0);
1965 if (bucket) {
1966 xlate_group_bucket(ctx, bucket);
1967 }
1968 }
1969
1970 static void
1971 xlate_select_group(struct xlate_ctx *ctx, struct group_dpif *group)
1972 {
1973 struct flow_wildcards *wc = &ctx->xout->wc;
1974 const struct ofputil_bucket *bucket;
1975 uint32_t basis;
1976
1977 basis = hash_bytes(ctx->xin->flow.dl_dst, sizeof ctx->xin->flow.dl_dst, 0);
1978 bucket = group_best_live_bucket(ctx, group, basis);
1979 if (bucket) {
1980 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1981 xlate_group_bucket(ctx, bucket);
1982 }
1983 }
1984
1985 static void
1986 xlate_group_action__(struct xlate_ctx *ctx, struct group_dpif *group)
1987 {
1988 switch (group_dpif_get_type(group)) {
1989 case OFPGT11_ALL:
1990 case OFPGT11_INDIRECT:
1991 xlate_all_group(ctx, group);
1992 break;
1993 case OFPGT11_SELECT:
1994 xlate_select_group(ctx, group);
1995 break;
1996 case OFPGT11_FF:
1997 xlate_ff_group(ctx, group);
1998 break;
1999 default:
2000 OVS_NOT_REACHED();
2001 }
2002 group_dpif_release(group);
2003 }
2004
2005 static bool
2006 xlate_group_action(struct xlate_ctx *ctx, uint32_t group_id)
2007 {
2008 if (xlate_resubmit_resource_check(ctx)) {
2009 struct group_dpif *group;
2010 bool got_group;
2011
2012 got_group = group_dpif_lookup(ctx->xbridge->ofproto, group_id, &group);
2013 if (got_group) {
2014 xlate_group_action__(ctx, group);
2015 } else {
2016 return true;
2017 }
2018 }
2019
2020 return false;
2021 }
2022
2023 static void
2024 xlate_ofpact_resubmit(struct xlate_ctx *ctx,
2025 const struct ofpact_resubmit *resubmit)
2026 {
2027 ofp_port_t in_port;
2028 uint8_t table_id;
2029
2030 in_port = resubmit->in_port;
2031 if (in_port == OFPP_IN_PORT) {
2032 in_port = ctx->xin->flow.in_port.ofp_port;
2033 }
2034
2035 table_id = resubmit->table_id;
2036 if (table_id == 255) {
2037 table_id = ctx->table_id;
2038 }
2039
2040 xlate_table_action(ctx, in_port, table_id, false);
2041 }
2042
2043 static void
2044 flood_packets(struct xlate_ctx *ctx, bool all)
2045 {
2046 const struct xport *xport;
2047
2048 HMAP_FOR_EACH (xport, ofp_node, &ctx->xbridge->xports) {
2049 if (xport->ofp_port == ctx->xin->flow.in_port.ofp_port) {
2050 continue;
2051 }
2052
2053 if (all) {
2054 compose_output_action__(ctx, xport->ofp_port, false);
2055 } else if (!(xport->config & OFPUTIL_PC_NO_FLOOD)) {
2056 compose_output_action(ctx, xport->ofp_port);
2057 }
2058 }
2059
2060 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2061 }
2062
2063 static void
2064 execute_controller_action(struct xlate_ctx *ctx, int len,
2065 enum ofp_packet_in_reason reason,
2066 uint16_t controller_id)
2067 {
2068 struct ofproto_packet_in *pin;
2069 struct ofpbuf *packet;
2070 struct flow key;
2071
2072 ctx->xout->slow |= SLOW_CONTROLLER;
2073 if (!ctx->xin->packet) {
2074 return;
2075 }
2076
2077 packet = ofpbuf_clone(ctx->xin->packet);
2078
2079 key.skb_priority = 0;
2080 key.pkt_mark = 0;
2081 memset(&key.tunnel, 0, sizeof key.tunnel);
2082
2083 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2084 &ctx->xout->odp_actions,
2085 &ctx->xout->wc,
2086 &ctx->mpls_depth_delta);
2087
2088 odp_execute_actions(NULL, packet, &key, ctx->xout->odp_actions.data,
2089 ctx->xout->odp_actions.size, NULL, NULL);
2090
2091 pin = xmalloc(sizeof *pin);
2092 pin->up.packet_len = packet->size;
2093 pin->up.packet = ofpbuf_steal_data(packet);
2094 pin->up.reason = reason;
2095 pin->up.table_id = ctx->table_id;
2096 pin->up.cookie = (ctx->rule
2097 ? rule_dpif_get_flow_cookie(ctx->rule)
2098 : OVS_BE64_MAX);
2099
2100 flow_get_metadata(&ctx->xin->flow, &pin->up.fmd);
2101
2102 pin->controller_id = controller_id;
2103 pin->send_len = len;
2104 pin->generated_by_table_miss = (ctx->rule
2105 && rule_dpif_is_table_miss(ctx->rule));
2106 ofproto_dpif_send_packet_in(ctx->xbridge->ofproto, pin);
2107 ofpbuf_delete(packet);
2108 }
2109
2110 static bool
2111 compose_mpls_push_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2112 {
2113 struct flow_wildcards *wc = &ctx->xout->wc;
2114 struct flow *flow = &ctx->xin->flow;
2115
2116 ovs_assert(eth_type_mpls(eth_type));
2117
2118 /* If mpls_depth_delta is negative then an MPLS POP action has been
2119 * composed and the resulting MPLS label stack is unknown. This means
2120 * an MPLS PUSH action can't be composed as it needs to know either the
2121 * top-most MPLS LSE to use as a template for the new MPLS LSE, or that
2122 * there is no MPLS label stack present. Thus, stop processing.
2123 *
2124 * If mpls_depth_delta is positive then an MPLS PUSH action has been
2125 * composed and no further MPLS PUSH action may be performed without
2126 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2127 * Thus, stop processing.
2128 *
2129 * If the MPLS LSE of the flow and base_flow differ then the MPLS LSE
2130 * has been updated. Performing a MPLS PUSH action may be would result in
2131 * losing MPLS LSE and ether type information held in xtx->xin->flow.
2132 * Thus, stop processing.
2133 *
2134 * It is planned that in the future this case will be handled
2135 * by recirculation */
2136 if (ctx->mpls_depth_delta ||
2137 ctx->xin->flow.mpls_lse != ctx->base_flow.mpls_lse) {
2138 return true;
2139 }
2140
2141 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2142
2143 ctx->pre_push_mpls_lse = ctx->xin->flow.mpls_lse;
2144
2145 if (eth_type_mpls(ctx->xin->flow.dl_type)) {
2146 flow->mpls_lse &= ~htonl(MPLS_BOS_MASK);
2147 } else {
2148 ovs_be32 label;
2149 uint8_t tc, ttl;
2150
2151 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2152 label = htonl(0x2); /* IPV6 Explicit Null. */
2153 } else {
2154 label = htonl(0x0); /* IPV4 Explicit Null. */
2155 }
2156 wc->masks.nw_tos |= IP_DSCP_MASK;
2157 wc->masks.nw_ttl = 0xff;
2158 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
2159 ttl = flow->nw_ttl ? flow->nw_ttl : 0x40;
2160 flow->mpls_lse = set_mpls_lse_values(ttl, tc, 1, label);
2161 }
2162 flow->dl_type = eth_type;
2163 ctx->mpls_depth_delta++;
2164
2165 return false;
2166 }
2167
2168 static bool
2169 compose_mpls_pop_action(struct xlate_ctx *ctx, ovs_be16 eth_type)
2170 {
2171 struct flow_wildcards *wc = &ctx->xout->wc;
2172
2173 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2174 return true;
2175 }
2176
2177 /* If mpls_depth_delta is negative then an MPLS POP action has been
2178 * composed. Performing another MPLS POP action
2179 * would result in losing ether type that results from
2180 * the already composed MPLS POP. Thus, stop processing.
2181 *
2182 * It is planned that in the future this case will be handled
2183 * by recirculation */
2184 if (ctx->mpls_depth_delta < 0) {
2185 return true;
2186 }
2187
2188 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2189
2190 /* If mpls_depth_delta is positive then an MPLS PUSH action has been
2191 * executed and the previous MPLS LSE saved in ctx->pre_push_mpls_lse. The
2192 * flow's MPLS LSE should be restored to that value to allow any
2193 * subsequent actions that update of the LSE to be executed correctly.
2194 */
2195 if (ctx->mpls_depth_delta > 0) {
2196 ctx->xin->flow.mpls_lse = ctx->pre_push_mpls_lse;
2197 }
2198
2199 ctx->xin->flow.dl_type = eth_type;
2200 ctx->mpls_depth_delta--;
2201
2202 return false;
2203 }
2204
2205 static bool
2206 compose_dec_ttl(struct xlate_ctx *ctx, struct ofpact_cnt_ids *ids)
2207 {
2208 struct flow *flow = &ctx->xin->flow;
2209
2210 if (!is_ip_any(flow)) {
2211 return false;
2212 }
2213
2214 ctx->xout->wc.masks.nw_ttl = 0xff;
2215 if (flow->nw_ttl > 1) {
2216 flow->nw_ttl--;
2217 return false;
2218 } else {
2219 size_t i;
2220
2221 for (i = 0; i < ids->n_controllers; i++) {
2222 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL,
2223 ids->cnt_ids[i]);
2224 }
2225
2226 /* Stop processing for current table. */
2227 return true;
2228 }
2229 }
2230
2231 static bool
2232 compose_set_mpls_label_action(struct xlate_ctx *ctx, ovs_be32 label)
2233 {
2234 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2235 return true;
2236 }
2237
2238 /* If mpls_depth_delta is negative then an MPLS POP action has been
2239 * executed and the resulting MPLS label stack is unknown. This means
2240 * a SET MPLS LABEL action can't be executed as it needs to manipulate
2241 * the top-most MPLS LSE. Thus, stop processing.
2242 *
2243 * It is planned that in the future this case will be handled
2244 * by recirculation.
2245 */
2246 if (ctx->mpls_depth_delta < 0) {
2247 return true;
2248 }
2249
2250 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_LABEL_MASK);
2251 set_mpls_lse_label(&ctx->xin->flow.mpls_lse, label);
2252 return false;
2253 }
2254
2255 static bool
2256 compose_set_mpls_tc_action(struct xlate_ctx *ctx, uint8_t tc)
2257 {
2258 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2259 return true;
2260 }
2261
2262 /* If mpls_depth_delta is negative then an MPLS POP action has been
2263 * executed and the resulting MPLS label stack is unknown. This means
2264 * a SET MPLS TC action can't be executed as it needs to manipulate
2265 * the top-most MPLS LSE. Thus, stop processing.
2266 *
2267 * It is planned that in the future this case will be handled
2268 * by recirculation.
2269 */
2270 if (ctx->mpls_depth_delta < 0) {
2271 return true;
2272 }
2273
2274 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TC_MASK);
2275 set_mpls_lse_tc(&ctx->xin->flow.mpls_lse, tc);
2276 return false;
2277 }
2278
2279 static bool
2280 compose_set_mpls_ttl_action(struct xlate_ctx *ctx, uint8_t ttl)
2281 {
2282 if (!eth_type_mpls(ctx->xin->flow.dl_type)) {
2283 return true;
2284 }
2285
2286 /* If mpls_depth_delta is negative then an MPLS POP action has been
2287 * executed and the resulting MPLS label stack is unknown. This means
2288 * a SET MPLS TTL push action can't be executed as it needs to manipulate
2289 * the top-most MPLS LSE. Thus, stop processing.
2290 *
2291 * It is planned that in the future this case will be handled
2292 * by recirculation.
2293 */
2294 if (ctx->mpls_depth_delta < 0) {
2295 return true;
2296 }
2297
2298 ctx->xout->wc.masks.mpls_lse |= htonl(MPLS_TTL_MASK);
2299 set_mpls_lse_ttl(&ctx->xin->flow.mpls_lse, ttl);
2300 return false;
2301 }
2302
2303 static bool
2304 compose_dec_mpls_ttl_action(struct xlate_ctx *ctx)
2305 {
2306 struct flow *flow = &ctx->xin->flow;
2307 uint8_t ttl = mpls_lse_to_ttl(flow->mpls_lse);
2308 struct flow_wildcards *wc = &ctx->xout->wc;
2309
2310 memset(&wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
2311
2312 if (!eth_type_mpls(flow->dl_type)) {
2313 return false;
2314 }
2315
2316 if (ttl > 1) {
2317 ttl--;
2318 set_mpls_lse_ttl(&flow->mpls_lse, ttl);
2319 return false;
2320 } else {
2321 execute_controller_action(ctx, UINT16_MAX, OFPR_INVALID_TTL, 0);
2322
2323 /* Stop processing for current table. */
2324 return true;
2325 }
2326 }
2327
2328 static void
2329 xlate_output_action(struct xlate_ctx *ctx,
2330 ofp_port_t port, uint16_t max_len, bool may_packet_in)
2331 {
2332 ofp_port_t prev_nf_output_iface = ctx->xout->nf_output_iface;
2333
2334 ctx->xout->nf_output_iface = NF_OUT_DROP;
2335
2336 switch (port) {
2337 case OFPP_IN_PORT:
2338 compose_output_action(ctx, ctx->xin->flow.in_port.ofp_port);
2339 break;
2340 case OFPP_TABLE:
2341 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2342 0, may_packet_in);
2343 break;
2344 case OFPP_NORMAL:
2345 xlate_normal(ctx);
2346 break;
2347 case OFPP_FLOOD:
2348 flood_packets(ctx, false);
2349 break;
2350 case OFPP_ALL:
2351 flood_packets(ctx, true);
2352 break;
2353 case OFPP_CONTROLLER:
2354 execute_controller_action(ctx, max_len, OFPR_ACTION, 0);
2355 break;
2356 case OFPP_NONE:
2357 break;
2358 case OFPP_LOCAL:
2359 default:
2360 if (port != ctx->xin->flow.in_port.ofp_port) {
2361 compose_output_action(ctx, port);
2362 } else {
2363 xlate_report(ctx, "skipping output to input port");
2364 }
2365 break;
2366 }
2367
2368 if (prev_nf_output_iface == NF_OUT_FLOOD) {
2369 ctx->xout->nf_output_iface = NF_OUT_FLOOD;
2370 } else if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2371 ctx->xout->nf_output_iface = prev_nf_output_iface;
2372 } else if (prev_nf_output_iface != NF_OUT_DROP &&
2373 ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2374 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2375 }
2376 }
2377
2378 static void
2379 xlate_output_reg_action(struct xlate_ctx *ctx,
2380 const struct ofpact_output_reg *or)
2381 {
2382 uint64_t port = mf_get_subfield(&or->src, &ctx->xin->flow);
2383 if (port <= UINT16_MAX) {
2384 union mf_subvalue value;
2385
2386 memset(&value, 0xff, sizeof value);
2387 mf_write_subfield_flow(&or->src, &value, &ctx->xout->wc.masks);
2388 xlate_output_action(ctx, u16_to_ofp(port),
2389 or->max_len, false);
2390 }
2391 }
2392
2393 static void
2394 xlate_enqueue_action(struct xlate_ctx *ctx,
2395 const struct ofpact_enqueue *enqueue)
2396 {
2397 ofp_port_t ofp_port = enqueue->port;
2398 uint32_t queue_id = enqueue->queue;
2399 uint32_t flow_priority, priority;
2400 int error;
2401
2402 /* Translate queue to priority. */
2403 error = dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &priority);
2404 if (error) {
2405 /* Fall back to ordinary output action. */
2406 xlate_output_action(ctx, enqueue->port, 0, false);
2407 return;
2408 }
2409
2410 /* Check output port. */
2411 if (ofp_port == OFPP_IN_PORT) {
2412 ofp_port = ctx->xin->flow.in_port.ofp_port;
2413 } else if (ofp_port == ctx->xin->flow.in_port.ofp_port) {
2414 return;
2415 }
2416
2417 /* Add datapath actions. */
2418 flow_priority = ctx->xin->flow.skb_priority;
2419 ctx->xin->flow.skb_priority = priority;
2420 compose_output_action(ctx, ofp_port);
2421 ctx->xin->flow.skb_priority = flow_priority;
2422
2423 /* Update NetFlow output port. */
2424 if (ctx->xout->nf_output_iface == NF_OUT_DROP) {
2425 ctx->xout->nf_output_iface = ofp_port;
2426 } else if (ctx->xout->nf_output_iface != NF_OUT_FLOOD) {
2427 ctx->xout->nf_output_iface = NF_OUT_MULTI;
2428 }
2429 }
2430
2431 static void
2432 xlate_set_queue_action(struct xlate_ctx *ctx, uint32_t queue_id)
2433 {
2434 uint32_t skb_priority;
2435
2436 if (!dpif_queue_to_priority(ctx->xbridge->dpif, queue_id, &skb_priority)) {
2437 ctx->xin->flow.skb_priority = skb_priority;
2438 } else {
2439 /* Couldn't translate queue to a priority. Nothing to do. A warning
2440 * has already been logged. */
2441 }
2442 }
2443
2444 static bool
2445 slave_enabled_cb(ofp_port_t ofp_port, void *xbridge_)
2446 {
2447 const struct xbridge *xbridge = xbridge_;
2448 struct xport *port;
2449
2450 switch (ofp_port) {
2451 case OFPP_IN_PORT:
2452 case OFPP_TABLE:
2453 case OFPP_NORMAL:
2454 case OFPP_FLOOD:
2455 case OFPP_ALL:
2456 case OFPP_NONE:
2457 return true;
2458 case OFPP_CONTROLLER: /* Not supported by the bundle action. */
2459 return false;
2460 default:
2461 port = get_ofp_port(xbridge, ofp_port);
2462 return port ? port->may_enable : false;
2463 }
2464 }
2465
2466 static void
2467 xlate_bundle_action(struct xlate_ctx *ctx,
2468 const struct ofpact_bundle *bundle)
2469 {
2470 ofp_port_t port;
2471
2472 port = bundle_execute(bundle, &ctx->xin->flow, &ctx->xout->wc,
2473 slave_enabled_cb,
2474 CONST_CAST(struct xbridge *, ctx->xbridge));
2475 if (bundle->dst.field) {
2476 nxm_reg_load(&bundle->dst, ofp_to_u16(port), &ctx->xin->flow,
2477 &ctx->xout->wc);
2478 } else {
2479 xlate_output_action(ctx, port, 0, false);
2480 }
2481 }
2482
2483 static void
2484 xlate_learn_action(struct xlate_ctx *ctx,
2485 const struct ofpact_learn *learn)
2486 {
2487 uint64_t ofpacts_stub[1024 / 8];
2488 struct ofputil_flow_mod fm;
2489 struct ofpbuf ofpacts;
2490
2491 ctx->xout->has_learn = true;
2492
2493 learn_mask(learn, &ctx->xout->wc);
2494
2495 if (!ctx->xin->may_learn) {
2496 return;
2497 }
2498
2499 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2500 learn_execute(learn, &ctx->xin->flow, &fm, &ofpacts);
2501 ofproto_dpif_flow_mod(ctx->xbridge->ofproto, &fm);
2502 ofpbuf_uninit(&ofpacts);
2503 }
2504
2505 static void
2506 xlate_fin_timeout(struct xlate_ctx *ctx,
2507 const struct ofpact_fin_timeout *oft)
2508 {
2509 if (ctx->xin->tcp_flags & (TCP_FIN | TCP_RST) && ctx->rule) {
2510 rule_dpif_reduce_timeouts(ctx->rule, oft->fin_idle_timeout,
2511 oft->fin_hard_timeout);
2512 }
2513 }
2514
2515 static void
2516 xlate_sample_action(struct xlate_ctx *ctx,
2517 const struct ofpact_sample *os)
2518 {
2519 union user_action_cookie cookie;
2520 /* Scale the probability from 16-bit to 32-bit while representing
2521 * the same percentage. */
2522 uint32_t probability = (os->probability << 16) | os->probability;
2523
2524 ctx->xout->slow |= commit_odp_actions(&ctx->xin->flow, &ctx->base_flow,
2525 &ctx->xout->odp_actions,
2526 &ctx->xout->wc,
2527 &ctx->mpls_depth_delta);
2528
2529 compose_flow_sample_cookie(os->probability, os->collector_set_id,
2530 os->obs_domain_id, os->obs_point_id, &cookie);
2531 compose_sample_action(ctx->xbridge, &ctx->xout->odp_actions, &ctx->xin->flow,
2532 probability, &cookie, sizeof cookie.flow_sample);
2533 }
2534
2535 static bool
2536 may_receive(const struct xport *xport, struct xlate_ctx *ctx)
2537 {
2538 if (xport->config & (eth_addr_equals(ctx->xin->flow.dl_dst, eth_addr_stp)
2539 ? OFPUTIL_PC_NO_RECV_STP
2540 : OFPUTIL_PC_NO_RECV)) {
2541 return false;
2542 }
2543
2544 /* Only drop packets here if both forwarding and learning are
2545 * disabled. If just learning is enabled, we need to have
2546 * OFPP_NORMAL and the learning action have a look at the packet
2547 * before we can drop it. */
2548 if (!xport_stp_forward_state(xport) && !xport_stp_learn_state(xport)) {
2549 return false;
2550 }
2551
2552 return true;
2553 }
2554
2555 static void
2556 xlate_write_actions(struct xlate_ctx *ctx, const struct ofpact *a)
2557 {
2558 struct ofpact_nest *on = ofpact_get_WRITE_ACTIONS(a);
2559 ofpbuf_put(&ctx->action_set, on->actions, ofpact_nest_get_action_len(on));
2560 ofpact_pad(&ctx->action_set);
2561 }
2562
2563 static void
2564 xlate_action_set(struct xlate_ctx *ctx)
2565 {
2566 uint64_t action_list_stub[1024 / 64];
2567 struct ofpbuf action_list;
2568
2569 ofpbuf_use_stub(&action_list, action_list_stub, sizeof action_list_stub);
2570 ofpacts_execute_action_set(&action_list, &ctx->action_set);
2571 do_xlate_actions(action_list.data, action_list.size, ctx);
2572 ofpbuf_uninit(&action_list);
2573 }
2574
2575 static void
2576 do_xlate_actions(const struct ofpact *ofpacts, size_t ofpacts_len,
2577 struct xlate_ctx *ctx)
2578 {
2579 struct flow_wildcards *wc = &ctx->xout->wc;
2580 struct flow *flow = &ctx->xin->flow;
2581 const struct ofpact *a;
2582
2583 /* dl_type already in the mask, not set below. */
2584
2585 OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
2586 struct ofpact_controller *controller;
2587 const struct ofpact_metadata *metadata;
2588 const struct ofpact_set_field *set_field;
2589 const struct mf_field *mf;
2590
2591 if (ctx->exit) {
2592 break;
2593 }
2594
2595 switch (a->type) {
2596 case OFPACT_OUTPUT:
2597 xlate_output_action(ctx, ofpact_get_OUTPUT(a)->port,
2598 ofpact_get_OUTPUT(a)->max_len, true);
2599 break;
2600
2601 case OFPACT_GROUP:
2602 if (xlate_group_action(ctx, ofpact_get_GROUP(a)->group_id)) {
2603 return;
2604 }
2605 break;
2606
2607 case OFPACT_CONTROLLER:
2608 controller = ofpact_get_CONTROLLER(a);
2609 execute_controller_action(ctx, controller->max_len,
2610 controller->reason,
2611 controller->controller_id);
2612 break;
2613
2614 case OFPACT_ENQUEUE:
2615 xlate_enqueue_action(ctx, ofpact_get_ENQUEUE(a));
2616 break;
2617
2618 case OFPACT_SET_VLAN_VID:
2619 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2620 if (flow->vlan_tci & htons(VLAN_CFI) ||
2621 ofpact_get_SET_VLAN_VID(a)->push_vlan_if_needed) {
2622 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
2623 flow->vlan_tci |= (htons(ofpact_get_SET_VLAN_VID(a)->vlan_vid)
2624 | htons(VLAN_CFI));
2625 }
2626 break;
2627
2628 case OFPACT_SET_VLAN_PCP:
2629 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
2630 if (flow->vlan_tci & htons(VLAN_CFI) ||
2631 ofpact_get_SET_VLAN_PCP(a)->push_vlan_if_needed) {
2632 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
2633 flow->vlan_tci |= htons((ofpact_get_SET_VLAN_PCP(a)->vlan_pcp
2634 << VLAN_PCP_SHIFT) | VLAN_CFI);
2635 }
2636 break;
2637
2638 case OFPACT_STRIP_VLAN:
2639 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2640 flow->vlan_tci = htons(0);
2641 break;
2642
2643 case OFPACT_PUSH_VLAN:
2644 /* XXX 802.1AD(QinQ) */
2645 memset(&wc->masks.vlan_tci, 0xff, sizeof wc->masks.vlan_tci);
2646 flow->vlan_tci = htons(VLAN_CFI);
2647 break;
2648
2649 case OFPACT_SET_ETH_SRC:
2650 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2651 memcpy(flow->dl_src, ofpact_get_SET_ETH_SRC(a)->mac, ETH_ADDR_LEN);
2652 break;
2653
2654 case OFPACT_SET_ETH_DST:
2655 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
2656 memcpy(flow->dl_dst, ofpact_get_SET_ETH_DST(a)->mac, ETH_ADDR_LEN);
2657 break;
2658
2659 case OFPACT_SET_IPV4_SRC:
2660 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2661 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2662 flow->nw_src = ofpact_get_SET_IPV4_SRC(a)->ipv4;
2663 }
2664 break;
2665
2666 case OFPACT_SET_IPV4_DST:
2667 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2668 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2669 flow->nw_dst = ofpact_get_SET_IPV4_DST(a)->ipv4;
2670 }
2671 break;
2672
2673 case OFPACT_SET_IP_DSCP:
2674 if (is_ip_any(flow)) {
2675 wc->masks.nw_tos |= IP_DSCP_MASK;
2676 flow->nw_tos &= ~IP_DSCP_MASK;
2677 flow->nw_tos |= ofpact_get_SET_IP_DSCP(a)->dscp;
2678 }
2679 break;
2680
2681 case OFPACT_SET_IP_ECN:
2682 if (is_ip_any(flow)) {
2683 wc->masks.nw_tos |= IP_ECN_MASK;
2684 flow->nw_tos &= ~IP_ECN_MASK;
2685 flow->nw_tos |= ofpact_get_SET_IP_ECN(a)->ecn;
2686 }
2687 break;
2688
2689 case OFPACT_SET_IP_TTL:
2690 if (is_ip_any(flow)) {
2691 wc->masks.nw_ttl = 0xff;
2692 flow->nw_ttl = ofpact_get_SET_IP_TTL(a)->ttl;
2693 }
2694 break;
2695
2696 case OFPACT_SET_L4_SRC_PORT:
2697 if (is_ip_any(flow)) {
2698 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2699 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2700 flow->tp_src = htons(ofpact_get_SET_L4_SRC_PORT(a)->port);
2701 }
2702 break;
2703
2704 case OFPACT_SET_L4_DST_PORT:
2705 if (is_ip_any(flow)) {
2706 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2707 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2708 flow->tp_dst = htons(ofpact_get_SET_L4_DST_PORT(a)->port);
2709 }
2710 break;
2711
2712 case OFPACT_RESUBMIT:
2713 xlate_ofpact_resubmit(ctx, ofpact_get_RESUBMIT(a));
2714 break;
2715
2716 case OFPACT_SET_TUNNEL:
2717 flow->tunnel.tun_id = htonll(ofpact_get_SET_TUNNEL(a)->tun_id);
2718 break;
2719
2720 case OFPACT_SET_QUEUE:
2721 xlate_set_queue_action(ctx, ofpact_get_SET_QUEUE(a)->queue_id);
2722 break;
2723
2724 case OFPACT_POP_QUEUE:
2725 flow->skb_priority = ctx->orig_skb_priority;
2726 break;
2727
2728 case OFPACT_REG_MOVE:
2729 nxm_execute_reg_move(ofpact_get_REG_MOVE(a), flow, wc);
2730 break;
2731
2732 case OFPACT_REG_LOAD:
2733 nxm_execute_reg_load(ofpact_get_REG_LOAD(a), flow, wc);
2734 break;
2735
2736 case OFPACT_SET_FIELD:
2737 set_field = ofpact_get_SET_FIELD(a);
2738 mf = set_field->field;
2739 mf_mask_field_and_prereqs(mf, &wc->masks);
2740
2741 /* Set field action only ever overwrites packet's outermost
2742 * applicable header fields. Do nothing if no header exists. */
2743 if ((mf->id != MFF_VLAN_VID || flow->vlan_tci & htons(VLAN_CFI))
2744 && ((mf->id != MFF_MPLS_LABEL && mf->id != MFF_MPLS_TC)
2745 || flow->mpls_lse)) {
2746 mf_set_flow_value(mf, &set_field->value, flow);
2747 }
2748 break;
2749
2750 case OFPACT_STACK_PUSH:
2751 nxm_execute_stack_push(ofpact_get_STACK_PUSH(a), flow, wc,
2752 &ctx->stack);
2753 break;
2754
2755 case OFPACT_STACK_POP:
2756 nxm_execute_stack_pop(ofpact_get_STACK_POP(a), flow, wc,
2757 &ctx->stack);
2758 break;
2759
2760 case OFPACT_PUSH_MPLS:
2761 if (compose_mpls_push_action(ctx,
2762 ofpact_get_PUSH_MPLS(a)->ethertype)) {
2763 return;
2764 }
2765 break;
2766
2767 case OFPACT_POP_MPLS:
2768 if (compose_mpls_pop_action(ctx,
2769 ofpact_get_POP_MPLS(a)->ethertype)) {
2770 return;
2771 }
2772 break;
2773
2774 case OFPACT_SET_MPLS_LABEL:
2775 if (compose_set_mpls_label_action(ctx,
2776 ofpact_get_SET_MPLS_LABEL(a)->label)) {
2777 return;
2778 }
2779 break;
2780
2781 case OFPACT_SET_MPLS_TC:
2782 if (compose_set_mpls_tc_action(ctx,
2783 ofpact_get_SET_MPLS_TC(a)->tc)) {
2784 return;
2785 }
2786 break;
2787
2788 case OFPACT_SET_MPLS_TTL:
2789 if (compose_set_mpls_ttl_action(ctx,
2790 ofpact_get_SET_MPLS_TTL(a)->ttl)) {
2791 return;
2792 }
2793 break;
2794
2795 case OFPACT_DEC_MPLS_TTL:
2796 if (compose_dec_mpls_ttl_action(ctx)) {
2797 return;
2798 }
2799 break;
2800
2801 case OFPACT_DEC_TTL:
2802 wc->masks.nw_ttl = 0xff;
2803 if (compose_dec_ttl(ctx, ofpact_get_DEC_TTL(a))) {
2804 return;
2805 }
2806 break;
2807
2808 case OFPACT_NOTE:
2809 /* Nothing to do. */
2810 break;
2811
2812 case OFPACT_MULTIPATH:
2813 multipath_execute(ofpact_get_MULTIPATH(a), flow, wc);
2814 break;
2815
2816 case OFPACT_BUNDLE:
2817 xlate_bundle_action(ctx, ofpact_get_BUNDLE(a));
2818 break;
2819
2820 case OFPACT_OUTPUT_REG:
2821 xlate_output_reg_action(ctx, ofpact_get_OUTPUT_REG(a));
2822 break;
2823
2824 case OFPACT_LEARN:
2825 xlate_learn_action(ctx, ofpact_get_LEARN(a));
2826 break;
2827
2828 case OFPACT_EXIT:
2829 ctx->exit = true;
2830 break;
2831
2832 case OFPACT_FIN_TIMEOUT:
2833 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2834 ctx->xout->has_fin_timeout = true;
2835 xlate_fin_timeout(ctx, ofpact_get_FIN_TIMEOUT(a));
2836 break;
2837
2838 case OFPACT_CLEAR_ACTIONS:
2839 ofpbuf_clear(&ctx->action_set);
2840 break;
2841
2842 case OFPACT_WRITE_ACTIONS:
2843 xlate_write_actions(ctx, a);
2844 break;
2845
2846 case OFPACT_WRITE_METADATA:
2847 metadata = ofpact_get_WRITE_METADATA(a);
2848 flow->metadata &= ~metadata->mask;
2849 flow->metadata |= metadata->metadata & metadata->mask;
2850 break;
2851
2852 case OFPACT_METER:
2853 /* Not implemented yet. */
2854 break;
2855
2856 case OFPACT_GOTO_TABLE: {
2857 struct ofpact_goto_table *ogt = ofpact_get_GOTO_TABLE(a);
2858
2859 ovs_assert(ctx->table_id < ogt->table_id);
2860 xlate_table_action(ctx, ctx->xin->flow.in_port.ofp_port,
2861 ogt->table_id, true);
2862 break;
2863 }
2864
2865 case OFPACT_SAMPLE:
2866 xlate_sample_action(ctx, ofpact_get_SAMPLE(a));
2867 break;
2868 }
2869 }
2870 }
2871
2872 void
2873 xlate_in_init(struct xlate_in *xin, struct ofproto_dpif *ofproto,
2874 const struct flow *flow, struct rule_dpif *rule,
2875 uint16_t tcp_flags, const struct ofpbuf *packet)
2876 {
2877 xin->ofproto = ofproto;
2878 xin->flow = *flow;
2879 xin->packet = packet;
2880 xin->may_learn = packet != NULL;
2881 xin->rule = rule;
2882 xin->ofpacts = NULL;
2883 xin->ofpacts_len = 0;
2884 xin->tcp_flags = tcp_flags;
2885 xin->resubmit_hook = NULL;
2886 xin->report_hook = NULL;
2887 xin->resubmit_stats = NULL;
2888 xin->skip_wildcards = false;
2889 }
2890
2891 void
2892 xlate_out_uninit(struct xlate_out *xout)
2893 {
2894 if (xout) {
2895 ofpbuf_uninit(&xout->odp_actions);
2896 }
2897 }
2898
2899 /* Translates the 'ofpacts_len' bytes of "struct ofpact"s starting at 'ofpacts'
2900 * into datapath actions, using 'ctx', and discards the datapath actions. */
2901 void
2902 xlate_actions_for_side_effects(struct xlate_in *xin)
2903 {
2904 struct xlate_out xout;
2905
2906 xlate_actions(xin, &xout);
2907 xlate_out_uninit(&xout);
2908 }
2909
2910 static void
2911 xlate_report(struct xlate_ctx *ctx, const char *s)
2912 {
2913 if (ctx->xin->report_hook) {
2914 ctx->xin->report_hook(ctx->xin, s, ctx->recurse);
2915 }
2916 }
2917
2918 void
2919 xlate_out_copy(struct xlate_out *dst, const struct xlate_out *src)
2920 {
2921 dst->wc = src->wc;
2922 dst->slow = src->slow;
2923 dst->has_learn = src->has_learn;
2924 dst->has_normal = src->has_normal;
2925 dst->has_fin_timeout = src->has_fin_timeout;
2926 dst->nf_output_iface = src->nf_output_iface;
2927 dst->mirrors = src->mirrors;
2928
2929 ofpbuf_use_stub(&dst->odp_actions, dst->odp_actions_stub,
2930 sizeof dst->odp_actions_stub);
2931 ofpbuf_put(&dst->odp_actions, src->odp_actions.data,
2932 src->odp_actions.size);
2933 }
2934 \f
2935 static struct skb_priority_to_dscp *
2936 get_skb_priority(const struct xport *xport, uint32_t skb_priority)
2937 {
2938 struct skb_priority_to_dscp *pdscp;
2939 uint32_t hash;
2940
2941 hash = hash_int(skb_priority, 0);
2942 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &xport->skb_priorities) {
2943 if (pdscp->skb_priority == skb_priority) {
2944 return pdscp;
2945 }
2946 }
2947 return NULL;
2948 }
2949
2950 static bool
2951 dscp_from_skb_priority(const struct xport *xport, uint32_t skb_priority,
2952 uint8_t *dscp)
2953 {
2954 struct skb_priority_to_dscp *pdscp = get_skb_priority(xport, skb_priority);
2955 *dscp = pdscp ? pdscp->dscp : 0;
2956 return pdscp != NULL;
2957 }
2958
2959 static void
2960 clear_skb_priorities(struct xport *xport)
2961 {
2962 struct skb_priority_to_dscp *pdscp, *next;
2963
2964 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &xport->skb_priorities) {
2965 hmap_remove(&xport->skb_priorities, &pdscp->hmap_node);
2966 free(pdscp);
2967 }
2968 }
2969
2970 static bool
2971 actions_output_to_local_port(const struct xlate_ctx *ctx)
2972 {
2973 odp_port_t local_odp_port = ofp_port_to_odp_port(ctx->xbridge, OFPP_LOCAL);
2974 const struct nlattr *a;
2975 unsigned int left;
2976
2977 NL_ATTR_FOR_EACH_UNSAFE (a, left, ctx->xout->odp_actions.data,
2978 ctx->xout->odp_actions.size) {
2979 if (nl_attr_type(a) == OVS_ACTION_ATTR_OUTPUT
2980 && nl_attr_get_odp_port(a) == local_odp_port) {
2981 return true;
2982 }
2983 }
2984 return false;
2985 }
2986
2987 /* Thread safe call to xlate_actions__(). */
2988 void
2989 xlate_actions(struct xlate_in *xin, struct xlate_out *xout)
2990 {
2991 ovs_rwlock_rdlock(&xlate_rwlock);
2992 xlate_actions__(xin, xout);
2993 ovs_rwlock_unlock(&xlate_rwlock);
2994 }
2995
2996 /* Translates the 'ofpacts_len' bytes of "struct ofpacts" starting at 'ofpacts'
2997 * into datapath actions in 'odp_actions', using 'ctx'.
2998 *
2999 * The caller must take responsibility for eventually freeing 'xout', with
3000 * xlate_out_uninit(). */
3001 static void
3002 xlate_actions__(struct xlate_in *xin, struct xlate_out *xout)
3003 OVS_REQ_RDLOCK(xlate_rwlock)
3004 {
3005 struct flow_wildcards *wc = &xout->wc;
3006 struct flow *flow = &xin->flow;
3007 struct rule_dpif *rule = NULL;
3008
3009 struct rule_actions *actions = NULL;
3010 enum slow_path_reason special;
3011 const struct ofpact *ofpacts;
3012 struct xport *in_port;
3013 struct flow orig_flow;
3014 struct xlate_ctx ctx;
3015 size_t ofpacts_len;
3016 bool tnl_may_send;
3017
3018 COVERAGE_INC(xlate_actions);
3019
3020 /* Flow initialization rules:
3021 * - 'base_flow' must match the kernel's view of the packet at the
3022 * time that action processing starts. 'flow' represents any
3023 * transformations we wish to make through actions.
3024 * - By default 'base_flow' and 'flow' are the same since the input
3025 * packet matches the output before any actions are applied.
3026 * - When using VLAN splinters, 'base_flow''s VLAN is set to the value
3027 * of the received packet as seen by the kernel. If we later output
3028 * to another device without any modifications this will cause us to
3029 * insert a new tag since the original one was stripped off by the
3030 * VLAN device.
3031 * - Tunnel metadata as received is retained in 'flow'. This allows
3032 * tunnel metadata matching also in later tables.
3033 * Since a kernel action for setting the tunnel metadata will only be
3034 * generated with actual tunnel output, changing the tunnel metadata
3035 * values in 'flow' (such as tun_id) will only have effect with a later
3036 * tunnel output action.
3037 * - Tunnel 'base_flow' is completely cleared since that is what the
3038 * kernel does. If we wish to maintain the original values an action
3039 * needs to be generated. */
3040
3041 ctx.xin = xin;
3042 ctx.xout = xout;
3043 ctx.xout->slow = 0;
3044 ctx.xout->has_learn = false;
3045 ctx.xout->has_normal = false;
3046 ctx.xout->has_fin_timeout = false;
3047 ctx.xout->nf_output_iface = NF_OUT_DROP;
3048 ctx.xout->mirrors = 0;
3049 ofpbuf_use_stub(&ctx.xout->odp_actions, ctx.xout->odp_actions_stub,
3050 sizeof ctx.xout->odp_actions_stub);
3051 ofpbuf_reserve(&ctx.xout->odp_actions, NL_A_U32_SIZE);
3052
3053 ctx.xbridge = xbridge_lookup(xin->ofproto);
3054 if (!ctx.xbridge) {
3055 goto out;
3056 }
3057
3058 ctx.rule = xin->rule;
3059
3060 ctx.base_flow = *flow;
3061 memset(&ctx.base_flow.tunnel, 0, sizeof ctx.base_flow.tunnel);
3062 ctx.orig_tunnel_ip_dst = flow->tunnel.ip_dst;
3063
3064 flow_wildcards_init_catchall(wc);
3065 memset(&wc->masks.in_port, 0xff, sizeof wc->masks.in_port);
3066 memset(&wc->masks.skb_priority, 0xff, sizeof wc->masks.skb_priority);
3067 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3068 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3069
3070 tnl_may_send = tnl_xlate_init(&ctx.base_flow, flow, wc);
3071 if (ctx.xbridge->netflow) {
3072 netflow_mask_wc(flow, wc);
3073 }
3074
3075 ctx.recurse = 0;
3076 ctx.resubmits = 0;
3077 ctx.orig_skb_priority = flow->skb_priority;
3078 ctx.table_id = 0;
3079 ctx.exit = false;
3080 ctx.mpls_depth_delta = 0;
3081
3082 if (!xin->ofpacts && !ctx.rule) {
3083 rule_dpif_lookup(ctx.xbridge->ofproto, flow,
3084 !xin->skip_wildcards ? wc : NULL, &rule);
3085 if (ctx.xin->resubmit_stats) {
3086 rule_dpif_credit_stats(rule, ctx.xin->resubmit_stats);
3087 }
3088 ctx.rule = rule;
3089 }
3090 xout->fail_open = ctx.rule && rule_dpif_is_fail_open(ctx.rule);
3091
3092 if (xin->ofpacts) {
3093 ofpacts = xin->ofpacts;
3094 ofpacts_len = xin->ofpacts_len;
3095 } else if (ctx.rule) {
3096 actions = rule_dpif_get_actions(ctx.rule);
3097 ofpacts = actions->ofpacts;
3098 ofpacts_len = actions->ofpacts_len;
3099 } else {
3100 OVS_NOT_REACHED();
3101 }
3102
3103 ofpbuf_use_stub(&ctx.stack, ctx.init_stack, sizeof ctx.init_stack);
3104 ofpbuf_use_stub(&ctx.action_set,
3105 ctx.action_set_stub, sizeof ctx.action_set_stub);
3106
3107 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3108 /* Do this conditionally because the copy is expensive enough that it
3109 * shows up in profiles. */
3110 orig_flow = *flow;
3111 }
3112
3113 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3114 switch (ctx.xbridge->frag) {
3115 case OFPC_FRAG_NORMAL:
3116 /* We must pretend that transport ports are unavailable. */
3117 flow->tp_src = ctx.base_flow.tp_src = htons(0);
3118 flow->tp_dst = ctx.base_flow.tp_dst = htons(0);
3119 break;
3120
3121 case OFPC_FRAG_DROP:
3122 goto out;
3123
3124 case OFPC_FRAG_REASM:
3125 OVS_NOT_REACHED();
3126
3127 case OFPC_FRAG_NX_MATCH:
3128 /* Nothing to do. */
3129 break;
3130
3131 case OFPC_INVALID_TTL_TO_CONTROLLER:
3132 OVS_NOT_REACHED();
3133 }
3134 }
3135
3136 in_port = get_ofp_port(ctx.xbridge, flow->in_port.ofp_port);
3137 if (in_port && in_port->is_tunnel && ctx.xin->resubmit_stats) {
3138 netdev_vport_inc_rx(in_port->netdev, ctx.xin->resubmit_stats);
3139 if (in_port->bfd) {
3140 bfd_account_rx(in_port->bfd, ctx.xin->resubmit_stats);
3141 }
3142 }
3143
3144 special = process_special(&ctx, flow, in_port, ctx.xin->packet);
3145 if (special) {
3146 ctx.xout->slow |= special;
3147 } else {
3148 size_t sample_actions_len;
3149
3150 if (flow->in_port.ofp_port
3151 != vsp_realdev_to_vlandev(ctx.xbridge->ofproto,
3152 flow->in_port.ofp_port,
3153 flow->vlan_tci)) {
3154 ctx.base_flow.vlan_tci = 0;
3155 }
3156
3157 add_sflow_action(&ctx);
3158 add_ipfix_action(&ctx);
3159 sample_actions_len = ctx.xout->odp_actions.size;
3160
3161 if (tnl_may_send && (!in_port || may_receive(in_port, &ctx))) {
3162 do_xlate_actions(ofpacts, ofpacts_len, &ctx);
3163
3164 /* We've let OFPP_NORMAL and the learning action look at the
3165 * packet, so drop it now if forwarding is disabled. */
3166 if (in_port && !xport_stp_forward_state(in_port)) {
3167 ctx.xout->odp_actions.size = sample_actions_len;
3168 }
3169 }
3170
3171 if (ctx.action_set.size) {
3172 xlate_action_set(&ctx);
3173 }
3174
3175 if (ctx.xbridge->has_in_band
3176 && in_band_must_output_to_local_port(flow)
3177 && !actions_output_to_local_port(&ctx)) {
3178 compose_output_action(&ctx, OFPP_LOCAL);
3179 }
3180
3181 fix_sflow_action(&ctx);
3182
3183 if (mbridge_has_mirrors(ctx.xbridge->mbridge)) {
3184 add_mirror_actions(&ctx, &orig_flow);
3185 }
3186 }
3187
3188 if (nl_attr_oversized(ctx.xout->odp_actions.size)) {
3189 /* These datapath actions are too big for a Netlink attribute, so we
3190 * can't hand them to the kernel directly. dpif_execute() can execute
3191 * them one by one with help, so just mark the result as SLOW_ACTION to
3192 * prevent the flow from being installed. */
3193 COVERAGE_INC(xlate_actions_oversize);
3194 ctx.xout->slow |= SLOW_ACTION;
3195 }
3196
3197 if (ctx.xin->resubmit_stats) {
3198 mirror_update_stats(ctx.xbridge->mbridge, xout->mirrors,
3199 ctx.xin->resubmit_stats->n_packets,
3200 ctx.xin->resubmit_stats->n_bytes);
3201
3202 if (ctx.xbridge->netflow) {
3203 const struct ofpact *ofpacts;
3204 size_t ofpacts_len;
3205
3206 ofpacts_len = actions->ofpacts_len;
3207 ofpacts = actions->ofpacts;
3208 if (ofpacts_len == 0
3209 || ofpacts->type != OFPACT_CONTROLLER
3210 || ofpact_next(ofpacts) < ofpact_end(ofpacts, ofpacts_len)) {
3211 /* Only update netflow if we don't have controller flow. We don't
3212 * report NetFlow expiration messages for such facets because they
3213 * are just part of the control logic for the network, not real
3214 * traffic. */
3215 netflow_flow_update(ctx.xbridge->netflow, flow,
3216 xout->nf_output_iface,
3217 ctx.xin->resubmit_stats);
3218 }
3219 }
3220 }
3221
3222 ofpbuf_uninit(&ctx.stack);
3223 ofpbuf_uninit(&ctx.action_set);
3224
3225 /* Clear the metadata and register wildcard masks, because we won't
3226 * use non-header fields as part of the cache. */
3227 flow_wildcards_clear_non_packet_fields(wc);
3228
3229 out:
3230 rule_actions_unref(actions);
3231 rule_dpif_unref(rule);
3232 }
3233
3234 /* Sends 'packet' out 'ofport'.
3235 * May modify 'packet'.
3236 * Returns 0 if successful, otherwise a positive errno value. */
3237 int
3238 xlate_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3239 {
3240 struct xport *xport;
3241 struct ofpact_output output;
3242 struct flow flow;
3243 union flow_in_port in_port_;
3244 int error;
3245
3246 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
3247 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
3248 in_port_.ofp_port = OFPP_NONE;
3249 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
3250
3251 ovs_rwlock_rdlock(&xlate_rwlock);
3252 xport = xport_lookup(ofport);
3253 if (!xport) {
3254 ovs_rwlock_unlock(&xlate_rwlock);
3255 return EINVAL;
3256 }
3257 output.port = xport->ofp_port;
3258 output.max_len = 0;
3259 error = ofproto_dpif_execute_actions(xport->xbridge->ofproto, &flow, NULL,
3260 &output.ofpact, sizeof output,
3261 packet);
3262 ovs_rwlock_unlock(&xlate_rwlock);
3263 return error;
3264 }