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