ofproto: Make rule construction and destruction more symmetric.
[sliver-openvswitch.git] / ofproto / ofproto-dpif.c
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "ofproto/private.h"
20
21 #include <errno.h>
22
23 #include "autopath.h"
24 #include "bond.h"
25 #include "byte-order.h"
26 #include "connmgr.h"
27 #include "coverage.h"
28 #include "cfm.h"
29 #include "dpif.h"
30 #include "dynamic-string.h"
31 #include "fail-open.h"
32 #include "hmapx.h"
33 #include "lacp.h"
34 #include "mac-learning.h"
35 #include "multipath.h"
36 #include "netdev.h"
37 #include "netlink.h"
38 #include "nx-match.h"
39 #include "odp-util.h"
40 #include "ofp-util.h"
41 #include "ofpbuf.h"
42 #include "ofp-print.h"
43 #include "ofproto-sflow.h"
44 #include "poll-loop.h"
45 #include "timer.h"
46 #include "unixctl.h"
47 #include "vlan-bitmap.h"
48 #include "vlog.h"
49
50 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
51
52 COVERAGE_DEFINE(ofproto_dpif_ctlr_action);
53 COVERAGE_DEFINE(ofproto_dpif_expired);
54 COVERAGE_DEFINE(ofproto_dpif_no_packet_in);
55 COVERAGE_DEFINE(ofproto_dpif_xlate);
56 COVERAGE_DEFINE(facet_changed_rule);
57 COVERAGE_DEFINE(facet_invalidated);
58 COVERAGE_DEFINE(facet_revalidate);
59 COVERAGE_DEFINE(facet_unexpected);
60
61 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
62  * flow translation. */
63 #define MAX_RESUBMIT_RECURSION 16
64
65 struct ofport_dpif;
66 struct ofproto_dpif;
67
68 struct rule_dpif {
69     struct rule up;
70
71     long long int used;         /* Time last used; time created if not used. */
72
73     /* These statistics:
74      *
75      *   - Do include packets and bytes from facets that have been deleted or
76      *     whose own statistics have been folded into the rule.
77      *
78      *   - Do include packets and bytes sent "by hand" that were accounted to
79      *     the rule without any facet being involved (this is a rare corner
80      *     case in rule_execute()).
81      *
82      *   - Do not include packet or bytes that can be obtained from any facet's
83      *     packet_count or byte_count member or that can be obtained from the
84      *     datapath by, e.g., dpif_flow_get() for any facet.
85      */
86     uint64_t packet_count;       /* Number of packets received. */
87     uint64_t byte_count;         /* Number of bytes received. */
88
89     struct list facets;          /* List of "struct facet"s. */
90 };
91
92 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
93 {
94     return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
95 }
96
97 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *ofproto,
98                                           const struct flow *flow);
99
100 #define MAX_MIRRORS 32
101 typedef uint32_t mirror_mask_t;
102 #define MIRROR_MASK_C(X) UINT32_C(X)
103 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
104 struct ofmirror {
105     struct ofproto_dpif *ofproto; /* Owning ofproto. */
106     size_t idx;                 /* In ofproto's "mirrors" array. */
107     void *aux;                  /* Key supplied by ofproto's client. */
108     char *name;                 /* Identifier for log messages. */
109
110     /* Selection criteria. */
111     struct hmapx srcs;          /* Contains "struct ofbundle *"s. */
112     struct hmapx dsts;          /* Contains "struct ofbundle *"s. */
113     unsigned long *vlans;       /* Bitmap of chosen VLANs, NULL selects all. */
114
115     /* Output (mutually exclusive). */
116     struct ofbundle *out;       /* Output port or NULL. */
117     int out_vlan;               /* Output VLAN or -1. */
118 };
119
120 static void mirror_destroy(struct ofmirror *);
121
122 /* A group of one or more OpenFlow ports. */
123 #define OFBUNDLE_FLOOD ((struct ofbundle *) 1)
124 struct ofbundle {
125     struct ofproto_dpif *ofproto; /* Owning ofproto. */
126     struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
127     void *aux;                  /* Key supplied by ofproto's client. */
128     char *name;                 /* Identifier for log messages. */
129
130     /* Configuration. */
131     struct list ports;          /* Contains "struct ofport"s. */
132     int vlan;                   /* -1=trunk port, else a 12-bit VLAN ID. */
133     unsigned long *trunks;      /* Bitmap of trunked VLANs, if 'vlan' == -1.
134                                  * NULL if all VLANs are trunked. */
135     struct lacp *lacp;          /* LACP if LACP is enabled, otherwise NULL. */
136     struct bond *bond;          /* Nonnull iff more than one port. */
137
138     /* Status. */
139     bool floodable;             /* True if no port has OFPPC_NO_FLOOD set. */
140
141     /* Port mirroring info. */
142     mirror_mask_t src_mirrors;  /* Mirrors triggered when packet received. */
143     mirror_mask_t dst_mirrors;  /* Mirrors triggered when packet sent. */
144     mirror_mask_t mirror_out;   /* Mirrors that output to this bundle. */
145 };
146
147 static void bundle_remove(struct ofport *);
148 static void bundle_destroy(struct ofbundle *);
149 static void bundle_del_port(struct ofport_dpif *);
150 static void bundle_run(struct ofbundle *);
151 static void bundle_wait(struct ofbundle *);
152
153 struct action_xlate_ctx {
154 /* action_xlate_ctx_init() initializes these members. */
155
156     /* The ofproto. */
157     struct ofproto_dpif *ofproto;
158
159     /* Flow to which the OpenFlow actions apply.  xlate_actions() will modify
160      * this flow when actions change header fields. */
161     struct flow flow;
162
163     /* The packet corresponding to 'flow', or a null pointer if we are
164      * revalidating without a packet to refer to. */
165     const struct ofpbuf *packet;
166
167     /* If nonnull, called just before executing a resubmit action.
168      *
169      * This is normally null so the client has to set it manually after
170      * calling action_xlate_ctx_init(). */
171     void (*resubmit_hook)(struct action_xlate_ctx *, struct rule_dpif *);
172
173     /* If true, the speciality of 'flow' should be checked before executing
174      * its actions.  If special_cb returns false on 'flow' rendered
175      * uninstallable and no actions will be executed. */
176     bool check_special;
177
178 /* xlate_actions() initializes and uses these members.  The client might want
179  * to look at them after it returns. */
180
181     struct ofpbuf *odp_actions; /* Datapath actions. */
182     tag_type tags;              /* Tags associated with OFPP_NORMAL actions. */
183     bool may_set_up_flow;       /* True ordinarily; false if the actions must
184                                  * be reassessed for every packet. */
185     uint16_t nf_output_iface;   /* Output interface index for NetFlow. */
186
187 /* xlate_actions() initializes and uses these members, but the client has no
188  * reason to look at them. */
189
190     int recurse;                /* Recursion level, via xlate_table_action. */
191     int last_pop_priority;      /* Offset in 'odp_actions' just past most
192                                  * recent ODP_ACTION_ATTR_SET_PRIORITY. */
193 };
194
195 static void action_xlate_ctx_init(struct action_xlate_ctx *,
196                                   struct ofproto_dpif *, const struct flow *,
197                                   const struct ofpbuf *);
198 static struct ofpbuf *xlate_actions(struct action_xlate_ctx *,
199                                     const union ofp_action *in, size_t n_in);
200
201 /* An exact-match instantiation of an OpenFlow flow. */
202 struct facet {
203     long long int used;         /* Time last used; time created if not used. */
204
205     /* These statistics:
206      *
207      *   - Do include packets and bytes sent "by hand", e.g. with
208      *     dpif_execute().
209      *
210      *   - Do include packets and bytes that were obtained from the datapath
211      *     when a flow was deleted (e.g. dpif_flow_del()) or when its
212      *     statistics were reset (e.g. dpif_flow_put() with
213      *     DPIF_FP_ZERO_STATS).
214      *
215      *   - Do not include any packets or bytes that can currently be obtained
216      *     from the datapath by, e.g., dpif_flow_get().
217      */
218     uint64_t packet_count;       /* Number of packets received. */
219     uint64_t byte_count;         /* Number of bytes received. */
220
221     uint64_t dp_packet_count;    /* Last known packet count in the datapath. */
222     uint64_t dp_byte_count;      /* Last known byte count in the datapath. */
223
224     uint64_t rs_packet_count;    /* Packets pushed to resubmit children. */
225     uint64_t rs_byte_count;      /* Bytes pushed to resubmit children. */
226     long long int rs_used;       /* Used time pushed to resubmit children. */
227
228     /* Number of bytes passed to account_cb.  This may include bytes that can
229      * currently obtained from the datapath (thus, it can be greater than
230      * byte_count). */
231     uint64_t accounted_bytes;
232
233     struct hmap_node hmap_node;  /* In owning ofproto's 'facets' hmap. */
234     struct list list_node;       /* In owning rule's 'facets' list. */
235     struct rule_dpif *rule;      /* Owning rule. */
236     struct flow flow;            /* Exact-match flow. */
237     bool installed;              /* Installed in datapath? */
238     bool may_install;            /* True ordinarily; false if actions must
239                                   * be reassessed for every packet. */
240     size_t actions_len;          /* Number of bytes in actions[]. */
241     struct nlattr *actions;      /* Datapath actions. */
242     tag_type tags;               /* Tags. */
243     struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
244 };
245
246 static struct facet *facet_create(struct rule_dpif *, const struct flow *,
247                                   const struct ofpbuf *packet);
248 static void facet_remove(struct ofproto_dpif *, struct facet *);
249 static void facet_free(struct facet *);
250
251 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
252 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
253                                         const struct flow *);
254 static bool facet_revalidate(struct ofproto_dpif *, struct facet *);
255
256 static void facet_execute(struct ofproto_dpif *, struct facet *,
257                           struct ofpbuf *packet);
258
259 static int facet_put__(struct ofproto_dpif *, struct facet *,
260                        const struct nlattr *actions, size_t actions_len,
261                        struct dpif_flow_stats *);
262 static void facet_install(struct ofproto_dpif *, struct facet *,
263                           bool zero_stats);
264 static void facet_uninstall(struct ofproto_dpif *, struct facet *);
265 static void facet_flush_stats(struct ofproto_dpif *, struct facet *);
266
267 static void facet_make_actions(struct ofproto_dpif *, struct facet *,
268                                const struct ofpbuf *packet);
269 static void facet_update_time(struct ofproto_dpif *, struct facet *,
270                               long long int used);
271 static void facet_update_stats(struct ofproto_dpif *, struct facet *,
272                                const struct dpif_flow_stats *);
273 static void facet_push_stats(struct facet *);
274 static void facet_account(struct ofproto_dpif *, struct facet *,
275                           uint64_t extra_bytes);
276
277 static bool facet_is_controller_flow(struct facet *);
278
279 static void flow_push_stats(const struct rule_dpif *,
280                             struct flow *, uint64_t packets, uint64_t bytes,
281                             long long int used);
282
283 struct ofport_dpif {
284     struct ofport up;
285
286     uint32_t odp_port;
287     struct ofbundle *bundle;    /* Bundle that contains this port, if any. */
288     struct list bundle_node;    /* In struct ofbundle's "ports" list. */
289     struct cfm *cfm;            /* Connectivity Fault Management, if any. */
290     tag_type tag;               /* Tag associated with this port. */
291 };
292
293 static struct ofport_dpif *
294 ofport_dpif_cast(const struct ofport *ofport)
295 {
296     assert(ofport->ofproto->ofproto_class == &ofproto_dpif_class);
297     return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
298 }
299
300 static void port_run(struct ofport_dpif *);
301 static void port_wait(struct ofport_dpif *);
302 static int set_cfm(struct ofport *, const struct cfm *,
303                    const uint16_t *remote_mps, size_t n_remote_mps);
304
305 struct ofproto_dpif {
306     struct ofproto up;
307     struct dpif *dpif;
308     int max_ports;
309
310     /* Bridging. */
311     struct netflow *netflow;
312     struct ofproto_sflow *sflow;
313     struct hmap bundles;        /* Contains "struct ofbundle"s. */
314     struct mac_learning *ml;
315     struct ofmirror *mirrors[MAX_MIRRORS];
316     bool has_bonded_bundles;
317
318     /* Expiration. */
319     struct timer next_expiration;
320
321     /* Facets. */
322     struct hmap facets;
323     bool need_revalidate;
324     struct tag_set revalidate_set;
325 };
326
327 static void ofproto_dpif_unixctl_init(void);
328
329 static struct ofproto_dpif *
330 ofproto_dpif_cast(const struct ofproto *ofproto)
331 {
332     assert(ofproto->ofproto_class == &ofproto_dpif_class);
333     return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
334 }
335
336 static struct ofport_dpif *get_ofp_port(struct ofproto_dpif *,
337                                         uint16_t ofp_port);
338 static struct ofport_dpif *get_odp_port(struct ofproto_dpif *,
339                                         uint32_t odp_port);
340
341 /* Packet processing. */
342 static void update_learning_table(struct ofproto_dpif *,
343                                   const struct flow *, int vlan,
344                                   struct ofbundle *);
345 static bool is_admissible(struct ofproto_dpif *, const struct flow *,
346                           bool have_packet, tag_type *, int *vlanp,
347                           struct ofbundle **in_bundlep);
348 static void handle_upcall(struct ofproto_dpif *, struct dpif_upcall *);
349
350 /* Flow expiration. */
351 static int expire(struct ofproto_dpif *);
352
353 /* Utilities. */
354 static int send_packet(struct ofproto_dpif *,
355                        uint32_t odp_port, uint16_t vlan_tci,
356                        const struct ofpbuf *packet);
357
358 /* Global variables. */
359 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
360 \f
361 /* Factory functions. */
362
363 static void
364 enumerate_types(struct sset *types)
365 {
366     dp_enumerate_types(types);
367 }
368
369 static int
370 enumerate_names(const char *type, struct sset *names)
371 {
372     return dp_enumerate_names(type, names);
373 }
374
375 static int
376 del(const char *type, const char *name)
377 {
378     struct dpif *dpif;
379     int error;
380
381     error = dpif_open(name, type, &dpif);
382     if (!error) {
383         error = dpif_delete(dpif);
384         dpif_close(dpif);
385     }
386     return error;
387 }
388 \f
389 /* Basic life-cycle. */
390
391 static struct ofproto *
392 alloc(void)
393 {
394     struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
395     return &ofproto->up;
396 }
397
398 static void
399 dealloc(struct ofproto *ofproto_)
400 {
401     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
402     free(ofproto);
403 }
404
405 static int
406 construct(struct ofproto *ofproto_)
407 {
408     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
409     const char *name = ofproto->up.name;
410     int error;
411     int i;
412
413     error = dpif_create_and_open(name, ofproto->up.type, &ofproto->dpif);
414     if (error) {
415         VLOG_ERR("failed to open datapath %s: %s", name, strerror(error));
416         return error;
417     }
418
419     ofproto->max_ports = dpif_get_max_ports(ofproto->dpif);
420
421     error = dpif_recv_set_mask(ofproto->dpif,
422                                ((1u << DPIF_UC_MISS) |
423                                 (1u << DPIF_UC_ACTION) |
424                                 (1u << DPIF_UC_SAMPLE)));
425     if (error) {
426         VLOG_ERR("failed to listen on datapath %s: %s", name, strerror(error));
427         dpif_close(ofproto->dpif);
428         return error;
429     }
430     dpif_flow_flush(ofproto->dpif);
431     dpif_recv_purge(ofproto->dpif);
432
433     ofproto->netflow = NULL;
434     ofproto->sflow = NULL;
435     hmap_init(&ofproto->bundles);
436     ofproto->ml = mac_learning_create();
437     for (i = 0; i < MAX_MIRRORS; i++) {
438         ofproto->mirrors[i] = NULL;
439     }
440     ofproto->has_bonded_bundles = false;
441
442     timer_set_duration(&ofproto->next_expiration, 1000);
443
444     hmap_init(&ofproto->facets);
445     ofproto->need_revalidate = false;
446     tag_set_init(&ofproto->revalidate_set);
447
448     ofproto_dpif_unixctl_init();
449
450     return 0;
451 }
452
453 static void
454 destruct(struct ofproto *ofproto_)
455 {
456     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
457     int i;
458
459     for (i = 0; i < MAX_MIRRORS; i++) {
460         mirror_destroy(ofproto->mirrors[i]);
461     }
462
463     netflow_destroy(ofproto->netflow);
464     ofproto_sflow_destroy(ofproto->sflow);
465     hmap_destroy(&ofproto->bundles);
466     mac_learning_destroy(ofproto->ml);
467
468     hmap_destroy(&ofproto->facets);
469
470     dpif_close(ofproto->dpif);
471 }
472
473 static int
474 run(struct ofproto *ofproto_)
475 {
476     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
477     struct ofport_dpif *ofport;
478     struct ofbundle *bundle;
479     int i;
480
481     dpif_run(ofproto->dpif);
482
483     for (i = 0; i < 50; i++) {
484         struct dpif_upcall packet;
485         int error;
486
487         error = dpif_recv(ofproto->dpif, &packet);
488         if (error) {
489             if (error == ENODEV) {
490                 /* Datapath destroyed. */
491                 return error;
492             }
493             break;
494         }
495
496         handle_upcall(ofproto, &packet);
497     }
498
499     if (timer_expired(&ofproto->next_expiration)) {
500         int delay = expire(ofproto);
501         timer_set_duration(&ofproto->next_expiration, delay);
502     }
503
504     if (ofproto->netflow) {
505         netflow_run(ofproto->netflow);
506     }
507     if (ofproto->sflow) {
508         ofproto_sflow_run(ofproto->sflow);
509     }
510
511     HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
512         port_run(ofport);
513     }
514     HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
515         bundle_run(bundle);
516     }
517
518     /* Now revalidate if there's anything to do. */
519     if (ofproto->need_revalidate
520         || !tag_set_is_empty(&ofproto->revalidate_set)) {
521         struct tag_set revalidate_set = ofproto->revalidate_set;
522         bool revalidate_all = ofproto->need_revalidate;
523         struct facet *facet, *next;
524
525         /* Clear the revalidation flags. */
526         tag_set_init(&ofproto->revalidate_set);
527         ofproto->need_revalidate = false;
528
529         HMAP_FOR_EACH_SAFE (facet, next, hmap_node, &ofproto->facets) {
530             if (revalidate_all
531                 || tag_set_intersects(&revalidate_set, facet->tags)) {
532                 facet_revalidate(ofproto, facet);
533             }
534         }
535     }
536
537     return 0;
538 }
539
540 static void
541 wait(struct ofproto *ofproto_)
542 {
543     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
544     struct ofport_dpif *ofport;
545     struct ofbundle *bundle;
546
547     dpif_wait(ofproto->dpif);
548     dpif_recv_wait(ofproto->dpif);
549     if (ofproto->sflow) {
550         ofproto_sflow_wait(ofproto->sflow);
551     }
552     if (!tag_set_is_empty(&ofproto->revalidate_set)) {
553         poll_immediate_wake();
554     }
555     HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
556         port_wait(ofport);
557     }
558     HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
559         bundle_wait(bundle);
560     }
561     if (ofproto->need_revalidate) {
562         /* Shouldn't happen, but if it does just go around again. */
563         VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
564         poll_immediate_wake();
565     } else {
566         timer_wait(&ofproto->next_expiration);
567     }
568 }
569
570 static void
571 flush(struct ofproto *ofproto_)
572 {
573     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
574     struct facet *facet, *next_facet;
575
576     HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
577         /* Mark the facet as not installed so that facet_remove() doesn't
578          * bother trying to uninstall it.  There is no point in uninstalling it
579          * individually since we are about to blow away all the facets with
580          * dpif_flow_flush(). */
581         facet->installed = false;
582         facet->dp_packet_count = 0;
583         facet->dp_byte_count = 0;
584         facet_remove(ofproto, facet);
585     }
586     dpif_flow_flush(ofproto->dpif);
587 }
588
589 static int
590 set_netflow(struct ofproto *ofproto_,
591             const struct netflow_options *netflow_options)
592 {
593     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
594
595     if (netflow_options) {
596         if (!ofproto->netflow) {
597             ofproto->netflow = netflow_create();
598         }
599         return netflow_set_options(ofproto->netflow, netflow_options);
600     } else {
601         netflow_destroy(ofproto->netflow);
602         ofproto->netflow = NULL;
603         return 0;
604     }
605 }
606
607 static struct ofport *
608 port_alloc(void)
609 {
610     struct ofport_dpif *port = xmalloc(sizeof *port);
611     return &port->up;
612 }
613
614 static void
615 port_dealloc(struct ofport *port_)
616 {
617     struct ofport_dpif *port = ofport_dpif_cast(port_);
618     free(port);
619 }
620
621 static int
622 port_construct(struct ofport *port_)
623 {
624     struct ofport_dpif *port = ofport_dpif_cast(port_);
625     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
626
627     port->odp_port = ofp_port_to_odp_port(port->up.ofp_port);
628     port->bundle = NULL;
629     port->cfm = NULL;
630     port->tag = tag_create_random();
631
632     if (ofproto->sflow) {
633         ofproto_sflow_add_port(ofproto->sflow, port->odp_port,
634                                netdev_get_name(port->up.netdev));
635     }
636
637     return 0;
638 }
639
640 static void
641 port_destruct(struct ofport *port_)
642 {
643     struct ofport_dpif *port = ofport_dpif_cast(port_);
644     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
645
646     bundle_remove(port_);
647     set_cfm(port_, NULL, NULL, 0);
648     if (ofproto->sflow) {
649         ofproto_sflow_del_port(ofproto->sflow, port->odp_port);
650     }
651 }
652
653 static void
654 port_modified(struct ofport *port_)
655 {
656     struct ofport_dpif *port = ofport_dpif_cast(port_);
657
658     if (port->bundle && port->bundle->bond) {
659         bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
660     }
661 }
662
663 static void
664 port_reconfigured(struct ofport *port_, ovs_be32 old_config)
665 {
666     struct ofport_dpif *port = ofport_dpif_cast(port_);
667     struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
668     ovs_be32 changed = old_config ^ port->up.opp.config;
669
670     if (changed & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP |
671                         OFPPC_NO_FWD | OFPPC_NO_FLOOD)) {
672         ofproto->need_revalidate = true;
673     }
674 }
675
676 static int
677 set_sflow(struct ofproto *ofproto_,
678           const struct ofproto_sflow_options *sflow_options)
679 {
680     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
681     struct ofproto_sflow *os = ofproto->sflow;
682     if (sflow_options) {
683         if (!os) {
684             struct ofport_dpif *ofport;
685
686             os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
687             HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
688                 ofproto_sflow_add_port(os, ofport->odp_port,
689                                        netdev_get_name(ofport->up.netdev));
690             }
691         }
692         ofproto_sflow_set_options(os, sflow_options);
693     } else {
694         ofproto_sflow_destroy(os);
695         ofproto->sflow = NULL;
696     }
697     return 0;
698 }
699
700 static int
701 set_cfm(struct ofport *ofport_, const struct cfm *cfm,
702         const uint16_t *remote_mps, size_t n_remote_mps)
703 {
704     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
705     int error;
706
707     if (!cfm) {
708         error = 0;
709     } else {
710         if (!ofport->cfm) {
711             ofport->cfm = cfm_create();
712         }
713
714         ofport->cfm->mpid = cfm->mpid;
715         ofport->cfm->interval = cfm->interval;
716         memcpy(ofport->cfm->maid, cfm->maid, CCM_MAID_LEN);
717
718         cfm_update_remote_mps(ofport->cfm, remote_mps, n_remote_mps);
719
720         if (cfm_configure(ofport->cfm)) {
721             return 0;
722         }
723
724         error = EINVAL;
725     }
726     cfm_destroy(ofport->cfm);
727     ofport->cfm = NULL;
728     return error;
729 }
730
731 static int
732 get_cfm(struct ofport *ofport_, const struct cfm **cfmp)
733 {
734     struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
735     *cfmp = ofport->cfm;
736     return 0;
737 }
738 \f
739 /* Bundles. */
740
741 /* Expires all MAC learning entries associated with 'port' and forces ofproto
742  * to revalidate every flow. */
743 static void
744 bundle_flush_macs(struct ofbundle *bundle)
745 {
746     struct ofproto_dpif *ofproto = bundle->ofproto;
747     struct mac_learning *ml = ofproto->ml;
748     struct mac_entry *mac, *next_mac;
749
750     ofproto->need_revalidate = true;
751     LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
752         if (mac->port.p == bundle) {
753             mac_learning_expire(ml, mac);
754         }
755     }
756 }
757
758 static struct ofbundle *
759 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
760 {
761     struct ofbundle *bundle;
762
763     HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
764                              &ofproto->bundles) {
765         if (bundle->aux == aux) {
766             return bundle;
767         }
768     }
769     return NULL;
770 }
771
772 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
773  * ones that are found to 'bundles'. */
774 static void
775 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
776                        void **auxes, size_t n_auxes,
777                        struct hmapx *bundles)
778 {
779     size_t i;
780
781     hmapx_init(bundles);
782     for (i = 0; i < n_auxes; i++) {
783         struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
784         if (bundle) {
785             hmapx_add(bundles, bundle);
786         }
787     }
788 }
789
790 static void
791 bundle_del_port(struct ofport_dpif *port)
792 {
793     struct ofbundle *bundle = port->bundle;
794
795     list_remove(&port->bundle_node);
796     port->bundle = NULL;
797
798     if (bundle->lacp) {
799         lacp_slave_unregister(bundle->lacp, port);
800     }
801     if (bundle->bond) {
802         bond_slave_unregister(bundle->bond, port);
803     }
804
805     bundle->floodable = true;
806     LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
807         if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
808             bundle->floodable = false;
809         }
810     }
811 }
812
813 static bool
814 bundle_add_port(struct ofbundle *bundle, uint32_t ofp_port,
815                 struct lacp_slave_settings *lacp)
816 {
817     struct ofport_dpif *port;
818
819     port = get_ofp_port(bundle->ofproto, ofp_port);
820     if (!port) {
821         return false;
822     }
823
824     if (port->bundle != bundle) {
825         if (port->bundle) {
826             bundle_del_port(port);
827         }
828
829         port->bundle = bundle;
830         list_push_back(&bundle->ports, &port->bundle_node);
831         if (port->up.opp.config & htonl(OFPPC_NO_FLOOD)) {
832             bundle->floodable = false;
833         }
834     }
835     if (lacp) {
836         lacp_slave_register(bundle->lacp, port, lacp);
837     }
838
839     return true;
840 }
841
842 static void
843 bundle_destroy(struct ofbundle *bundle)
844 {
845     struct ofproto_dpif *ofproto;
846     struct ofport_dpif *port, *next_port;
847     int i;
848
849     if (!bundle) {
850         return;
851     }
852
853     ofproto = bundle->ofproto;
854     for (i = 0; i < MAX_MIRRORS; i++) {
855         struct ofmirror *m = ofproto->mirrors[i];
856         if (m) {
857             if (m->out == bundle) {
858                 mirror_destroy(m);
859             } else if (hmapx_find_and_delete(&m->srcs, bundle)
860                        || hmapx_find_and_delete(&m->dsts, bundle)) {
861                 ofproto->need_revalidate = true;
862             }
863         }
864     }
865
866     LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
867         bundle_del_port(port);
868     }
869
870     bundle_flush_macs(bundle);
871     hmap_remove(&ofproto->bundles, &bundle->hmap_node);
872     free(bundle->name);
873     free(bundle->trunks);
874     lacp_destroy(bundle->lacp);
875     bond_destroy(bundle->bond);
876     free(bundle);
877 }
878
879 static int
880 bundle_set(struct ofproto *ofproto_, void *aux,
881            const struct ofproto_bundle_settings *s)
882 {
883     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
884     bool need_flush = false;
885     const unsigned long *trunks;
886     struct ofport_dpif *port;
887     struct ofbundle *bundle;
888     size_t i;
889     bool ok;
890
891     if (!s) {
892         bundle_destroy(bundle_lookup(ofproto, aux));
893         return 0;
894     }
895
896     assert(s->n_slaves == 1 || s->bond != NULL);
897     assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
898
899     bundle = bundle_lookup(ofproto, aux);
900     if (!bundle) {
901         bundle = xmalloc(sizeof *bundle);
902
903         bundle->ofproto = ofproto;
904         hmap_insert(&ofproto->bundles, &bundle->hmap_node,
905                     hash_pointer(aux, 0));
906         bundle->aux = aux;
907         bundle->name = NULL;
908
909         list_init(&bundle->ports);
910         bundle->vlan = -1;
911         bundle->trunks = NULL;
912         bundle->lacp = NULL;
913         bundle->bond = NULL;
914
915         bundle->floodable = true;
916
917         bundle->src_mirrors = 0;
918         bundle->dst_mirrors = 0;
919         bundle->mirror_out = 0;
920     }
921
922     if (!bundle->name || strcmp(s->name, bundle->name)) {
923         free(bundle->name);
924         bundle->name = xstrdup(s->name);
925     }
926
927     /* LACP. */
928     if (s->lacp) {
929         if (!bundle->lacp) {
930             bundle->lacp = lacp_create();
931         }
932         lacp_configure(bundle->lacp, s->lacp);
933     } else {
934         lacp_destroy(bundle->lacp);
935         bundle->lacp = NULL;
936     }
937
938     /* Update set of ports. */
939     ok = true;
940     for (i = 0; i < s->n_slaves; i++) {
941         if (!bundle_add_port(bundle, s->slaves[i],
942                              s->lacp ? &s->lacp_slaves[i] : NULL)) {
943             ok = false;
944         }
945     }
946     if (!ok || list_size(&bundle->ports) != s->n_slaves) {
947         struct ofport_dpif *next_port;
948
949         LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
950             for (i = 0; i < s->n_slaves; i++) {
951                 if (s->slaves[i] == odp_port_to_ofp_port(port->odp_port)) {
952                     goto found;
953                 }
954             }
955
956             bundle_del_port(port);
957         found: ;
958         }
959     }
960     assert(list_size(&bundle->ports) <= s->n_slaves);
961
962     if (list_is_empty(&bundle->ports)) {
963         bundle_destroy(bundle);
964         return EINVAL;
965     }
966
967     /* Set VLAN tag. */
968     if (s->vlan != bundle->vlan) {
969         bundle->vlan = s->vlan;
970         need_flush = true;
971     }
972
973     /* Get trunked VLANs. */
974     trunks = s->vlan == -1 ? NULL : s->trunks;
975     if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
976         free(bundle->trunks);
977         bundle->trunks = vlan_bitmap_clone(trunks);
978         need_flush = true;
979     }
980
981     /* Bonding. */
982     if (!list_is_short(&bundle->ports)) {
983         bundle->ofproto->has_bonded_bundles = true;
984         if (bundle->bond) {
985             if (bond_reconfigure(bundle->bond, s->bond)) {
986                 ofproto->need_revalidate = true;
987             }
988         } else {
989             bundle->bond = bond_create(s->bond);
990         }
991
992         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
993             uint16_t stable_id = (bundle->lacp
994                                   ? lacp_slave_get_port_id(bundle->lacp, port)
995                                   : port->odp_port);
996             bond_slave_register(bundle->bond, port, stable_id,
997                                 port->up.netdev);
998         }
999     } else {
1000         bond_destroy(bundle->bond);
1001         bundle->bond = NULL;
1002     }
1003
1004     /* If we changed something that would affect MAC learning, un-learn
1005      * everything on this port and force flow revalidation. */
1006     if (need_flush) {
1007         bundle_flush_macs(bundle);
1008     }
1009
1010     return 0;
1011 }
1012
1013 static void
1014 bundle_remove(struct ofport *port_)
1015 {
1016     struct ofport_dpif *port = ofport_dpif_cast(port_);
1017     struct ofbundle *bundle = port->bundle;
1018
1019     if (bundle) {
1020         bundle_del_port(port);
1021         if (list_is_empty(&bundle->ports)) {
1022             bundle_destroy(bundle);
1023         } else if (list_is_short(&bundle->ports)) {
1024             bond_destroy(bundle->bond);
1025             bundle->bond = NULL;
1026         }
1027     }
1028 }
1029
1030 static void
1031 send_pdu_cb(void *port_, const struct lacp_pdu *pdu)
1032 {
1033     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
1034     struct ofport_dpif *port = port_;
1035     uint8_t ea[ETH_ADDR_LEN];
1036     int error;
1037
1038     error = netdev_get_etheraddr(port->up.netdev, ea);
1039     if (!error) {
1040         struct lacp_pdu *packet_pdu;
1041         struct ofpbuf packet;
1042
1043         ofpbuf_init(&packet, 0);
1044         packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
1045                                  sizeof *packet_pdu);
1046         *packet_pdu = *pdu;
1047         error = netdev_send(port->up.netdev, &packet);
1048         if (error) {
1049             VLOG_WARN_RL(&rl, "port %s: sending LACP PDU on iface %s failed "
1050                          "(%s)", port->bundle->name,
1051                          netdev_get_name(port->up.netdev), strerror(error));
1052         }
1053         ofpbuf_uninit(&packet);
1054     } else {
1055         VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
1056                     "%s (%s)", port->bundle->name,
1057                     netdev_get_name(port->up.netdev), strerror(error));
1058     }
1059 }
1060
1061 static void
1062 bundle_send_learning_packets(struct ofbundle *bundle)
1063 {
1064     struct ofproto_dpif *ofproto = bundle->ofproto;
1065     int error, n_packets, n_errors;
1066     struct mac_entry *e;
1067
1068     error = n_packets = n_errors = 0;
1069     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
1070         if (e->port.p != bundle) {
1071             int ret = bond_send_learning_packet(bundle->bond, e->mac, e->vlan);
1072             if (ret) {
1073                 error = ret;
1074                 n_errors++;
1075             }
1076             n_packets++;
1077         }
1078     }
1079
1080     if (n_errors) {
1081         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1082         VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
1083                      "packets, last error was: %s",
1084                      bundle->name, n_errors, n_packets, strerror(error));
1085     } else {
1086         VLOG_DBG("bond %s: sent %d gratuitous learning packets",
1087                  bundle->name, n_packets);
1088     }
1089 }
1090
1091 static void
1092 bundle_run(struct ofbundle *bundle)
1093 {
1094     if (bundle->lacp) {
1095         lacp_run(bundle->lacp, send_pdu_cb);
1096     }
1097     if (bundle->bond) {
1098         struct ofport_dpif *port;
1099
1100         LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1101             bool may_enable = lacp_slave_may_enable(bundle->lacp, port);
1102             bond_slave_set_lacp_may_enable(bundle->bond, port, may_enable);
1103         }
1104
1105         bond_run(bundle->bond, &bundle->ofproto->revalidate_set,
1106                  lacp_negotiated(bundle->lacp));
1107         if (bond_should_send_learning_packets(bundle->bond)) {
1108             bundle_send_learning_packets(bundle);
1109         }
1110     }
1111 }
1112
1113 static void
1114 bundle_wait(struct ofbundle *bundle)
1115 {
1116     if (bundle->lacp) {
1117         lacp_wait(bundle->lacp);
1118     }
1119     if (bundle->bond) {
1120         bond_wait(bundle->bond);
1121     }
1122 }
1123 \f
1124 /* Mirrors. */
1125
1126 static int
1127 mirror_scan(struct ofproto_dpif *ofproto)
1128 {
1129     int idx;
1130
1131     for (idx = 0; idx < MAX_MIRRORS; idx++) {
1132         if (!ofproto->mirrors[idx]) {
1133             return idx;
1134         }
1135     }
1136     return -1;
1137 }
1138
1139 static struct ofmirror *
1140 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
1141 {
1142     int i;
1143
1144     for (i = 0; i < MAX_MIRRORS; i++) {
1145         struct ofmirror *mirror = ofproto->mirrors[i];
1146         if (mirror && mirror->aux == aux) {
1147             return mirror;
1148         }
1149     }
1150
1151     return NULL;
1152 }
1153
1154 static int
1155 mirror_set(struct ofproto *ofproto_, void *aux,
1156            const struct ofproto_mirror_settings *s)
1157 {
1158     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1159     mirror_mask_t mirror_bit;
1160     struct ofbundle *bundle;
1161     struct ofmirror *mirror;
1162     struct ofbundle *out;
1163     struct hmapx srcs;          /* Contains "struct ofbundle *"s. */
1164     struct hmapx dsts;          /* Contains "struct ofbundle *"s. */
1165     int out_vlan;
1166
1167     mirror = mirror_lookup(ofproto, aux);
1168     if (!s) {
1169         mirror_destroy(mirror);
1170         return 0;
1171     }
1172     if (!mirror) {
1173         int idx;
1174
1175         idx = mirror_scan(ofproto);
1176         if (idx < 0) {
1177             VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
1178                       "cannot create %s",
1179                       ofproto->up.name, MAX_MIRRORS, s->name);
1180             return EFBIG;
1181         }
1182
1183         mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
1184         mirror->ofproto = ofproto;
1185         mirror->idx = idx;
1186         mirror->out_vlan = -1;
1187         mirror->name = NULL;
1188     }
1189
1190     if (!mirror->name || strcmp(s->name, mirror->name)) {
1191         free(mirror->name);
1192         mirror->name = xstrdup(s->name);
1193     }
1194
1195     /* Get the new configuration. */
1196     if (s->out_bundle) {
1197         out = bundle_lookup(ofproto, s->out_bundle);
1198         if (!out) {
1199             mirror_destroy(mirror);
1200             return EINVAL;
1201         }
1202         out_vlan = -1;
1203     } else {
1204         out = NULL;
1205         out_vlan = s->out_vlan;
1206     }
1207     bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
1208     bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
1209
1210     /* If the configuration has not changed, do nothing. */
1211     if (hmapx_equals(&srcs, &mirror->srcs)
1212         && hmapx_equals(&dsts, &mirror->dsts)
1213         && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
1214         && mirror->out == out
1215         && mirror->out_vlan == out_vlan)
1216     {
1217         hmapx_destroy(&srcs);
1218         hmapx_destroy(&dsts);
1219         return 0;
1220     }
1221
1222     hmapx_swap(&srcs, &mirror->srcs);
1223     hmapx_destroy(&srcs);
1224
1225     hmapx_swap(&dsts, &mirror->dsts);
1226     hmapx_destroy(&dsts);
1227
1228     free(mirror->vlans);
1229     mirror->vlans = vlan_bitmap_clone(s->src_vlans);
1230
1231     mirror->out = out;
1232     mirror->out_vlan = out_vlan;
1233
1234     /* Update bundles. */
1235     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1236     HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
1237         if (hmapx_contains(&mirror->srcs, bundle)) {
1238             bundle->src_mirrors |= mirror_bit;
1239         } else {
1240             bundle->src_mirrors &= ~mirror_bit;
1241         }
1242
1243         if (hmapx_contains(&mirror->dsts, bundle)) {
1244             bundle->dst_mirrors |= mirror_bit;
1245         } else {
1246             bundle->dst_mirrors &= ~mirror_bit;
1247         }
1248
1249         if (mirror->out == bundle) {
1250             bundle->mirror_out |= mirror_bit;
1251         } else {
1252             bundle->mirror_out &= ~mirror_bit;
1253         }
1254     }
1255
1256     ofproto->need_revalidate = true;
1257     mac_learning_flush(ofproto->ml);
1258
1259     return 0;
1260 }
1261
1262 static void
1263 mirror_destroy(struct ofmirror *mirror)
1264 {
1265     struct ofproto_dpif *ofproto;
1266     mirror_mask_t mirror_bit;
1267     struct ofbundle *bundle;
1268
1269     if (!mirror) {
1270         return;
1271     }
1272
1273     ofproto = mirror->ofproto;
1274     ofproto->need_revalidate = true;
1275     mac_learning_flush(ofproto->ml);
1276
1277     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
1278     HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1279         bundle->src_mirrors &= ~mirror_bit;
1280         bundle->dst_mirrors &= ~mirror_bit;
1281         bundle->mirror_out &= ~mirror_bit;
1282     }
1283
1284     hmapx_destroy(&mirror->srcs);
1285     hmapx_destroy(&mirror->dsts);
1286     free(mirror->vlans);
1287
1288     ofproto->mirrors[mirror->idx] = NULL;
1289     free(mirror->name);
1290     free(mirror);
1291 }
1292
1293 static int
1294 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
1295 {
1296     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1297     if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
1298         ofproto->need_revalidate = true;
1299         mac_learning_flush(ofproto->ml);
1300     }
1301     return 0;
1302 }
1303
1304 static bool
1305 is_mirror_output_bundle(struct ofproto *ofproto_, void *aux)
1306 {
1307     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1308     struct ofbundle *bundle = bundle_lookup(ofproto, aux);
1309     return bundle && bundle->mirror_out != 0;
1310 }
1311 \f
1312 /* Ports. */
1313
1314 static struct ofport_dpif *
1315 get_ofp_port(struct ofproto_dpif *ofproto, uint16_t ofp_port)
1316 {
1317     return ofport_dpif_cast(ofproto_get_port(&ofproto->up, ofp_port));
1318 }
1319
1320 static struct ofport_dpif *
1321 get_odp_port(struct ofproto_dpif *ofproto, uint32_t odp_port)
1322 {
1323     return get_ofp_port(ofproto, odp_port_to_ofp_port(odp_port));
1324 }
1325
1326 static void
1327 ofproto_port_from_dpif_port(struct ofproto_port *ofproto_port,
1328                             struct dpif_port *dpif_port)
1329 {
1330     ofproto_port->name = dpif_port->name;
1331     ofproto_port->type = dpif_port->type;
1332     ofproto_port->ofp_port = odp_port_to_ofp_port(dpif_port->port_no);
1333 }
1334
1335 static void
1336 port_run(struct ofport_dpif *ofport)
1337 {
1338     if (ofport->cfm) {
1339         cfm_run(ofport->cfm);
1340
1341         if (cfm_should_send_ccm(ofport->cfm)) {
1342             struct ofpbuf packet;
1343             struct ccm *ccm;
1344
1345             ofpbuf_init(&packet, 0);
1346             ccm = eth_compose(&packet, eth_addr_ccm, ofport->up.opp.hw_addr,
1347                               ETH_TYPE_CFM, sizeof *ccm);
1348             cfm_compose_ccm(ofport->cfm, ccm);
1349             send_packet(ofproto_dpif_cast(ofport->up.ofproto),
1350                         ofport->odp_port, 0, &packet);
1351             ofpbuf_uninit(&packet);
1352         }
1353     }
1354 }
1355
1356 static void
1357 port_wait(struct ofport_dpif *ofport)
1358 {
1359     if (ofport->cfm) {
1360         cfm_wait(ofport->cfm);
1361     }
1362 }
1363
1364 static int
1365 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
1366                    struct ofproto_port *ofproto_port)
1367 {
1368     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1369     struct dpif_port dpif_port;
1370     int error;
1371
1372     error = dpif_port_query_by_name(ofproto->dpif, devname, &dpif_port);
1373     if (!error) {
1374         ofproto_port_from_dpif_port(ofproto_port, &dpif_port);
1375     }
1376     return error;
1377 }
1378
1379 static int
1380 port_add(struct ofproto *ofproto_, struct netdev *netdev, uint16_t *ofp_portp)
1381 {
1382     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1383     uint16_t odp_port;
1384     int error;
1385
1386     error = dpif_port_add(ofproto->dpif, netdev, &odp_port);
1387     if (!error) {
1388         *ofp_portp = odp_port_to_ofp_port(odp_port);
1389     }
1390     return error;
1391 }
1392
1393 static int
1394 port_del(struct ofproto *ofproto_, uint16_t ofp_port)
1395 {
1396     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1397     int error;
1398
1399     error = dpif_port_del(ofproto->dpif, ofp_port_to_odp_port(ofp_port));
1400     if (!error) {
1401         struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
1402         if (ofport) {
1403             /* The caller is going to close ofport->up.netdev.  If this is a
1404              * bonded port, then the bond is using that netdev, so remove it
1405              * from the bond.  The client will need to reconfigure everything
1406              * after deleting ports, so then the slave will get re-added. */
1407             bundle_remove(&ofport->up);
1408         }
1409     }
1410     return error;
1411 }
1412
1413 struct port_dump_state {
1414     struct dpif_port_dump dump;
1415     bool done;
1416 };
1417
1418 static int
1419 port_dump_start(const struct ofproto *ofproto_, void **statep)
1420 {
1421     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1422     struct port_dump_state *state;
1423
1424     *statep = state = xmalloc(sizeof *state);
1425     dpif_port_dump_start(&state->dump, ofproto->dpif);
1426     state->done = false;
1427     return 0;
1428 }
1429
1430 static int
1431 port_dump_next(const struct ofproto *ofproto_ OVS_UNUSED, void *state_,
1432                struct ofproto_port *port)
1433 {
1434     struct port_dump_state *state = state_;
1435     struct dpif_port dpif_port;
1436
1437     if (dpif_port_dump_next(&state->dump, &dpif_port)) {
1438         ofproto_port_from_dpif_port(port, &dpif_port);
1439         return 0;
1440     } else {
1441         int error = dpif_port_dump_done(&state->dump);
1442         state->done = true;
1443         return error ? error : EOF;
1444     }
1445 }
1446
1447 static int
1448 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
1449 {
1450     struct port_dump_state *state = state_;
1451
1452     if (!state->done) {
1453         dpif_port_dump_done(&state->dump);
1454     }
1455     free(state);
1456     return 0;
1457 }
1458
1459 static int
1460 port_poll(const struct ofproto *ofproto_, char **devnamep)
1461 {
1462     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1463     return dpif_port_poll(ofproto->dpif, devnamep);
1464 }
1465
1466 static void
1467 port_poll_wait(const struct ofproto *ofproto_)
1468 {
1469     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1470     dpif_port_poll_wait(ofproto->dpif);
1471 }
1472
1473 static int
1474 port_is_lacp_current(const struct ofport *ofport_)
1475 {
1476     const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1477     return (ofport->bundle && ofport->bundle->lacp
1478             ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
1479             : -1);
1480 }
1481 \f
1482 /* Upcall handling. */
1483
1484 /* Given 'upcall', of type DPIF_UC_ACTION or DPIF_UC_MISS, sends an
1485  * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
1486  * their individual configurations.
1487  *
1488  * If 'clone' is true, the caller retains ownership of 'upcall->packet'.
1489  * Otherwise, ownership is transferred to this function. */
1490 static void
1491 send_packet_in(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall,
1492                const struct flow *flow, bool clone)
1493 {
1494     struct ofputil_packet_in pin;
1495
1496     pin.packet = upcall->packet;
1497     pin.in_port = flow->in_port;
1498     pin.reason = upcall->type == DPIF_UC_MISS ? OFPR_NO_MATCH : OFPR_ACTION;
1499     pin.buffer_id = 0;          /* not yet known */
1500     pin.send_len = upcall->userdata;
1501     connmgr_send_packet_in(ofproto->up.connmgr, &pin, flow,
1502                            clone ? NULL : upcall->packet);
1503 }
1504
1505 static bool
1506 process_special(struct ofproto_dpif *ofproto, const struct flow *flow,
1507                 const struct ofpbuf *packet)
1508 {
1509     if (cfm_should_process_flow(flow)) {
1510         struct ofport_dpif *ofport = get_ofp_port(ofproto, flow->in_port);
1511         if (ofport && ofport->cfm) {
1512             cfm_process_heartbeat(ofport->cfm, packet);
1513         }
1514         return true;
1515     } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
1516         struct ofport_dpif *port = get_ofp_port(ofproto, flow->in_port);
1517         if (port && port->bundle && port->bundle->lacp) {
1518             const struct lacp_pdu *pdu = parse_lacp_packet(packet);
1519             if (pdu) {
1520                 lacp_process_pdu(port->bundle->lacp, port, pdu);
1521             }
1522             return true;
1523         }
1524     }
1525     return false;
1526 }
1527
1528 static void
1529 handle_miss_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1530 {
1531     struct facet *facet;
1532     struct flow flow;
1533
1534     /* Obtain in_port and tun_id, at least. */
1535     odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1536
1537     /* Set header pointers in 'flow'. */
1538     flow_extract(upcall->packet, flow.tun_id, flow.in_port, &flow);
1539
1540     /* Handle 802.1ag and LACP. */
1541     if (process_special(ofproto, &flow, upcall->packet)) {
1542         ofpbuf_delete(upcall->packet);
1543         return;
1544     }
1545
1546     /* Check with in-band control to see if this packet should be sent
1547      * to the local port regardless of the flow table. */
1548     if (connmgr_msg_in_hook(ofproto->up.connmgr, &flow, upcall->packet)) {
1549         send_packet(ofproto, OFPP_LOCAL, 0, upcall->packet);
1550     }
1551
1552     facet = facet_lookup_valid(ofproto, &flow);
1553     if (!facet) {
1554         struct rule_dpif *rule = rule_dpif_lookup(ofproto, &flow);
1555         if (!rule) {
1556             /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
1557             struct ofport_dpif *port = get_ofp_port(ofproto, flow.in_port);
1558             if (port) {
1559                 if (port->up.opp.config & htonl(OFPPC_NO_PACKET_IN)) {
1560                     COVERAGE_INC(ofproto_dpif_no_packet_in);
1561                     /* XXX install 'drop' flow entry */
1562                     ofpbuf_delete(upcall->packet);
1563                     return;
1564                 }
1565             } else {
1566                 VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
1567                              flow.in_port);
1568             }
1569
1570             send_packet_in(ofproto, upcall, &flow, false);
1571             return;
1572         }
1573
1574         facet = facet_create(rule, &flow, upcall->packet);
1575     } else if (!facet->may_install) {
1576         /* The facet is not installable, that is, we need to process every
1577          * packet, so process the current packet's actions into 'facet'. */
1578         facet_make_actions(ofproto, facet, upcall->packet);
1579     }
1580
1581     if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
1582         /*
1583          * Extra-special case for fail-open mode.
1584          *
1585          * We are in fail-open mode and the packet matched the fail-open rule,
1586          * but we are connected to a controller too.  We should send the packet
1587          * up to the controller in the hope that it will try to set up a flow
1588          * and thereby allow us to exit fail-open.
1589          *
1590          * See the top-level comment in fail-open.c for more information.
1591          */
1592         send_packet_in(ofproto, upcall, &flow, true);
1593     }
1594
1595     facet_execute(ofproto, facet, upcall->packet);
1596     facet_install(ofproto, facet, false);
1597 }
1598
1599 static void
1600 handle_upcall(struct ofproto_dpif *ofproto, struct dpif_upcall *upcall)
1601 {
1602     struct flow flow;
1603
1604     switch (upcall->type) {
1605     case DPIF_UC_ACTION:
1606         COVERAGE_INC(ofproto_dpif_ctlr_action);
1607         odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1608         send_packet_in(ofproto, upcall, &flow, false);
1609         break;
1610
1611     case DPIF_UC_SAMPLE:
1612         if (ofproto->sflow) {
1613             odp_flow_key_to_flow(upcall->key, upcall->key_len, &flow);
1614             ofproto_sflow_received(ofproto->sflow, upcall, &flow);
1615         }
1616         ofpbuf_delete(upcall->packet);
1617         break;
1618
1619     case DPIF_UC_MISS:
1620         handle_miss_upcall(ofproto, upcall);
1621         break;
1622
1623     case DPIF_N_UC_TYPES:
1624     default:
1625         VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
1626         break;
1627     }
1628 }
1629 \f
1630 /* Flow expiration. */
1631
1632 static int facet_max_idle(const struct ofproto_dpif *);
1633 static void update_stats(struct ofproto_dpif *);
1634 static void rule_expire(struct rule_dpif *);
1635 static void expire_facets(struct ofproto_dpif *, int dp_max_idle);
1636
1637 /* This function is called periodically by run().  Its job is to collect
1638  * updates for the flows that have been installed into the datapath, most
1639  * importantly when they last were used, and then use that information to
1640  * expire flows that have not been used recently.
1641  *
1642  * Returns the number of milliseconds after which it should be called again. */
1643 static int
1644 expire(struct ofproto_dpif *ofproto)
1645 {
1646     struct rule_dpif *rule, *next_rule;
1647     struct cls_cursor cursor;
1648     int dp_max_idle;
1649
1650     /* Update stats for each flow in the datapath. */
1651     update_stats(ofproto);
1652
1653     /* Expire facets that have been idle too long. */
1654     dp_max_idle = facet_max_idle(ofproto);
1655     expire_facets(ofproto, dp_max_idle);
1656
1657     /* Expire OpenFlow flows whose idle_timeout or hard_timeout has passed. */
1658     cls_cursor_init(&cursor, &ofproto->up.cls, NULL);
1659     CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1660         rule_expire(rule);
1661     }
1662
1663     /* All outstanding data in existing flows has been accounted, so it's a
1664      * good time to do bond rebalancing. */
1665     if (ofproto->has_bonded_bundles) {
1666         struct ofbundle *bundle;
1667
1668         HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1669             if (bundle->bond) {
1670                 bond_rebalance(bundle->bond, &ofproto->revalidate_set);
1671             }
1672         }
1673     }
1674
1675     return MIN(dp_max_idle, 1000);
1676 }
1677
1678 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
1679  *
1680  * This function also pushes statistics updates to rules which each facet
1681  * resubmits into.  Generally these statistics will be accurate.  However, if a
1682  * facet changes the rule it resubmits into at some time in between
1683  * update_stats() runs, it is possible that statistics accrued to the
1684  * old rule will be incorrectly attributed to the new rule.  This could be
1685  * avoided by calling update_stats() whenever rules are created or
1686  * deleted.  However, the performance impact of making so many calls to the
1687  * datapath do not justify the benefit of having perfectly accurate statistics.
1688  */
1689 static void
1690 update_stats(struct ofproto_dpif *p)
1691 {
1692     const struct dpif_flow_stats *stats;
1693     struct dpif_flow_dump dump;
1694     const struct nlattr *key;
1695     size_t key_len;
1696
1697     dpif_flow_dump_start(&dump, p->dpif);
1698     while (dpif_flow_dump_next(&dump, &key, &key_len, NULL, NULL, &stats)) {
1699         struct facet *facet;
1700         struct flow flow;
1701
1702         if (odp_flow_key_to_flow(key, key_len, &flow)) {
1703             struct ds s;
1704
1705             ds_init(&s);
1706             odp_flow_key_format(key, key_len, &s);
1707             VLOG_WARN_RL(&rl, "failed to convert ODP flow key to flow: %s",
1708                          ds_cstr(&s));
1709             ds_destroy(&s);
1710
1711             continue;
1712         }
1713         facet = facet_find(p, &flow);
1714
1715         if (facet && facet->installed) {
1716
1717             if (stats->n_packets >= facet->dp_packet_count) {
1718                 uint64_t extra = stats->n_packets - facet->dp_packet_count;
1719                 facet->packet_count += extra;
1720             } else {
1721                 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
1722             }
1723
1724             if (stats->n_bytes >= facet->dp_byte_count) {
1725                 facet->byte_count += stats->n_bytes - facet->dp_byte_count;
1726             } else {
1727                 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
1728             }
1729
1730             facet->dp_packet_count = stats->n_packets;
1731             facet->dp_byte_count = stats->n_bytes;
1732
1733             facet_update_time(p, facet, stats->used);
1734             facet_account(p, facet, stats->n_bytes);
1735             facet_push_stats(facet);
1736         } else {
1737             /* There's a flow in the datapath that we know nothing about.
1738              * Delete it. */
1739             COVERAGE_INC(facet_unexpected);
1740             dpif_flow_del(p->dpif, key, key_len, NULL);
1741         }
1742     }
1743     dpif_flow_dump_done(&dump);
1744 }
1745
1746 /* Calculates and returns the number of milliseconds of idle time after which
1747  * facets should expire from the datapath and we should fold their statistics
1748  * into their parent rules in userspace. */
1749 static int
1750 facet_max_idle(const struct ofproto_dpif *ofproto)
1751 {
1752     /*
1753      * Idle time histogram.
1754      *
1755      * Most of the time a switch has a relatively small number of facets.  When
1756      * this is the case we might as well keep statistics for all of them in
1757      * userspace and to cache them in the kernel datapath for performance as
1758      * well.
1759      *
1760      * As the number of facets increases, the memory required to maintain
1761      * statistics about them in userspace and in the kernel becomes
1762      * significant.  However, with a large number of facets it is likely that
1763      * only a few of them are "heavy hitters" that consume a large amount of
1764      * bandwidth.  At this point, only heavy hitters are worth caching in the
1765      * kernel and maintaining in userspaces; other facets we can discard.
1766      *
1767      * The technique used to compute the idle time is to build a histogram with
1768      * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each.  Each facet
1769      * that is installed in the kernel gets dropped in the appropriate bucket.
1770      * After the histogram has been built, we compute the cutoff so that only
1771      * the most-recently-used 1% of facets (but at least 1000 flows) are kept
1772      * cached.  At least the most-recently-used bucket of facets is kept, so
1773      * actually an arbitrary number of facets can be kept in any given
1774      * expiration run (though the next run will delete most of those unless
1775      * they receive additional data).
1776      *
1777      * This requires a second pass through the facets, in addition to the pass
1778      * made by update_stats(), because the former function never looks
1779      * at uninstallable facets.
1780      */
1781     enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
1782     enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
1783     int buckets[N_BUCKETS] = { 0 };
1784     struct facet *facet;
1785     int total, bucket;
1786     long long int now;
1787     int i;
1788
1789     total = hmap_count(&ofproto->facets);
1790     if (total <= 1000) {
1791         return N_BUCKETS * BUCKET_WIDTH;
1792     }
1793
1794     /* Build histogram. */
1795     now = time_msec();
1796     HMAP_FOR_EACH (facet, hmap_node, &ofproto->facets) {
1797         long long int idle = now - facet->used;
1798         int bucket = (idle <= 0 ? 0
1799                       : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
1800                       : (unsigned int) idle / BUCKET_WIDTH);
1801         buckets[bucket]++;
1802     }
1803
1804     /* Find the first bucket whose flows should be expired. */
1805     for (bucket = 0; bucket < N_BUCKETS; bucket++) {
1806         if (buckets[bucket]) {
1807             int subtotal = 0;
1808             do {
1809                 subtotal += buckets[bucket++];
1810             } while (bucket < N_BUCKETS && subtotal < MAX(1000, total / 100));
1811             break;
1812         }
1813     }
1814
1815     if (VLOG_IS_DBG_ENABLED()) {
1816         struct ds s;
1817
1818         ds_init(&s);
1819         ds_put_cstr(&s, "keep");
1820         for (i = 0; i < N_BUCKETS; i++) {
1821             if (i == bucket) {
1822                 ds_put_cstr(&s, ", drop");
1823             }
1824             if (buckets[i]) {
1825                 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
1826             }
1827         }
1828         VLOG_INFO("%s: %s (msec:count)", ofproto->up.name, ds_cstr(&s));
1829         ds_destroy(&s);
1830     }
1831
1832     return bucket * BUCKET_WIDTH;
1833 }
1834
1835 static void
1836 facet_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
1837 {
1838     if (ofproto->netflow && !facet_is_controller_flow(facet) &&
1839         netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
1840         struct ofexpired expired;
1841
1842         if (facet->installed) {
1843             struct dpif_flow_stats stats;
1844
1845             facet_put__(ofproto, facet, facet->actions, facet->actions_len,
1846                         &stats);
1847             facet_update_stats(ofproto, facet, &stats);
1848         }
1849
1850         expired.flow = facet->flow;
1851         expired.packet_count = facet->packet_count;
1852         expired.byte_count = facet->byte_count;
1853         expired.used = facet->used;
1854         netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
1855     }
1856 }
1857
1858 static void
1859 expire_facets(struct ofproto_dpif *ofproto, int dp_max_idle)
1860 {
1861     long long int cutoff = time_msec() - dp_max_idle;
1862     struct facet *facet, *next_facet;
1863
1864     HMAP_FOR_EACH_SAFE (facet, next_facet, hmap_node, &ofproto->facets) {
1865         facet_active_timeout(ofproto, facet);
1866         if (facet->used < cutoff) {
1867             facet_remove(ofproto, facet);
1868         }
1869     }
1870 }
1871
1872 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
1873  * then delete it entirely. */
1874 static void
1875 rule_expire(struct rule_dpif *rule)
1876 {
1877     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1878     struct facet *facet, *next_facet;
1879     long long int now;
1880     uint8_t reason;
1881
1882     /* Has 'rule' expired? */
1883     now = time_msec();
1884     if (rule->up.hard_timeout
1885         && now > rule->up.created + rule->up.hard_timeout * 1000) {
1886         reason = OFPRR_HARD_TIMEOUT;
1887     } else if (rule->up.idle_timeout && list_is_empty(&rule->facets)
1888                && now > rule->used + rule->up.idle_timeout * 1000) {
1889         reason = OFPRR_IDLE_TIMEOUT;
1890     } else {
1891         return;
1892     }
1893
1894     COVERAGE_INC(ofproto_dpif_expired);
1895
1896     /* Update stats.  (This is a no-op if the rule expired due to an idle
1897      * timeout, because that only happens when the rule has no facets left.) */
1898     LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
1899         facet_remove(ofproto, facet);
1900     }
1901
1902     /* Get rid of the rule. */
1903     ofproto_rule_expire(&rule->up, reason);
1904 }
1905 \f
1906 /* Facets. */
1907
1908 /* Creates and returns a new facet owned by 'rule', given a 'flow' and an
1909  * example 'packet' within that flow.
1910  *
1911  * The caller must already have determined that no facet with an identical
1912  * 'flow' exists in 'ofproto' and that 'flow' is the best match for 'rule' in
1913  * the ofproto's classifier table. */
1914 static struct facet *
1915 facet_create(struct rule_dpif *rule, const struct flow *flow,
1916              const struct ofpbuf *packet)
1917 {
1918     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
1919     struct facet *facet;
1920
1921     facet = xzalloc(sizeof *facet);
1922     facet->used = time_msec();
1923     hmap_insert(&ofproto->facets, &facet->hmap_node, flow_hash(flow, 0));
1924     list_push_back(&rule->facets, &facet->list_node);
1925     facet->rule = rule;
1926     facet->flow = *flow;
1927     netflow_flow_init(&facet->nf_flow);
1928     netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
1929
1930     facet_make_actions(ofproto, facet, packet);
1931
1932     return facet;
1933 }
1934
1935 static void
1936 facet_free(struct facet *facet)
1937 {
1938     free(facet->actions);
1939     free(facet);
1940 }
1941
1942 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
1943  * 'packet', which arrived on 'in_port'.
1944  *
1945  * Takes ownership of 'packet'. */
1946 static bool
1947 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
1948                     const struct nlattr *odp_actions, size_t actions_len,
1949                     struct ofpbuf *packet)
1950 {
1951     if (actions_len == NLA_ALIGN(NLA_HDRLEN + sizeof(uint64_t))
1952         && odp_actions->nla_type == ODP_ACTION_ATTR_CONTROLLER) {
1953         /* As an optimization, avoid a round-trip from userspace to kernel to
1954          * userspace.  This also avoids possibly filling up kernel packet
1955          * buffers along the way. */
1956         struct dpif_upcall upcall;
1957
1958         upcall.type = DPIF_UC_ACTION;
1959         upcall.packet = packet;
1960         upcall.key = NULL;
1961         upcall.key_len = 0;
1962         upcall.userdata = nl_attr_get_u64(odp_actions);
1963         upcall.sample_pool = 0;
1964         upcall.actions = NULL;
1965         upcall.actions_len = 0;
1966
1967         send_packet_in(ofproto, &upcall, flow, false);
1968
1969         return true;
1970     } else {
1971         int error;
1972
1973         error = dpif_execute(ofproto->dpif, odp_actions, actions_len, packet);
1974         ofpbuf_delete(packet);
1975         return !error;
1976     }
1977 }
1978
1979 /* Executes the actions indicated by 'facet' on 'packet' and credits 'facet''s
1980  * statistics appropriately.  'packet' must have at least sizeof(struct
1981  * ofp_packet_in) bytes of headroom.
1982  *
1983  * For correct results, 'packet' must actually be in 'facet''s flow; that is,
1984  * applying flow_extract() to 'packet' would yield the same flow as
1985  * 'facet->flow'.
1986  *
1987  * 'facet' must have accurately composed ODP actions; that is, it must not be
1988  * in need of revalidation.
1989  *
1990  * Takes ownership of 'packet'. */
1991 static void
1992 facet_execute(struct ofproto_dpif *ofproto, struct facet *facet,
1993               struct ofpbuf *packet)
1994 {
1995     struct dpif_flow_stats stats;
1996
1997     assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
1998
1999     flow_extract_stats(&facet->flow, packet, &stats);
2000     stats.used = time_msec();
2001     if (execute_odp_actions(ofproto, &facet->flow,
2002                             facet->actions, facet->actions_len, packet)) {
2003         facet_update_stats(ofproto, facet, &stats);
2004     }
2005 }
2006
2007 /* Remove 'facet' from 'ofproto' and free up the associated memory:
2008  *
2009  *   - If 'facet' was installed in the datapath, uninstalls it and updates its
2010  *     rule's statistics, via facet_uninstall().
2011  *
2012  *   - Removes 'facet' from its rule and from ofproto->facets.
2013  */
2014 static void
2015 facet_remove(struct ofproto_dpif *ofproto, struct facet *facet)
2016 {
2017     facet_uninstall(ofproto, facet);
2018     facet_flush_stats(ofproto, facet);
2019     hmap_remove(&ofproto->facets, &facet->hmap_node);
2020     list_remove(&facet->list_node);
2021     facet_free(facet);
2022 }
2023
2024 /* Composes the ODP actions for 'facet' based on its rule's actions. */
2025 static void
2026 facet_make_actions(struct ofproto_dpif *p, struct facet *facet,
2027                    const struct ofpbuf *packet)
2028 {
2029     const struct rule_dpif *rule = facet->rule;
2030     struct ofpbuf *odp_actions;
2031     struct action_xlate_ctx ctx;
2032
2033     action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
2034     odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2035     facet->tags = ctx.tags;
2036     facet->may_install = ctx.may_set_up_flow;
2037     facet->nf_flow.output_iface = ctx.nf_output_iface;
2038
2039     if (facet->actions_len != odp_actions->size
2040         || memcmp(facet->actions, odp_actions->data, odp_actions->size)) {
2041         free(facet->actions);
2042         facet->actions_len = odp_actions->size;
2043         facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2044     }
2045
2046     ofpbuf_delete(odp_actions);
2047 }
2048
2049 static int
2050 facet_put__(struct ofproto_dpif *ofproto, struct facet *facet,
2051             const struct nlattr *actions, size_t actions_len,
2052             struct dpif_flow_stats *stats)
2053 {
2054     struct odputil_keybuf keybuf;
2055     enum dpif_flow_put_flags flags;
2056     struct ofpbuf key;
2057
2058     flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
2059     if (stats) {
2060         flags |= DPIF_FP_ZERO_STATS;
2061         facet->dp_packet_count = 0;
2062         facet->dp_byte_count = 0;
2063     }
2064
2065     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2066     odp_flow_key_from_flow(&key, &facet->flow);
2067
2068     return dpif_flow_put(ofproto->dpif, flags, key.data, key.size,
2069                          actions, actions_len, stats);
2070 }
2071
2072 /* If 'facet' is installable, inserts or re-inserts it into 'p''s datapath.  If
2073  * 'zero_stats' is true, clears any existing statistics from the datapath for
2074  * 'facet'. */
2075 static void
2076 facet_install(struct ofproto_dpif *p, struct facet *facet, bool zero_stats)
2077 {
2078     struct dpif_flow_stats stats;
2079
2080     if (facet->may_install
2081         && !facet_put__(p, facet, facet->actions, facet->actions_len,
2082                         zero_stats ? &stats : NULL)) {
2083         facet->installed = true;
2084     }
2085 }
2086
2087 static void
2088 facet_account(struct ofproto_dpif *ofproto,
2089               struct facet *facet, uint64_t extra_bytes)
2090 {
2091     uint64_t total_bytes, n_bytes;
2092     struct ofbundle *in_bundle;
2093     const struct nlattr *a;
2094     tag_type dummy = 0;
2095     unsigned int left;
2096     int vlan;
2097
2098     total_bytes = facet->byte_count + extra_bytes;
2099     if (total_bytes <= facet->accounted_bytes) {
2100         return;
2101     }
2102     n_bytes = total_bytes - facet->accounted_bytes;
2103     facet->accounted_bytes = total_bytes;
2104
2105     /* Test that 'tags' is nonzero to ensure that only flows that include an
2106      * OFPP_NORMAL action are used for learning and bond slave rebalancing.
2107      * This works because OFPP_NORMAL always sets a nonzero tag value.
2108      *
2109      * Feed information from the active flows back into the learning table to
2110      * ensure that table is always in sync with what is actually flowing
2111      * through the datapath. */
2112     if (!facet->tags
2113         || !is_admissible(ofproto, &facet->flow, false, &dummy,
2114                           &vlan, &in_bundle)) {
2115         return;
2116     }
2117
2118     update_learning_table(ofproto, &facet->flow, vlan, in_bundle);
2119
2120     if (!ofproto->has_bonded_bundles) {
2121         return;
2122     }
2123     NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->actions, facet->actions_len) {
2124         if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
2125             struct ofport_dpif *port;
2126
2127             port = get_odp_port(ofproto, nl_attr_get_u32(a));
2128             if (port && port->bundle && port->bundle->bond) {
2129                 bond_account(port->bundle->bond, &facet->flow, vlan, n_bytes);
2130             }
2131         }
2132     }
2133 }
2134
2135 /* If 'rule' is installed in the datapath, uninstalls it. */
2136 static void
2137 facet_uninstall(struct ofproto_dpif *p, struct facet *facet)
2138 {
2139     if (facet->installed) {
2140         struct odputil_keybuf keybuf;
2141         struct dpif_flow_stats stats;
2142         struct ofpbuf key;
2143
2144         ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2145         odp_flow_key_from_flow(&key, &facet->flow);
2146
2147         if (!dpif_flow_del(p->dpif, key.data, key.size, &stats)) {
2148             facet_update_stats(p, facet, &stats);
2149         }
2150         facet->installed = false;
2151         facet->dp_packet_count = 0;
2152         facet->dp_byte_count = 0;
2153     } else {
2154         assert(facet->dp_packet_count == 0);
2155         assert(facet->dp_byte_count == 0);
2156     }
2157 }
2158
2159 /* Returns true if the only action for 'facet' is to send to the controller.
2160  * (We don't report NetFlow expiration messages for such facets because they
2161  * are just part of the control logic for the network, not real traffic). */
2162 static bool
2163 facet_is_controller_flow(struct facet *facet)
2164 {
2165     return (facet
2166             && facet->rule->up.n_actions == 1
2167             && action_outputs_to_port(&facet->rule->up.actions[0],
2168                                       htons(OFPP_CONTROLLER)));
2169 }
2170
2171 /* Folds all of 'facet''s statistics into its rule.  Also updates the
2172  * accounting ofhook and emits a NetFlow expiration if appropriate.  All of
2173  * 'facet''s statistics in the datapath should have been zeroed and folded into
2174  * its packet and byte counts before this function is called. */
2175 static void
2176 facet_flush_stats(struct ofproto_dpif *ofproto, struct facet *facet)
2177 {
2178     assert(!facet->dp_byte_count);
2179     assert(!facet->dp_packet_count);
2180
2181     facet_push_stats(facet);
2182     facet_account(ofproto, facet, 0);
2183
2184     if (ofproto->netflow && !facet_is_controller_flow(facet)) {
2185         struct ofexpired expired;
2186         expired.flow = facet->flow;
2187         expired.packet_count = facet->packet_count;
2188         expired.byte_count = facet->byte_count;
2189         expired.used = facet->used;
2190         netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
2191     }
2192
2193     facet->rule->packet_count += facet->packet_count;
2194     facet->rule->byte_count += facet->byte_count;
2195
2196     /* Reset counters to prevent double counting if 'facet' ever gets
2197      * reinstalled. */
2198     facet->packet_count = 0;
2199     facet->byte_count = 0;
2200     facet->rs_packet_count = 0;
2201     facet->rs_byte_count = 0;
2202     facet->accounted_bytes = 0;
2203
2204     netflow_flow_clear(&facet->nf_flow);
2205 }
2206
2207 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2208  * Returns it if found, otherwise a null pointer.
2209  *
2210  * The returned facet might need revalidation; use facet_lookup_valid()
2211  * instead if that is important. */
2212 static struct facet *
2213 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
2214 {
2215     struct facet *facet;
2216
2217     HMAP_FOR_EACH_WITH_HASH (facet, hmap_node, flow_hash(flow, 0),
2218                              &ofproto->facets) {
2219         if (flow_equal(flow, &facet->flow)) {
2220             return facet;
2221         }
2222     }
2223
2224     return NULL;
2225 }
2226
2227 /* Searches 'ofproto''s table of facets for one exactly equal to 'flow'.
2228  * Returns it if found, otherwise a null pointer.
2229  *
2230  * The returned facet is guaranteed to be valid. */
2231 static struct facet *
2232 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
2233 {
2234     struct facet *facet = facet_find(ofproto, flow);
2235
2236     /* The facet we found might not be valid, since we could be in need of
2237      * revalidation.  If it is not valid, don't return it. */
2238     if (facet
2239         && ofproto->need_revalidate
2240         && !facet_revalidate(ofproto, facet)) {
2241         COVERAGE_INC(facet_invalidated);
2242         return NULL;
2243     }
2244
2245     return facet;
2246 }
2247
2248 /* Re-searches 'ofproto''s classifier for a rule matching 'facet':
2249  *
2250  *   - If the rule found is different from 'facet''s current rule, moves
2251  *     'facet' to the new rule and recompiles its actions.
2252  *
2253  *   - If the rule found is the same as 'facet''s current rule, leaves 'facet'
2254  *     where it is and recompiles its actions anyway.
2255  *
2256  *   - If there is none, destroys 'facet'.
2257  *
2258  * Returns true if 'facet' still exists, false if it has been destroyed. */
2259 static bool
2260 facet_revalidate(struct ofproto_dpif *ofproto, struct facet *facet)
2261 {
2262     struct action_xlate_ctx ctx;
2263     struct ofpbuf *odp_actions;
2264     struct rule_dpif *new_rule;
2265     bool actions_changed;
2266
2267     COVERAGE_INC(facet_revalidate);
2268
2269     /* Determine the new rule. */
2270     new_rule = rule_dpif_lookup(ofproto, &facet->flow);
2271     if (!new_rule) {
2272         /* No new rule, so delete the facet. */
2273         facet_remove(ofproto, facet);
2274         return false;
2275     }
2276
2277     /* Calculate new ODP actions.
2278      *
2279      * We do not modify any 'facet' state yet, because we might need to, e.g.,
2280      * emit a NetFlow expiration and, if so, we need to have the old state
2281      * around to properly compose it. */
2282     action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
2283     odp_actions = xlate_actions(&ctx,
2284                                 new_rule->up.actions, new_rule->up.n_actions);
2285     actions_changed = (facet->actions_len != odp_actions->size
2286                        || memcmp(facet->actions, odp_actions->data,
2287                                  facet->actions_len));
2288
2289     /* If the ODP actions changed or the installability changed, then we need
2290      * to talk to the datapath. */
2291     if (actions_changed || ctx.may_set_up_flow != facet->installed) {
2292         if (ctx.may_set_up_flow) {
2293             struct dpif_flow_stats stats;
2294
2295             facet_put__(ofproto, facet,
2296                         odp_actions->data, odp_actions->size, &stats);
2297             facet_update_stats(ofproto, facet, &stats);
2298         } else {
2299             facet_uninstall(ofproto, facet);
2300         }
2301
2302         /* The datapath flow is gone or has zeroed stats, so push stats out of
2303          * 'facet' into 'rule'. */
2304         facet_flush_stats(ofproto, facet);
2305     }
2306
2307     /* Update 'facet' now that we've taken care of all the old state. */
2308     facet->tags = ctx.tags;
2309     facet->nf_flow.output_iface = ctx.nf_output_iface;
2310     facet->may_install = ctx.may_set_up_flow;
2311     if (actions_changed) {
2312         free(facet->actions);
2313         facet->actions_len = odp_actions->size;
2314         facet->actions = xmemdup(odp_actions->data, odp_actions->size);
2315     }
2316     if (facet->rule != new_rule) {
2317         COVERAGE_INC(facet_changed_rule);
2318         list_remove(&facet->list_node);
2319         list_push_back(&new_rule->facets, &facet->list_node);
2320         facet->rule = new_rule;
2321         facet->used = new_rule->up.created;
2322         facet->rs_used = facet->used;
2323     }
2324
2325     ofpbuf_delete(odp_actions);
2326
2327     return true;
2328 }
2329
2330 /* Updates 'facet''s used time.  Caller is responsible for calling
2331  * facet_push_stats() to update the flows which 'facet' resubmits into. */
2332 static void
2333 facet_update_time(struct ofproto_dpif *ofproto, struct facet *facet,
2334                   long long int used)
2335 {
2336     if (used > facet->used) {
2337         facet->used = used;
2338         if (used > facet->rule->used) {
2339             facet->rule->used = used;
2340         }
2341         netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, used);
2342     }
2343 }
2344
2345 /* Folds the statistics from 'stats' into the counters in 'facet'.
2346  *
2347  * Because of the meaning of a facet's counters, it only makes sense to do this
2348  * if 'stats' are not tracked in the datapath, that is, if 'stats' represents a
2349  * packet that was sent by hand or if it represents statistics that have been
2350  * cleared out of the datapath. */
2351 static void
2352 facet_update_stats(struct ofproto_dpif *ofproto, struct facet *facet,
2353                    const struct dpif_flow_stats *stats)
2354 {
2355     if (stats->n_packets || stats->used > facet->used) {
2356         facet_update_time(ofproto, facet, stats->used);
2357         facet->packet_count += stats->n_packets;
2358         facet->byte_count += stats->n_bytes;
2359         facet_push_stats(facet);
2360         netflow_flow_update_flags(&facet->nf_flow, stats->tcp_flags);
2361     }
2362 }
2363
2364 static void
2365 facet_push_stats(struct facet *facet)
2366 {
2367     uint64_t rs_packets, rs_bytes;
2368
2369     assert(facet->packet_count >= facet->rs_packet_count);
2370     assert(facet->byte_count >= facet->rs_byte_count);
2371     assert(facet->used >= facet->rs_used);
2372
2373     rs_packets = facet->packet_count - facet->rs_packet_count;
2374     rs_bytes = facet->byte_count - facet->rs_byte_count;
2375
2376     if (rs_packets || rs_bytes || facet->used > facet->rs_used) {
2377         facet->rs_packet_count = facet->packet_count;
2378         facet->rs_byte_count = facet->byte_count;
2379         facet->rs_used = facet->used;
2380
2381         flow_push_stats(facet->rule, &facet->flow,
2382                         rs_packets, rs_bytes, facet->used);
2383     }
2384 }
2385
2386 struct ofproto_push {
2387     struct action_xlate_ctx ctx;
2388     uint64_t packets;
2389     uint64_t bytes;
2390     long long int used;
2391 };
2392
2393 static void
2394 push_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
2395 {
2396     struct ofproto_push *push = CONTAINER_OF(ctx, struct ofproto_push, ctx);
2397
2398     if (rule) {
2399         rule->packet_count += push->packets;
2400         rule->byte_count += push->bytes;
2401         rule->used = MAX(push->used, rule->used);
2402     }
2403 }
2404
2405 /* Pushes flow statistics to the rules which 'flow' resubmits into given
2406  * 'rule''s actions. */
2407 static void
2408 flow_push_stats(const struct rule_dpif *rule,
2409                 struct flow *flow, uint64_t packets, uint64_t bytes,
2410                 long long int used)
2411 {
2412     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2413     struct ofproto_push push;
2414
2415     push.packets = packets;
2416     push.bytes = bytes;
2417     push.used = used;
2418
2419     action_xlate_ctx_init(&push.ctx, ofproto, flow, NULL);
2420     push.ctx.resubmit_hook = push_resubmit;
2421     ofpbuf_delete(xlate_actions(&push.ctx,
2422                                 rule->up.actions, rule->up.n_actions));
2423 }
2424 \f
2425 /* Rules. */
2426
2427 static struct rule_dpif *
2428 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow)
2429 {
2430     return rule_dpif_cast(ofproto_rule_lookup(&ofproto->up, flow));
2431 }
2432
2433 static struct rule *
2434 rule_alloc(void)
2435 {
2436     struct rule_dpif *rule = xmalloc(sizeof *rule);
2437     return &rule->up;
2438 }
2439
2440 static void
2441 rule_dealloc(struct rule *rule_)
2442 {
2443     struct rule_dpif *rule = rule_dpif_cast(rule_);
2444     free(rule);
2445 }
2446
2447 static int
2448 rule_construct(struct rule *rule_)
2449 {
2450     struct rule_dpif *rule = rule_dpif_cast(rule_);
2451     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2452     struct rule_dpif *old_rule;
2453     int error;
2454
2455     error = validate_actions(rule->up.actions, rule->up.n_actions,
2456                              &rule->up.cr.flow, ofproto->max_ports);
2457     if (error) {
2458         return error;
2459     }
2460
2461     old_rule = rule_dpif_cast(rule_from_cls_rule(classifier_find_rule_exactly(
2462                                                      &ofproto->up.cls,
2463                                                      &rule->up.cr)));
2464     if (old_rule) {
2465         ofproto_rule_destroy(&old_rule->up);
2466     }
2467
2468     rule->used = rule->up.created;
2469     rule->packet_count = 0;
2470     rule->byte_count = 0;
2471     list_init(&rule->facets);
2472     classifier_insert(&ofproto->up.cls, &rule->up.cr);
2473
2474     ofproto->need_revalidate = true;
2475
2476     return 0;
2477 }
2478
2479 static void
2480 rule_destruct(struct rule *rule_)
2481 {
2482     struct rule_dpif *rule = rule_dpif_cast(rule_);
2483     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2484     struct facet *facet, *next_facet;
2485
2486     classifier_remove(&ofproto->up.cls, &rule->up.cr);
2487     LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2488         facet_revalidate(ofproto, facet);
2489     }
2490     ofproto->need_revalidate = true;
2491 }
2492
2493 static void
2494 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2495 {
2496     struct rule_dpif *rule = rule_dpif_cast(rule_);
2497     struct facet *facet;
2498
2499     /* Start from historical data for 'rule' itself that are no longer tracked
2500      * in facets.  This counts, for example, facets that have expired. */
2501     *packets = rule->packet_count;
2502     *bytes = rule->byte_count;
2503
2504     /* Add any statistics that are tracked by facets.  This includes
2505      * statistical data recently updated by ofproto_update_stats() as well as
2506      * stats for packets that were executed "by hand" via dpif_execute(). */
2507     LIST_FOR_EACH (facet, list_node, &rule->facets) {
2508         *packets += facet->packet_count;
2509         *bytes += facet->byte_count;
2510     }
2511 }
2512
2513 static int
2514 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2515 {
2516     struct rule_dpif *rule = rule_dpif_cast(rule_);
2517     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2518     struct action_xlate_ctx ctx;
2519     struct ofpbuf *odp_actions;
2520     struct facet *facet;
2521     size_t size;
2522
2523     /* First look for a related facet.  If we find one, account it to that. */
2524     facet = facet_lookup_valid(ofproto, flow);
2525     if (facet && facet->rule == rule) {
2526         facet_execute(ofproto, facet, packet);
2527         return 0;
2528     }
2529
2530     /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2531      * create a new facet for it and use that. */
2532     if (rule_dpif_lookup(ofproto, flow) == rule) {
2533         facet = facet_create(rule, flow, packet);
2534         facet_execute(ofproto, facet, packet);
2535         facet_install(ofproto, facet, true);
2536         return 0;
2537     }
2538
2539     /* We can't account anything to a facet.  If we were to try, then that
2540      * facet would have a non-matching rule, busting our invariants. */
2541     action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2542     odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2543     size = packet->size;
2544     if (execute_odp_actions(ofproto, flow, odp_actions->data,
2545                             odp_actions->size, packet)) {
2546         rule->used = time_msec();
2547         rule->packet_count++;
2548         rule->byte_count += size;
2549         flow_push_stats(rule, flow, 1, size, rule->used);
2550     }
2551     ofpbuf_delete(odp_actions);
2552
2553     return 0;
2554 }
2555
2556 static int
2557 rule_modify_actions(struct rule *rule_,
2558                     const union ofp_action *actions, size_t n_actions)
2559 {
2560     struct rule_dpif *rule = rule_dpif_cast(rule_);
2561     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2562     int error;
2563
2564     error = validate_actions(actions, n_actions, &rule->up.cr.flow,
2565                              ofproto->max_ports);
2566     if (!error) {
2567         ofproto->need_revalidate = true;
2568     }
2569     return error;
2570 }
2571 \f
2572 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.  If 'vlan_tci' is
2573  * zero the packet will not have any 802.1Q hader; if it is nonzero, then the
2574  * packet will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
2575  *
2576  * Returns 0 if successful, otherwise a positive errno value. */
2577 static int
2578 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port, uint16_t vlan_tci,
2579             const struct ofpbuf *packet)
2580 {
2581     struct ofpbuf odp_actions;
2582     int error;
2583
2584     ofpbuf_init(&odp_actions, 32);
2585     if (vlan_tci != 0) {
2586         nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2587                        ntohs(vlan_tci & ~VLAN_CFI));
2588     }
2589     nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2590     error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
2591                          packet);
2592     ofpbuf_uninit(&odp_actions);
2593
2594     if (error) {
2595         VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2596                      ofproto->up.name, odp_port, strerror(error));
2597     }
2598     return error;
2599 }
2600 \f
2601 /* OpenFlow to ODP action translation. */
2602
2603 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2604                              struct action_xlate_ctx *ctx);
2605 static bool xlate_normal(struct action_xlate_ctx *);
2606
2607 static void
2608 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2609 {
2610     const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2611     uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2612
2613     if (ofport) {
2614         if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2615             /* Forwarding disabled on port. */
2616             return;
2617         }
2618     } else {
2619         /*
2620          * We don't have an ofport record for this port, but it doesn't hurt to
2621          * allow forwarding to it anyhow.  Maybe such a port will appear later
2622          * and we're pre-populating the flow table.
2623          */
2624     }
2625
2626     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2627     ctx->nf_output_iface = ofp_port;
2628 }
2629
2630 static void
2631 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2632 {
2633     if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2634         struct rule_dpif *rule;
2635         uint16_t old_in_port;
2636
2637         /* Look up a flow with 'in_port' as the input port.  Then restore the
2638          * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2639          * have surprising behavior). */
2640         old_in_port = ctx->flow.in_port;
2641         ctx->flow.in_port = in_port;
2642         rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2643         ctx->flow.in_port = old_in_port;
2644
2645         if (ctx->resubmit_hook) {
2646             ctx->resubmit_hook(ctx, rule);
2647         }
2648
2649         if (rule) {
2650             ctx->recurse++;
2651             do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2652             ctx->recurse--;
2653         }
2654     } else {
2655         static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2656
2657         VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2658                     MAX_RESUBMIT_RECURSION);
2659     }
2660 }
2661
2662 static void
2663 flood_packets(struct ofproto_dpif *ofproto,
2664               uint16_t ofp_in_port, ovs_be32 mask,
2665               uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2666 {
2667     struct ofport_dpif *ofport;
2668
2669     HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2670         uint16_t ofp_port = ofport->up.ofp_port;
2671         if (ofp_port != ofp_in_port && !(ofport->up.opp.config & mask)) {
2672             nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT,
2673                            ofport->odp_port);
2674         }
2675     }
2676     *nf_output_iface = NF_OUT_FLOOD;
2677 }
2678
2679 static void
2680 xlate_output_action__(struct action_xlate_ctx *ctx,
2681                       uint16_t port, uint16_t max_len)
2682 {
2683     uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2684
2685     ctx->nf_output_iface = NF_OUT_DROP;
2686
2687     switch (port) {
2688     case OFPP_IN_PORT:
2689         add_output_action(ctx, ctx->flow.in_port);
2690         break;
2691     case OFPP_TABLE:
2692         xlate_table_action(ctx, ctx->flow.in_port);
2693         break;
2694     case OFPP_NORMAL:
2695         xlate_normal(ctx);
2696         break;
2697     case OFPP_FLOOD:
2698         flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
2699                       &ctx->nf_output_iface, ctx->odp_actions);
2700         break;
2701     case OFPP_ALL:
2702         flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
2703                       &ctx->nf_output_iface, ctx->odp_actions);
2704         break;
2705     case OFPP_CONTROLLER:
2706         nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2707         break;
2708     case OFPP_LOCAL:
2709         add_output_action(ctx, OFPP_LOCAL);
2710         break;
2711     default:
2712         if (port != ctx->flow.in_port) {
2713             add_output_action(ctx, port);
2714         }
2715         break;
2716     }
2717
2718     if (prev_nf_output_iface == NF_OUT_FLOOD) {
2719         ctx->nf_output_iface = NF_OUT_FLOOD;
2720     } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2721         ctx->nf_output_iface = prev_nf_output_iface;
2722     } else if (prev_nf_output_iface != NF_OUT_DROP &&
2723                ctx->nf_output_iface != NF_OUT_FLOOD) {
2724         ctx->nf_output_iface = NF_OUT_MULTI;
2725     }
2726 }
2727
2728 static void
2729 xlate_output_action(struct action_xlate_ctx *ctx,
2730                     const struct ofp_action_output *oao)
2731 {
2732     xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2733 }
2734
2735 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2736  * optimization, because we're going to add another action that sets the
2737  * priority immediately after, or because there are no actions following the
2738  * pop.  */
2739 static void
2740 remove_pop_action(struct action_xlate_ctx *ctx)
2741 {
2742     if (ctx->odp_actions->size == ctx->last_pop_priority) {
2743         ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2744         ctx->last_pop_priority = -1;
2745     }
2746 }
2747
2748 static void
2749 add_pop_action(struct action_xlate_ctx *ctx)
2750 {
2751     if (ctx->odp_actions->size != ctx->last_pop_priority) {
2752         nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2753         ctx->last_pop_priority = ctx->odp_actions->size;
2754     }
2755 }
2756
2757 static void
2758 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2759                      const struct ofp_action_enqueue *oae)
2760 {
2761     uint16_t ofp_port, odp_port;
2762     uint32_t priority;
2763     int error;
2764
2765     error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2766                                    &priority);
2767     if (error) {
2768         /* Fall back to ordinary output action. */
2769         xlate_output_action__(ctx, ntohs(oae->port), 0);
2770         return;
2771     }
2772
2773     /* Figure out ODP output port. */
2774     ofp_port = ntohs(oae->port);
2775     if (ofp_port == OFPP_IN_PORT) {
2776         ofp_port = ctx->flow.in_port;
2777     }
2778     odp_port = ofp_port_to_odp_port(ofp_port);
2779
2780     /* Add ODP actions. */
2781     remove_pop_action(ctx);
2782     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2783     add_output_action(ctx, odp_port);
2784     add_pop_action(ctx);
2785
2786     /* Update NetFlow output port. */
2787     if (ctx->nf_output_iface == NF_OUT_DROP) {
2788         ctx->nf_output_iface = odp_port;
2789     } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2790         ctx->nf_output_iface = NF_OUT_MULTI;
2791     }
2792 }
2793
2794 static void
2795 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2796                        const struct nx_action_set_queue *nasq)
2797 {
2798     uint32_t priority;
2799     int error;
2800
2801     error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2802                                    &priority);
2803     if (error) {
2804         /* Couldn't translate queue to a priority, so ignore.  A warning
2805          * has already been logged. */
2806         return;
2807     }
2808
2809     remove_pop_action(ctx);
2810     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2811 }
2812
2813 static void
2814 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2815 {
2816     ovs_be16 tci = ctx->flow.vlan_tci;
2817     if (!(tci & htons(VLAN_CFI))) {
2818         nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2819     } else {
2820         nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2821                         tci & ~htons(VLAN_CFI));
2822     }
2823 }
2824
2825 struct xlate_reg_state {
2826     ovs_be16 vlan_tci;
2827     ovs_be64 tun_id;
2828 };
2829
2830 static void
2831 save_reg_state(const struct action_xlate_ctx *ctx,
2832                struct xlate_reg_state *state)
2833 {
2834     state->vlan_tci = ctx->flow.vlan_tci;
2835     state->tun_id = ctx->flow.tun_id;
2836 }
2837
2838 static void
2839 update_reg_state(struct action_xlate_ctx *ctx,
2840                  const struct xlate_reg_state *state)
2841 {
2842     if (ctx->flow.vlan_tci != state->vlan_tci) {
2843         xlate_set_dl_tci(ctx);
2844     }
2845     if (ctx->flow.tun_id != state->tun_id) {
2846         nl_msg_put_be64(ctx->odp_actions,
2847                         ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2848     }
2849 }
2850
2851 static void
2852 xlate_autopath(struct action_xlate_ctx *ctx,
2853                const struct nx_action_autopath *naa)
2854 {
2855     uint16_t ofp_port = ntohl(naa->id);
2856     struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
2857
2858     if (!port || !port->bundle) {
2859         ofp_port = OFPP_NONE;
2860     } else if (port->bundle->bond) {
2861         /* Autopath does not support VLAN hashing. */
2862         struct ofport_dpif *slave = bond_choose_output_slave(
2863             port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
2864         if (slave) {
2865             ofp_port = slave->up.ofp_port;
2866         }
2867     }
2868     autopath_execute(naa, &ctx->flow, ofp_port);
2869 }
2870
2871 static void
2872 xlate_nicira_action(struct action_xlate_ctx *ctx,
2873                     const struct nx_action_header *nah)
2874 {
2875     const struct nx_action_resubmit *nar;
2876     const struct nx_action_set_tunnel *nast;
2877     const struct nx_action_set_queue *nasq;
2878     const struct nx_action_multipath *nam;
2879     const struct nx_action_autopath *naa;
2880     enum nx_action_subtype subtype = ntohs(nah->subtype);
2881     struct xlate_reg_state state;
2882     ovs_be64 tun_id;
2883
2884     assert(nah->vendor == htonl(NX_VENDOR_ID));
2885     switch (subtype) {
2886     case NXAST_RESUBMIT:
2887         nar = (const struct nx_action_resubmit *) nah;
2888         xlate_table_action(ctx, ntohs(nar->in_port));
2889         break;
2890
2891     case NXAST_SET_TUNNEL:
2892         nast = (const struct nx_action_set_tunnel *) nah;
2893         tun_id = htonll(ntohl(nast->tun_id));
2894         nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2895         ctx->flow.tun_id = tun_id;
2896         break;
2897
2898     case NXAST_DROP_SPOOFED_ARP:
2899         if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2900             nl_msg_put_flag(ctx->odp_actions,
2901                             ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2902         }
2903         break;
2904
2905     case NXAST_SET_QUEUE:
2906         nasq = (const struct nx_action_set_queue *) nah;
2907         xlate_set_queue_action(ctx, nasq);
2908         break;
2909
2910     case NXAST_POP_QUEUE:
2911         add_pop_action(ctx);
2912         break;
2913
2914     case NXAST_REG_MOVE:
2915         save_reg_state(ctx, &state);
2916         nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2917                              &ctx->flow);
2918         update_reg_state(ctx, &state);
2919         break;
2920
2921     case NXAST_REG_LOAD:
2922         save_reg_state(ctx, &state);
2923         nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2924                              &ctx->flow);
2925         update_reg_state(ctx, &state);
2926         break;
2927
2928     case NXAST_NOTE:
2929         /* Nothing to do. */
2930         break;
2931
2932     case NXAST_SET_TUNNEL64:
2933         tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2934         nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2935         ctx->flow.tun_id = tun_id;
2936         break;
2937
2938     case NXAST_MULTIPATH:
2939         nam = (const struct nx_action_multipath *) nah;
2940         multipath_execute(nam, &ctx->flow);
2941         break;
2942
2943     case NXAST_AUTOPATH:
2944         naa = (const struct nx_action_autopath *) nah;
2945         xlate_autopath(ctx, naa);
2946         break;
2947
2948     /* If you add a new action here that modifies flow data, don't forget to
2949      * update the flow key in ctx->flow at the same time. */
2950
2951     case NXAST_SNAT__OBSOLETE:
2952     default:
2953         VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2954         break;
2955     }
2956 }
2957
2958 static void
2959 do_xlate_actions(const union ofp_action *in, size_t n_in,
2960                  struct action_xlate_ctx *ctx)
2961 {
2962     const struct ofport_dpif *port;
2963     struct actions_iterator iter;
2964     const union ofp_action *ia;
2965
2966     port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
2967     if (port
2968         && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2969         port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2970                                ? htonl(OFPPC_NO_RECV_STP)
2971                                : htonl(OFPPC_NO_RECV))) {
2972         /* Drop this flow. */
2973         return;
2974     }
2975
2976     for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2977         enum ofp_action_type type = ntohs(ia->type);
2978         const struct ofp_action_dl_addr *oada;
2979
2980         switch (type) {
2981         case OFPAT_OUTPUT:
2982             xlate_output_action(ctx, &ia->output);
2983             break;
2984
2985         case OFPAT_SET_VLAN_VID:
2986             ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2987             ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2988             xlate_set_dl_tci(ctx);
2989             break;
2990
2991         case OFPAT_SET_VLAN_PCP:
2992             ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2993             ctx->flow.vlan_tci |= htons(
2994                 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2995             xlate_set_dl_tci(ctx);
2996             break;
2997
2998         case OFPAT_STRIP_VLAN:
2999             ctx->flow.vlan_tci = htons(0);
3000             xlate_set_dl_tci(ctx);
3001             break;
3002
3003         case OFPAT_SET_DL_SRC:
3004             oada = ((struct ofp_action_dl_addr *) ia);
3005             nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
3006                               oada->dl_addr, ETH_ADDR_LEN);
3007             memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3008             break;
3009
3010         case OFPAT_SET_DL_DST:
3011             oada = ((struct ofp_action_dl_addr *) ia);
3012             nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
3013                               oada->dl_addr, ETH_ADDR_LEN);
3014             memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3015             break;
3016
3017         case OFPAT_SET_NW_SRC:
3018             nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
3019                             ia->nw_addr.nw_addr);
3020             ctx->flow.nw_src = ia->nw_addr.nw_addr;
3021             break;
3022
3023         case OFPAT_SET_NW_DST:
3024             nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
3025                             ia->nw_addr.nw_addr);
3026             ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3027             break;
3028
3029         case OFPAT_SET_NW_TOS:
3030             nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
3031                           ia->nw_tos.nw_tos);
3032             ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3033             break;
3034
3035         case OFPAT_SET_TP_SRC:
3036             nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
3037                             ia->tp_port.tp_port);
3038             ctx->flow.tp_src = ia->tp_port.tp_port;
3039             break;
3040
3041         case OFPAT_SET_TP_DST:
3042             nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
3043                             ia->tp_port.tp_port);
3044             ctx->flow.tp_dst = ia->tp_port.tp_port;
3045             break;
3046
3047         case OFPAT_VENDOR:
3048             xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
3049             break;
3050
3051         case OFPAT_ENQUEUE:
3052             xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3053             break;
3054
3055         default:
3056             VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
3057             break;
3058         }
3059     }
3060 }
3061
3062 static void
3063 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3064                       struct ofproto_dpif *ofproto, const struct flow *flow,
3065                       const struct ofpbuf *packet)
3066 {
3067     ctx->ofproto = ofproto;
3068     ctx->flow = *flow;
3069     ctx->packet = packet;
3070     ctx->resubmit_hook = NULL;
3071     ctx->check_special = true;
3072 }
3073
3074 static struct ofpbuf *
3075 xlate_actions(struct action_xlate_ctx *ctx,
3076               const union ofp_action *in, size_t n_in)
3077 {
3078     COVERAGE_INC(ofproto_dpif_xlate);
3079
3080     ctx->odp_actions = ofpbuf_new(512);
3081     ctx->tags = 0;
3082     ctx->may_set_up_flow = true;
3083     ctx->nf_output_iface = NF_OUT_DROP;
3084     ctx->recurse = 0;
3085     ctx->last_pop_priority = -1;
3086
3087     if (ctx->check_special
3088         && process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3089         ctx->may_set_up_flow = false;
3090     } else {
3091         do_xlate_actions(in, n_in, ctx);
3092     }
3093
3094     remove_pop_action(ctx);
3095
3096     /* Check with in-band control to see if we're allowed to set up this
3097      * flow. */
3098     if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3099                                  ctx->odp_actions->data,
3100                                  ctx->odp_actions->size)) {
3101         ctx->may_set_up_flow = false;
3102     }
3103
3104     return ctx->odp_actions;
3105 }
3106 \f
3107 /* OFPP_NORMAL implementation. */
3108
3109 struct dst {
3110     struct ofport_dpif *port;
3111     uint16_t vlan;
3112 };
3113
3114 struct dst_set {
3115     struct dst builtin[32];
3116     struct dst *dsts;
3117     size_t n, allocated;
3118 };
3119
3120 static void dst_set_init(struct dst_set *);
3121 static void dst_set_add(struct dst_set *, const struct dst *);
3122 static void dst_set_free(struct dst_set *);
3123
3124 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3125
3126 static bool
3127 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3128         const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3129 {
3130     dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3131                  : in_bundle->vlan >= 0 ? in_bundle->vlan
3132                  : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3133                  : vlan_tci_to_vid(ctx->flow.vlan_tci));
3134
3135     dst->port = (!out_bundle->bond
3136                  ? ofbundle_get_a_port(out_bundle)
3137                  : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3138                                             dst->vlan, &ctx->tags));
3139
3140     return dst->port != NULL;
3141 }
3142
3143 static int
3144 mirror_mask_ffs(mirror_mask_t mask)
3145 {
3146     BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3147     return ffs(mask);
3148 }
3149
3150 static void
3151 dst_set_init(struct dst_set *set)
3152 {
3153     set->dsts = set->builtin;
3154     set->n = 0;
3155     set->allocated = ARRAY_SIZE(set->builtin);
3156 }
3157
3158 static void
3159 dst_set_add(struct dst_set *set, const struct dst *dst)
3160 {
3161     if (set->n >= set->allocated) {
3162         size_t new_allocated;
3163         struct dst *new_dsts;
3164
3165         new_allocated = set->allocated * 2;
3166         new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3167         memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3168
3169         dst_set_free(set);
3170
3171         set->dsts = new_dsts;
3172         set->allocated = new_allocated;
3173     }
3174     set->dsts[set->n++] = *dst;
3175 }
3176
3177 static void
3178 dst_set_free(struct dst_set *set)
3179 {
3180     if (set->dsts != set->builtin) {
3181         free(set->dsts);
3182     }
3183 }
3184
3185 static bool
3186 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3187 {
3188     size_t i;
3189     for (i = 0; i < set->n; i++) {
3190         if (set->dsts[i].vlan == test->vlan
3191             && set->dsts[i].port == test->port) {
3192             return true;
3193         }
3194     }
3195     return false;
3196 }
3197
3198 static bool
3199 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3200 {
3201     return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
3202 }
3203
3204 static bool
3205 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3206 {
3207     return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3208 }
3209
3210 /* Returns an arbitrary interface within 'bundle'. */
3211 static struct ofport_dpif *
3212 ofbundle_get_a_port(const struct ofbundle *bundle)
3213 {
3214     return CONTAINER_OF(list_front(&bundle->ports),
3215                         struct ofport_dpif, bundle_node);
3216 }
3217
3218 static void
3219 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3220              const struct ofbundle *in_bundle,
3221              const struct ofbundle *out_bundle, struct dst_set *set)
3222 {
3223     struct dst dst;
3224
3225     if (out_bundle == OFBUNDLE_FLOOD) {
3226         struct ofbundle *bundle;
3227
3228         HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3229             if (bundle != in_bundle
3230                 && ofbundle_includes_vlan(bundle, vlan)
3231                 && bundle->floodable
3232                 && !bundle->mirror_out
3233                 && set_dst(ctx, &dst, in_bundle, bundle)) {
3234                 dst_set_add(set, &dst);
3235             }
3236         }
3237         ctx->nf_output_iface = NF_OUT_FLOOD;
3238     } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3239         dst_set_add(set, &dst);
3240         ctx->nf_output_iface = dst.port->odp_port;
3241     }
3242 }
3243
3244 static bool
3245 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3246 {
3247     return vlan_bitmap_contains(m->vlans, vlan);
3248 }
3249
3250 static void
3251 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3252                     uint16_t vlan, const struct ofbundle *in_bundle,
3253                     struct dst_set *set)
3254 {
3255     struct ofproto_dpif *ofproto = ctx->ofproto;
3256     mirror_mask_t mirrors;
3257     int flow_vlan;
3258     size_t i;
3259
3260     mirrors = in_bundle->src_mirrors;
3261     for (i = 0; i < set->n; i++) {
3262         mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3263     }
3264
3265     if (!mirrors) {
3266         return;
3267     }
3268
3269     flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3270     if (flow_vlan == 0) {
3271         flow_vlan = OFP_VLAN_NONE;
3272     }
3273
3274     while (mirrors) {
3275         struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3276         if (vlan_is_mirrored(m, vlan)) {
3277             struct dst dst;
3278
3279             if (m->out) {
3280                 if (set_dst(ctx, &dst, in_bundle, m->out)
3281                     && !dst_is_duplicate(set, &dst)) {
3282                     dst_set_add(set, &dst);
3283                 }
3284             } else {
3285                 struct ofbundle *bundle;
3286
3287                 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3288                     if (ofbundle_includes_vlan(bundle, m->out_vlan)
3289                         && set_dst(ctx, &dst, in_bundle, bundle))
3290                     {
3291                         if (bundle->vlan < 0) {
3292                             dst.vlan = m->out_vlan;
3293                         }
3294                         if (dst_is_duplicate(set, &dst)) {
3295                             continue;
3296                         }
3297
3298                         /* Use the vlan tag on the original flow instead of
3299                          * the one passed in the vlan parameter.  This ensures
3300                          * that we compare the vlan from before any implicit
3301                          * tagging tags place. This is necessary because
3302                          * dst->vlan is the final vlan, after removing implicit
3303                          * tags. */
3304                         if (bundle == in_bundle && dst.vlan == flow_vlan) {
3305                             /* Don't send out input port on same VLAN. */
3306                             continue;
3307                         }
3308                         dst_set_add(set, &dst);
3309                     }
3310                 }
3311             }
3312         }
3313         mirrors &= mirrors - 1;
3314     }
3315 }
3316
3317 static void
3318 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3319                 const struct ofbundle *in_bundle,
3320                 const struct ofbundle *out_bundle)
3321 {
3322     uint16_t initial_vlan, cur_vlan;
3323     const struct dst *dst;
3324     struct dst_set set;
3325
3326     dst_set_init(&set);
3327     compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3328     compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3329
3330     /* Output all the packets we can without having to change the VLAN. */
3331     initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3332     if (initial_vlan == 0) {
3333         initial_vlan = OFP_VLAN_NONE;
3334     }
3335     for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3336         if (dst->vlan != initial_vlan) {
3337             continue;
3338         }
3339         nl_msg_put_u32(ctx->odp_actions,
3340                        ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3341     }
3342
3343     /* Then output the rest. */
3344     cur_vlan = initial_vlan;
3345     for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3346         if (dst->vlan == initial_vlan) {
3347             continue;
3348         }
3349         if (dst->vlan != cur_vlan) {
3350             if (dst->vlan == OFP_VLAN_NONE) {
3351                 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3352             } else {
3353                 ovs_be16 tci;
3354                 tci = htons(dst->vlan & VLAN_VID_MASK);
3355                 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3356                 nl_msg_put_be16(ctx->odp_actions,
3357                                 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3358             }
3359             cur_vlan = dst->vlan;
3360         }
3361         nl_msg_put_u32(ctx->odp_actions,
3362                        ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3363     }
3364
3365     dst_set_free(&set);
3366 }
3367
3368 /* Returns the effective vlan of a packet, taking into account both the
3369  * 802.1Q header and implicitly tagged ports.  A value of 0 indicates that
3370  * the packet is untagged and -1 indicates it has an invalid header and
3371  * should be dropped. */
3372 static int
3373 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3374               struct ofbundle *in_bundle, bool have_packet)
3375 {
3376     int vlan = vlan_tci_to_vid(flow->vlan_tci);
3377     if (in_bundle->vlan >= 0) {
3378         if (vlan) {
3379             if (have_packet) {
3380                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3381                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3382                              "packet received on port %s configured with "
3383                              "implicit VLAN %"PRIu16,
3384                              ofproto->up.name, vlan,
3385                              in_bundle->name, in_bundle->vlan);
3386             }
3387             return -1;
3388         }
3389         vlan = in_bundle->vlan;
3390     } else {
3391         if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3392             if (have_packet) {
3393                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3394                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3395                              "packet received on port %s not configured for "
3396                              "trunking VLAN %d",
3397                              ofproto->up.name, vlan, in_bundle->name, vlan);
3398             }
3399             return -1;
3400         }
3401     }
3402
3403     return vlan;
3404 }
3405
3406 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3407  * migration.  Older Citrix-patched Linux DomU used gratuitous ARP replies to
3408  * indicate this; newer upstream kernels use gratuitous ARP requests. */
3409 static bool
3410 is_gratuitous_arp(const struct flow *flow)
3411 {
3412     return (flow->dl_type == htons(ETH_TYPE_ARP)
3413             && eth_addr_is_broadcast(flow->dl_dst)
3414             && (flow->nw_proto == ARP_OP_REPLY
3415                 || (flow->nw_proto == ARP_OP_REQUEST
3416                     && flow->nw_src == flow->nw_dst)));
3417 }
3418
3419 static void
3420 update_learning_table(struct ofproto_dpif *ofproto,
3421                       const struct flow *flow, int vlan,
3422                       struct ofbundle *in_bundle)
3423 {
3424     struct mac_entry *mac;
3425
3426     if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3427         return;
3428     }
3429
3430     mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3431     if (is_gratuitous_arp(flow)) {
3432         /* We don't want to learn from gratuitous ARP packets that are
3433          * reflected back over bond slaves so we lock the learning table. */
3434         if (!in_bundle->bond) {
3435             mac_entry_set_grat_arp_lock(mac);
3436         } else if (mac_entry_is_grat_arp_locked(mac)) {
3437             return;
3438         }
3439     }
3440
3441     if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3442         /* The log messages here could actually be useful in debugging,
3443          * so keep the rate limit relatively high. */
3444         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3445         VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3446                     "on port %s in VLAN %d",
3447                     ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3448                     in_bundle->name, vlan);
3449
3450         mac->port.p = in_bundle;
3451         tag_set_add(&ofproto->revalidate_set,
3452                     mac_learning_changed(ofproto->ml, mac));
3453     }
3454 }
3455
3456 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3457  * dropped.  Returns true if they may be forwarded, false if they should be
3458  * dropped.
3459  *
3460  * If 'have_packet' is true, it indicates that the caller is processing a
3461  * received packet.  If 'have_packet' is false, then the caller is just
3462  * revalidating an existing flow because configuration has changed.  Either
3463  * way, 'have_packet' only affects logging (there is no point in logging errors
3464  * during revalidation).
3465  *
3466  * Sets '*in_portp' to the input port.  This will be a null pointer if
3467  * flow->in_port does not designate a known input port (in which case
3468  * is_admissible() returns false).
3469  *
3470  * When returning true, sets '*vlanp' to the effective VLAN of the input
3471  * packet, as returned by flow_get_vlan().
3472  *
3473  * May also add tags to '*tags', although the current implementation only does
3474  * so in one special case.
3475  */
3476 static bool
3477 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3478               bool have_packet,
3479               tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3480 {
3481     struct ofport_dpif *in_port;
3482     struct ofbundle *in_bundle;
3483     int vlan;
3484
3485     /* Find the port and bundle for the received packet. */
3486     in_port = get_ofp_port(ofproto, flow->in_port);
3487     *in_bundlep = in_bundle = in_port->bundle;
3488     if (!in_port || !in_bundle) {
3489         /* No interface?  Something fishy... */
3490         if (have_packet) {
3491             /* Odd.  A few possible reasons here:
3492              *
3493              * - We deleted a port but there are still a few packets queued up
3494              *   from it.
3495              *
3496              * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3497              *   we don't know about.
3498              *
3499              * - Packet arrived on the local port but the local port is not
3500              *   part of a bundle.
3501              */
3502             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3503
3504             VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3505                          "port %"PRIu16,
3506                          ofproto->up.name, flow->in_port);
3507         }
3508         return false;
3509     }
3510     *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3511     if (vlan < 0) {
3512         return false;
3513     }
3514
3515     /* Drop frames for reserved multicast addresses. */
3516     if (eth_addr_is_reserved(flow->dl_dst)) {
3517         return false;
3518     }
3519
3520     /* Drop frames on bundles reserved for mirroring. */
3521     if (in_bundle->mirror_out) {
3522         if (have_packet) {
3523             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3524             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3525                          "%s, which is reserved exclusively for mirroring",
3526                          ofproto->up.name, in_bundle->name);
3527         }
3528         return false;
3529     }
3530
3531     if (in_bundle->bond) {
3532         struct mac_entry *mac;
3533
3534         switch (bond_check_admissibility(in_bundle->bond, in_port,
3535                                          flow->dl_dst, tags)) {
3536         case BV_ACCEPT:
3537             break;
3538
3539         case BV_DROP:
3540             return false;
3541
3542         case BV_DROP_IF_MOVED:
3543             mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3544             if (mac && mac->port.p != in_bundle &&
3545                 (!is_gratuitous_arp(flow)
3546                  || mac_entry_is_grat_arp_locked(mac))) {
3547                 return false;
3548             }
3549             break;
3550         }
3551     }
3552
3553     return true;
3554 }
3555
3556 /* If the composed actions may be applied to any packet in the given 'flow',
3557  * returns true.  Otherwise, the actions should only be applied to 'packet', or
3558  * not at all, if 'packet' was NULL. */
3559 static bool
3560 xlate_normal(struct action_xlate_ctx *ctx)
3561 {
3562     struct ofbundle *in_bundle;
3563     struct ofbundle *out_bundle;
3564     struct mac_entry *mac;
3565     int vlan;
3566
3567     /* Check whether we should drop packets in this flow. */
3568     if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3569                        &ctx->tags, &vlan, &in_bundle)) {
3570         out_bundle = NULL;
3571         goto done;
3572     }
3573
3574     /* Learn source MAC (but don't try to learn from revalidation). */
3575     if (ctx->packet) {
3576         update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3577     }
3578
3579     /* Determine output bundle. */
3580     mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3581                               &ctx->tags);
3582     if (mac) {
3583         out_bundle = mac->port.p;
3584     } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3585         /* If we are revalidating but don't have a learning entry then eject
3586          * the flow.  Installing a flow that floods packets opens up a window
3587          * of time where we could learn from a packet reflected on a bond and
3588          * blackhole packets before the learning table is updated to reflect
3589          * the correct port. */
3590         return false;
3591     } else {
3592         out_bundle = OFBUNDLE_FLOOD;
3593     }
3594
3595     /* Don't send packets out their input bundles. */
3596     if (in_bundle == out_bundle) {
3597         out_bundle = NULL;
3598     }
3599
3600 done:
3601     if (in_bundle) {
3602         compose_actions(ctx, vlan, in_bundle, out_bundle);
3603     }
3604
3605     return true;
3606 }
3607 \f
3608 static bool
3609 get_drop_frags(struct ofproto *ofproto_)
3610 {
3611     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3612     bool drop_frags;
3613
3614     dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3615     return drop_frags;
3616 }
3617
3618 static void
3619 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3620 {
3621     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3622
3623     dpif_set_drop_frags(ofproto->dpif, drop_frags);
3624 }
3625
3626 static int
3627 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3628            const struct flow *flow,
3629            const union ofp_action *ofp_actions, size_t n_ofp_actions)
3630 {
3631     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3632     int error;
3633
3634     error = validate_actions(ofp_actions, n_ofp_actions, flow,
3635                              ofproto->max_ports);
3636     if (!error) {
3637         struct action_xlate_ctx ctx;
3638         struct ofpbuf *odp_actions;
3639
3640         action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3641         odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3642         dpif_execute(ofproto->dpif, odp_actions->data, odp_actions->size,
3643                      packet);
3644         ofpbuf_delete(odp_actions);
3645     }
3646     return error;
3647 }
3648
3649 static void
3650 get_netflow_ids(const struct ofproto *ofproto_,
3651                 uint8_t *engine_type, uint8_t *engine_id)
3652 {
3653     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3654
3655     dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3656 }
3657 \f
3658 static struct ofproto_dpif *
3659 ofproto_dpif_lookup(const char *name)
3660 {
3661     struct ofproto *ofproto = ofproto_lookup(name);
3662     return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3663             ? ofproto_dpif_cast(ofproto)
3664             : NULL);
3665 }
3666
3667 static void
3668 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3669                          const char *args, void *aux OVS_UNUSED)
3670 {
3671     struct ds ds = DS_EMPTY_INITIALIZER;
3672     const struct ofproto_dpif *ofproto;
3673     const struct mac_entry *e;
3674
3675     ofproto = ofproto_dpif_lookup(args);
3676     if (!ofproto) {
3677         unixctl_command_reply(conn, 501, "no such bridge");
3678         return;
3679     }
3680
3681     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
3682     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3683         struct ofbundle *bundle = e->port.p;
3684         ds_put_format(&ds, "%5d  %4d  "ETH_ADDR_FMT"  %3d\n",
3685                       ofbundle_get_a_port(bundle)->odp_port,
3686                       e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3687     }
3688     unixctl_command_reply(conn, 200, ds_cstr(&ds));
3689     ds_destroy(&ds);
3690 }
3691
3692 struct ofproto_trace {
3693     struct action_xlate_ctx ctx;
3694     struct flow flow;
3695     struct ds *result;
3696 };
3697
3698 static void
3699 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3700 {
3701     ds_put_char_multiple(result, '\t', level);
3702     if (!rule) {
3703         ds_put_cstr(result, "No match\n");
3704         return;
3705     }
3706
3707     ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3708                   ntohll(rule->flow_cookie));
3709     cls_rule_format(&rule->cr, result);
3710     ds_put_char(result, '\n');
3711
3712     ds_put_char_multiple(result, '\t', level);
3713     ds_put_cstr(result, "OpenFlow ");
3714     ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
3715                       rule->n_actions * sizeof *rule->actions);
3716     ds_put_char(result, '\n');
3717 }
3718
3719 static void
3720 trace_format_flow(struct ds *result, int level, const char *title,
3721                  struct ofproto_trace *trace)
3722 {
3723     ds_put_char_multiple(result, '\t', level);
3724     ds_put_format(result, "%s: ", title);
3725     if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3726         ds_put_cstr(result, "unchanged");
3727     } else {
3728         flow_format(result, &trace->ctx.flow);
3729         trace->flow = trace->ctx.flow;
3730     }
3731     ds_put_char(result, '\n');
3732 }
3733
3734 static void
3735 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3736 {
3737     struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3738     struct ds *result = trace->result;
3739
3740     ds_put_char(result, '\n');
3741     trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3742     trace_format_rule(result, ctx->recurse + 1, &rule->up);
3743 }
3744
3745 static void
3746 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3747                       void *aux OVS_UNUSED)
3748 {
3749     char *dpname, *in_port_s, *tun_id_s, *packet_s;
3750     char *args = xstrdup(args_);
3751     char *save_ptr = NULL;
3752     struct ofproto_dpif *ofproto;
3753     struct ofpbuf packet;
3754     struct rule_dpif *rule;
3755     struct ds result;
3756     struct flow flow;
3757     uint16_t in_port;
3758     ovs_be64 tun_id;
3759     char *s;
3760
3761     ofpbuf_init(&packet, strlen(args) / 2);
3762     ds_init(&result);
3763
3764     dpname = strtok_r(args, " ", &save_ptr);
3765     tun_id_s = strtok_r(NULL, " ", &save_ptr);
3766     in_port_s = strtok_r(NULL, " ", &save_ptr);
3767     packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
3768     if (!dpname || !in_port_s || !packet_s) {
3769         unixctl_command_reply(conn, 501, "Bad command syntax");
3770         goto exit;
3771     }
3772
3773     ofproto = ofproto_dpif_lookup(dpname);
3774     if (!ofproto) {
3775         unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
3776                               "for help)");
3777         goto exit;
3778     }
3779
3780     tun_id = htonll(strtoull(tun_id_s, NULL, 0));
3781     in_port = ofp_port_to_odp_port(atoi(in_port_s));
3782
3783     packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
3784     packet_s += strspn(packet_s, " ");
3785     if (*packet_s != '\0') {
3786         unixctl_command_reply(conn, 501, "Trailing garbage in command");
3787         goto exit;
3788     }
3789     if (packet.size < ETH_HEADER_LEN) {
3790         unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
3791         goto exit;
3792     }
3793
3794     ds_put_cstr(&result, "Packet: ");
3795     s = ofp_packet_to_string(packet.data, packet.size, packet.size);
3796     ds_put_cstr(&result, s);
3797     free(s);
3798
3799     flow_extract(&packet, tun_id, in_port, &flow);
3800     ds_put_cstr(&result, "Flow: ");
3801     flow_format(&result, &flow);
3802     ds_put_char(&result, '\n');
3803
3804     rule = rule_dpif_lookup(ofproto, &flow);
3805     trace_format_rule(&result, 0, &rule->up);
3806     if (rule) {
3807         struct ofproto_trace trace;
3808         struct ofpbuf *odp_actions;
3809
3810         trace.result = &result;
3811         trace.flow = flow;
3812         action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
3813         trace.ctx.resubmit_hook = trace_resubmit;
3814         odp_actions = xlate_actions(&trace.ctx,
3815                                     rule->up.actions, rule->up.n_actions);
3816
3817         ds_put_char(&result, '\n');
3818         trace_format_flow(&result, 0, "Final flow", &trace);
3819         ds_put_cstr(&result, "Datapath actions: ");
3820         format_odp_actions(&result, odp_actions->data, odp_actions->size);
3821         ofpbuf_delete(odp_actions);
3822     }
3823
3824     unixctl_command_reply(conn, 200, ds_cstr(&result));
3825
3826 exit:
3827     ds_destroy(&result);
3828     ofpbuf_uninit(&packet);
3829     free(args);
3830 }
3831
3832 static void
3833 ofproto_dpif_unixctl_init(void)
3834 {
3835     static bool registered;
3836     if (registered) {
3837         return;
3838     }
3839     registered = true;
3840
3841     unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
3842     unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
3843 }
3844 \f
3845 const struct ofproto_class ofproto_dpif_class = {
3846     enumerate_types,
3847     enumerate_names,
3848     del,
3849     alloc,
3850     construct,
3851     destruct,
3852     dealloc,
3853     run,
3854     wait,
3855     flush,
3856     port_alloc,
3857     port_construct,
3858     port_destruct,
3859     port_dealloc,
3860     port_modified,
3861     port_reconfigured,
3862     port_query_by_name,
3863     port_add,
3864     port_del,
3865     port_dump_start,
3866     port_dump_next,
3867     port_dump_done,
3868     port_poll,
3869     port_poll_wait,
3870     port_is_lacp_current,
3871     rule_alloc,
3872     rule_construct,
3873     rule_destruct,
3874     rule_dealloc,
3875     rule_get_stats,
3876     rule_execute,
3877     rule_modify_actions,
3878     get_drop_frags,
3879     set_drop_frags,
3880     packet_out,
3881     set_netflow,
3882     get_netflow_ids,
3883     set_sflow,
3884     set_cfm,
3885     get_cfm,
3886     bundle_set,
3887     bundle_remove,
3888     mirror_set,
3889     set_flood_vlans,
3890     is_mirror_output_bundle,
3891 };