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