ofproto: Eliminate reference to dpif_upcall from ofproto.
[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 cls_rule *displaced_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     rule->used = rule->up.created;
2462     rule->packet_count = 0;
2463     rule->byte_count = 0;
2464     list_init(&rule->facets);
2465
2466     displaced_rule = classifier_insert(&ofproto->up.cls, &rule->up.cr);
2467     if (displaced_rule) {
2468         ofproto_rule_destroy(rule_from_cls_rule(displaced_rule));
2469     }
2470     ofproto->need_revalidate = true;
2471
2472     return 0;
2473 }
2474
2475 static void
2476 rule_destruct(struct rule *rule_)
2477 {
2478     struct rule_dpif *rule = rule_dpif_cast(rule_);
2479     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2480     struct facet *facet, *next_facet;
2481
2482     ofproto->need_revalidate = true;
2483     LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
2484         facet_revalidate(ofproto, facet);
2485     }
2486 }
2487
2488 static void
2489 rule_remove(struct rule *rule_)
2490 {
2491     struct rule_dpif *rule = rule_dpif_cast(rule_);
2492     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2493
2494     ofproto->need_revalidate = true;
2495     classifier_remove(&ofproto->up.cls, &rule->up.cr);
2496 }
2497
2498 static void
2499 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
2500 {
2501     struct rule_dpif *rule = rule_dpif_cast(rule_);
2502     struct facet *facet;
2503
2504     /* Start from historical data for 'rule' itself that are no longer tracked
2505      * in facets.  This counts, for example, facets that have expired. */
2506     *packets = rule->packet_count;
2507     *bytes = rule->byte_count;
2508
2509     /* Add any statistics that are tracked by facets.  This includes
2510      * statistical data recently updated by ofproto_update_stats() as well as
2511      * stats for packets that were executed "by hand" via dpif_execute(). */
2512     LIST_FOR_EACH (facet, list_node, &rule->facets) {
2513         *packets += facet->packet_count;
2514         *bytes += facet->byte_count;
2515     }
2516 }
2517
2518 static int
2519 rule_execute(struct rule *rule_, struct flow *flow, struct ofpbuf *packet)
2520 {
2521     struct rule_dpif *rule = rule_dpif_cast(rule_);
2522     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2523     struct action_xlate_ctx ctx;
2524     struct ofpbuf *odp_actions;
2525     struct facet *facet;
2526     size_t size;
2527
2528     /* First look for a related facet.  If we find one, account it to that. */
2529     facet = facet_lookup_valid(ofproto, flow);
2530     if (facet && facet->rule == rule) {
2531         facet_execute(ofproto, facet, packet);
2532         return 0;
2533     }
2534
2535     /* Otherwise, if 'rule' is in fact the correct rule for 'packet', then
2536      * create a new facet for it and use that. */
2537     if (rule_dpif_lookup(ofproto, flow) == rule) {
2538         facet = facet_create(rule, flow, packet);
2539         facet_execute(ofproto, facet, packet);
2540         facet_install(ofproto, facet, true);
2541         return 0;
2542     }
2543
2544     /* We can't account anything to a facet.  If we were to try, then that
2545      * facet would have a non-matching rule, busting our invariants. */
2546     action_xlate_ctx_init(&ctx, ofproto, flow, packet);
2547     odp_actions = xlate_actions(&ctx, rule->up.actions, rule->up.n_actions);
2548     size = packet->size;
2549     if (execute_odp_actions(ofproto, flow, odp_actions->data,
2550                             odp_actions->size, packet)) {
2551         rule->used = time_msec();
2552         rule->packet_count++;
2553         rule->byte_count += size;
2554         flow_push_stats(rule, flow, 1, size, rule->used);
2555     }
2556     ofpbuf_delete(odp_actions);
2557
2558     return 0;
2559 }
2560
2561 static int
2562 rule_modify_actions(struct rule *rule_,
2563                     const union ofp_action *actions, size_t n_actions)
2564 {
2565     struct rule_dpif *rule = rule_dpif_cast(rule_);
2566     struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2567     int error;
2568
2569     error = validate_actions(actions, n_actions, &rule->up.cr.flow,
2570                              ofproto->max_ports);
2571     if (!error) {
2572         ofproto->need_revalidate = true;
2573     }
2574     return error;
2575 }
2576 \f
2577 /* Sends 'packet' out of port 'odp_port' within 'ofproto'.  If 'vlan_tci' is
2578  * zero the packet will not have any 802.1Q hader; if it is nonzero, then the
2579  * packet will be sent with the VLAN TCI specified by 'vlan_tci & ~VLAN_CFI'.
2580  *
2581  * Returns 0 if successful, otherwise a positive errno value. */
2582 static int
2583 send_packet(struct ofproto_dpif *ofproto, uint32_t odp_port, uint16_t vlan_tci,
2584             const struct ofpbuf *packet)
2585 {
2586     struct ofpbuf odp_actions;
2587     int error;
2588
2589     ofpbuf_init(&odp_actions, 32);
2590     if (vlan_tci != 0) {
2591         nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2592                        ntohs(vlan_tci & ~VLAN_CFI));
2593     }
2594     nl_msg_put_u32(&odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2595     error = dpif_execute(ofproto->dpif, odp_actions.data, odp_actions.size,
2596                          packet);
2597     ofpbuf_uninit(&odp_actions);
2598
2599     if (error) {
2600         VLOG_WARN_RL(&rl, "%s: failed to send packet on port %"PRIu32" (%s)",
2601                      ofproto->up.name, odp_port, strerror(error));
2602     }
2603     return error;
2604 }
2605 \f
2606 /* OpenFlow to ODP action translation. */
2607
2608 static void do_xlate_actions(const union ofp_action *in, size_t n_in,
2609                              struct action_xlate_ctx *ctx);
2610 static bool xlate_normal(struct action_xlate_ctx *);
2611
2612 static void
2613 add_output_action(struct action_xlate_ctx *ctx, uint16_t ofp_port)
2614 {
2615     const struct ofport_dpif *ofport = get_ofp_port(ctx->ofproto, ofp_port);
2616     uint16_t odp_port = ofp_port_to_odp_port(ofp_port);
2617
2618     if (ofport) {
2619         if (ofport->up.opp.config & htonl(OFPPC_NO_FWD)) {
2620             /* Forwarding disabled on port. */
2621             return;
2622         }
2623     } else {
2624         /*
2625          * We don't have an ofport record for this port, but it doesn't hurt to
2626          * allow forwarding to it anyhow.  Maybe such a port will appear later
2627          * and we're pre-populating the flow table.
2628          */
2629     }
2630
2631     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_OUTPUT, odp_port);
2632     ctx->nf_output_iface = ofp_port;
2633 }
2634
2635 static void
2636 xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
2637 {
2638     if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
2639         struct rule_dpif *rule;
2640         uint16_t old_in_port;
2641
2642         /* Look up a flow with 'in_port' as the input port.  Then restore the
2643          * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
2644          * have surprising behavior). */
2645         old_in_port = ctx->flow.in_port;
2646         ctx->flow.in_port = in_port;
2647         rule = rule_dpif_lookup(ctx->ofproto, &ctx->flow);
2648         ctx->flow.in_port = old_in_port;
2649
2650         if (ctx->resubmit_hook) {
2651             ctx->resubmit_hook(ctx, rule);
2652         }
2653
2654         if (rule) {
2655             ctx->recurse++;
2656             do_xlate_actions(rule->up.actions, rule->up.n_actions, ctx);
2657             ctx->recurse--;
2658         }
2659     } else {
2660         static struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
2661
2662         VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
2663                     MAX_RESUBMIT_RECURSION);
2664     }
2665 }
2666
2667 static void
2668 flood_packets(struct ofproto_dpif *ofproto,
2669               uint16_t ofp_in_port, ovs_be32 mask,
2670               uint16_t *nf_output_iface, struct ofpbuf *odp_actions)
2671 {
2672     struct ofport_dpif *ofport;
2673
2674     HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2675         uint16_t ofp_port = ofport->up.ofp_port;
2676         if (ofp_port != ofp_in_port && !(ofport->up.opp.config & mask)) {
2677             nl_msg_put_u32(odp_actions, ODP_ACTION_ATTR_OUTPUT,
2678                            ofport->odp_port);
2679         }
2680     }
2681     *nf_output_iface = NF_OUT_FLOOD;
2682 }
2683
2684 static void
2685 xlate_output_action__(struct action_xlate_ctx *ctx,
2686                       uint16_t port, uint16_t max_len)
2687 {
2688     uint16_t prev_nf_output_iface = ctx->nf_output_iface;
2689
2690     ctx->nf_output_iface = NF_OUT_DROP;
2691
2692     switch (port) {
2693     case OFPP_IN_PORT:
2694         add_output_action(ctx, ctx->flow.in_port);
2695         break;
2696     case OFPP_TABLE:
2697         xlate_table_action(ctx, ctx->flow.in_port);
2698         break;
2699     case OFPP_NORMAL:
2700         xlate_normal(ctx);
2701         break;
2702     case OFPP_FLOOD:
2703         flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(OFPPC_NO_FLOOD),
2704                       &ctx->nf_output_iface, ctx->odp_actions);
2705         break;
2706     case OFPP_ALL:
2707         flood_packets(ctx->ofproto, ctx->flow.in_port, htonl(0),
2708                       &ctx->nf_output_iface, ctx->odp_actions);
2709         break;
2710     case OFPP_CONTROLLER:
2711         nl_msg_put_u64(ctx->odp_actions, ODP_ACTION_ATTR_CONTROLLER, max_len);
2712         break;
2713     case OFPP_LOCAL:
2714         add_output_action(ctx, OFPP_LOCAL);
2715         break;
2716     default:
2717         if (port != ctx->flow.in_port) {
2718             add_output_action(ctx, port);
2719         }
2720         break;
2721     }
2722
2723     if (prev_nf_output_iface == NF_OUT_FLOOD) {
2724         ctx->nf_output_iface = NF_OUT_FLOOD;
2725     } else if (ctx->nf_output_iface == NF_OUT_DROP) {
2726         ctx->nf_output_iface = prev_nf_output_iface;
2727     } else if (prev_nf_output_iface != NF_OUT_DROP &&
2728                ctx->nf_output_iface != NF_OUT_FLOOD) {
2729         ctx->nf_output_iface = NF_OUT_MULTI;
2730     }
2731 }
2732
2733 static void
2734 xlate_output_action(struct action_xlate_ctx *ctx,
2735                     const struct ofp_action_output *oao)
2736 {
2737     xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
2738 }
2739
2740 /* If the final ODP action in 'ctx' is "pop priority", drop it, as an
2741  * optimization, because we're going to add another action that sets the
2742  * priority immediately after, or because there are no actions following the
2743  * pop.  */
2744 static void
2745 remove_pop_action(struct action_xlate_ctx *ctx)
2746 {
2747     if (ctx->odp_actions->size == ctx->last_pop_priority) {
2748         ctx->odp_actions->size -= NLA_ALIGN(NLA_HDRLEN);
2749         ctx->last_pop_priority = -1;
2750     }
2751 }
2752
2753 static void
2754 add_pop_action(struct action_xlate_ctx *ctx)
2755 {
2756     if (ctx->odp_actions->size != ctx->last_pop_priority) {
2757         nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_POP_PRIORITY);
2758         ctx->last_pop_priority = ctx->odp_actions->size;
2759     }
2760 }
2761
2762 static void
2763 xlate_enqueue_action(struct action_xlate_ctx *ctx,
2764                      const struct ofp_action_enqueue *oae)
2765 {
2766     uint16_t ofp_port, odp_port;
2767     uint32_t priority;
2768     int error;
2769
2770     error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
2771                                    &priority);
2772     if (error) {
2773         /* Fall back to ordinary output action. */
2774         xlate_output_action__(ctx, ntohs(oae->port), 0);
2775         return;
2776     }
2777
2778     /* Figure out ODP output port. */
2779     ofp_port = ntohs(oae->port);
2780     if (ofp_port == OFPP_IN_PORT) {
2781         ofp_port = ctx->flow.in_port;
2782     }
2783     odp_port = ofp_port_to_odp_port(ofp_port);
2784
2785     /* Add ODP actions. */
2786     remove_pop_action(ctx);
2787     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2788     add_output_action(ctx, odp_port);
2789     add_pop_action(ctx);
2790
2791     /* Update NetFlow output port. */
2792     if (ctx->nf_output_iface == NF_OUT_DROP) {
2793         ctx->nf_output_iface = odp_port;
2794     } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
2795         ctx->nf_output_iface = NF_OUT_MULTI;
2796     }
2797 }
2798
2799 static void
2800 xlate_set_queue_action(struct action_xlate_ctx *ctx,
2801                        const struct nx_action_set_queue *nasq)
2802 {
2803     uint32_t priority;
2804     int error;
2805
2806     error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
2807                                    &priority);
2808     if (error) {
2809         /* Couldn't translate queue to a priority, so ignore.  A warning
2810          * has already been logged. */
2811         return;
2812     }
2813
2814     remove_pop_action(ctx);
2815     nl_msg_put_u32(ctx->odp_actions, ODP_ACTION_ATTR_SET_PRIORITY, priority);
2816 }
2817
2818 static void
2819 xlate_set_dl_tci(struct action_xlate_ctx *ctx)
2820 {
2821     ovs_be16 tci = ctx->flow.vlan_tci;
2822     if (!(tci & htons(VLAN_CFI))) {
2823         nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
2824     } else {
2825         nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_TCI,
2826                         tci & ~htons(VLAN_CFI));
2827     }
2828 }
2829
2830 struct xlate_reg_state {
2831     ovs_be16 vlan_tci;
2832     ovs_be64 tun_id;
2833 };
2834
2835 static void
2836 save_reg_state(const struct action_xlate_ctx *ctx,
2837                struct xlate_reg_state *state)
2838 {
2839     state->vlan_tci = ctx->flow.vlan_tci;
2840     state->tun_id = ctx->flow.tun_id;
2841 }
2842
2843 static void
2844 update_reg_state(struct action_xlate_ctx *ctx,
2845                  const struct xlate_reg_state *state)
2846 {
2847     if (ctx->flow.vlan_tci != state->vlan_tci) {
2848         xlate_set_dl_tci(ctx);
2849     }
2850     if (ctx->flow.tun_id != state->tun_id) {
2851         nl_msg_put_be64(ctx->odp_actions,
2852                         ODP_ACTION_ATTR_SET_TUNNEL, ctx->flow.tun_id);
2853     }
2854 }
2855
2856 static void
2857 xlate_autopath(struct action_xlate_ctx *ctx,
2858                const struct nx_action_autopath *naa)
2859 {
2860     uint16_t ofp_port = ntohl(naa->id);
2861     struct ofport_dpif *port = get_ofp_port(ctx->ofproto, ofp_port);
2862
2863     if (!port || !port->bundle) {
2864         ofp_port = OFPP_NONE;
2865     } else if (port->bundle->bond) {
2866         /* Autopath does not support VLAN hashing. */
2867         struct ofport_dpif *slave = bond_choose_output_slave(
2868             port->bundle->bond, &ctx->flow, OFP_VLAN_NONE, &ctx->tags);
2869         if (slave) {
2870             ofp_port = slave->up.ofp_port;
2871         }
2872     }
2873     autopath_execute(naa, &ctx->flow, ofp_port);
2874 }
2875
2876 static void
2877 xlate_nicira_action(struct action_xlate_ctx *ctx,
2878                     const struct nx_action_header *nah)
2879 {
2880     const struct nx_action_resubmit *nar;
2881     const struct nx_action_set_tunnel *nast;
2882     const struct nx_action_set_queue *nasq;
2883     const struct nx_action_multipath *nam;
2884     const struct nx_action_autopath *naa;
2885     enum nx_action_subtype subtype = ntohs(nah->subtype);
2886     struct xlate_reg_state state;
2887     ovs_be64 tun_id;
2888
2889     assert(nah->vendor == htonl(NX_VENDOR_ID));
2890     switch (subtype) {
2891     case NXAST_RESUBMIT:
2892         nar = (const struct nx_action_resubmit *) nah;
2893         xlate_table_action(ctx, ntohs(nar->in_port));
2894         break;
2895
2896     case NXAST_SET_TUNNEL:
2897         nast = (const struct nx_action_set_tunnel *) nah;
2898         tun_id = htonll(ntohl(nast->tun_id));
2899         nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2900         ctx->flow.tun_id = tun_id;
2901         break;
2902
2903     case NXAST_DROP_SPOOFED_ARP:
2904         if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
2905             nl_msg_put_flag(ctx->odp_actions,
2906                             ODP_ACTION_ATTR_DROP_SPOOFED_ARP);
2907         }
2908         break;
2909
2910     case NXAST_SET_QUEUE:
2911         nasq = (const struct nx_action_set_queue *) nah;
2912         xlate_set_queue_action(ctx, nasq);
2913         break;
2914
2915     case NXAST_POP_QUEUE:
2916         add_pop_action(ctx);
2917         break;
2918
2919     case NXAST_REG_MOVE:
2920         save_reg_state(ctx, &state);
2921         nxm_execute_reg_move((const struct nx_action_reg_move *) nah,
2922                              &ctx->flow);
2923         update_reg_state(ctx, &state);
2924         break;
2925
2926     case NXAST_REG_LOAD:
2927         save_reg_state(ctx, &state);
2928         nxm_execute_reg_load((const struct nx_action_reg_load *) nah,
2929                              &ctx->flow);
2930         update_reg_state(ctx, &state);
2931         break;
2932
2933     case NXAST_NOTE:
2934         /* Nothing to do. */
2935         break;
2936
2937     case NXAST_SET_TUNNEL64:
2938         tun_id = ((const struct nx_action_set_tunnel64 *) nah)->tun_id;
2939         nl_msg_put_be64(ctx->odp_actions, ODP_ACTION_ATTR_SET_TUNNEL, tun_id);
2940         ctx->flow.tun_id = tun_id;
2941         break;
2942
2943     case NXAST_MULTIPATH:
2944         nam = (const struct nx_action_multipath *) nah;
2945         multipath_execute(nam, &ctx->flow);
2946         break;
2947
2948     case NXAST_AUTOPATH:
2949         naa = (const struct nx_action_autopath *) nah;
2950         xlate_autopath(ctx, naa);
2951         break;
2952
2953     /* If you add a new action here that modifies flow data, don't forget to
2954      * update the flow key in ctx->flow at the same time. */
2955
2956     case NXAST_SNAT__OBSOLETE:
2957     default:
2958         VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
2959         break;
2960     }
2961 }
2962
2963 static void
2964 do_xlate_actions(const union ofp_action *in, size_t n_in,
2965                  struct action_xlate_ctx *ctx)
2966 {
2967     const struct ofport_dpif *port;
2968     struct actions_iterator iter;
2969     const union ofp_action *ia;
2970
2971     port = get_ofp_port(ctx->ofproto, ctx->flow.in_port);
2972     if (port
2973         && port->up.opp.config & htonl(OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
2974         port->up.opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
2975                                ? htonl(OFPPC_NO_RECV_STP)
2976                                : htonl(OFPPC_NO_RECV))) {
2977         /* Drop this flow. */
2978         return;
2979     }
2980
2981     for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
2982         enum ofp_action_type type = ntohs(ia->type);
2983         const struct ofp_action_dl_addr *oada;
2984
2985         switch (type) {
2986         case OFPAT_OUTPUT:
2987             xlate_output_action(ctx, &ia->output);
2988             break;
2989
2990         case OFPAT_SET_VLAN_VID:
2991             ctx->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
2992             ctx->flow.vlan_tci |= ia->vlan_vid.vlan_vid | htons(VLAN_CFI);
2993             xlate_set_dl_tci(ctx);
2994             break;
2995
2996         case OFPAT_SET_VLAN_PCP:
2997             ctx->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
2998             ctx->flow.vlan_tci |= htons(
2999                 (ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
3000             xlate_set_dl_tci(ctx);
3001             break;
3002
3003         case OFPAT_STRIP_VLAN:
3004             ctx->flow.vlan_tci = htons(0);
3005             xlate_set_dl_tci(ctx);
3006             break;
3007
3008         case OFPAT_SET_DL_SRC:
3009             oada = ((struct ofp_action_dl_addr *) ia);
3010             nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_SRC,
3011                               oada->dl_addr, ETH_ADDR_LEN);
3012             memcpy(ctx->flow.dl_src, oada->dl_addr, ETH_ADDR_LEN);
3013             break;
3014
3015         case OFPAT_SET_DL_DST:
3016             oada = ((struct ofp_action_dl_addr *) ia);
3017             nl_msg_put_unspec(ctx->odp_actions, ODP_ACTION_ATTR_SET_DL_DST,
3018                               oada->dl_addr, ETH_ADDR_LEN);
3019             memcpy(ctx->flow.dl_dst, oada->dl_addr, ETH_ADDR_LEN);
3020             break;
3021
3022         case OFPAT_SET_NW_SRC:
3023             nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_SRC,
3024                             ia->nw_addr.nw_addr);
3025             ctx->flow.nw_src = ia->nw_addr.nw_addr;
3026             break;
3027
3028         case OFPAT_SET_NW_DST:
3029             nl_msg_put_be32(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_DST,
3030                             ia->nw_addr.nw_addr);
3031             ctx->flow.nw_dst = ia->nw_addr.nw_addr;
3032             break;
3033
3034         case OFPAT_SET_NW_TOS:
3035             nl_msg_put_u8(ctx->odp_actions, ODP_ACTION_ATTR_SET_NW_TOS,
3036                           ia->nw_tos.nw_tos);
3037             ctx->flow.nw_tos = ia->nw_tos.nw_tos;
3038             break;
3039
3040         case OFPAT_SET_TP_SRC:
3041             nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_SRC,
3042                             ia->tp_port.tp_port);
3043             ctx->flow.tp_src = ia->tp_port.tp_port;
3044             break;
3045
3046         case OFPAT_SET_TP_DST:
3047             nl_msg_put_be16(ctx->odp_actions, ODP_ACTION_ATTR_SET_TP_DST,
3048                             ia->tp_port.tp_port);
3049             ctx->flow.tp_dst = ia->tp_port.tp_port;
3050             break;
3051
3052         case OFPAT_VENDOR:
3053             xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
3054             break;
3055
3056         case OFPAT_ENQUEUE:
3057             xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
3058             break;
3059
3060         default:
3061             VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
3062             break;
3063         }
3064     }
3065 }
3066
3067 static void
3068 action_xlate_ctx_init(struct action_xlate_ctx *ctx,
3069                       struct ofproto_dpif *ofproto, const struct flow *flow,
3070                       const struct ofpbuf *packet)
3071 {
3072     ctx->ofproto = ofproto;
3073     ctx->flow = *flow;
3074     ctx->packet = packet;
3075     ctx->resubmit_hook = NULL;
3076     ctx->check_special = true;
3077 }
3078
3079 static struct ofpbuf *
3080 xlate_actions(struct action_xlate_ctx *ctx,
3081               const union ofp_action *in, size_t n_in)
3082 {
3083     COVERAGE_INC(ofproto_dpif_xlate);
3084
3085     ctx->odp_actions = ofpbuf_new(512);
3086     ctx->tags = 0;
3087     ctx->may_set_up_flow = true;
3088     ctx->nf_output_iface = NF_OUT_DROP;
3089     ctx->recurse = 0;
3090     ctx->last_pop_priority = -1;
3091
3092     if (ctx->check_special
3093         && process_special(ctx->ofproto, &ctx->flow, ctx->packet)) {
3094         ctx->may_set_up_flow = false;
3095     } else {
3096         do_xlate_actions(in, n_in, ctx);
3097     }
3098
3099     remove_pop_action(ctx);
3100
3101     /* Check with in-band control to see if we're allowed to set up this
3102      * flow. */
3103     if (!connmgr_may_set_up_flow(ctx->ofproto->up.connmgr, &ctx->flow,
3104                                  ctx->odp_actions->data,
3105                                  ctx->odp_actions->size)) {
3106         ctx->may_set_up_flow = false;
3107     }
3108
3109     return ctx->odp_actions;
3110 }
3111 \f
3112 /* OFPP_NORMAL implementation. */
3113
3114 struct dst {
3115     struct ofport_dpif *port;
3116     uint16_t vlan;
3117 };
3118
3119 struct dst_set {
3120     struct dst builtin[32];
3121     struct dst *dsts;
3122     size_t n, allocated;
3123 };
3124
3125 static void dst_set_init(struct dst_set *);
3126 static void dst_set_add(struct dst_set *, const struct dst *);
3127 static void dst_set_free(struct dst_set *);
3128
3129 static struct ofport_dpif *ofbundle_get_a_port(const struct ofbundle *);
3130
3131 static bool
3132 set_dst(struct action_xlate_ctx *ctx, struct dst *dst,
3133         const struct ofbundle *in_bundle, const struct ofbundle *out_bundle)
3134 {
3135     dst->vlan = (out_bundle->vlan >= 0 ? OFP_VLAN_NONE
3136                  : in_bundle->vlan >= 0 ? in_bundle->vlan
3137                  : ctx->flow.vlan_tci == 0 ? OFP_VLAN_NONE
3138                  : vlan_tci_to_vid(ctx->flow.vlan_tci));
3139
3140     dst->port = (!out_bundle->bond
3141                  ? ofbundle_get_a_port(out_bundle)
3142                  : bond_choose_output_slave(out_bundle->bond, &ctx->flow,
3143                                             dst->vlan, &ctx->tags));
3144
3145     return dst->port != NULL;
3146 }
3147
3148 static int
3149 mirror_mask_ffs(mirror_mask_t mask)
3150 {
3151     BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
3152     return ffs(mask);
3153 }
3154
3155 static void
3156 dst_set_init(struct dst_set *set)
3157 {
3158     set->dsts = set->builtin;
3159     set->n = 0;
3160     set->allocated = ARRAY_SIZE(set->builtin);
3161 }
3162
3163 static void
3164 dst_set_add(struct dst_set *set, const struct dst *dst)
3165 {
3166     if (set->n >= set->allocated) {
3167         size_t new_allocated;
3168         struct dst *new_dsts;
3169
3170         new_allocated = set->allocated * 2;
3171         new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
3172         memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
3173
3174         dst_set_free(set);
3175
3176         set->dsts = new_dsts;
3177         set->allocated = new_allocated;
3178     }
3179     set->dsts[set->n++] = *dst;
3180 }
3181
3182 static void
3183 dst_set_free(struct dst_set *set)
3184 {
3185     if (set->dsts != set->builtin) {
3186         free(set->dsts);
3187     }
3188 }
3189
3190 static bool
3191 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
3192 {
3193     size_t i;
3194     for (i = 0; i < set->n; i++) {
3195         if (set->dsts[i].vlan == test->vlan
3196             && set->dsts[i].port == test->port) {
3197             return true;
3198         }
3199     }
3200     return false;
3201 }
3202
3203 static bool
3204 ofbundle_trunks_vlan(const struct ofbundle *bundle, uint16_t vlan)
3205 {
3206     return bundle->vlan < 0 && vlan_bitmap_contains(bundle->trunks, vlan);
3207 }
3208
3209 static bool
3210 ofbundle_includes_vlan(const struct ofbundle *bundle, uint16_t vlan)
3211 {
3212     return vlan == bundle->vlan || ofbundle_trunks_vlan(bundle, vlan);
3213 }
3214
3215 /* Returns an arbitrary interface within 'bundle'. */
3216 static struct ofport_dpif *
3217 ofbundle_get_a_port(const struct ofbundle *bundle)
3218 {
3219     return CONTAINER_OF(list_front(&bundle->ports),
3220                         struct ofport_dpif, bundle_node);
3221 }
3222
3223 static void
3224 compose_dsts(struct action_xlate_ctx *ctx, uint16_t vlan,
3225              const struct ofbundle *in_bundle,
3226              const struct ofbundle *out_bundle, struct dst_set *set)
3227 {
3228     struct dst dst;
3229
3230     if (out_bundle == OFBUNDLE_FLOOD) {
3231         struct ofbundle *bundle;
3232
3233         HMAP_FOR_EACH (bundle, hmap_node, &ctx->ofproto->bundles) {
3234             if (bundle != in_bundle
3235                 && ofbundle_includes_vlan(bundle, vlan)
3236                 && bundle->floodable
3237                 && !bundle->mirror_out
3238                 && set_dst(ctx, &dst, in_bundle, bundle)) {
3239                 dst_set_add(set, &dst);
3240             }
3241         }
3242         ctx->nf_output_iface = NF_OUT_FLOOD;
3243     } else if (out_bundle && set_dst(ctx, &dst, in_bundle, out_bundle)) {
3244         dst_set_add(set, &dst);
3245         ctx->nf_output_iface = dst.port->odp_port;
3246     }
3247 }
3248
3249 static bool
3250 vlan_is_mirrored(const struct ofmirror *m, int vlan)
3251 {
3252     return vlan_bitmap_contains(m->vlans, vlan);
3253 }
3254
3255 static void
3256 compose_mirror_dsts(struct action_xlate_ctx *ctx,
3257                     uint16_t vlan, const struct ofbundle *in_bundle,
3258                     struct dst_set *set)
3259 {
3260     struct ofproto_dpif *ofproto = ctx->ofproto;
3261     mirror_mask_t mirrors;
3262     int flow_vlan;
3263     size_t i;
3264
3265     mirrors = in_bundle->src_mirrors;
3266     for (i = 0; i < set->n; i++) {
3267         mirrors |= set->dsts[i].port->bundle->dst_mirrors;
3268     }
3269
3270     if (!mirrors) {
3271         return;
3272     }
3273
3274     flow_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3275     if (flow_vlan == 0) {
3276         flow_vlan = OFP_VLAN_NONE;
3277     }
3278
3279     while (mirrors) {
3280         struct ofmirror *m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
3281         if (vlan_is_mirrored(m, vlan)) {
3282             struct dst dst;
3283
3284             if (m->out) {
3285                 if (set_dst(ctx, &dst, in_bundle, m->out)
3286                     && !dst_is_duplicate(set, &dst)) {
3287                     dst_set_add(set, &dst);
3288                 }
3289             } else {
3290                 struct ofbundle *bundle;
3291
3292                 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3293                     if (ofbundle_includes_vlan(bundle, m->out_vlan)
3294                         && set_dst(ctx, &dst, in_bundle, bundle))
3295                     {
3296                         if (bundle->vlan < 0) {
3297                             dst.vlan = m->out_vlan;
3298                         }
3299                         if (dst_is_duplicate(set, &dst)) {
3300                             continue;
3301                         }
3302
3303                         /* Use the vlan tag on the original flow instead of
3304                          * the one passed in the vlan parameter.  This ensures
3305                          * that we compare the vlan from before any implicit
3306                          * tagging tags place. This is necessary because
3307                          * dst->vlan is the final vlan, after removing implicit
3308                          * tags. */
3309                         if (bundle == in_bundle && dst.vlan == flow_vlan) {
3310                             /* Don't send out input port on same VLAN. */
3311                             continue;
3312                         }
3313                         dst_set_add(set, &dst);
3314                     }
3315                 }
3316             }
3317         }
3318         mirrors &= mirrors - 1;
3319     }
3320 }
3321
3322 static void
3323 compose_actions(struct action_xlate_ctx *ctx, uint16_t vlan,
3324                 const struct ofbundle *in_bundle,
3325                 const struct ofbundle *out_bundle)
3326 {
3327     uint16_t initial_vlan, cur_vlan;
3328     const struct dst *dst;
3329     struct dst_set set;
3330
3331     dst_set_init(&set);
3332     compose_dsts(ctx, vlan, in_bundle, out_bundle, &set);
3333     compose_mirror_dsts(ctx, vlan, in_bundle, &set);
3334
3335     /* Output all the packets we can without having to change the VLAN. */
3336     initial_vlan = vlan_tci_to_vid(ctx->flow.vlan_tci);
3337     if (initial_vlan == 0) {
3338         initial_vlan = OFP_VLAN_NONE;
3339     }
3340     for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3341         if (dst->vlan != initial_vlan) {
3342             continue;
3343         }
3344         nl_msg_put_u32(ctx->odp_actions,
3345                        ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3346     }
3347
3348     /* Then output the rest. */
3349     cur_vlan = initial_vlan;
3350     for (dst = set.dsts; dst < &set.dsts[set.n]; dst++) {
3351         if (dst->vlan == initial_vlan) {
3352             continue;
3353         }
3354         if (dst->vlan != cur_vlan) {
3355             if (dst->vlan == OFP_VLAN_NONE) {
3356                 nl_msg_put_flag(ctx->odp_actions, ODP_ACTION_ATTR_STRIP_VLAN);
3357             } else {
3358                 ovs_be16 tci;
3359                 tci = htons(dst->vlan & VLAN_VID_MASK);
3360                 tci |= ctx->flow.vlan_tci & htons(VLAN_PCP_MASK);
3361                 nl_msg_put_be16(ctx->odp_actions,
3362                                 ODP_ACTION_ATTR_SET_DL_TCI, tci);
3363             }
3364             cur_vlan = dst->vlan;
3365         }
3366         nl_msg_put_u32(ctx->odp_actions,
3367                        ODP_ACTION_ATTR_OUTPUT, dst->port->odp_port);
3368     }
3369
3370     dst_set_free(&set);
3371 }
3372
3373 /* Returns the effective vlan of a packet, taking into account both the
3374  * 802.1Q header and implicitly tagged ports.  A value of 0 indicates that
3375  * the packet is untagged and -1 indicates it has an invalid header and
3376  * should be dropped. */
3377 static int
3378 flow_get_vlan(struct ofproto_dpif *ofproto, const struct flow *flow,
3379               struct ofbundle *in_bundle, bool have_packet)
3380 {
3381     int vlan = vlan_tci_to_vid(flow->vlan_tci);
3382     if (in_bundle->vlan >= 0) {
3383         if (vlan) {
3384             if (have_packet) {
3385                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3386                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3387                              "packet received on port %s configured with "
3388                              "implicit VLAN %"PRIu16,
3389                              ofproto->up.name, vlan,
3390                              in_bundle->name, in_bundle->vlan);
3391             }
3392             return -1;
3393         }
3394         vlan = in_bundle->vlan;
3395     } else {
3396         if (!ofbundle_includes_vlan(in_bundle, vlan)) {
3397             if (have_packet) {
3398                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3399                 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
3400                              "packet received on port %s not configured for "
3401                              "trunking VLAN %d",
3402                              ofproto->up.name, vlan, in_bundle->name, vlan);
3403             }
3404             return -1;
3405         }
3406     }
3407
3408     return vlan;
3409 }
3410
3411 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
3412  * migration.  Older Citrix-patched Linux DomU used gratuitous ARP replies to
3413  * indicate this; newer upstream kernels use gratuitous ARP requests. */
3414 static bool
3415 is_gratuitous_arp(const struct flow *flow)
3416 {
3417     return (flow->dl_type == htons(ETH_TYPE_ARP)
3418             && eth_addr_is_broadcast(flow->dl_dst)
3419             && (flow->nw_proto == ARP_OP_REPLY
3420                 || (flow->nw_proto == ARP_OP_REQUEST
3421                     && flow->nw_src == flow->nw_dst)));
3422 }
3423
3424 static void
3425 update_learning_table(struct ofproto_dpif *ofproto,
3426                       const struct flow *flow, int vlan,
3427                       struct ofbundle *in_bundle)
3428 {
3429     struct mac_entry *mac;
3430
3431     if (!mac_learning_may_learn(ofproto->ml, flow->dl_src, vlan)) {
3432         return;
3433     }
3434
3435     mac = mac_learning_insert(ofproto->ml, flow->dl_src, vlan);
3436     if (is_gratuitous_arp(flow)) {
3437         /* We don't want to learn from gratuitous ARP packets that are
3438          * reflected back over bond slaves so we lock the learning table. */
3439         if (!in_bundle->bond) {
3440             mac_entry_set_grat_arp_lock(mac);
3441         } else if (mac_entry_is_grat_arp_locked(mac)) {
3442             return;
3443         }
3444     }
3445
3446     if (mac_entry_is_new(mac) || mac->port.p != in_bundle) {
3447         /* The log messages here could actually be useful in debugging,
3448          * so keep the rate limit relatively high. */
3449         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
3450         VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
3451                     "on port %s in VLAN %d",
3452                     ofproto->up.name, ETH_ADDR_ARGS(flow->dl_src),
3453                     in_bundle->name, vlan);
3454
3455         mac->port.p = in_bundle;
3456         tag_set_add(&ofproto->revalidate_set,
3457                     mac_learning_changed(ofproto->ml, mac));
3458     }
3459 }
3460
3461 /* Determines whether packets in 'flow' within 'br' should be forwarded or
3462  * dropped.  Returns true if they may be forwarded, false if they should be
3463  * dropped.
3464  *
3465  * If 'have_packet' is true, it indicates that the caller is processing a
3466  * received packet.  If 'have_packet' is false, then the caller is just
3467  * revalidating an existing flow because configuration has changed.  Either
3468  * way, 'have_packet' only affects logging (there is no point in logging errors
3469  * during revalidation).
3470  *
3471  * Sets '*in_portp' to the input port.  This will be a null pointer if
3472  * flow->in_port does not designate a known input port (in which case
3473  * is_admissible() returns false).
3474  *
3475  * When returning true, sets '*vlanp' to the effective VLAN of the input
3476  * packet, as returned by flow_get_vlan().
3477  *
3478  * May also add tags to '*tags', although the current implementation only does
3479  * so in one special case.
3480  */
3481 static bool
3482 is_admissible(struct ofproto_dpif *ofproto, const struct flow *flow,
3483               bool have_packet,
3484               tag_type *tags, int *vlanp, struct ofbundle **in_bundlep)
3485 {
3486     struct ofport_dpif *in_port;
3487     struct ofbundle *in_bundle;
3488     int vlan;
3489
3490     /* Find the port and bundle for the received packet. */
3491     in_port = get_ofp_port(ofproto, flow->in_port);
3492     *in_bundlep = in_bundle = in_port->bundle;
3493     if (!in_port || !in_bundle) {
3494         /* No interface?  Something fishy... */
3495         if (have_packet) {
3496             /* Odd.  A few possible reasons here:
3497              *
3498              * - We deleted a port but there are still a few packets queued up
3499              *   from it.
3500              *
3501              * - Someone externally added a port (e.g. "ovs-dpctl add-if") that
3502              *   we don't know about.
3503              *
3504              * - Packet arrived on the local port but the local port is not
3505              *   part of a bundle.
3506              */
3507             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3508
3509             VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
3510                          "port %"PRIu16,
3511                          ofproto->up.name, flow->in_port);
3512         }
3513         return false;
3514     }
3515     *vlanp = vlan = flow_get_vlan(ofproto, flow, in_bundle, have_packet);
3516     if (vlan < 0) {
3517         return false;
3518     }
3519
3520     /* Drop frames for reserved multicast addresses. */
3521     if (eth_addr_is_reserved(flow->dl_dst)) {
3522         return false;
3523     }
3524
3525     /* Drop frames on bundles reserved for mirroring. */
3526     if (in_bundle->mirror_out) {
3527         if (have_packet) {
3528             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3529             VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
3530                          "%s, which is reserved exclusively for mirroring",
3531                          ofproto->up.name, in_bundle->name);
3532         }
3533         return false;
3534     }
3535
3536     if (in_bundle->bond) {
3537         struct mac_entry *mac;
3538
3539         switch (bond_check_admissibility(in_bundle->bond, in_port,
3540                                          flow->dl_dst, tags)) {
3541         case BV_ACCEPT:
3542             break;
3543
3544         case BV_DROP:
3545             return false;
3546
3547         case BV_DROP_IF_MOVED:
3548             mac = mac_learning_lookup(ofproto->ml, flow->dl_src, vlan, NULL);
3549             if (mac && mac->port.p != in_bundle &&
3550                 (!is_gratuitous_arp(flow)
3551                  || mac_entry_is_grat_arp_locked(mac))) {
3552                 return false;
3553             }
3554             break;
3555         }
3556     }
3557
3558     return true;
3559 }
3560
3561 /* If the composed actions may be applied to any packet in the given 'flow',
3562  * returns true.  Otherwise, the actions should only be applied to 'packet', or
3563  * not at all, if 'packet' was NULL. */
3564 static bool
3565 xlate_normal(struct action_xlate_ctx *ctx)
3566 {
3567     struct ofbundle *in_bundle;
3568     struct ofbundle *out_bundle;
3569     struct mac_entry *mac;
3570     int vlan;
3571
3572     /* Check whether we should drop packets in this flow. */
3573     if (!is_admissible(ctx->ofproto, &ctx->flow, ctx->packet != NULL,
3574                        &ctx->tags, &vlan, &in_bundle)) {
3575         out_bundle = NULL;
3576         goto done;
3577     }
3578
3579     /* Learn source MAC (but don't try to learn from revalidation). */
3580     if (ctx->packet) {
3581         update_learning_table(ctx->ofproto, &ctx->flow, vlan, in_bundle);
3582     }
3583
3584     /* Determine output bundle. */
3585     mac = mac_learning_lookup(ctx->ofproto->ml, ctx->flow.dl_dst, vlan,
3586                               &ctx->tags);
3587     if (mac) {
3588         out_bundle = mac->port.p;
3589     } else if (!ctx->packet && !eth_addr_is_multicast(ctx->flow.dl_dst)) {
3590         /* If we are revalidating but don't have a learning entry then eject
3591          * the flow.  Installing a flow that floods packets opens up a window
3592          * of time where we could learn from a packet reflected on a bond and
3593          * blackhole packets before the learning table is updated to reflect
3594          * the correct port. */
3595         return false;
3596     } else {
3597         out_bundle = OFBUNDLE_FLOOD;
3598     }
3599
3600     /* Don't send packets out their input bundles. */
3601     if (in_bundle == out_bundle) {
3602         out_bundle = NULL;
3603     }
3604
3605 done:
3606     if (in_bundle) {
3607         compose_actions(ctx, vlan, in_bundle, out_bundle);
3608     }
3609
3610     return true;
3611 }
3612 \f
3613 static bool
3614 get_drop_frags(struct ofproto *ofproto_)
3615 {
3616     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3617     bool drop_frags;
3618
3619     dpif_get_drop_frags(ofproto->dpif, &drop_frags);
3620     return drop_frags;
3621 }
3622
3623 static void
3624 set_drop_frags(struct ofproto *ofproto_, bool drop_frags)
3625 {
3626     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3627
3628     dpif_set_drop_frags(ofproto->dpif, drop_frags);
3629 }
3630
3631 static int
3632 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3633            const struct flow *flow,
3634            const union ofp_action *ofp_actions, size_t n_ofp_actions)
3635 {
3636     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3637     int error;
3638
3639     error = validate_actions(ofp_actions, n_ofp_actions, flow,
3640                              ofproto->max_ports);
3641     if (!error) {
3642         struct action_xlate_ctx ctx;
3643         struct ofpbuf *odp_actions;
3644
3645         action_xlate_ctx_init(&ctx, ofproto, flow, packet);
3646         odp_actions = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
3647         dpif_execute(ofproto->dpif, odp_actions->data, odp_actions->size,
3648                      packet);
3649         ofpbuf_delete(odp_actions);
3650     }
3651     return error;
3652 }
3653
3654 static void
3655 get_netflow_ids(const struct ofproto *ofproto_,
3656                 uint8_t *engine_type, uint8_t *engine_id)
3657 {
3658     struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3659
3660     dpif_get_netflow_ids(ofproto->dpif, engine_type, engine_id);
3661 }
3662 \f
3663 static struct ofproto_dpif *
3664 ofproto_dpif_lookup(const char *name)
3665 {
3666     struct ofproto *ofproto = ofproto_lookup(name);
3667     return (ofproto && ofproto->ofproto_class == &ofproto_dpif_class
3668             ? ofproto_dpif_cast(ofproto)
3669             : NULL);
3670 }
3671
3672 static void
3673 ofproto_unixctl_fdb_show(struct unixctl_conn *conn,
3674                          const char *args, void *aux OVS_UNUSED)
3675 {
3676     struct ds ds = DS_EMPTY_INITIALIZER;
3677     const struct ofproto_dpif *ofproto;
3678     const struct mac_entry *e;
3679
3680     ofproto = ofproto_dpif_lookup(args);
3681     if (!ofproto) {
3682         unixctl_command_reply(conn, 501, "no such bridge");
3683         return;
3684     }
3685
3686     ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
3687     LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3688         struct ofbundle *bundle = e->port.p;
3689         ds_put_format(&ds, "%5d  %4d  "ETH_ADDR_FMT"  %3d\n",
3690                       ofbundle_get_a_port(bundle)->odp_port,
3691                       e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
3692     }
3693     unixctl_command_reply(conn, 200, ds_cstr(&ds));
3694     ds_destroy(&ds);
3695 }
3696
3697 struct ofproto_trace {
3698     struct action_xlate_ctx ctx;
3699     struct flow flow;
3700     struct ds *result;
3701 };
3702
3703 static void
3704 trace_format_rule(struct ds *result, int level, const struct rule *rule)
3705 {
3706     ds_put_char_multiple(result, '\t', level);
3707     if (!rule) {
3708         ds_put_cstr(result, "No match\n");
3709         return;
3710     }
3711
3712     ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
3713                   ntohll(rule->flow_cookie));
3714     cls_rule_format(&rule->cr, result);
3715     ds_put_char(result, '\n');
3716
3717     ds_put_char_multiple(result, '\t', level);
3718     ds_put_cstr(result, "OpenFlow ");
3719     ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
3720                       rule->n_actions * sizeof *rule->actions);
3721     ds_put_char(result, '\n');
3722 }
3723
3724 static void
3725 trace_format_flow(struct ds *result, int level, const char *title,
3726                  struct ofproto_trace *trace)
3727 {
3728     ds_put_char_multiple(result, '\t', level);
3729     ds_put_format(result, "%s: ", title);
3730     if (flow_equal(&trace->ctx.flow, &trace->flow)) {
3731         ds_put_cstr(result, "unchanged");
3732     } else {
3733         flow_format(result, &trace->ctx.flow);
3734         trace->flow = trace->ctx.flow;
3735     }
3736     ds_put_char(result, '\n');
3737 }
3738
3739 static void
3740 trace_resubmit(struct action_xlate_ctx *ctx, struct rule_dpif *rule)
3741 {
3742     struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
3743     struct ds *result = trace->result;
3744
3745     ds_put_char(result, '\n');
3746     trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
3747     trace_format_rule(result, ctx->recurse + 1, &rule->up);
3748 }
3749
3750 static void
3751 ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
3752                       void *aux OVS_UNUSED)
3753 {
3754     char *dpname, *in_port_s, *tun_id_s, *packet_s;
3755     char *args = xstrdup(args_);
3756     char *save_ptr = NULL;
3757     struct ofproto_dpif *ofproto;
3758     struct ofpbuf packet;
3759     struct rule_dpif *rule;
3760     struct ds result;
3761     struct flow flow;
3762     uint16_t in_port;
3763     ovs_be64 tun_id;
3764     char *s;
3765
3766     ofpbuf_init(&packet, strlen(args) / 2);
3767     ds_init(&result);
3768
3769     dpname = strtok_r(args, " ", &save_ptr);
3770     tun_id_s = strtok_r(NULL, " ", &save_ptr);
3771     in_port_s = strtok_r(NULL, " ", &save_ptr);
3772     packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
3773     if (!dpname || !in_port_s || !packet_s) {
3774         unixctl_command_reply(conn, 501, "Bad command syntax");
3775         goto exit;
3776     }
3777
3778     ofproto = ofproto_dpif_lookup(dpname);
3779     if (!ofproto) {
3780         unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
3781                               "for help)");
3782         goto exit;
3783     }
3784
3785     tun_id = htonll(strtoull(tun_id_s, NULL, 0));
3786     in_port = ofp_port_to_odp_port(atoi(in_port_s));
3787
3788     packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
3789     packet_s += strspn(packet_s, " ");
3790     if (*packet_s != '\0') {
3791         unixctl_command_reply(conn, 501, "Trailing garbage in command");
3792         goto exit;
3793     }
3794     if (packet.size < ETH_HEADER_LEN) {
3795         unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
3796         goto exit;
3797     }
3798
3799     ds_put_cstr(&result, "Packet: ");
3800     s = ofp_packet_to_string(packet.data, packet.size, packet.size);
3801     ds_put_cstr(&result, s);
3802     free(s);
3803
3804     flow_extract(&packet, tun_id, in_port, &flow);
3805     ds_put_cstr(&result, "Flow: ");
3806     flow_format(&result, &flow);
3807     ds_put_char(&result, '\n');
3808
3809     rule = rule_dpif_lookup(ofproto, &flow);
3810     trace_format_rule(&result, 0, &rule->up);
3811     if (rule) {
3812         struct ofproto_trace trace;
3813         struct ofpbuf *odp_actions;
3814
3815         trace.result = &result;
3816         trace.flow = flow;
3817         action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
3818         trace.ctx.resubmit_hook = trace_resubmit;
3819         odp_actions = xlate_actions(&trace.ctx,
3820                                     rule->up.actions, rule->up.n_actions);
3821
3822         ds_put_char(&result, '\n');
3823         trace_format_flow(&result, 0, "Final flow", &trace);
3824         ds_put_cstr(&result, "Datapath actions: ");
3825         format_odp_actions(&result, odp_actions->data, odp_actions->size);
3826         ofpbuf_delete(odp_actions);
3827     }
3828
3829     unixctl_command_reply(conn, 200, ds_cstr(&result));
3830
3831 exit:
3832     ds_destroy(&result);
3833     ofpbuf_uninit(&packet);
3834     free(args);
3835 }
3836
3837 static void
3838 ofproto_dpif_unixctl_init(void)
3839 {
3840     static bool registered;
3841     if (registered) {
3842         return;
3843     }
3844     registered = true;
3845
3846     unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
3847     unixctl_command_register("fdb/show", ofproto_unixctl_fdb_show, NULL);
3848 }
3849 \f
3850 const struct ofproto_class ofproto_dpif_class = {
3851     enumerate_types,
3852     enumerate_names,
3853     del,
3854     alloc,
3855     construct,
3856     destruct,
3857     dealloc,
3858     run,
3859     wait,
3860     flush,
3861     port_alloc,
3862     port_construct,
3863     port_destruct,
3864     port_dealloc,
3865     port_modified,
3866     port_reconfigured,
3867     port_query_by_name,
3868     port_add,
3869     port_del,
3870     port_dump_start,
3871     port_dump_next,
3872     port_dump_done,
3873     port_poll,
3874     port_poll_wait,
3875     port_is_lacp_current,
3876     rule_alloc,
3877     rule_construct,
3878     rule_destruct,
3879     rule_dealloc,
3880     rule_remove,
3881     rule_get_stats,
3882     rule_execute,
3883     rule_modify_actions,
3884     get_drop_frags,
3885     set_drop_frags,
3886     packet_out,
3887     set_netflow,
3888     get_netflow_ids,
3889     set_sflow,
3890     set_cfm,
3891     get_cfm,
3892     bundle_set,
3893     bundle_remove,
3894     mirror_set,
3895     set_flood_vlans,
3896     is_mirror_output_bundle,
3897 };