~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to ofproto/ofproto-dpif.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "connmgr.h"
30
30
#include "coverage.h"
31
31
#include "cfm.h"
 
32
#include "ovs-lldp.h"
32
33
#include "dpif.h"
33
34
#include "dynamic-string.h"
34
35
#include "fail-open.h"
37
38
#include "lacp.h"
38
39
#include "learn.h"
39
40
#include "mac-learning.h"
 
41
#include "mcast-snooping.h"
40
42
#include "meta-flow.h"
41
43
#include "multipath.h"
42
44
#include "netdev-vport.h"
57
59
#include "ofproto-dpif-sflow.h"
58
60
#include "ofproto-dpif-upcall.h"
59
61
#include "ofproto-dpif-xlate.h"
 
62
#include "poll-loop.h"
60
63
#include "ovs-rcu.h"
61
 
#include "poll-loop.h"
 
64
#include "ovs-router.h"
62
65
#include "seq.h"
63
66
#include "simap.h"
64
67
#include "smap.h"
67
70
#include "unaligned.h"
68
71
#include "unixctl.h"
69
72
#include "vlan-bitmap.h"
70
 
#include "vlog.h"
 
73
#include "openvswitch/vlog.h"
71
74
 
72
75
VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
73
76
 
74
77
COVERAGE_DEFINE(ofproto_dpif_expired);
75
78
COVERAGE_DEFINE(packet_in_overflow);
76
79
 
77
 
/* No bfd/cfm status change. */
78
 
#define NO_STATUS_CHANGE -1
79
 
 
80
80
struct flow_miss;
81
81
 
82
82
struct rule_dpif {
88
88
     *   recently been processed by a revalidator. */
89
89
    struct ovs_mutex stats_mutex;
90
90
    struct dpif_flow_stats stats OVS_GUARDED;
 
91
 
 
92
   /* In non-NULL, will point to a new rule (for which a reference is held) to
 
93
    * which all the stats updates should be forwarded. This exists only
 
94
    * transitionally when flows are replaced.
 
95
    *
 
96
    * Protected by stats_mutex.  If both 'rule->stats_mutex' and
 
97
    * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
 
98
    * order, */
 
99
    struct rule_dpif *new_rule OVS_GUARDED;
 
100
 
 
101
    /* If non-zero then the recirculation id that has
 
102
     * been allocated for use with this rule.
 
103
     * The recirculation id and associated internal flow should
 
104
     * be freed when the rule is freed */
 
105
    uint32_t recirc_id;
91
106
};
92
107
 
93
108
/* RULE_CAST() depends on this. */
108
123
    struct ovs_mutex stats_mutex;
109
124
    uint64_t packet_count OVS_GUARDED;  /* Number of packets received. */
110
125
    uint64_t byte_count OVS_GUARDED;    /* Number of bytes received. */
111
 
    struct bucket_counter *bucket_stats OVS_GUARDED;  /* Bucket statistics. */
112
126
};
113
127
 
114
128
struct ofbundle {
118
132
    char *name;                 /* Identifier for log messages. */
119
133
 
120
134
    /* Configuration. */
121
 
    struct list ports;          /* Contains "struct ofport"s. */
 
135
    struct ovs_list ports;      /* Contains "struct ofport"s. */
122
136
    enum port_vlan_mode vlan_mode; /* VLAN mode */
123
137
    int vlan;                   /* -1=trunk port, else a 12-bit VLAN ID. */
124
138
    unsigned long *trunks;      /* Bitmap of trunked VLANs, if 'vlan' == -1.
137
151
static void bundle_del_port(struct ofport_dpif *);
138
152
static void bundle_run(struct ofbundle *);
139
153
static void bundle_wait(struct ofbundle *);
 
154
static void bundle_flush_macs(struct ofbundle *, bool);
 
155
static void bundle_move(struct ofbundle *, struct ofbundle *);
140
156
 
141
157
static void stp_run(struct ofproto_dpif *ofproto);
142
158
static void stp_wait(struct ofproto_dpif *ofproto);
143
159
static int set_stp_port(struct ofport *,
144
160
                        const struct ofproto_port_stp_settings *);
145
161
 
 
162
static void rstp_run(struct ofproto_dpif *ofproto);
 
163
static void set_rstp_port(struct ofport *,
 
164
                         const struct ofproto_port_rstp_settings *);
 
165
 
146
166
struct ofport_dpif {
147
167
    struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
148
168
    struct ofport up;
149
169
 
150
170
    odp_port_t odp_port;
151
171
    struct ofbundle *bundle;    /* Bundle that contains this port, if any. */
152
 
    struct list bundle_node;    /* In struct ofbundle's "ports" list. */
 
172
    struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
153
173
    struct cfm *cfm;            /* Connectivity Fault Management, if any. */
154
174
    struct bfd *bfd;            /* BFD, if any. */
 
175
    struct lldp *lldp;          /* lldp, if any. */
155
176
    bool may_enable;            /* May be enabled in bonds. */
156
177
    bool is_tunnel;             /* This port is a tunnel. */
157
178
    bool is_layer3;             /* This is a layer 3 port. */
163
184
    enum stp_state stp_state;   /* Always STP_DISABLED if STP not in use. */
164
185
    long long int stp_state_entered;
165
186
 
 
187
    /* Rapid Spanning Tree. */
 
188
    struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
 
189
    enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
 
190
 
166
191
    /* Queue to DSCP mapping. */
167
192
    struct ofproto_port_queue *qdscp;
168
193
    size_t n_qdscp;
209
234
static void port_run(struct ofport_dpif *);
210
235
static int set_bfd(struct ofport *, const struct smap *);
211
236
static int set_cfm(struct ofport *, const struct cfm_settings *);
 
237
static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
212
238
static void ofport_update_peer(struct ofport_dpif *);
213
239
 
214
 
struct dpif_completion {
215
 
    struct list list_node;
216
 
    struct ofoperation *op;
217
 
};
218
 
 
219
240
/* Reasons that we might need to revalidate every datapath flow, and
220
241
 * corresponding coverage counters.
221
242
 *
227
248
enum revalidate_reason {
228
249
    REV_RECONFIGURE = 1,       /* Switch configuration changed. */
229
250
    REV_STP,                   /* Spanning tree protocol port status change. */
 
251
    REV_RSTP,                  /* RSTP port status change. */
230
252
    REV_BOND,                  /* Bonding changed. */
231
253
    REV_PORT_TOGGLED,          /* Port enabled or disabled by CFM, LACP, ...*/
232
254
    REV_FLOW_TABLE,            /* Flow table changed. */
233
255
    REV_MAC_LEARNING,          /* Mac learning changed. */
 
256
    REV_MCAST_SNOOPING,        /* Multicast snooping changed. */
234
257
};
235
258
COVERAGE_DEFINE(rev_reconfigure);
236
259
COVERAGE_DEFINE(rev_stp);
 
260
COVERAGE_DEFINE(rev_rstp);
237
261
COVERAGE_DEFINE(rev_bond);
238
262
COVERAGE_DEFINE(rev_port_toggled);
239
263
COVERAGE_DEFINE(rev_flow_table);
240
264
COVERAGE_DEFINE(rev_mac_learning);
241
 
 
242
 
/* Stores mapping between 'recirc_id' and 'ofproto-dpif'. */
243
 
struct dpif_backer_recirc_node {
244
 
    struct hmap_node hmap_node;
245
 
    struct ofproto_dpif *ofproto;
246
 
    uint32_t recirc_id;
247
 
};
 
265
COVERAGE_DEFINE(rev_mcast_snooping);
248
266
 
249
267
/* All datapaths of a given type share a single dpif backer instance. */
250
268
struct dpif_backer {
262
280
 
263
281
    bool recv_set_enable; /* Enables or disables receiving packets. */
264
282
 
265
 
    /* Recirculation. */
266
 
    struct recirc_id_pool *rid_pool;       /* Recirculation ID pool. */
267
 
    struct hmap recirc_map;         /* Map of 'recirc_id's to 'ofproto's. */
268
 
    struct ovs_mutex recirc_mutex;  /* Protects 'recirc_map'. */
269
 
    bool enable_recirc;   /* True if the datapath supports recirculation */
270
 
 
271
 
    /* True if the datapath supports variable-length
272
 
     * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
273
 
     * False if the datapath supports only 8-byte (or shorter) userdata. */
274
 
    bool variable_length_userdata;
275
 
 
276
 
    /* Maximum number of MPLS label stack entries that the datapath supports
277
 
     * in a match */
278
 
    size_t max_mpls_depth;
 
283
    /* Version string of the datapath stored in OVSDB. */
 
284
    char *dp_version_string;
 
285
 
 
286
    /* Datapath feature support. */
 
287
    struct dpif_backer_support support;
 
288
    struct atomic_count tnl_count;
279
289
};
280
290
 
281
291
/* All existing ofproto_backer instances, indexed by ofproto->up.type. */
286
296
    struct ofproto up;
287
297
    struct dpif_backer *backer;
288
298
 
 
299
    ATOMIC(cls_version_t) tables_version;  /* For classifier lookups. */
 
300
 
289
301
    uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
290
302
 
291
303
    /* Special OpenFlow rules. */
299
311
    struct dpif_ipfix *ipfix;
300
312
    struct hmap bundles;        /* Contains "struct ofbundle"s. */
301
313
    struct mac_learning *ml;
 
314
    struct mcast_snooping *ms;
302
315
    bool has_bonded_bundles;
303
316
    bool lacp_enabled;
304
317
    struct mbridge *mbridge;
311
324
    struct stp *stp;
312
325
    long long int stp_last_tick;
313
326
 
 
327
    /* Rapid Spanning Tree. */
 
328
    struct rstp *rstp;
 
329
    long long int rstp_last_tick;
 
330
 
314
331
    /* VLAN splinters. */
315
332
    struct ovs_mutex vsp_mutex;
316
333
    struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
332
349
/* All existing ofproto_dpif instances, indexed by ->up.name. */
333
350
static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
334
351
 
335
 
static void ofproto_dpif_unixctl_init(void);
 
352
static bool ofproto_use_tnl_push_pop = true;
 
353
static void ofproto_unixctl_init(void);
336
354
 
337
355
static inline struct ofproto_dpif *
338
356
ofproto_dpif_cast(const struct ofproto *ofproto)
344
362
size_t
345
363
ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
346
364
{
347
 
    return ofproto->backer->max_mpls_depth;
 
365
    return ofproto->backer->support.max_mpls_depth;
348
366
}
349
367
 
350
368
bool
351
369
ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
352
370
{
353
 
    return ofproto->backer->enable_recirc;
354
 
}
355
 
 
356
 
static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
357
 
                                        ofp_port_t ofp_port);
 
371
    return ofproto->backer->support.recirc;
 
372
}
 
373
 
 
374
bool
 
375
ofproto_dpif_get_enable_ufid(struct dpif_backer *backer)
 
376
{
 
377
    return backer->support.ufid;
 
378
}
 
379
 
358
380
static void ofproto_trace(struct ofproto_dpif *, struct flow *,
359
 
                          const struct ofpbuf *packet,
 
381
                          const struct dp_packet *packet,
360
382
                          const struct ofpact[], size_t ofpacts_len,
361
383
                          struct ds *);
362
384
 
410
432
{
411
433
    struct shash_node *node;
412
434
 
413
 
    fat_rwlock_init(&xlate_rwlock);
414
 
 
415
435
    /* Make a local copy, since we don't own 'iface_hints' elements. */
416
436
    SHASH_FOR_EACH(node, iface_hints) {
417
437
        const struct iface_hint *orig_hint = node->data;
423
443
 
424
444
        shash_add(&init_ofp_ports, node->name, new_hint);
425
445
    }
 
446
 
 
447
    ofproto_unixctl_init();
 
448
    udpif_init();
426
449
}
427
450
 
428
451
static void
501
524
        return 0;
502
525
    }
503
526
 
504
 
    dpif_run(backer->dpif);
 
527
 
 
528
    if (dpif_run(backer->dpif)) {
 
529
        backer->need_revalidate = REV_RECONFIGURE;
 
530
    }
 
531
 
 
532
    udpif_run(backer->udpif);
505
533
 
506
534
    /* If vswitchd started with other_config:flow_restore_wait set as "true",
507
535
     * and the configuration has now changed to "false", enable receiving
524
552
        udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
525
553
    }
526
554
 
 
555
    dpif_poll_threads_set(backer->dpif, n_dpdk_rxqs, pmd_cpu_mask);
 
556
 
527
557
    if (backer->need_revalidate) {
528
558
        struct ofproto_dpif *ofproto;
529
559
        struct simap_node *node;
571
601
 
572
602
                iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
573
603
                if (tnl_port_reconfigure(iter, iter->up.netdev,
574
 
                                         iter->odp_port)) {
 
604
                                         iter->odp_port,
 
605
                                         ovs_native_tunneling_is_on(ofproto), dp_port)) {
575
606
                    backer->need_revalidate = REV_RECONFIGURE;
576
607
                }
577
608
            }
583
614
        simap_destroy(&tmp_backers);
584
615
 
585
616
        switch (backer->need_revalidate) {
586
 
        case REV_RECONFIGURE:   COVERAGE_INC(rev_reconfigure);   break;
587
 
        case REV_STP:           COVERAGE_INC(rev_stp);           break;
588
 
        case REV_BOND:          COVERAGE_INC(rev_bond);          break;
589
 
        case REV_PORT_TOGGLED:  COVERAGE_INC(rev_port_toggled);  break;
590
 
        case REV_FLOW_TABLE:    COVERAGE_INC(rev_flow_table);    break;
591
 
        case REV_MAC_LEARNING:  COVERAGE_INC(rev_mac_learning);  break;
 
617
        case REV_RECONFIGURE:    COVERAGE_INC(rev_reconfigure);    break;
 
618
        case REV_STP:            COVERAGE_INC(rev_stp);            break;
 
619
        case REV_RSTP:           COVERAGE_INC(rev_rstp);           break;
 
620
        case REV_BOND:           COVERAGE_INC(rev_bond);           break;
 
621
        case REV_PORT_TOGGLED:   COVERAGE_INC(rev_port_toggled);   break;
 
622
        case REV_FLOW_TABLE:     COVERAGE_INC(rev_flow_table);     break;
 
623
        case REV_MAC_LEARNING:   COVERAGE_INC(rev_mac_learning);   break;
 
624
        case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
592
625
        }
593
626
        backer->need_revalidate = 0;
594
627
 
600
633
                continue;
601
634
            }
602
635
 
603
 
            fat_rwlock_wrlock(&xlate_rwlock);
 
636
            xlate_txn_start();
604
637
            xlate_ofproto_set(ofproto, ofproto->up.name,
605
 
                              ofproto->backer->dpif, ofproto->miss_rule,
606
 
                              ofproto->no_packet_in_rule, ofproto->ml,
607
 
                              ofproto->stp, ofproto->mbridge,
608
 
                              ofproto->sflow, ofproto->ipfix,
609
 
                              ofproto->netflow, ofproto->up.frag_handling,
 
638
                              ofproto->backer->dpif, ofproto->ml,
 
639
                              ofproto->stp, ofproto->rstp, ofproto->ms,
 
640
                              ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
 
641
                              ofproto->netflow,
610
642
                              ofproto->up.forward_bpdu,
611
643
                              connmgr_has_in_band(ofproto->up.connmgr),
612
 
                              ofproto->backer->enable_recirc,
613
 
                              ofproto->backer->variable_length_userdata,
614
 
                              ofproto->backer->max_mpls_depth);
 
644
                              &ofproto->backer->support);
615
645
 
616
646
            HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
617
647
                xlate_bundle_set(ofproto, bundle, bundle->name,
627
657
                    : -1;
628
658
                xlate_ofport_set(ofproto, ofport->bundle, ofport,
629
659
                                 ofport->up.ofp_port, ofport->odp_port,
630
 
                                 ofport->up.netdev, ofport->cfm,
631
 
                                 ofport->bfd, ofport->peer, stp_port,
632
 
                                 ofport->qdscp, ofport->n_qdscp,
633
 
                                 ofport->up.pp.config, ofport->up.pp.state,
634
 
                                 ofport->is_tunnel, ofport->may_enable);
 
660
                                 ofport->up.netdev, ofport->cfm, ofport->bfd,
 
661
                                 ofport->lldp, ofport->peer, stp_port,
 
662
                                 ofport->rstp_port, ofport->qdscp,
 
663
                                 ofport->n_qdscp, ofport->up.pp.config,
 
664
                                 ofport->up.pp.state, ofport->is_tunnel,
 
665
                                 ofport->may_enable);
635
666
            }
636
 
            fat_rwlock_unlock(&xlate_rwlock);
 
667
            xlate_txn_commit();
637
668
        }
638
669
 
639
670
        udpif_revalidate(backer->udpif);
795
826
static struct ofproto *
796
827
alloc(void)
797
828
{
798
 
    struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
 
829
    struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
799
830
    return &ofproto->up;
800
831
}
801
832
 
806
837
    free(ofproto);
807
838
}
808
839
 
809
 
/* Called when 'ofproto' is destructed.  Checks for and clears any
810
 
 * recirc_id leak. */
811
 
static void
812
 
dpif_backer_recirc_clear_ofproto(struct dpif_backer *backer,
813
 
                                 struct ofproto_dpif *ofproto)
814
 
{
815
 
    struct dpif_backer_recirc_node *node;
816
 
 
817
 
    ovs_mutex_lock(&backer->recirc_mutex);
818
 
    HMAP_FOR_EACH (node, hmap_node, &backer->recirc_map) {
819
 
        if (node->ofproto == ofproto) {
820
 
            VLOG_ERR("recirc_id %"PRIu32", not freed when ofproto (%s) "
821
 
                     "is destructed", node->recirc_id, ofproto->up.name);
822
 
            hmap_remove(&backer->recirc_map, &node->hmap_node);
823
 
            ovsrcu_postpone(free, node);
824
 
        }
825
 
    }
826
 
    ovs_mutex_unlock(&backer->recirc_mutex);
827
 
}
828
 
 
829
840
static void
830
841
close_dpif_backer(struct dpif_backer *backer)
831
842
{
841
852
    ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
842
853
    hmap_destroy(&backer->odp_to_ofport_map);
843
854
    shash_find_and_delete(&all_dpif_backers, backer->type);
844
 
    recirc_id_pool_destroy(backer->rid_pool);
845
 
    hmap_destroy(&backer->recirc_map);
846
 
    ovs_mutex_destroy(&backer->recirc_mutex);
847
855
    free(backer->type);
 
856
    free(backer->dp_version_string);
848
857
    dpif_close(backer->dpif);
849
858
    free(backer);
850
859
}
851
860
 
852
861
/* Datapath port slated for removal from datapath. */
853
862
struct odp_garbage {
854
 
    struct list list_node;
 
863
    struct ovs_list list_node;
855
864
    odp_port_t odp_port;
856
865
};
857
866
 
858
867
static bool check_variable_length_userdata(struct dpif_backer *backer);
859
 
static size_t check_max_mpls_depth(struct dpif_backer *backer);
860
 
static bool check_recirc(struct dpif_backer *backer);
 
868
static void check_support(struct dpif_backer *backer);
861
869
 
862
870
static int
863
871
open_dpif_backer(const char *type, struct dpif_backer **backerp)
866
874
    struct dpif_port_dump port_dump;
867
875
    struct dpif_port port;
868
876
    struct shash_node *node;
869
 
    struct list garbage_list;
870
 
    struct odp_garbage *garbage, *next;
 
877
    struct ovs_list garbage_list;
 
878
    struct odp_garbage *garbage;
871
879
 
872
880
    struct sset names;
873
881
    char *backer_name;
874
882
    const char *name;
875
883
    int error;
876
884
 
 
885
    recirc_init();
 
886
 
877
887
    backer = shash_find_data(&all_dpif_backers, type);
878
888
    if (backer) {
879
889
        backer->refcount++;
943
953
    }
944
954
    dpif_port_dump_done(&port_dump);
945
955
 
946
 
    LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
 
956
    LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
947
957
        dpif_port_del(backer->dpif, garbage->odp_port);
948
 
        list_remove(&garbage->list_node);
949
958
        free(garbage);
950
959
    }
951
960
 
952
961
    shash_add(&all_dpif_backers, type, backer);
953
962
 
954
 
    backer->enable_recirc = check_recirc(backer);
955
 
    backer->variable_length_userdata = check_variable_length_userdata(backer);
956
 
    backer->max_mpls_depth = check_max_mpls_depth(backer);
957
 
    backer->rid_pool = recirc_id_pool_create();
958
 
    ovs_mutex_init(&backer->recirc_mutex);
959
 
    hmap_init(&backer->recirc_map);
 
963
    check_support(backer);
 
964
    atomic_count_init(&backer->tnl_count, 0);
960
965
 
961
966
    error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
962
967
    if (error) {
970
975
        udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
971
976
    }
972
977
 
 
978
    /* This check fails if performed before udpif threads have been set,
 
979
     * as the kernel module checks that the 'pid' in userspace action
 
980
     * is non-zero. */
 
981
    backer->support.variable_length_userdata
 
982
        = check_variable_length_userdata(backer);
 
983
    backer->dp_version_string = dpif_get_dp_version(backer->dpif);
 
984
 
973
985
    return error;
974
986
}
975
987
 
 
988
bool
 
989
ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
 
990
{
 
991
    return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
 
992
           atomic_count_get(&ofproto->backer->tnl_count);
 
993
}
 
994
 
976
995
/* Tests whether 'backer''s datapath supports recirculation.  Only newer
977
996
 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys.  We need to disable some
978
997
 * features on older datapaths that don't support this feature.
986
1005
    struct flow flow;
987
1006
    struct odputil_keybuf keybuf;
988
1007
    struct ofpbuf key;
989
 
    int error;
990
 
    bool enable_recirc = false;
 
1008
    bool enable_recirc;
991
1009
 
992
1010
    memset(&flow, 0, sizeof flow);
993
1011
    flow.recirc_id = 1;
994
1012
    flow.dp_hash = 1;
995
1013
 
996
1014
    ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
997
 
    odp_flow_key_from_flow(&key, &flow, NULL, 0);
998
 
 
999
 
    error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
1000
 
                          ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL,
1001
 
                          0, NULL);
1002
 
    if (error && error != EEXIST) {
1003
 
        if (error != EINVAL) {
1004
 
            VLOG_WARN("%s: Reciculation flow probe failed (%s)",
1005
 
                      dpif_name(backer->dpif), ovs_strerror(error));
1006
 
        }
1007
 
        goto done;
1008
 
    }
1009
 
 
1010
 
    error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key),
1011
 
                          NULL);
1012
 
    if (error) {
1013
 
        VLOG_WARN("%s: failed to delete recirculation feature probe flow",
1014
 
                  dpif_name(backer->dpif));
1015
 
    }
1016
 
 
1017
 
    enable_recirc = true;
1018
 
 
1019
 
done:
 
1015
    odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
 
1016
    enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
 
1017
                                       NULL);
 
1018
 
1020
1019
    if (enable_recirc) {
1021
1020
        VLOG_INFO("%s: Datapath supports recirculation",
1022
1021
                  dpif_name(backer->dpif));
1028
1027
    return enable_recirc;
1029
1028
}
1030
1029
 
 
1030
/* Tests whether 'dpif' supports unique flow ids. We can skip serializing
 
1031
 * some flow attributes for datapaths that support this feature.
 
1032
 *
 
1033
 * Returns true if 'dpif' supports UFID for flow operations.
 
1034
 * Returns false if  'dpif' does not support UFID. */
 
1035
static bool
 
1036
check_ufid(struct dpif_backer *backer)
 
1037
{
 
1038
    struct flow flow;
 
1039
    struct odputil_keybuf keybuf;
 
1040
    struct ofpbuf key;
 
1041
    ovs_u128 ufid;
 
1042
    bool enable_ufid;
 
1043
 
 
1044
    memset(&flow, 0, sizeof flow);
 
1045
    flow.dl_type = htons(0x1234);
 
1046
 
 
1047
    ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
 
1048
    odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
 
1049
    dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
 
1050
 
 
1051
    enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
 
1052
 
 
1053
    if (enable_ufid) {
 
1054
        VLOG_INFO("%s: Datapath supports unique flow ids",
 
1055
                  dpif_name(backer->dpif));
 
1056
    } else {
 
1057
        VLOG_INFO("%s: Datapath does not support unique flow ids",
 
1058
                  dpif_name(backer->dpif));
 
1059
    }
 
1060
    return enable_ufid;
 
1061
}
 
1062
 
1031
1063
/* Tests whether 'backer''s datapath supports variable-length
1032
1064
 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.  We need
1033
1065
 * to disable some features on older datapaths that don't support this
1042
1074
    struct eth_header *eth;
1043
1075
    struct ofpbuf actions;
1044
1076
    struct dpif_execute execute;
1045
 
    struct ofpbuf packet;
 
1077
    struct dp_packet packet;
1046
1078
    size_t start;
1047
1079
    int error;
1048
1080
 
1061
1093
    nl_msg_end_nested(&actions, start);
1062
1094
 
1063
1095
    /* Compose a dummy ethernet packet. */
1064
 
    ofpbuf_init(&packet, ETH_HEADER_LEN);
1065
 
    eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
 
1096
    dp_packet_init(&packet, ETH_HEADER_LEN);
 
1097
    eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1066
1098
    eth->eth_type = htons(0x1234);
1067
1099
 
1068
1100
    /* Execute the actions.  On older datapaths this fails with ERANGE, on
1069
1101
     * newer datapaths it succeeds. */
1070
 
    execute.actions = ofpbuf_data(&actions);
1071
 
    execute.actions_len = ofpbuf_size(&actions);
 
1102
    execute.actions = actions.data;
 
1103
    execute.actions_len = actions.size;
1072
1104
    execute.packet = &packet;
1073
 
    execute.md = PKT_METADATA_INITIALIZER(0);
1074
1105
    execute.needs_help = false;
 
1106
    execute.probe = true;
1075
1107
 
1076
1108
    error = dpif_execute(backer->dpif, &execute);
1077
1109
 
1078
 
    ofpbuf_uninit(&packet);
 
1110
    dp_packet_uninit(&packet);
1079
1111
    ofpbuf_uninit(&actions);
1080
1112
 
1081
1113
    switch (error) {
1115
1147
    for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1116
1148
        struct odputil_keybuf keybuf;
1117
1149
        struct ofpbuf key;
1118
 
        int error;
1119
1150
 
1120
1151
        memset(&flow, 0, sizeof flow);
1121
1152
        flow.dl_type = htons(ETH_TYPE_MPLS);
1122
1153
        flow_set_mpls_bos(&flow, n, 1);
1123
1154
 
1124
1155
        ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1125
 
        odp_flow_key_from_flow(&key, &flow, NULL, 0);
1126
 
 
1127
 
        error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
1128
 
                              ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL, 0, NULL);
1129
 
        if (error && error != EEXIST) {
1130
 
            if (error != EINVAL) {
1131
 
                VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1132
 
                          dpif_name(backer->dpif), ovs_strerror(error));
1133
 
            }
 
1156
        odp_flow_key_from_flow(&key, &flow, NULL, 0, false);
 
1157
        if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1134
1158
            break;
1135
1159
        }
1136
 
 
1137
 
        error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key), NULL);
1138
 
        if (error) {
1139
 
            VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1140
 
                      dpif_name(backer->dpif));
1141
 
        }
1142
1160
    }
1143
1161
 
1144
1162
    VLOG_INFO("%s: MPLS label stack length probed as %d",
1146
1164
    return n;
1147
1165
}
1148
1166
 
 
1167
/* Tests whether 'backer''s datapath supports masked data in
 
1168
 * OVS_ACTION_ATTR_SET actions.  We need to disable some features on older
 
1169
 * datapaths that don't support this feature. */
 
1170
static bool
 
1171
check_masked_set_action(struct dpif_backer *backer)
 
1172
{
 
1173
    struct eth_header *eth;
 
1174
    struct ofpbuf actions;
 
1175
    struct dpif_execute execute;
 
1176
    struct dp_packet packet;
 
1177
    int error;
 
1178
    struct ovs_key_ethernet key, mask;
 
1179
 
 
1180
    /* Compose a set action that will cause an EINVAL error on older
 
1181
     * datapaths that don't support masked set actions.
 
1182
     * Avoid using a full mask, as it could be translated to a non-masked
 
1183
     * set action instead. */
 
1184
    ofpbuf_init(&actions, 64);
 
1185
    memset(&key, 0x53, sizeof key);
 
1186
    memset(&mask, 0x7f, sizeof mask);
 
1187
    commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
 
1188
                             sizeof key);
 
1189
 
 
1190
    /* Compose a dummy ethernet packet. */
 
1191
    dp_packet_init(&packet, ETH_HEADER_LEN);
 
1192
    eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
 
1193
    eth->eth_type = htons(0x1234);
 
1194
 
 
1195
    /* Execute the actions.  On older datapaths this fails with EINVAL, on
 
1196
     * newer datapaths it succeeds. */
 
1197
    execute.actions = actions.data;
 
1198
    execute.actions_len = actions.size;
 
1199
    execute.packet = &packet;
 
1200
    execute.needs_help = false;
 
1201
    execute.probe = true;
 
1202
 
 
1203
    error = dpif_execute(backer->dpif, &execute);
 
1204
 
 
1205
    dp_packet_uninit(&packet);
 
1206
    ofpbuf_uninit(&actions);
 
1207
 
 
1208
    if (error) {
 
1209
        /* Masked set action is not supported. */
 
1210
        VLOG_INFO("%s: datapath does not support masked set action feature.",
 
1211
                  dpif_name(backer->dpif));
 
1212
    }
 
1213
    return !error;
 
1214
}
 
1215
 
 
1216
static void
 
1217
check_support(struct dpif_backer *backer)
 
1218
{
 
1219
    /* This feature needs to be tested after udpif threads are set. */
 
1220
    backer->support.variable_length_userdata = false;
 
1221
 
 
1222
    backer->support.recirc = check_recirc(backer);
 
1223
    backer->support.max_mpls_depth = check_max_mpls_depth(backer);
 
1224
    backer->support.masked_set_action = check_masked_set_action(backer);
 
1225
    backer->support.ufid = check_ufid(backer);
 
1226
    backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
 
1227
}
 
1228
 
1149
1229
static int
1150
1230
construct(struct ofproto *ofproto_)
1151
1231
{
1153
1233
    struct shash_node *node, *next;
1154
1234
    int error;
1155
1235
 
 
1236
    /* Tunnel module can get used right after the udpif threads are running. */
 
1237
    ofproto_tunnel_init();
 
1238
 
1156
1239
    error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1157
1240
    if (error) {
1158
1241
        return error;
1159
1242
    }
1160
1243
 
 
1244
    atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1161
1245
    ofproto->netflow = NULL;
1162
1246
    ofproto->sflow = NULL;
1163
1247
    ofproto->ipfix = NULL;
1164
1248
    ofproto->stp = NULL;
 
1249
    ofproto->rstp = NULL;
1165
1250
    ofproto->dump_seq = 0;
1166
1251
    hmap_init(&ofproto->bundles);
1167
1252
    ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
 
1253
    ofproto->ms = NULL;
1168
1254
    ofproto->mbridge = mbridge_create();
1169
1255
    ofproto->has_bonded_bundles = false;
1170
1256
    ofproto->lacp_enabled = false;
1173
1259
 
1174
1260
    guarded_list_init(&ofproto->pins);
1175
1261
 
1176
 
    ofproto_dpif_unixctl_init();
1177
 
 
1178
1262
    hmap_init(&ofproto->vlandev_map);
1179
1263
    hmap_init(&ofproto->realdev_vid_map);
1180
1264
 
1226
1310
    match_init_catchall(&match);
1227
1311
    match_set_reg(&match, 0, id);
1228
1312
 
1229
 
    error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, ofpacts, &rule);
 
1313
    error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
 
1314
                                           &rule);
1230
1315
    *rulep = error ? NULL : rule_dpif_cast(rule);
1231
1316
 
1232
1317
    return error;
1239
1324
    uint64_t ofpacts_stub[128 / 8];
1240
1325
    struct ofpbuf ofpacts;
1241
1326
    struct rule *unused_rulep OVS_UNUSED;
1242
 
    struct ofpact_resubmit *resubmit;
1243
1327
    struct match match;
1244
1328
    int error;
1245
1329
    int id;
1280
1364
    ofpbuf_clear(&ofpacts);
1281
1365
    match_init_catchall(&match);
1282
1366
    match_set_recirc_id(&match, 0);
1283
 
    error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, &ofpacts,
1284
 
                                           &unused_rulep);
1285
 
    if (error) {
1286
 
        return error;
1287
 
    }
1288
 
 
1289
 
    /* Continue rule lookups for not-matched recirc rules from table 0.
1290
 
     *
1291
 
     * (priority=1), actions=resubmit(, 0)
1292
 
     */
1293
 
    resubmit = ofpact_put_RESUBMIT(&ofpacts);
1294
 
    resubmit->ofpact.compat = 0;
1295
 
    resubmit->in_port = OFPP_IN_PORT;
1296
 
    resubmit->table_id = 0;
1297
 
 
1298
 
    match_init_catchall(&match);
1299
 
 
1300
 
    error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, &ofpacts,
1301
 
                                           &unused_rulep);
1302
 
 
 
1367
    error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
 
1368
                                           &unused_rulep);
1303
1369
    return error;
1304
1370
}
1305
1371
 
1307
1373
destruct(struct ofproto *ofproto_)
1308
1374
{
1309
1375
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1310
 
    struct rule_dpif *rule, *next_rule;
1311
 
    struct ofproto_packet_in *pin, *next_pin;
 
1376
    struct ofproto_packet_in *pin;
 
1377
    struct rule_dpif *rule;
1312
1378
    struct oftable *table;
1313
 
    struct list pins;
 
1379
    struct ovs_list pins;
1314
1380
 
1315
1381
    ofproto->backer->need_revalidate = REV_RECONFIGURE;
1316
 
    fat_rwlock_wrlock(&xlate_rwlock);
 
1382
    xlate_txn_start();
1317
1383
    xlate_remove_ofproto(ofproto);
1318
 
    fat_rwlock_unlock(&xlate_rwlock);
 
1384
    xlate_txn_commit();
1319
1385
 
1320
1386
    /* Ensure that the upcall processing threads have no remaining references
1321
1387
     * to the ofproto or anything in it. */
1324
1390
    hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1325
1391
 
1326
1392
    OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1327
 
        struct cls_cursor cursor;
1328
 
 
1329
 
        fat_rwlock_rdlock(&table->cls.rwlock);
1330
 
        cls_cursor_init(&cursor, &table->cls, NULL);
1331
 
        fat_rwlock_unlock(&table->cls.rwlock);
1332
 
        CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
 
1393
        CLS_FOR_EACH (rule, up.cr, &table->cls) {
1333
1394
            ofproto_rule_delete(&ofproto->up, &rule->up);
1334
1395
        }
1335
1396
    }
 
1397
    ofproto_group_delete_all(&ofproto->up);
1336
1398
 
1337
1399
    guarded_list_pop_all(&ofproto->pins, &pins);
1338
 
    LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1339
 
        list_remove(&pin->list_node);
 
1400
    LIST_FOR_EACH_POP (pin, list_node, &pins) {
1340
1401
        free(CONST_CAST(void *, pin->up.packet));
1341
1402
        free(pin);
1342
1403
    }
1343
1404
    guarded_list_destroy(&ofproto->pins);
1344
1405
 
1345
 
    dpif_backer_recirc_clear_ofproto(ofproto->backer, ofproto);
 
1406
    recirc_free_ofproto(ofproto, ofproto->up.name);
1346
1407
 
1347
1408
    mbridge_unref(ofproto->mbridge);
1348
1409
 
1349
1410
    netflow_unref(ofproto->netflow);
1350
1411
    dpif_sflow_unref(ofproto->sflow);
 
1412
    dpif_ipfix_unref(ofproto->ipfix);
1351
1413
    hmap_destroy(&ofproto->bundles);
1352
1414
    mac_learning_unref(ofproto->ml);
 
1415
    mcast_snooping_unref(ofproto->ms);
1353
1416
 
1354
1417
    hmap_destroy(&ofproto->vlandev_map);
1355
1418
    hmap_destroy(&ofproto->realdev_vid_map);
1377
1440
        ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1378
1441
        mac_learning_flush(ofproto->ml);
1379
1442
        ovs_rwlock_unlock(&ofproto->ml->rwlock);
 
1443
        mcast_snooping_mdb_flush(ofproto->ms);
1380
1444
    }
1381
1445
 
1382
1446
    /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1388
1452
    /* Do not perform any periodic activity required by 'ofproto' while
1389
1453
     * waiting for flow restore to complete. */
1390
1454
    if (!ofproto_get_flow_restore_wait()) {
1391
 
        struct ofproto_packet_in *pin, *next_pin;
1392
 
        struct list pins;
 
1455
        struct ofproto_packet_in *pin;
 
1456
        struct ovs_list pins;
1393
1457
 
1394
1458
        guarded_list_pop_all(&ofproto->pins, &pins);
1395
 
        LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
 
1459
        LIST_FOR_EACH_POP (pin, list_node, &pins) {
1396
1460
            connmgr_send_packet_in(ofproto->up.connmgr, pin);
1397
 
            list_remove(&pin->list_node);
1398
1461
            free(CONST_CAST(void *, pin->up.packet));
1399
1462
            free(pin);
1400
1463
        }
1429
1492
    }
1430
1493
 
1431
1494
    stp_run(ofproto);
 
1495
    rstp_run(ofproto);
1432
1496
    ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1433
1497
    if (mac_learning_run(ofproto->ml)) {
1434
1498
        ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1435
1499
    }
1436
1500
    ovs_rwlock_unlock(&ofproto->ml->rwlock);
1437
1501
 
 
1502
    if (mcast_snooping_run(ofproto->ms)) {
 
1503
        ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
 
1504
    }
 
1505
 
1438
1506
    new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1439
1507
    if (ofproto->dump_seq != new_dump_seq) {
1440
1508
        struct rule *rule, *next_rule;
1464
1532
            }
1465
1533
        }
1466
1534
    }
1467
 
 
1468
1535
    return 0;
1469
1536
}
1470
1537
 
1496
1563
    ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1497
1564
    mac_learning_wait(ofproto->ml);
1498
1565
    ovs_rwlock_unlock(&ofproto->ml->rwlock);
 
1566
    mcast_snooping_wait(ofproto->ms);
1499
1567
    stp_wait(ofproto);
1500
1568
    if (ofproto->backer->need_revalidate) {
1501
1569
        /* Shouldn't happen, but if it does just go around again. */
1530
1598
}
1531
1599
 
1532
1600
static void
1533
 
get_features(struct ofproto *ofproto_ OVS_UNUSED,
1534
 
             bool *arp_match_ip, enum ofputil_action_bitmap *actions)
 
1601
query_tables(struct ofproto *ofproto,
 
1602
             struct ofputil_table_features *features,
 
1603
             struct ofputil_table_stats *stats)
1535
1604
{
1536
 
    *arp_match_ip = true;
1537
 
    *actions = (OFPUTIL_A_OUTPUT |
1538
 
                OFPUTIL_A_SET_VLAN_VID |
1539
 
                OFPUTIL_A_SET_VLAN_PCP |
1540
 
                OFPUTIL_A_STRIP_VLAN |
1541
 
                OFPUTIL_A_SET_DL_SRC |
1542
 
                OFPUTIL_A_SET_DL_DST |
1543
 
                OFPUTIL_A_SET_NW_SRC |
1544
 
                OFPUTIL_A_SET_NW_DST |
1545
 
                OFPUTIL_A_SET_NW_TOS |
1546
 
                OFPUTIL_A_SET_TP_SRC |
1547
 
                OFPUTIL_A_SET_TP_DST |
1548
 
                OFPUTIL_A_ENQUEUE);
 
1605
    strcpy(features->name, "classifier");
 
1606
 
 
1607
    if (stats) {
 
1608
        int i;
 
1609
 
 
1610
        for (i = 0; i < ofproto->n_tables; i++) {
 
1611
            unsigned long missed, matched;
 
1612
 
 
1613
            atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
 
1614
            atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
 
1615
 
 
1616
            stats[i].matched_count = matched;
 
1617
            stats[i].lookup_count = matched + missed;
 
1618
        }
 
1619
    }
1549
1620
}
1550
1621
 
1551
1622
static void
1552
 
get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
 
1623
set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1553
1624
{
1554
1625
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1555
 
    struct dpif_dp_stats s;
1556
 
    uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1557
 
    uint64_t n_lookup;
1558
 
    long long int used;
1559
 
 
1560
 
    strcpy(ots->name, "classifier");
1561
 
 
1562
 
    dpif_get_dp_stats(ofproto->backer->dpif, &s);
1563
 
    rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1564
 
    rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1565
 
                   &used);
1566
 
    rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1567
 
                   &used);
1568
 
    n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1569
 
    ots->lookup_count = htonll(n_lookup);
1570
 
    ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
 
1626
 
 
1627
    atomic_store_relaxed(&ofproto->tables_version, version);
1571
1628
}
1572
1629
 
 
1630
 
1573
1631
static struct ofport *
1574
1632
port_alloc(void)
1575
1633
{
1576
 
    struct ofport_dpif *port = xmalloc(sizeof *port);
 
1634
    struct ofport_dpif *port = xzalloc(sizeof *port);
1577
1635
    return &port->up;
1578
1636
}
1579
1637
 
1591
1649
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1592
1650
    const struct netdev *netdev = port->up.netdev;
1593
1651
    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
 
1652
    const char *dp_port_name;
1594
1653
    struct dpif_port dpif_port;
1595
1654
    int error;
1596
1655
 
1598
1657
    port->bundle = NULL;
1599
1658
    port->cfm = NULL;
1600
1659
    port->bfd = NULL;
1601
 
    port->may_enable = true;
 
1660
    port->lldp = NULL;
 
1661
    port->may_enable = false;
1602
1662
    port->stp_port = NULL;
1603
1663
    port->stp_state = STP_DISABLED;
 
1664
    port->rstp_port = NULL;
 
1665
    port->rstp_state = RSTP_DISABLED;
1604
1666
    port->is_tunnel = false;
1605
1667
    port->peer = NULL;
1606
1668
    port->qdscp = NULL;
1612
1674
 
1613
1675
    if (netdev_vport_is_patch(netdev)) {
1614
1676
        /* By bailing out here, we don't submit the port to the sFlow module
1615
 
         * to be considered for counter polling export.  This is correct
1616
 
         * because the patch port represents an interface that sFlow considers
1617
 
         * to be "internal" to the switch as a whole, and therefore not an
1618
 
         * candidate for counter polling. */
 
1677
         * to be considered for counter polling export.  This is correct
 
1678
         * because the patch port represents an interface that sFlow considers
 
1679
         * to be "internal" to the switch as a whole, and therefore not a
 
1680
         * candidate for counter polling. */
1619
1681
        port->odp_port = ODPP_NONE;
1620
1682
        ofport_update_peer(port);
1621
1683
        return 0;
1622
1684
    }
1623
1685
 
1624
 
    error = dpif_port_query_by_name(ofproto->backer->dpif,
1625
 
                                    netdev_vport_get_dpif_port(netdev, namebuf,
1626
 
                                                               sizeof namebuf),
 
1686
    dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
 
1687
    error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1627
1688
                                    &dpif_port);
1628
1689
    if (error) {
1629
1690
        return error;
1632
1693
    port->odp_port = dpif_port.port_no;
1633
1694
 
1634
1695
    if (netdev_get_tunnel_config(netdev)) {
1635
 
        tnl_port_add(port, port->up.netdev, port->odp_port);
 
1696
        atomic_count_inc(&ofproto->backer->tnl_count);
 
1697
        error = tnl_port_add(port, port->up.netdev, port->odp_port,
 
1698
                             ovs_native_tunneling_is_on(ofproto), dp_port_name);
 
1699
        if (error) {
 
1700
            atomic_count_dec(&ofproto->backer->tnl_count);
 
1701
            dpif_port_destroy(&dpif_port);
 
1702
            return error;
 
1703
        }
 
1704
 
1636
1705
        port->is_tunnel = true;
 
1706
        if (ofproto->ipfix) {
 
1707
           dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
 
1708
        }
1637
1709
    } else {
1638
1710
        /* Sanity-check that a mapping doesn't already exist.  This
1639
1711
         * shouldn't happen for non-tunnel ports. */
1668
1740
    const char *dp_port_name;
1669
1741
 
1670
1742
    ofproto->backer->need_revalidate = REV_RECONFIGURE;
1671
 
    fat_rwlock_wrlock(&xlate_rwlock);
 
1743
    xlate_txn_start();
1672
1744
    xlate_ofport_remove(port);
1673
 
    fat_rwlock_unlock(&xlate_rwlock);
 
1745
    xlate_txn_commit();
1674
1746
 
1675
1747
    dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1676
1748
                                              sizeof namebuf);
1695
1767
        ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1696
1768
    }
1697
1769
 
 
1770
    if (port->is_tunnel) {
 
1771
        atomic_count_dec(&ofproto->backer->tnl_count);
 
1772
    }
 
1773
 
 
1774
    if (port->is_tunnel && ofproto->ipfix) {
 
1775
       dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
 
1776
    }
 
1777
 
1698
1778
    tnl_port_del(port);
1699
1779
    sset_find_and_delete(&ofproto->ports, devname);
1700
1780
    sset_find_and_delete(&ofproto->ghost_ports, devname);
1701
1781
    bundle_remove(port_);
1702
1782
    set_cfm(port_, NULL);
1703
1783
    set_bfd(port_, NULL);
 
1784
    set_lldp(port_, NULL);
1704
1785
    if (port->stp_port) {
1705
1786
        stp_port_disable(port->stp_port);
1706
1787
    }
 
1788
    set_rstp_port(port_, NULL);
1707
1789
    if (ofproto->sflow) {
1708
1790
        dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1709
1791
    }
1715
1797
port_modified(struct ofport *port_)
1716
1798
{
1717
1799
    struct ofport_dpif *port = ofport_dpif_cast(port_);
 
1800
    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
 
1801
    const char *dp_port_name;
 
1802
    struct netdev *netdev = port->up.netdev;
1718
1803
 
1719
1804
    if (port->bundle && port->bundle->bond) {
1720
 
        bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
 
1805
        bond_slave_set_netdev(port->bundle->bond, port, netdev);
1721
1806
    }
1722
1807
 
1723
1808
    if (port->cfm) {
1724
 
        cfm_set_netdev(port->cfm, port->up.netdev);
 
1809
        cfm_set_netdev(port->cfm, netdev);
1725
1810
    }
1726
1811
 
1727
1812
    if (port->bfd) {
1728
 
        bfd_set_netdev(port->bfd, port->up.netdev);
 
1813
        bfd_set_netdev(port->bfd, netdev);
1729
1814
    }
1730
1815
 
1731
1816
    ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1732
 
                                     port->up.pp.hw_addr);
1733
 
 
1734
 
    if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1735
 
                                                port->odp_port)) {
1736
 
        ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1737
 
            REV_RECONFIGURE;
 
1817
                                     port->lldp, port->up.pp.hw_addr);
 
1818
 
 
1819
    dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
 
1820
 
 
1821
    if (port->is_tunnel) {
 
1822
        struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
 
1823
 
 
1824
        if (tnl_port_reconfigure(port, netdev, port->odp_port,
 
1825
                                 ovs_native_tunneling_is_on(ofproto),
 
1826
                                 dp_port_name)) {
 
1827
            ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
1828
        }
1738
1829
    }
1739
1830
 
1740
1831
    ofport_update_peer(port);
1799
1890
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1800
1891
    struct dpif_ipfix *di = ofproto->ipfix;
1801
1892
    bool has_options = bridge_exporter_options || flow_exporters_options;
 
1893
    bool new_di = false;
1802
1894
 
1803
1895
    if (has_options && !di) {
1804
1896
        di = ofproto->ipfix = dpif_ipfix_create();
 
1897
        new_di = true;
1805
1898
    }
1806
1899
 
1807
1900
    if (di) {
1811
1904
            di, bridge_exporter_options, flow_exporters_options,
1812
1905
            n_flow_exporters_options);
1813
1906
 
 
1907
        /* Add tunnel ports only when a new ipfix created */
 
1908
        if (new_di == true) {
 
1909
            struct ofport_dpif *ofport;
 
1910
            HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
 
1911
                if (ofport->is_tunnel == true) {
 
1912
                    dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
 
1913
                }
 
1914
            }
 
1915
        }
 
1916
 
1814
1917
        if (!has_options) {
1815
1918
            dpif_ipfix_unref(di);
1816
1919
            ofproto->ipfix = NULL;
1847
1950
        ofproto->backer->need_revalidate = REV_RECONFIGURE;
1848
1951
    }
1849
1952
    ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1850
 
                                     ofport->up.pp.hw_addr);
 
1953
                                     ofport->lldp, ofport->up.pp.hw_addr);
1851
1954
    return error;
1852
1955
}
1853
1956
 
 
1957
static bool
 
1958
cfm_status_changed(struct ofport *ofport_)
 
1959
{
 
1960
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
1961
 
 
1962
    return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
 
1963
}
 
1964
 
1854
1965
static int
1855
 
get_cfm_status(const struct ofport *ofport_, bool force,
1856
 
               struct ofproto_cfm_status *status)
 
1966
get_cfm_status(const struct ofport *ofport_,
 
1967
               struct cfm_status *status)
1857
1968
{
1858
1969
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1859
1970
    int ret = 0;
1860
1971
 
1861
1972
    if (ofport->cfm) {
1862
 
        if (cfm_check_status_change(ofport->cfm) || force) {
1863
 
            status->faults = cfm_get_fault(ofport->cfm);
1864
 
            status->flap_count = cfm_get_flap_count(ofport->cfm);
1865
 
            status->remote_opstate = cfm_get_opup(ofport->cfm);
1866
 
            status->health = cfm_get_health(ofport->cfm);
1867
 
            cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1868
 
        } else {
1869
 
            ret = NO_STATUS_CHANGE;
1870
 
        }
 
1973
        cfm_get_status(ofport->cfm, status);
1871
1974
    } else {
1872
1975
        ret = ENOENT;
1873
1976
    }
1889
1992
        ofproto->backer->need_revalidate = REV_RECONFIGURE;
1890
1993
    }
1891
1994
    ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1892
 
                                     ofport->up.pp.hw_addr);
 
1995
                                     ofport->lldp, ofport->up.pp.hw_addr);
1893
1996
    return 0;
1894
1997
}
1895
1998
 
 
1999
static bool
 
2000
bfd_status_changed(struct ofport *ofport_)
 
2001
{
 
2002
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
2003
 
 
2004
    return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
 
2005
}
 
2006
 
1896
2007
static int
1897
 
get_bfd_status(struct ofport *ofport_, bool force, struct smap *smap)
 
2008
get_bfd_status(struct ofport *ofport_, struct smap *smap)
1898
2009
{
1899
2010
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1900
2011
    int ret = 0;
1901
2012
 
1902
2013
    if (ofport->bfd) {
1903
 
        if (bfd_check_status_change(ofport->bfd) || force) {
1904
 
            bfd_get_status(ofport->bfd, smap);
1905
 
        } else {
1906
 
            ret = NO_STATUS_CHANGE;
1907
 
        }
 
2014
        bfd_get_status(ofport->bfd, smap);
1908
2015
    } else {
1909
2016
        ret = ENOENT;
1910
2017
    }
1911
2018
 
1912
2019
    return ret;
1913
2020
}
 
2021
 
 
2022
static int
 
2023
set_lldp(struct ofport *ofport_,
 
2024
         const struct smap *cfg)
 
2025
{
 
2026
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
2027
    int error = 0;
 
2028
 
 
2029
    if (cfg) {
 
2030
        if (!ofport->lldp) {
 
2031
            struct ofproto_dpif *ofproto;
 
2032
 
 
2033
            ofproto = ofproto_dpif_cast(ofport->up.ofproto);
 
2034
            ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
2035
            ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
 
2036
        }
 
2037
 
 
2038
        if (!lldp_configure(ofport->lldp, cfg)) {
 
2039
            error = EINVAL;
 
2040
        }
 
2041
    }
 
2042
    if (error) {
 
2043
        lldp_unref(ofport->lldp);
 
2044
        ofport->lldp = NULL;
 
2045
    }
 
2046
 
 
2047
    ofproto_dpif_monitor_port_update(ofport,
 
2048
                                     ofport->bfd,
 
2049
                                     ofport->cfm,
 
2050
                                     ofport->lldp,
 
2051
                                     ofport->up.pp.hw_addr);
 
2052
    return error;
 
2053
}
 
2054
 
 
2055
static bool
 
2056
get_lldp_status(const struct ofport *ofport_,
 
2057
               struct lldp_status *status OVS_UNUSED)
 
2058
{
 
2059
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
2060
 
 
2061
    return ofport->lldp ? true : false;
 
2062
}
 
2063
 
 
2064
static int
 
2065
set_aa(struct ofproto *ofproto OVS_UNUSED,
 
2066
       const struct aa_settings *s)
 
2067
{
 
2068
    return aa_configure(s);
 
2069
}
 
2070
 
 
2071
static int
 
2072
aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
 
2073
               const struct aa_mapping_settings *s)
 
2074
{
 
2075
    return aa_mapping_register(aux, s);
 
2076
}
 
2077
 
 
2078
static int
 
2079
aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
 
2080
{
 
2081
    return aa_mapping_unregister(aux);
 
2082
}
 
2083
 
 
2084
static int
 
2085
aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
 
2086
{
 
2087
    return aa_get_vlan_queued(list);
 
2088
}
 
2089
 
 
2090
static unsigned int
 
2091
aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
 
2092
{
 
2093
    return aa_get_vlan_queue_size();
 
2094
}
 
2095
 
1914
2096
 
1915
2097
/* Spanning Tree. */
1916
2098
 
1917
 
static void
1918
 
send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
 
2099
/* Called while rstp_mutex is held. */
 
2100
static void
 
2101
rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
 
2102
{
 
2103
    struct ofproto_dpif *ofproto = ofproto_;
 
2104
    struct ofport_dpif *ofport = ofport_;
 
2105
    struct eth_header *eth = dp_packet_l2(pkt);
 
2106
 
 
2107
    netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
 
2108
    if (eth_addr_is_zero(eth->eth_src)) {
 
2109
        VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
 
2110
                     "does not have a configured source MAC address.",
 
2111
                     ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
 
2112
    } else {
 
2113
        ofproto_dpif_send_packet(ofport, pkt);
 
2114
    }
 
2115
    dp_packet_delete(pkt);
 
2116
}
 
2117
 
 
2118
static void
 
2119
send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
1919
2120
{
1920
2121
    struct ofproto_dpif *ofproto = ofproto_;
1921
2122
    struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1926
2127
        VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1927
2128
                     ofproto->up.name, port_num);
1928
2129
    } else {
1929
 
        struct eth_header *eth = ofpbuf_l2(pkt);
 
2130
        struct eth_header *eth = dp_packet_l2(pkt);
1930
2131
 
1931
2132
        netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1932
2133
        if (eth_addr_is_zero(eth->eth_src)) {
1936
2137
            ofproto_dpif_send_packet(ofport, pkt);
1937
2138
        }
1938
2139
    }
1939
 
    ofpbuf_delete(pkt);
 
2140
    dp_packet_delete(pkt);
 
2141
}
 
2142
 
 
2143
/* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
 
2144
static void
 
2145
set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
 
2146
{
 
2147
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
 
2148
 
 
2149
    /* Only revalidate flows if the configuration changed. */
 
2150
    if (!s != !ofproto->rstp) {
 
2151
        ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
2152
    }
 
2153
 
 
2154
    if (s) {
 
2155
        if (!ofproto->rstp) {
 
2156
            ofproto->rstp = rstp_create(ofproto_->name, s->address,
 
2157
                                        rstp_send_bpdu_cb, ofproto);
 
2158
            ofproto->rstp_last_tick = time_msec();
 
2159
        }
 
2160
        rstp_set_bridge_address(ofproto->rstp, s->address);
 
2161
        rstp_set_bridge_priority(ofproto->rstp, s->priority);
 
2162
        rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
 
2163
        rstp_set_bridge_force_protocol_version(ofproto->rstp,
 
2164
                                               s->force_protocol_version);
 
2165
        rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
 
2166
        rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
 
2167
        rstp_set_bridge_transmit_hold_count(ofproto->rstp,
 
2168
                                            s->transmit_hold_count);
 
2169
    } else {
 
2170
        struct ofport *ofport;
 
2171
        HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
 
2172
            set_rstp_port(ofport, NULL);
 
2173
        }
 
2174
        rstp_unref(ofproto->rstp);
 
2175
        ofproto->rstp = NULL;
 
2176
    }
 
2177
}
 
2178
 
 
2179
static void
 
2180
get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
 
2181
{
 
2182
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
 
2183
 
 
2184
    if (ofproto->rstp) {
 
2185
        s->enabled = true;
 
2186
        s->root_id = rstp_get_root_id(ofproto->rstp);
 
2187
        s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
 
2188
        s->designated_id = rstp_get_designated_id(ofproto->rstp);
 
2189
        s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
 
2190
        s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
 
2191
        s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
 
2192
    } else {
 
2193
        s->enabled = false;
 
2194
    }
 
2195
}
 
2196
 
 
2197
static void
 
2198
update_rstp_port_state(struct ofport_dpif *ofport)
 
2199
{
 
2200
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
 
2201
    enum rstp_state state;
 
2202
 
 
2203
    /* Figure out new state. */
 
2204
    state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
 
2205
        : RSTP_DISABLED;
 
2206
 
 
2207
    /* Update state. */
 
2208
    if (ofport->rstp_state != state) {
 
2209
        enum ofputil_port_state of_state;
 
2210
        bool fwd_change;
 
2211
 
 
2212
        VLOG_DBG("port %s: RSTP state changed from %s to %s",
 
2213
                 netdev_get_name(ofport->up.netdev),
 
2214
                 rstp_state_name(ofport->rstp_state),
 
2215
                 rstp_state_name(state));
 
2216
 
 
2217
        if (rstp_learn_in_state(ofport->rstp_state)
 
2218
            != rstp_learn_in_state(state)) {
 
2219
            /* XXX: Learning action flows should also be flushed. */
 
2220
            if (ofport->bundle) {
 
2221
                if (!rstp_shift_root_learned_address(ofproto->rstp)
 
2222
                    || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
 
2223
                    bundle_flush_macs(ofport->bundle, false);
 
2224
                }
 
2225
            }
 
2226
        }
 
2227
        fwd_change = rstp_forward_in_state(ofport->rstp_state)
 
2228
            != rstp_forward_in_state(state);
 
2229
 
 
2230
        ofproto->backer->need_revalidate = REV_RSTP;
 
2231
        ofport->rstp_state = state;
 
2232
 
 
2233
        if (fwd_change && ofport->bundle) {
 
2234
            bundle_update(ofport->bundle);
 
2235
        }
 
2236
 
 
2237
        /* Update the RSTP state bits in the OpenFlow port description. */
 
2238
        of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
 
2239
        of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
 
2240
                : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
 
2241
                : state == RSTP_DISCARDING ?  OFPUTIL_PS_STP_LISTEN
 
2242
                : 0);
 
2243
        ofproto_port_set_state(&ofport->up, of_state);
 
2244
    }
 
2245
}
 
2246
 
 
2247
static void
 
2248
rstp_run(struct ofproto_dpif *ofproto)
 
2249
{
 
2250
    if (ofproto->rstp) {
 
2251
        long long int now = time_msec();
 
2252
        long long int elapsed = now - ofproto->rstp_last_tick;
 
2253
        struct rstp_port *rp;
 
2254
        struct ofport_dpif *ofport;
 
2255
 
 
2256
        /* Every second, decrease the values of the timers. */
 
2257
        if (elapsed >= 1000) {
 
2258
            rstp_tick_timers(ofproto->rstp);
 
2259
            ofproto->rstp_last_tick = now;
 
2260
        }
 
2261
        rp = NULL;
 
2262
        while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
 
2263
            update_rstp_port_state(ofport);
 
2264
        }
 
2265
        rp = NULL;
 
2266
        ofport = NULL;
 
2267
        /* FIXME: This check should be done on-event (i.e., when setting
 
2268
         * p->fdb_flush) and not periodically.
 
2269
         */
 
2270
        while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
 
2271
            if (!rstp_shift_root_learned_address(ofproto->rstp)
 
2272
                || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
 
2273
                bundle_flush_macs(ofport->bundle, false);
 
2274
            }
 
2275
        }
 
2276
 
 
2277
        if (rstp_shift_root_learned_address(ofproto->rstp)) {
 
2278
            bundle_move(((struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp))->bundle,
 
2279
                        ((struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp))->bundle);
 
2280
            rstp_reset_root_changed(ofproto->rstp);
 
2281
        }
 
2282
    }
1940
2283
}
1941
2284
 
1942
2285
/* Configures STP on 'ofproto_' using the settings defined in 's'. */
2008
2351
        enum ofputil_port_state of_state;
2009
2352
        bool fwd_change;
2010
2353
 
2011
 
        VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2012
 
                    netdev_get_name(ofport->up.netdev),
2013
 
                    stp_state_name(ofport->stp_state),
2014
 
                    stp_state_name(state));
 
2354
        VLOG_DBG("port %s: STP state changed from %s to %s",
 
2355
                 netdev_get_name(ofport->up.netdev),
 
2356
                 stp_state_name(ofport->stp_state),
 
2357
                 stp_state_name(state));
2015
2358
        if (stp_learn_in_state(ofport->stp_state)
2016
2359
                != stp_learn_in_state(state)) {
2017
2360
            /* xxx Learning action flows should also be flushed. */
2018
2361
            ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2019
2362
            mac_learning_flush(ofproto->ml);
2020
2363
            ovs_rwlock_unlock(&ofproto->ml->rwlock);
 
2364
            mcast_snooping_mdb_flush(ofproto->ms);
2021
2365
        }
2022
2366
        fwd_change = stp_forward_in_state(ofport->stp_state)
2023
2367
                        != stp_forward_in_state(state);
2060
2404
        }
2061
2405
        return 0;
2062
2406
    } else if (sp && stp_port_no(sp) != s->port_num
2063
 
            && ofport == stp_port_get_aux(sp)) {
 
2407
               && ofport == stp_port_get_aux(sp)) {
2064
2408
        /* The port-id changed, so disable the old one if it's not
2065
2409
         * already in use by another port. */
2066
2410
        stp_port_disable(sp);
2067
2411
    }
2068
2412
 
2069
2413
    sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
 
2414
 
 
2415
    /* Set name before enabling the port so that debugging messages can print
 
2416
     * the name. */
 
2417
    stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2070
2418
    stp_port_enable(sp);
2071
2419
 
2072
2420
    stp_port_set_aux(sp, ofport);
2143
2491
            ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2144
2492
            mac_learning_flush(ofproto->ml);
2145
2493
            ovs_rwlock_unlock(&ofproto->ml->rwlock);
 
2494
            mcast_snooping_mdb_flush(ofproto->ms);
2146
2495
        }
2147
2496
    }
2148
2497
}
2154
2503
        poll_timer_wait(1000);
2155
2504
    }
2156
2505
}
 
2506
 
 
2507
/* Configures RSTP on 'ofport_' using the settings defined in 's'.  The
 
2508
 * caller is responsible for assigning RSTP port numbers and ensuring
 
2509
 * there are no duplicates. */
 
2510
static void
 
2511
set_rstp_port(struct ofport *ofport_,
 
2512
              const struct ofproto_port_rstp_settings *s)
 
2513
{
 
2514
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
2515
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
 
2516
    struct rstp_port *rp = ofport->rstp_port;
 
2517
 
 
2518
    if (!s || !s->enable) {
 
2519
        if (rp) {
 
2520
            rstp_port_unref(rp);
 
2521
            ofport->rstp_port = NULL;
 
2522
            update_rstp_port_state(ofport);
 
2523
        }
 
2524
        return;
 
2525
    }
 
2526
 
 
2527
    /* Check if need to add a new port. */
 
2528
    if (!rp) {
 
2529
        rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
 
2530
    }
 
2531
 
 
2532
    rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
 
2533
                  s->admin_edge_port, s->auto_edge,
 
2534
                  s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
 
2535
                  ofport);
 
2536
    update_rstp_port_state(ofport);
 
2537
    /* Synchronize operational status. */
 
2538
    rstp_port_set_mac_operational(rp, ofport->may_enable);
 
2539
}
 
2540
 
 
2541
static void
 
2542
get_rstp_port_status(struct ofport *ofport_,
 
2543
        struct ofproto_port_rstp_status *s)
 
2544
{
 
2545
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
2546
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
 
2547
    struct rstp_port *rp = ofport->rstp_port;
 
2548
 
 
2549
    if (!ofproto->rstp || !rp) {
 
2550
        s->enabled = false;
 
2551
        return;
 
2552
    }
 
2553
 
 
2554
    s->enabled = true;
 
2555
    rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
 
2556
                         &s->designated_bridge_id, &s->designated_port_id,
 
2557
                         &s->designated_path_cost, &s->tx_count,
 
2558
                         &s->rx_count, &s->error_count, &s->uptime);
 
2559
}
 
2560
 
2157
2561
 
2158
2562
static int
2159
2563
set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2221
2625
    ovs_rwlock_unlock(&ml->rwlock);
2222
2626
}
2223
2627
 
 
2628
static void
 
2629
bundle_move(struct ofbundle *old, struct ofbundle *new)
 
2630
{
 
2631
    struct ofproto_dpif *ofproto = old->ofproto;
 
2632
    struct mac_learning *ml = ofproto->ml;
 
2633
    struct mac_entry *mac, *next_mac;
 
2634
 
 
2635
    ovs_assert(new->ofproto == old->ofproto);
 
2636
 
 
2637
    ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
2638
    ovs_rwlock_wrlock(&ml->rwlock);
 
2639
    LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
 
2640
        if (mac_entry_get_port(ml, mac) == old) {
 
2641
            mac_entry_set_port(ml, mac, new);
 
2642
        }
 
2643
    }
 
2644
    ovs_rwlock_unlock(&ml->rwlock);
 
2645
}
 
2646
 
2224
2647
static struct ofbundle *
2225
2648
bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2226
2649
{
2244
2667
    LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2245
2668
        if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2246
2669
            || port->is_layer3
2247
 
            || !stp_forward_in_state(port->stp_state)) {
 
2670
            || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
 
2671
            || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2248
2672
            bundle->floodable = false;
2249
2673
            break;
2250
2674
        }
2277
2701
{
2278
2702
    struct ofport_dpif *port;
2279
2703
 
2280
 
    port = get_ofp_port(bundle->ofproto, ofp_port);
 
2704
    port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2281
2705
    if (!port) {
2282
2706
        return false;
2283
2707
    }
2292
2716
        list_push_back(&bundle->ports, &port->bundle_node);
2293
2717
        if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2294
2718
            || port->is_layer3
2295
 
            || !stp_forward_in_state(port->stp_state)) {
 
2719
            || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
 
2720
            || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2296
2721
            bundle->floodable = false;
2297
2722
        }
2298
2723
    }
2317
2742
    ofproto = bundle->ofproto;
2318
2743
    mbridge_unregister_bundle(ofproto->mbridge, bundle);
2319
2744
 
2320
 
    fat_rwlock_wrlock(&xlate_rwlock);
 
2745
    xlate_txn_start();
2321
2746
    xlate_bundle_remove(bundle);
2322
 
    fat_rwlock_unlock(&xlate_rwlock);
 
2747
    xlate_txn_commit();
2323
2748
 
2324
2749
    LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2325
2750
        bundle_del_port(port);
2543
2968
 
2544
2969
    error = netdev_get_etheraddr(port->up.netdev, ea);
2545
2970
    if (!error) {
2546
 
        struct ofpbuf packet;
 
2971
        struct dp_packet packet;
2547
2972
        void *packet_pdu;
2548
2973
 
2549
 
        ofpbuf_init(&packet, 0);
 
2974
        dp_packet_init(&packet, 0);
2550
2975
        packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2551
2976
                                 pdu_size);
2552
2977
        memcpy(packet_pdu, pdu, pdu_size);
2553
2978
 
2554
2979
        ofproto_dpif_send_packet(port, &packet);
2555
 
        ofpbuf_uninit(&packet);
 
2980
        dp_packet_uninit(&packet);
2556
2981
    } else {
2557
2982
        VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2558
2983
                    "%s (%s)", port->bundle->name,
2564
2989
bundle_send_learning_packets(struct ofbundle *bundle)
2565
2990
{
2566
2991
    struct ofproto_dpif *ofproto = bundle->ofproto;
2567
 
    struct ofpbuf *learning_packet;
2568
2992
    int error, n_packets, n_errors;
2569
2993
    struct mac_entry *e;
2570
 
    struct list packets;
 
2994
    struct pkt_list {
 
2995
        struct ovs_list list_node;
 
2996
        struct ofport_dpif *port;
 
2997
        struct dp_packet *pkt;
 
2998
    } *pkt_node;
 
2999
    struct ovs_list packets;
2571
3000
 
2572
3001
    list_init(&packets);
2573
3002
    ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2574
3003
    LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2575
3004
        if (mac_entry_get_port(ofproto->ml, e) != bundle) {
2576
 
            void *port_void;
2577
 
 
2578
 
            learning_packet = bond_compose_learning_packet(bundle->bond,
2579
 
                                                           e->mac, e->vlan,
2580
 
                                                           &port_void);
2581
 
            /* Temporarily use 'frame' as a private pointer (see below). */
2582
 
            ovs_assert(learning_packet->frame == ofpbuf_data(learning_packet));
2583
 
            learning_packet->frame = port_void;
2584
 
            list_push_back(&packets, &learning_packet->list_node);
 
3005
            pkt_node = xmalloc(sizeof *pkt_node);
 
3006
            pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
 
3007
                                                         e->mac, e->vlan,
 
3008
                                                         (void **)&pkt_node->port);
 
3009
            list_push_back(&packets, &pkt_node->list_node);
2585
3010
        }
2586
3011
    }
2587
3012
    ovs_rwlock_unlock(&ofproto->ml->rwlock);
2588
3013
 
2589
3014
    error = n_packets = n_errors = 0;
2590
 
    LIST_FOR_EACH (learning_packet, list_node, &packets) {
 
3015
    LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
2591
3016
        int ret;
2592
 
        void *port_void = learning_packet->frame;
2593
3017
 
2594
 
        /* Restore 'frame'. */
2595
 
        learning_packet->frame = ofpbuf_data(learning_packet);
2596
 
        ret = ofproto_dpif_send_packet(port_void, learning_packet);
 
3018
        ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
 
3019
        dp_packet_delete(pkt_node->pkt);
 
3020
        free(pkt_node);
2597
3021
        if (ret) {
2598
3022
            error = ret;
2599
3023
            n_errors++;
2600
3024
        }
2601
3025
        n_packets++;
2602
3026
    }
2603
 
    ofpbuf_list_delete(&packets);
2604
3027
 
2605
3028
    if (n_errors) {
2606
3029
        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2727
3150
    mac_learning_set_max_entries(ofproto->ml, max_entries);
2728
3151
    ovs_rwlock_unlock(&ofproto->ml->rwlock);
2729
3152
}
 
3153
 
 
3154
/* Configures multicast snooping on 'ofport' using the settings
 
3155
 * defined in 's'. */
 
3156
static int
 
3157
set_mcast_snooping(struct ofproto *ofproto_,
 
3158
                   const struct ofproto_mcast_snooping_settings *s)
 
3159
{
 
3160
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
 
3161
 
 
3162
    /* Only revalidate flows if the configuration changed. */
 
3163
    if (!s != !ofproto->ms) {
 
3164
        ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
3165
    }
 
3166
 
 
3167
    if (s) {
 
3168
        if (!ofproto->ms) {
 
3169
            ofproto->ms = mcast_snooping_create();
 
3170
        }
 
3171
 
 
3172
        ovs_rwlock_wrlock(&ofproto->ms->rwlock);
 
3173
        mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
 
3174
        mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
 
3175
        if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
 
3176
            ofproto->backer->need_revalidate = REV_RECONFIGURE;
 
3177
        }
 
3178
        ovs_rwlock_unlock(&ofproto->ms->rwlock);
 
3179
    } else {
 
3180
        mcast_snooping_unref(ofproto->ms);
 
3181
        ofproto->ms = NULL;
 
3182
    }
 
3183
 
 
3184
    return 0;
 
3185
}
 
3186
 
 
3187
/* Configures multicast snooping port's flood settings on 'ofproto'. */
 
3188
static int
 
3189
set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
 
3190
                        const struct ofproto_mcast_snooping_port_settings *s)
 
3191
{
 
3192
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
 
3193
    struct ofbundle *bundle = bundle_lookup(ofproto, aux);
 
3194
 
 
3195
    if (ofproto->ms && s) {
 
3196
        ovs_rwlock_wrlock(&ofproto->ms->rwlock);
 
3197
        mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
 
3198
        mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
 
3199
                                              s->flood_reports);
 
3200
        ovs_rwlock_unlock(&ofproto->ms->rwlock);
 
3201
    }
 
3202
    return 0;
 
3203
}
 
3204
 
2730
3205
 
2731
3206
/* Ports. */
2732
3207
 
2733
 
static struct ofport_dpif *
2734
 
get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
 
3208
struct ofport_dpif *
 
3209
ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2735
3210
{
2736
3211
    struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2737
3212
    return ofport ? ofport_dpif_cast(ofport) : NULL;
2837
3312
 
2838
3313
    if (ofport->may_enable != enable) {
2839
3314
        struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
 
3315
 
2840
3316
        ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
 
3317
 
 
3318
        if (ofport->rstp_port) {
 
3319
            rstp_port_set_mac_operational(ofport->rstp_port, enable);
 
3320
        }
2841
3321
    }
2842
3322
 
2843
3323
    ofport->may_enable = enable;
2920
3400
port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2921
3401
{
2922
3402
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2923
 
    struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
 
3403
    struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
2924
3404
    int error = 0;
2925
3405
 
2926
3406
    if (!ofport) {
2985
3465
    return error;
2986
3466
}
2987
3467
 
 
3468
static int
 
3469
port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
 
3470
{
 
3471
    struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
 
3472
    if (ofport->bundle && ofport->bundle->lacp) {
 
3473
        if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
 
3474
            return 0;
 
3475
        }
 
3476
    }
 
3477
    return -1;
 
3478
}
 
3479
 
2988
3480
struct port_dump_state {
2989
3481
    uint32_t bucket;
2990
3482
    uint32_t offset;
3095
3587
    long long int now = time_msec();
3096
3588
    int reason = -1;
3097
3589
 
3098
 
    ovs_assert(!rule->up.pending);
3099
 
 
3100
3590
    hard_timeout = rule->up.hard_timeout;
3101
3591
    idle_timeout = rule->up.idle_timeout;
3102
3592
 
3138
3628
                             const struct flow *flow,
3139
3629
                             struct rule_dpif *rule,
3140
3630
                             const struct ofpact *ofpacts, size_t ofpacts_len,
3141
 
                             struct ofpbuf *packet)
 
3631
                             struct dp_packet *packet)
3142
3632
{
3143
3633
    struct dpif_flow_stats stats;
3144
3634
    struct xlate_out xout;
3155
3645
        rule_dpif_credit_stats(rule, &stats);
3156
3646
    }
3157
3647
 
3158
 
    xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
 
3648
    xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
 
3649
                  stats.tcp_flags, packet);
3159
3650
    xin.ofpacts = ofpacts;
3160
3651
    xin.ofpacts_len = ofpacts_len;
3161
3652
    xin.resubmit_stats = &stats;
3162
3653
    xlate_actions(&xin, &xout);
3163
3654
 
3164
 
    execute.actions = ofpbuf_data(&xout.odp_actions);
3165
 
    execute.actions_len = ofpbuf_size(&xout.odp_actions);
 
3655
    execute.actions = xout.odp_actions->data;
 
3656
    execute.actions_len = xout.odp_actions->size;
 
3657
 
 
3658
    pkt_metadata_from_flow(&packet->md, flow);
3166
3659
    execute.packet = packet;
3167
 
    execute.md = pkt_metadata_from_flow(flow);
3168
3660
    execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
 
3661
    execute.probe = false;
3169
3662
 
3170
3663
    /* Fix up in_port. */
3171
3664
    in_port = flow->in_port.ofp_port;
3172
3665
    if (in_port == OFPP_NONE) {
3173
3666
        in_port = OFPP_LOCAL;
3174
3667
    }
3175
 
    execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
 
3668
    execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3176
3669
 
3177
3670
    error = dpif_execute(ofproto->backer->dpif, &execute);
3178
3671
 
3186
3679
                       const struct dpif_flow_stats *stats)
3187
3680
{
3188
3681
    ovs_mutex_lock(&rule->stats_mutex);
3189
 
    rule->stats.n_packets += stats->n_packets;
3190
 
    rule->stats.n_bytes += stats->n_bytes;
3191
 
    rule->stats.used = MAX(rule->stats.used, stats->used);
 
3682
    if (OVS_UNLIKELY(rule->new_rule)) {
 
3683
        rule_dpif_credit_stats(rule->new_rule, stats);
 
3684
    } else {
 
3685
        rule->stats.n_packets += stats->n_packets;
 
3686
        rule->stats.n_bytes += stats->n_bytes;
 
3687
        rule->stats.used = MAX(rule->stats.used, stats->used);
 
3688
    }
3192
3689
    ovs_mutex_unlock(&rule->stats_mutex);
3193
3690
}
3194
3691
 
3206
3703
    ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3207
3704
}
3208
3705
 
3209
 
/* Returns 'rule''s actions.  The caller owns a reference on the returned
3210
 
 * actions and must eventually release it (with rule_actions_unref()) to avoid
3211
 
 * a memory leak. */
 
3706
/* Returns 'rule''s actions.  The returned actions are RCU-protected, and can
 
3707
 * be read until the calling thread quiesces. */
3212
3708
const struct rule_actions *
3213
3709
rule_dpif_get_actions(const struct rule_dpif *rule)
3214
3710
{
3215
3711
    return rule_get_actions(&rule->up);
3216
3712
}
3217
3713
 
3218
 
/* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3219
 
 * If 'wc' is non-null, sets the fields that were relevant as part of
3220
 
 * the lookup. Returns the table_id where a match or miss occurred.
3221
 
 *
3222
 
 * The return value will be zero unless there was a miss and
3223
 
 * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3224
 
 * where misses occur.
3225
 
 *
3226
 
 * The rule is returned in '*rule', which is valid at least until the next
3227
 
 * RCU quiescent period.  If the '*rule' needs to stay around longer,
3228
 
 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3229
 
 * on it before this returns. */
3230
 
uint8_t
3231
 
rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3232
 
                 struct flow_wildcards *wc, struct rule_dpif **rule,
3233
 
                 bool take_ref)
 
3714
/* Sets 'rule''s recirculation id. */
 
3715
static void
 
3716
rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
 
3717
    OVS_REQUIRES(rule->up.mutex)
3234
3718
{
3235
 
    enum rule_dpif_lookup_verdict verdict;
3236
 
    enum ofputil_port_config config = 0;
3237
 
    uint8_t table_id;
3238
 
 
3239
 
    if (ofproto_dpif_get_enable_recirc(ofproto)) {
3240
 
        /* Always exactly match recirc_id since datapath supports
3241
 
         * recirculation.  */
3242
 
        if (wc) {
3243
 
            wc->masks.recirc_id = UINT32_MAX;
3244
 
        }
3245
 
        table_id = rule_dpif_lookup_get_init_table_id(flow);
 
3719
    ovs_assert(!rule->recirc_id || rule->recirc_id == id);
 
3720
    if (rule->recirc_id == id) {
 
3721
        /* Release the new reference to the same id. */
 
3722
        recirc_free_id(id);
3246
3723
    } else {
3247
 
        table_id = 0;
3248
 
    }
3249
 
 
3250
 
    verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3251
 
                                          &table_id, rule, take_ref);
3252
 
 
3253
 
    switch (verdict) {
3254
 
    case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3255
 
        return table_id;
3256
 
    case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3257
 
        struct ofport_dpif *port;
3258
 
 
3259
 
        port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3260
 
        if (!port) {
3261
 
            VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3262
 
                         flow->in_port.ofp_port);
3263
 
        }
3264
 
        config = port ? port->up.pp.config : 0;
3265
 
        break;
3266
 
    }
3267
 
    case RULE_DPIF_LOOKUP_VERDICT_DROP:
3268
 
        config = OFPUTIL_PC_NO_PACKET_IN;
3269
 
        break;
3270
 
    case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
3271
 
        if (!connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3272
 
            config = OFPUTIL_PC_NO_PACKET_IN;
3273
 
        }
3274
 
        break;
3275
 
    default:
3276
 
        OVS_NOT_REACHED();
3277
 
    }
3278
 
 
3279
 
    choose_miss_rule(config, ofproto->miss_rule,
3280
 
                     ofproto->no_packet_in_rule, rule, take_ref);
3281
 
    return table_id;
3282
 
}
3283
 
 
3284
 
/* The returned rule is valid at least until the next RCU quiescent period.
3285
 
 * If the '*rule' needs to stay around longer, a non-zero 'take_ref' must be
3286
 
 * passed in to cause a reference to be taken on it before this returns. */
 
3724
        rule->recirc_id = id;
 
3725
    }
 
3726
}
 
3727
 
 
3728
/* Sets 'rule''s recirculation id. */
 
3729
void
 
3730
rule_set_recirc_id(struct rule *rule_, uint32_t id)
 
3731
{
 
3732
    struct rule_dpif *rule = rule_dpif_cast(rule_);
 
3733
 
 
3734
    ovs_mutex_lock(&rule->up.mutex);
 
3735
    rule_dpif_set_recirc_id(rule, id);
 
3736
    ovs_mutex_unlock(&rule->up.mutex);
 
3737
}
 
3738
 
 
3739
cls_version_t
 
3740
ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
 
3741
{
 
3742
    cls_version_t version;
 
3743
 
 
3744
    atomic_read_relaxed(&ofproto->tables_version, &version);
 
3745
 
 
3746
    return version;
 
3747
}
 
3748
 
 
3749
/* The returned rule (if any) is valid at least until the next RCU quiescent
 
3750
 * period.  If the rule needs to stay around longer, a non-zero 'take_ref'
 
3751
 * must be passed in to cause a reference to be taken on it.
 
3752
 *
 
3753
 * 'flow' is non-const to allow for temporary modifications during the lookup.
 
3754
 * Any changes are restored before returning. */
3287
3755
static struct rule_dpif *
3288
 
rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3289
 
                          const struct flow *flow, struct flow_wildcards *wc,
3290
 
                          bool take_ref)
 
3756
rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
 
3757
                          uint8_t table_id, struct flow *flow,
 
3758
                          struct flow_wildcards *wc, bool take_ref)
3291
3759
{
3292
3760
    struct classifier *cls = &ofproto->up.tables[table_id].cls;
3293
3761
    const struct cls_rule *cls_rule;
3294
3762
    struct rule_dpif *rule;
3295
3763
 
3296
 
    fat_rwlock_rdlock(&cls->rwlock);
3297
 
    if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3298
 
        if (wc) {
3299
 
            memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3300
 
            if (is_ip_any(flow)) {
3301
 
                wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3302
 
            }
3303
 
        }
3304
 
 
3305
 
        if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3306
 
            if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3307
 
                /* We must pretend that transport ports are unavailable. */
3308
 
                struct flow ofpc_normal_flow = *flow;
3309
 
                ofpc_normal_flow.tp_src = htons(0);
3310
 
                ofpc_normal_flow.tp_dst = htons(0);
3311
 
                cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3312
 
            } else {
3313
 
                /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3314
 
                cls_rule = &ofproto->drop_frags_rule->up.cr;
3315
 
            }
3316
 
        } else {
3317
 
            cls_rule = classifier_lookup(cls, flow, wc);
3318
 
        }
3319
 
    } else {
3320
 
        cls_rule = classifier_lookup(cls, flow, wc);
3321
 
    }
3322
 
 
3323
 
    rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3324
 
    if (take_ref) {
3325
 
        rule_dpif_ref(rule);
3326
 
    }
3327
 
    fat_rwlock_unlock(&cls->rwlock);
 
3764
    do {
 
3765
        cls_rule = classifier_lookup(cls, version, flow, wc);
 
3766
 
 
3767
        rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
 
3768
 
 
3769
        /* Try again if the rule was released before we get the reference. */
 
3770
    } while (rule && take_ref && !rule_dpif_try_ref(rule));
3328
3771
 
3329
3772
    return rule;
3330
3773
}
3331
3774
 
3332
 
/* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3333
 
 * Stores the rule that was found in '*rule', or NULL if none was found.
3334
 
 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3335
 
 * lookup.
 
3775
/* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
 
3776
 * table '*table_id'.  Returns the rule that was found, which may be one of the
 
3777
 * special rules according to packet miss hadling.  If 'may_packet_in' is
 
3778
 * false, returning of the miss_rule (which issues packet ins for the
 
3779
 * controller) is avoided.  Updates 'wc', if nonnull, to reflect the fields
 
3780
 * that were used during the lookup.
3336
3781
 *
3337
3782
 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3338
3783
 * if none is found then the table miss configuration for that table is
3343
3788
 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3344
3789
 * '*table_id'.
3345
3790
 *
3346
 
 * Returns:
3347
 
 *
3348
 
 *    - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3349
 
 *
3350
 
 *    - RULE_OFPTC_TABLE_MISS_CONTROLLER if no rule was found and either:
3351
 
 *      + 'honor_table_miss' is false
3352
 
 *      + a table miss configuration specified that the packet should be
3353
 
 *        sent to the controller in this case.
3354
 
 *
3355
 
 *    - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found, 'honor_table_miss'
3356
 
 *      is true and a table miss configuration specified that the packet
3357
 
 *      should be dropped in this case.
3358
 
 *
3359
 
 *    - RULE_DPIF_LOOKUP_VERDICT_DEFAULT if no rule was found,
3360
 
 *      'honor_table_miss' is true and a table miss configuration has
3361
 
 *      not been specified in this case.
3362
 
 *
3363
3791
 * The rule is returned in '*rule', which is valid at least until the next
3364
3792
 * RCU quiescent period.  If the '*rule' needs to stay around longer,
3365
3793
 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3366
 
 * on it before this returns. */
3367
 
enum rule_dpif_lookup_verdict
 
3794
 * on it before this returns.
 
3795
 *
 
3796
 * 'in_port' allows the lookup to take place as if the in port had the value
 
3797
 * 'in_port'.  This is needed for resubmit action support.
 
3798
 *
 
3799
 * 'flow' is non-const to allow for temporary modifications during the lookup.
 
3800
 * Any changes are restored before returning. */
 
3801
struct rule_dpif *
3368
3802
rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3369
 
                            const struct flow *flow,
3370
 
                            struct flow_wildcards *wc,
3371
 
                            bool honor_table_miss,
3372
 
                            uint8_t *table_id, struct rule_dpif **rule,
3373
 
                            bool take_ref)
 
3803
                            cls_version_t version, struct flow *flow,
 
3804
                            struct flow_wildcards *wc, bool take_ref,
 
3805
                            const struct dpif_flow_stats *stats,
 
3806
                            uint8_t *table_id, ofp_port_t in_port,
 
3807
                            bool may_packet_in, bool honor_table_miss)
3374
3808
{
 
3809
    ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
 
3810
    ofp_port_t old_in_port = flow->in_port.ofp_port;
 
3811
    enum ofputil_table_miss miss_config;
 
3812
    struct rule_dpif *rule;
3375
3813
    uint8_t next_id;
3376
3814
 
 
3815
    /* We always unwildcard nw_frag (for IP), so they
 
3816
     * need not be unwildcarded here. */
 
3817
    if (flow->nw_frag & FLOW_NW_FRAG_ANY
 
3818
        && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
 
3819
        if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
 
3820
            /* We must pretend that transport ports are unavailable. */
 
3821
            flow->tp_src = htons(0);
 
3822
            flow->tp_dst = htons(0);
 
3823
        } else {
 
3824
            /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
 
3825
             * Use the drop_frags_rule (which cannot disappear). */
 
3826
            rule = ofproto->drop_frags_rule;
 
3827
            if (take_ref) {
 
3828
                rule_dpif_ref(rule);
 
3829
            }
 
3830
            if (stats) {
 
3831
                struct oftable *tbl = &ofproto->up.tables[*table_id];
 
3832
                unsigned long orig;
 
3833
 
 
3834
                atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
 
3835
            }
 
3836
            return rule;
 
3837
        }
 
3838
    }
 
3839
 
 
3840
    /* Look up a flow with 'in_port' as the input port.  Then restore the
 
3841
     * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
 
3842
     * have surprising behavior). */
 
3843
    flow->in_port.ofp_port = in_port;
 
3844
 
 
3845
    /* Our current implementation depends on n_tables == N_TABLES, and
 
3846
     * TBL_INTERNAL being the last table. */
 
3847
    BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
 
3848
 
 
3849
    miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
 
3850
 
3377
3851
    for (next_id = *table_id;
3378
3852
         next_id < ofproto->up.n_tables;
3379
3853
         next_id++, next_id += (next_id == TBL_INTERNAL))
3380
3854
    {
3381
3855
        *table_id = next_id;
3382
 
        *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc,
3383
 
                                          take_ref);
3384
 
        if (*rule) {
3385
 
            return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3386
 
        } else if (!honor_table_miss) {
3387
 
            return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3388
 
        } else {
3389
 
            switch (ofproto_table_get_config(&ofproto->up, *table_id)) {
3390
 
            case OFPROTO_TABLE_MISS_CONTINUE:
3391
 
                break;
3392
 
 
3393
 
            case OFPROTO_TABLE_MISS_CONTROLLER:
3394
 
                return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3395
 
 
3396
 
            case OFPROTO_TABLE_MISS_DROP:
3397
 
                return RULE_DPIF_LOOKUP_VERDICT_DROP;
3398
 
 
3399
 
            case OFPROTO_TABLE_MISS_DEFAULT:
3400
 
                return RULE_DPIF_LOOKUP_VERDICT_DEFAULT;
3401
 
            }
3402
 
        }
3403
 
    }
3404
 
 
3405
 
    return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3406
 
}
3407
 
 
3408
 
/* Given a port configuration (specified as zero if there's no port), chooses
3409
 
 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3410
 
 * flow table miss.
3411
 
 *
3412
 
 * The rule is returned in '*rule', which is valid at least until the next
3413
 
 * RCU quiescent period.  If the '*rule' needs to stay around longer,
3414
 
 * a reference must be taken on it (rule_dpif_ref()).
3415
 
 */
3416
 
void
3417
 
choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3418
 
                 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule,
3419
 
                 bool take_ref)
3420
 
{
3421
 
    *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
 
3856
        rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc,
 
3857
                                         take_ref);
 
3858
        if (stats) {
 
3859
            struct oftable *tbl = &ofproto->up.tables[next_id];
 
3860
            unsigned long orig;
 
3861
 
 
3862
            atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
 
3863
                               stats->n_packets, &orig);
 
3864
        }
 
3865
        if (rule) {
 
3866
            goto out;   /* Match. */
 
3867
        }
 
3868
        if (honor_table_miss) {
 
3869
            miss_config = ofproto_table_get_miss_config(&ofproto->up,
 
3870
                                                        *table_id);
 
3871
            if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
 
3872
                continue;
 
3873
            }
 
3874
        }
 
3875
        break;
 
3876
    }
 
3877
    /* Miss. */
 
3878
    rule = ofproto->no_packet_in_rule;
 
3879
    if (may_packet_in) {
 
3880
        if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
 
3881
            || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
 
3882
            struct ofport_dpif *port;
 
3883
 
 
3884
            port = ofp_port_to_ofport(ofproto, old_in_port);
 
3885
            if (!port) {
 
3886
                VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
 
3887
                             old_in_port);
 
3888
            } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
 
3889
                rule = ofproto->miss_rule;
 
3890
            }
 
3891
        } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
 
3892
                   connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
 
3893
            rule = ofproto->miss_rule;
 
3894
        }
 
3895
    }
3422
3896
    if (take_ref) {
3423
 
        rule_dpif_ref(*rule);
 
3897
        rule_dpif_ref(rule);
3424
3898
    }
 
3899
out:
 
3900
    /* Restore port numbers, as they may have been modified above. */
 
3901
    flow->tp_src = old_tp_src;
 
3902
    flow->tp_dst = old_tp_dst;
 
3903
    /* Restore the old in port. */
 
3904
    flow->in_port.ofp_port = old_in_port;
 
3905
 
 
3906
    return rule;
3425
3907
}
3426
3908
 
3427
3909
static void
3431
3913
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3432
3914
 
3433
3915
    ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3434
 
    ofoperation_complete(rule->up.pending, 0);
3435
3916
}
3436
3917
 
3437
3918
static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3442
3923
static struct rule *
3443
3924
rule_alloc(void)
3444
3925
{
3445
 
    struct rule_dpif *rule = xmalloc(sizeof *rule);
 
3926
    struct rule_dpif *rule = xzalloc(sizeof *rule);
3446
3927
    return &rule->up;
3447
3928
}
3448
3929
 
3462
3943
    rule->stats.n_packets = 0;
3463
3944
    rule->stats.n_bytes = 0;
3464
3945
    rule->stats.used = rule->up.modified;
 
3946
    rule->recirc_id = 0;
 
3947
    rule->new_rule = NULL;
 
3948
 
3465
3949
    return 0;
3466
3950
}
3467
3951
 
3468
3952
static void
3469
 
rule_insert(struct rule *rule_)
 
3953
rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
3470
3954
    OVS_REQUIRES(ofproto_mutex)
3471
3955
{
3472
3956
    struct rule_dpif *rule = rule_dpif_cast(rule_);
 
3957
 
 
3958
    if (old_rule_ && forward_stats) {
 
3959
        struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
 
3960
 
 
3961
        ovs_assert(!old_rule->new_rule);
 
3962
 
 
3963
        /* Take a reference to the new rule, and refer all stats updates from
 
3964
         * the old rule to the new rule. */
 
3965
        rule_dpif_ref(rule);
 
3966
 
 
3967
        ovs_mutex_lock(&old_rule->stats_mutex);
 
3968
        ovs_mutex_lock(&rule->stats_mutex);
 
3969
        old_rule->new_rule = rule;       /* Forward future stats. */
 
3970
        rule->stats = old_rule->stats;   /* Transfer stats to the new rule. */
 
3971
        ovs_mutex_unlock(&rule->stats_mutex);
 
3972
        ovs_mutex_unlock(&old_rule->stats_mutex);
 
3973
    }
 
3974
 
3473
3975
    complete_operation(rule);
3474
3976
}
3475
3977
 
3483
3985
 
3484
3986
static void
3485
3987
rule_destruct(struct rule *rule_)
 
3988
    OVS_NO_THREAD_SAFETY_ANALYSIS
3486
3989
{
3487
3990
    struct rule_dpif *rule = rule_dpif_cast(rule_);
 
3991
 
3488
3992
    ovs_mutex_destroy(&rule->stats_mutex);
 
3993
    /* Release reference to the new rule, if any. */
 
3994
    if (rule->new_rule) {
 
3995
        rule_dpif_unref(rule->new_rule);
 
3996
    }
 
3997
    if (rule->recirc_id) {
 
3998
        recirc_free_id(rule->recirc_id);
 
3999
    }
3489
4000
}
3490
4001
 
3491
4002
static void
3495
4006
    struct rule_dpif *rule = rule_dpif_cast(rule_);
3496
4007
 
3497
4008
    ovs_mutex_lock(&rule->stats_mutex);
3498
 
    *packets = rule->stats.n_packets;
3499
 
    *bytes = rule->stats.n_bytes;
3500
 
    *used = rule->stats.used;
 
4009
    if (OVS_UNLIKELY(rule->new_rule)) {
 
4010
        rule_get_stats(&rule->new_rule->up, packets, bytes, used);
 
4011
    } else {
 
4012
        *packets = rule->stats.n_packets;
 
4013
        *bytes = rule->stats.n_bytes;
 
4014
        *used = rule->stats.used;
 
4015
    }
3501
4016
    ovs_mutex_unlock(&rule->stats_mutex);
3502
4017
}
3503
4018
 
3504
4019
static void
3505
4020
rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3506
 
                  struct ofpbuf *packet)
 
4021
                  struct dp_packet *packet)
3507
4022
{
3508
4023
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3509
4024
 
3512
4027
 
3513
4028
static enum ofperr
3514
4029
rule_execute(struct rule *rule, const struct flow *flow,
3515
 
             struct ofpbuf *packet)
 
4030
             struct dp_packet *packet)
3516
4031
{
3517
4032
    rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3518
 
    ofpbuf_delete(packet);
 
4033
    dp_packet_delete(packet);
3519
4034
    return 0;
3520
4035
}
3521
4036
 
3522
 
static void
3523
 
rule_modify_actions(struct rule *rule_, bool reset_counters)
3524
 
    OVS_REQUIRES(ofproto_mutex)
3525
 
{
3526
 
    struct rule_dpif *rule = rule_dpif_cast(rule_);
3527
 
 
3528
 
    if (reset_counters) {
3529
 
        ovs_mutex_lock(&rule->stats_mutex);
3530
 
        rule->stats.n_packets = 0;
3531
 
        rule->stats.n_bytes = 0;
3532
 
        ovs_mutex_unlock(&rule->stats_mutex);
3533
 
    }
3534
 
 
3535
 
    complete_operation(rule);
3536
 
}
3537
 
 
3538
4037
static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3539
4038
{
3540
4039
    return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3558
4057
group_construct_stats(struct group_dpif *group)
3559
4058
    OVS_REQUIRES(group->stats_mutex)
3560
4059
{
 
4060
    struct ofputil_bucket *bucket;
 
4061
    const struct ovs_list *buckets;
 
4062
 
3561
4063
    group->packet_count = 0;
3562
4064
    group->byte_count = 0;
3563
 
    if (!group->bucket_stats) {
3564
 
        group->bucket_stats = xcalloc(group->up.n_buckets,
3565
 
                                      sizeof *group->bucket_stats);
3566
 
    } else {
3567
 
        memset(group->bucket_stats, 0, group->up.n_buckets *
3568
 
               sizeof *group->bucket_stats);
3569
 
    }
 
4065
 
 
4066
    group_dpif_get_buckets(group, &buckets);
 
4067
    LIST_FOR_EACH (bucket, list_node, buckets) {
 
4068
        bucket->stats.packet_count = 0;
 
4069
        bucket->stats.byte_count = 0;
 
4070
    }
 
4071
}
 
4072
 
 
4073
void
 
4074
group_dpif_credit_stats(struct group_dpif *group,
 
4075
                        struct ofputil_bucket *bucket,
 
4076
                        const struct dpif_flow_stats *stats)
 
4077
{
 
4078
    ovs_mutex_lock(&group->stats_mutex);
 
4079
    group->packet_count += stats->n_packets;
 
4080
    group->byte_count += stats->n_bytes;
 
4081
    if (bucket) {
 
4082
        bucket->stats.packet_count += stats->n_packets;
 
4083
        bucket->stats.byte_count += stats->n_bytes;
 
4084
    } else { /* Credit to all buckets */
 
4085
        const struct ovs_list *buckets;
 
4086
 
 
4087
        group_dpif_get_buckets(group, &buckets);
 
4088
        LIST_FOR_EACH (bucket, list_node, buckets) {
 
4089
            bucket->stats.packet_count += stats->n_packets;
 
4090
            bucket->stats.byte_count += stats->n_bytes;
 
4091
        }
 
4092
    }
 
4093
    ovs_mutex_unlock(&group->stats_mutex);
3570
4094
}
3571
4095
 
3572
4096
static enum ofperr
3595
4119
}
3596
4120
 
3597
4121
static void
3598
 
group_destruct__(struct group_dpif *group)
3599
 
    OVS_REQUIRES(group->stats_mutex)
3600
 
{
3601
 
    free(group->bucket_stats);
3602
 
    group->bucket_stats = NULL;
3603
 
}
3604
 
 
3605
 
static void
3606
4122
group_destruct(struct ofgroup *group_)
3607
4123
{
3608
4124
    struct group_dpif *group = group_dpif_cast(group_);
3609
 
    ovs_mutex_lock(&group->stats_mutex);
3610
 
    group_destruct__(group);
3611
 
    ovs_mutex_unlock(&group->stats_mutex);
3612
4125
    ovs_mutex_destroy(&group->stats_mutex);
3613
4126
}
3614
4127
 
3615
4128
static enum ofperr
3616
 
group_modify(struct ofgroup *group_, struct ofgroup *victim_)
 
4129
group_modify(struct ofgroup *group_)
3617
4130
{
3618
4131
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3619
 
    struct group_dpif *group = group_dpif_cast(group_);
3620
 
    struct group_dpif *victim = group_dpif_cast(victim_);
3621
 
 
3622
 
    ovs_mutex_lock(&group->stats_mutex);
3623
 
    if (victim->up.n_buckets < group->up.n_buckets) {
3624
 
        group_destruct__(group);
3625
 
    }
3626
 
    group_construct_stats(group);
3627
 
    ovs_mutex_unlock(&group->stats_mutex);
3628
4132
 
3629
4133
    ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3630
4134
 
3635
4139
group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3636
4140
{
3637
4141
    struct group_dpif *group = group_dpif_cast(group_);
 
4142
    struct ofputil_bucket *bucket;
 
4143
    const struct ovs_list *buckets;
 
4144
    struct bucket_counter *bucket_stats;
3638
4145
 
3639
4146
    ovs_mutex_lock(&group->stats_mutex);
3640
4147
    ogs->packet_count = group->packet_count;
3641
4148
    ogs->byte_count = group->byte_count;
3642
 
    memcpy(ogs->bucket_stats, group->bucket_stats,
3643
 
           group->up.n_buckets * sizeof *group->bucket_stats);
 
4149
 
 
4150
    group_dpif_get_buckets(group, &buckets);
 
4151
    bucket_stats = ogs->bucket_stats;
 
4152
    LIST_FOR_EACH (bucket, list_node, buckets) {
 
4153
        bucket_stats->packet_count = bucket->stats.packet_count;
 
4154
        bucket_stats->byte_count = bucket->stats.byte_count;
 
4155
        bucket_stats++;
 
4156
    }
3644
4157
    ovs_mutex_unlock(&group->stats_mutex);
3645
4158
 
3646
4159
    return 0;
3647
4160
}
3648
4161
 
 
4162
/* If the group exists, this function increments the groups's reference count.
 
4163
 *
 
4164
 * Make sure to call group_dpif_unref() after no longer needing to maintain
 
4165
 * a reference to the group. */
3649
4166
bool
3650
4167
group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3651
4168
                  struct group_dpif **group)
3652
 
    OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3653
4169
{
3654
4170
    struct ofgroup *ofgroup;
3655
4171
    bool found;
3656
4172
 
3657
 
    *group = NULL;
3658
4173
    found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3659
4174
    *group = found ?  group_dpif_cast(ofgroup) : NULL;
3660
4175
 
3662
4177
}
3663
4178
 
3664
4179
void
3665
 
group_dpif_release(struct group_dpif *group)
3666
 
    OVS_RELEASES(group->up.rwlock)
3667
 
{
3668
 
    ofproto_group_release(&group->up);
3669
 
}
3670
 
 
3671
 
void
3672
4180
group_dpif_get_buckets(const struct group_dpif *group,
3673
 
                       const struct list **buckets)
 
4181
                       const struct ovs_list **buckets)
3674
4182
{
3675
4183
    *buckets = &group->up.buckets;
3676
4184
}
3680
4188
{
3681
4189
    return group->up.type;
3682
4190
}
 
4191
 
 
4192
const char *
 
4193
group_dpif_get_selection_method(const struct group_dpif *group)
 
4194
{
 
4195
    return group->up.props.selection_method;
 
4196
}
3683
4197
 
3684
4198
/* Sends 'packet' out 'ofport'.
3685
4199
 * May modify 'packet'.
3686
4200
 * Returns 0 if successful, otherwise a positive errno value. */
3687
4201
int
3688
 
ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
 
4202
ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
3689
4203
{
3690
4204
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3691
4205
    int error;
3694
4208
 
3695
4209
    ovs_mutex_lock(&ofproto->stats_mutex);
3696
4210
    ofproto->stats.tx_packets++;
3697
 
    ofproto->stats.tx_bytes += ofpbuf_size(packet);
 
4211
    ofproto->stats.tx_bytes += dp_packet_size(packet);
3698
4212
    ovs_mutex_unlock(&ofproto->stats_mutex);
3699
4213
    return error;
3700
4214
}
 
4215
 
 
4216
uint64_t
 
4217
group_dpif_get_selection_method_param(const struct group_dpif *group)
 
4218
{
 
4219
    return group->up.props.selection_method_param;
 
4220
}
 
4221
 
 
4222
const struct field_array *
 
4223
group_dpif_get_fields(const struct group_dpif *group)
 
4224
{
 
4225
    return &group->up.props.fields;
 
4226
}
3701
4227
 
 
4228
/* Return the version string of the datapath that backs up
 
4229
 * this 'ofproto'.
 
4230
 */
 
4231
static const char *
 
4232
get_datapath_version(const struct ofproto *ofproto_)
 
4233
{
 
4234
    struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
 
4235
 
 
4236
    return ofproto->backer->dp_version_string;
 
4237
}
 
4238
 
3702
4239
static bool
3703
4240
set_frag_handling(struct ofproto *ofproto_,
3704
4241
                  enum ofp_config_flags frag_handling)
3713
4250
}
3714
4251
 
3715
4252
static enum ofperr
3716
 
packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
 
4253
packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
3717
4254
           const struct flow *flow,
3718
4255
           const struct ofpact *ofpacts, size_t ofpacts_len)
3719
4256
{
3796
4333
    unixctl_command_reply(conn, "table successfully flushed");
3797
4334
}
3798
4335
 
 
4336
static void
 
4337
ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
 
4338
                                     const char *argv[], void *aux OVS_UNUSED)
 
4339
{
 
4340
    struct ofproto_dpif *ofproto;
 
4341
 
 
4342
    if (argc > 1) {
 
4343
        ofproto = ofproto_dpif_lookup(argv[1]);
 
4344
        if (!ofproto) {
 
4345
            unixctl_command_reply_error(conn, "no such bridge");
 
4346
            return;
 
4347
        }
 
4348
 
 
4349
        if (!mcast_snooping_enabled(ofproto->ms)) {
 
4350
            unixctl_command_reply_error(conn, "multicast snooping is disabled");
 
4351
            return;
 
4352
        }
 
4353
        mcast_snooping_mdb_flush(ofproto->ms);
 
4354
    } else {
 
4355
        HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
 
4356
            if (!mcast_snooping_enabled(ofproto->ms)) {
 
4357
                continue;
 
4358
            }
 
4359
            mcast_snooping_mdb_flush(ofproto->ms);
 
4360
        }
 
4361
    }
 
4362
 
 
4363
    unixctl_command_reply(conn, "table successfully flushed");
 
4364
}
 
4365
 
3799
4366
static struct ofport_dpif *
3800
4367
ofbundle_get_a_port(const struct ofbundle *bundle)
3801
4368
{
3834
4401
    ds_destroy(&ds);
3835
4402
}
3836
4403
 
 
4404
static void
 
4405
ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
 
4406
                                    int argc OVS_UNUSED,
 
4407
                                    const char *argv[],
 
4408
                                    void *aux OVS_UNUSED)
 
4409
{
 
4410
    struct ds ds = DS_EMPTY_INITIALIZER;
 
4411
    const struct ofproto_dpif *ofproto;
 
4412
    const struct ofbundle *bundle;
 
4413
    const struct mcast_group *grp;
 
4414
    struct mcast_group_bundle *b;
 
4415
    struct mcast_mrouter_bundle *mrouter;
 
4416
 
 
4417
    ofproto = ofproto_dpif_lookup(argv[1]);
 
4418
    if (!ofproto) {
 
4419
        unixctl_command_reply_error(conn, "no such bridge");
 
4420
        return;
 
4421
    }
 
4422
 
 
4423
    if (!mcast_snooping_enabled(ofproto->ms)) {
 
4424
        unixctl_command_reply_error(conn, "multicast snooping is disabled");
 
4425
        return;
 
4426
    }
 
4427
 
 
4428
    ds_put_cstr(&ds, " port  VLAN  GROUP                Age\n");
 
4429
    ovs_rwlock_rdlock(&ofproto->ms->rwlock);
 
4430
    LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
 
4431
        LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
 
4432
            char name[OFP_MAX_PORT_NAME_LEN];
 
4433
 
 
4434
            bundle = b->port;
 
4435
            ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
 
4436
                                   name, sizeof name);
 
4437
            ds_put_format(&ds, "%5s  %4d  "IP_FMT"         %3d\n",
 
4438
                          name, grp->vlan, IP_ARGS(grp->ip4),
 
4439
                          mcast_bundle_age(ofproto->ms, b));
 
4440
        }
 
4441
    }
 
4442
 
 
4443
    /* ports connected to multicast routers */
 
4444
    LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
 
4445
        char name[OFP_MAX_PORT_NAME_LEN];
 
4446
 
 
4447
        bundle = mrouter->port;
 
4448
        ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
 
4449
                               name, sizeof name);
 
4450
            ds_put_format(&ds, "%5s  %4d  querier             %3d\n",
 
4451
                      name, mrouter->vlan,
 
4452
                      mcast_mrouter_age(ofproto->ms, mrouter));
 
4453
    }
 
4454
    ovs_rwlock_unlock(&ofproto->ms->rwlock);
 
4455
    unixctl_command_reply(conn, ds_cstr(&ds));
 
4456
    ds_destroy(&ds);
 
4457
}
 
4458
 
3837
4459
struct trace_ctx {
3838
4460
    struct xlate_out xout;
3839
4461
    struct xlate_in xin;
3907
4529
trace_format_odp(struct ds *result, int level, const char *title,
3908
4530
                 struct trace_ctx *trace)
3909
4531
{
3910
 
    struct ofpbuf *odp_actions = &trace->xout.odp_actions;
 
4532
    struct ofpbuf *odp_actions = trace->xout.odp_actions;
3911
4533
 
3912
4534
    ds_put_char_multiple(result, '\t', level);
3913
4535
    ds_put_format(result, "%s: ", title);
3914
 
    format_odp_actions(result, ofpbuf_data(odp_actions),
3915
 
                               ofpbuf_size(odp_actions));
 
4536
    format_odp_actions(result, odp_actions->data, odp_actions->size);
3916
4537
    ds_put_char(result, '\n');
3917
4538
}
3918
4539
 
3930
4551
    ds_put_char(result, '\n');
3931
4552
}
3932
4553
 
 
4554
static void trace_report(struct xlate_in *xin, const char *s, int recurse);
 
4555
 
3933
4556
static void
3934
4557
trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3935
4558
{
3936
4559
    struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3937
4560
    struct ds *result = trace->result;
3938
4561
 
 
4562
    if (!recurse) {
 
4563
        if (rule == xin->ofproto->miss_rule) {
 
4564
            trace_report(xin, "No match, flow generates \"packet in\"s.",
 
4565
                         recurse);
 
4566
        } else if (rule == xin->ofproto->no_packet_in_rule) {
 
4567
            trace_report(xin, "No match, packets dropped because "
 
4568
                         "OFPPC_NO_PACKET_IN is set on in_port.", recurse);
 
4569
        } else if (rule == xin->ofproto->drop_frags_rule) {
 
4570
            trace_report(xin, "Packets dropped because they are IP "
 
4571
                         "fragments and the fragment handling mode is "
 
4572
                         "\"drop\".", recurse);
 
4573
        }
 
4574
    }
 
4575
 
3939
4576
    ds_put_char(result, '\n');
3940
 
    trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3941
 
    trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3942
 
    trace_format_odp(result,  recurse + 1, "Resubmitted  odp", trace);
3943
 
    trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3944
 
    trace_format_rule(result, recurse + 1, rule);
 
4577
    if (recurse) {
 
4578
        trace_format_flow(result, recurse, "Resubmitted flow", trace);
 
4579
        trace_format_regs(result, recurse, "Resubmitted regs", trace);
 
4580
        trace_format_odp(result,  recurse, "Resubmitted  odp", trace);
 
4581
        trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
 
4582
    }
 
4583
    trace_format_rule(result, recurse, rule);
3945
4584
}
3946
4585
 
3947
4586
static void
3963
4602
 *
3964
4603
 * On success, initializes '*ofprotop' and 'flow' and returns NULL.  On failure
3965
4604
 * returns a nonnull malloced error message. */
3966
 
static char * WARN_UNUSED_RESULT
 
4605
static char * OVS_WARN_UNUSED_RESULT
3967
4606
parse_flow_and_packet(int argc, const char *argv[],
3968
4607
                      struct ofproto_dpif **ofprotop, struct flow *flow,
3969
 
                      struct ofpbuf **packetp)
 
4608
                      struct dp_packet **packetp)
3970
4609
{
3971
4610
    const struct dpif_backer *backer = NULL;
3972
4611
    const char *error = NULL;
3973
4612
    char *m_err = NULL;
3974
4613
    struct simap port_names = SIMAP_INITIALIZER(&port_names);
3975
 
    struct ofpbuf *packet;
 
4614
    struct dp_packet *packet;
3976
4615
    struct ofpbuf odp_key;
3977
4616
    struct ofpbuf odp_mask;
3978
4617
 
3981
4620
 
3982
4621
    /* Handle "-generate" or a hex string as the last argument. */
3983
4622
    if (!strcmp(argv[argc - 1], "-generate")) {
3984
 
        packet = ofpbuf_new(0);
 
4623
        packet = dp_packet_new(0);
3985
4624
        argc--;
3986
4625
    } else {
3987
4626
        error = eth_from_hex(argv[argc - 1], &packet);
4036
4675
            goto exit;
4037
4676
        }
4038
4677
 
4039
 
        if (xlate_receive(backer, NULL, ofpbuf_data(&odp_key),
4040
 
                          ofpbuf_size(&odp_key), flow,
4041
 
                          ofprotop, NULL, NULL, NULL, NULL)) {
 
4678
        if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
 
4679
            error = "Failed to parse datapath flow key";
 
4680
            goto exit;
 
4681
        }
 
4682
 
 
4683
        *ofprotop = xlate_lookup_ofproto(backer, flow,
 
4684
                                         &flow->in_port.ofp_port);
 
4685
        if (*ofprotop == NULL) {
4042
4686
            error = "Invalid datapath flow";
4043
4687
            goto exit;
4044
4688
        }
 
4689
 
 
4690
        vsp_adjust_flow(*ofprotop, flow, NULL);
 
4691
 
4045
4692
    } else {
4046
4693
        char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4047
4694
 
4048
4695
        if (err) {
4049
 
            m_err = xasprintf("Bad flow syntax: %s", err);
 
4696
            m_err = xasprintf("Bad openflow flow syntax: %s", err);
4050
4697
            free(err);
4051
4698
            goto exit;
4052
4699
        } else {
4065
4712
 
4066
4713
    /* Generate a packet, if requested. */
4067
4714
    if (packet) {
4068
 
        if (!ofpbuf_size(packet)) {
 
4715
        if (!dp_packet_size(packet)) {
4069
4716
            flow_compose(packet, flow);
4070
4717
        } else {
4071
 
            struct pkt_metadata md = pkt_metadata_from_flow(flow);
4072
 
 
4073
4718
            /* Use the metadata from the flow and the packet argument
4074
4719
             * to reconstruct the flow. */
4075
 
            flow_extract(packet, &md, flow);
 
4720
            pkt_metadata_from_flow(&packet->md, flow);
 
4721
            flow_extract(packet, flow);
4076
4722
        }
4077
4723
    }
4078
4724
 
4081
4727
        m_err = xstrdup(error);
4082
4728
    }
4083
4729
    if (m_err) {
4084
 
        ofpbuf_delete(packet);
 
4730
        dp_packet_delete(packet);
4085
4731
        packet = NULL;
4086
4732
    }
4087
4733
    *packetp = packet;
4096
4742
                      void *aux OVS_UNUSED)
4097
4743
{
4098
4744
    struct ofproto_dpif *ofproto;
4099
 
    struct ofpbuf *packet;
 
4745
    struct dp_packet *packet;
4100
4746
    char *error;
4101
4747
    struct flow flow;
4102
4748
 
4108
4754
        ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4109
4755
        unixctl_command_reply(conn, ds_cstr(&result));
4110
4756
        ds_destroy(&result);
4111
 
        ofpbuf_delete(packet);
 
4757
        dp_packet_delete(packet);
4112
4758
    } else {
4113
4759
        unixctl_command_reply_error(conn, error);
4114
4760
        free(error);
4123
4769
    struct ofproto_dpif *ofproto;
4124
4770
    bool enforce_consistency;
4125
4771
    struct ofpbuf ofpacts;
4126
 
    struct ofpbuf *packet;
 
4772
    struct dp_packet *packet;
4127
4773
    struct ds result;
4128
4774
    struct flow flow;
4129
4775
    uint16_t in_port;
4137
4783
    ofpbuf_init(&ofpacts, 0);
4138
4784
 
4139
4785
    /* Parse actions. */
4140
 
    error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
 
4786
    error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4141
4787
    if (error) {
4142
4788
        unixctl_command_reply_error(conn, error);
4143
4789
        free(error);
4164
4810
 
4165
4811
    /* Do the same checks as handle_packet_out() in ofproto.c.
4166
4812
     *
4167
 
     * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
 
4813
     * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4168
4814
     * strictly correct because these actions aren't in any table, but it's OK
4169
4815
     * because it 'table_id' is used only to check goto_table instructions, but
4170
4816
     * packet-outs take a list of actions and therefore it can't include
4178
4824
        goto exit;
4179
4825
    }
4180
4826
    if (enforce_consistency) {
4181
 
        retval = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
 
4827
        retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4182
4828
                                           &flow, u16_to_ofp(ofproto->up.max_ports),
4183
4829
                                           0, 0, usable_protocols);
4184
4830
    } else {
4185
 
        retval = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &flow,
 
4831
        retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4186
4832
                               u16_to_ofp(ofproto->up.max_ports), 0, 0,
4187
4833
                               &usable_protocols);
4188
4834
    }
4195
4841
    }
4196
4842
 
4197
4843
    ofproto_trace(ofproto, &flow, packet,
4198
 
                  ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &result);
 
4844
                  ofpacts.data, ofpacts.size, &result);
4199
4845
    unixctl_command_reply(conn, ds_cstr(&result));
4200
4846
 
4201
4847
exit:
4202
4848
    ds_destroy(&result);
4203
 
    ofpbuf_delete(packet);
 
4849
    dp_packet_delete(packet);
4204
4850
    ofpbuf_uninit(&ofpacts);
4205
4851
}
4206
4852
 
4215
4861
 * trace, otherwise the actions are determined by a flow table lookup. */
4216
4862
static void
4217
4863
ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4218
 
              const struct ofpbuf *packet,
 
4864
              const struct dp_packet *packet,
4219
4865
              const struct ofpact ofpacts[], size_t ofpacts_len,
4220
4866
              struct ds *ds)
4221
4867
{
4222
 
    struct rule_dpif *rule;
4223
4868
    struct trace_ctx trace;
4224
4869
 
4225
4870
    ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4228
4873
    ds_put_char(ds, '\n');
4229
4874
 
4230
4875
    flow_wildcards_init_catchall(&trace.wc);
4231
 
    if (ofpacts) {
4232
 
        rule = NULL;
4233
 
    } else {
4234
 
        rule_dpif_lookup(ofproto, flow, &trace.wc, &rule, false);
4235
 
 
4236
 
        trace_format_rule(ds, 0, rule);
4237
 
        if (rule == ofproto->miss_rule) {
4238
 
            ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4239
 
        } else if (rule == ofproto->no_packet_in_rule) {
4240
 
            ds_put_cstr(ds, "\nNo match, packets dropped because "
4241
 
                        "OFPPC_NO_PACKET_IN is set on in_port.\n");
4242
 
        } else if (rule == ofproto->drop_frags_rule) {
4243
 
            ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4244
 
                        "and the fragment handling mode is \"drop\".\n");
4245
 
        }
4246
 
    }
4247
 
 
4248
 
    if (rule || ofpacts) {
4249
 
        trace.result = ds;
4250
 
        trace.key = flow; /* Original flow key, used for megaflow. */
4251
 
        trace.flow = *flow; /* May be modified by actions. */
4252
 
        xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4253
 
                      packet);
4254
 
        if (ofpacts) {
4255
 
            trace.xin.ofpacts = ofpacts;
4256
 
            trace.xin.ofpacts_len = ofpacts_len;
4257
 
        }
4258
 
        trace.xin.resubmit_hook = trace_resubmit;
4259
 
        trace.xin.report_hook = trace_report;
4260
 
 
4261
 
        xlate_actions(&trace.xin, &trace.xout);
4262
 
 
4263
 
        ds_put_char(ds, '\n');
4264
 
        trace_format_flow(ds, 0, "Final flow", &trace);
4265
 
        trace_format_megaflow(ds, 0, "Megaflow", &trace);
4266
 
 
4267
 
        ds_put_cstr(ds, "Datapath actions: ");
4268
 
        format_odp_actions(ds, ofpbuf_data(&trace.xout.odp_actions),
4269
 
                           ofpbuf_size(&trace.xout.odp_actions));
4270
 
 
4271
 
        if (trace.xout.slow) {
4272
 
            enum slow_path_reason slow;
4273
 
 
4274
 
            ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4275
 
                        "slow path because it:");
4276
 
 
4277
 
            slow = trace.xout.slow;
4278
 
            while (slow) {
4279
 
                enum slow_path_reason bit = rightmost_1bit(slow);
4280
 
 
4281
 
                ds_put_format(ds, "\n\t- %s.",
4282
 
                              slow_path_reason_to_explanation(bit));
4283
 
 
4284
 
                slow &= ~bit;
4285
 
            }
4286
 
        }
4287
 
 
4288
 
        xlate_out_uninit(&trace.xout);
4289
 
    }
 
4876
 
 
4877
    trace.result = ds;
 
4878
    trace.key = flow; /* Original flow key, used for megaflow. */
 
4879
    trace.flow = *flow; /* May be modified by actions. */
 
4880
    xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
 
4881
                  ntohs(flow->tcp_flags), packet);
 
4882
    trace.xin.ofpacts = ofpacts;
 
4883
    trace.xin.ofpacts_len = ofpacts_len;
 
4884
    trace.xin.resubmit_hook = trace_resubmit;
 
4885
    trace.xin.report_hook = trace_report;
 
4886
 
 
4887
    xlate_actions(&trace.xin, &trace.xout);
 
4888
 
 
4889
    ds_put_char(ds, '\n');
 
4890
    trace_format_flow(ds, 0, "Final flow", &trace);
 
4891
    trace_format_megaflow(ds, 0, "Megaflow", &trace);
 
4892
 
 
4893
    ds_put_cstr(ds, "Datapath actions: ");
 
4894
    format_odp_actions(ds, trace.xout.odp_actions->data,
 
4895
                       trace.xout.odp_actions->size);
 
4896
 
 
4897
    if (trace.xout.slow) {
 
4898
        enum slow_path_reason slow;
 
4899
 
 
4900
        ds_put_cstr(ds, "\nThis flow is handled by the userspace "
 
4901
                    "slow path because it:");
 
4902
 
 
4903
        slow = trace.xout.slow;
 
4904
        while (slow) {
 
4905
            enum slow_path_reason bit = rightmost_1bit(slow);
 
4906
 
 
4907
            ds_put_format(ds, "\n\t- %s.",
 
4908
                          slow_path_reason_to_explanation(bit));
 
4909
 
 
4910
            slow &= ~bit;
 
4911
        }
 
4912
    }
 
4913
 
 
4914
    xlate_out_uninit(&trace.xout);
4290
4915
}
4291
4916
 
4292
4917
/* Store the current ofprotos in 'ofproto_shash'.  Returns a sorted list
4416
5041
    ds_destroy(&ds);
4417
5042
}
4418
5043
 
4419
 
static bool
4420
 
ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4421
 
                           const struct nlattr *key, size_t key_len)
4422
 
{
4423
 
    struct ofproto_dpif *ofp;
4424
 
    struct flow flow;
4425
 
 
4426
 
    xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4427
 
                  NULL, NULL, NULL, NULL);
4428
 
    return ofp == ofproto;
4429
 
}
4430
 
 
4431
5044
static void
4432
5045
ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4433
5046
                                int argc OVS_UNUSED, const char *argv[],
4434
5047
                                void *aux OVS_UNUSED)
4435
5048
{
 
5049
    const struct ofproto_dpif *ofproto;
 
5050
 
4436
5051
    struct ds ds = DS_EMPTY_INITIALIZER;
4437
 
    const struct dpif_flow_stats *stats;
4438
 
    const struct ofproto_dpif *ofproto;
4439
 
    struct dpif_flow_dump flow_dump;
4440
 
    const struct nlattr *actions;
4441
 
    const struct nlattr *mask;
4442
 
    const struct nlattr *key;
4443
 
    size_t actions_len;
4444
 
    size_t mask_len;
4445
 
    size_t key_len;
4446
5052
    bool verbosity = false;
 
5053
 
4447
5054
    struct dpif_port dpif_port;
4448
5055
    struct dpif_port_dump port_dump;
4449
5056
    struct hmap portno_names;
4450
 
    void *state = NULL;
 
5057
 
 
5058
    struct dpif_flow_dump *flow_dump;
 
5059
    struct dpif_flow_dump_thread *flow_dump_thread;
 
5060
    struct dpif_flow f;
4451
5061
    int error;
4452
5062
 
4453
5063
    ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4466
5076
    }
4467
5077
 
4468
5078
    ds_init(&ds);
4469
 
    error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4470
 
    if (error) {
4471
 
        goto exit;
4472
 
    }
4473
 
    dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4474
 
    while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4475
 
                               &mask, &mask_len, &actions, &actions_len,
4476
 
                               &stats)) {
4477
 
        if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
 
5079
    flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
 
5080
    flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
 
5081
    while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
 
5082
        struct flow flow;
 
5083
 
 
5084
        if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
 
5085
            || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
4478
5086
            continue;
4479
5087
        }
4480
5088
 
4481
 
        odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4482
 
                        verbosity);
 
5089
        if (verbosity) {
 
5090
            odp_format_ufid(&f.ufid, &ds);
 
5091
            ds_put_cstr(&ds, " ");
 
5092
        }
 
5093
        odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
 
5094
                        &portno_names, &ds, verbosity);
4483
5095
        ds_put_cstr(&ds, ", ");
4484
 
        dpif_flow_stats_format(stats, &ds);
 
5096
        dpif_flow_stats_format(&f.stats, &ds);
4485
5097
        ds_put_cstr(&ds, ", actions:");
4486
 
        format_odp_actions(&ds, actions, actions_len);
 
5098
        format_odp_actions(&ds, f.actions, f.actions_len);
4487
5099
        ds_put_char(&ds, '\n');
4488
5100
    }
4489
 
    dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4490
 
    error = dpif_flow_dump_done(&flow_dump);
 
5101
    dpif_flow_dump_thread_destroy(flow_dump_thread);
 
5102
    error = dpif_flow_dump_destroy(flow_dump);
4491
5103
 
4492
 
exit:
4493
5104
    if (error) {
4494
5105
        ds_clear(&ds);
4495
5106
        ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4503
5114
}
4504
5115
 
4505
5116
static void
4506
 
ofproto_dpif_unixctl_init(void)
 
5117
ofproto_revalidate_all_backers(void)
 
5118
{
 
5119
    const struct shash_node **backers;
 
5120
    int i;
 
5121
 
 
5122
    backers = shash_sort(&all_dpif_backers);
 
5123
    for (i = 0; i < shash_count(&all_dpif_backers); i++) {
 
5124
        struct dpif_backer *backer = backers[i]->data;
 
5125
        backer->need_revalidate = REV_RECONFIGURE;
 
5126
    }
 
5127
    free(backers);
 
5128
}
 
5129
 
 
5130
static void
 
5131
disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
 
5132
                     const char *argv[], void *aux OVS_UNUSED)
 
5133
{
 
5134
    if (!strcasecmp(argv[1], "off")) {
 
5135
        ofproto_use_tnl_push_pop = false;
 
5136
        unixctl_command_reply(conn, "Tunnel push-pop off");
 
5137
        ofproto_revalidate_all_backers();
 
5138
    } else if (!strcasecmp(argv[1], "on")) {
 
5139
        ofproto_use_tnl_push_pop = true;
 
5140
        unixctl_command_reply(conn, "Tunnel push-pop on");
 
5141
        ofproto_revalidate_all_backers();
 
5142
    }
 
5143
}
 
5144
 
 
5145
static void
 
5146
ofproto_unixctl_init(void)
4507
5147
{
4508
5148
    static bool registered;
4509
5149
    if (registered) {
4523
5163
                             ofproto_unixctl_fdb_flush, NULL);
4524
5164
    unixctl_command_register("fdb/show", "bridge", 1, 1,
4525
5165
                             ofproto_unixctl_fdb_show, NULL);
 
5166
    unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
 
5167
                             ofproto_unixctl_mcast_snooping_flush, NULL);
 
5168
    unixctl_command_register("mdb/show", "bridge", 1, 1,
 
5169
                             ofproto_unixctl_mcast_snooping_show, NULL);
4526
5170
    unixctl_command_register("dpif/dump-dps", "", 0, 0,
4527
5171
                             ofproto_unixctl_dpif_dump_dps, NULL);
4528
5172
    unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4529
5173
                             NULL);
4530
5174
    unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4531
5175
                             ofproto_unixctl_dpif_dump_flows, NULL);
 
5176
 
 
5177
    unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
 
5178
                             disable_tnl_push_pop, NULL);
4532
5179
}
4533
5180
 
4534
5181
/* Returns true if 'table' is the table used for internal rules,
4685
5332
/* Given 'flow', a flow representing a packet received on 'ofproto', checks
4686
5333
 * whether 'flow->in_port' represents a Linux VLAN device.  If so, changes
4687
5334
 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4688
 
 * 'flow->vlan_tci' to the VLAN VID, and returns true.  Otherwise (which is
4689
 
 * always the case unless VLAN splinters are enabled), returns false without
4690
 
 * making any changes. */
 
5335
 * 'flow->vlan_tci' to the VLAN VID, and returns true.  Optionally pushes the
 
5336
 * appropriate VLAN on 'packet' if provided.  Otherwise (which is always the
 
5337
 * case unless VLAN splinters are enabled), returns false without making any
 
5338
 * changes. */
4691
5339
bool
4692
 
vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
 
5340
vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
 
5341
                struct dp_packet *packet)
4693
5342
    OVS_EXCLUDED(ofproto->vsp_mutex)
4694
5343
{
4695
5344
    ofp_port_t realdev;
4711
5360
     * the VLAN device's VLAN ID. */
4712
5361
    flow->in_port.ofp_port = realdev;
4713
5362
    flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
 
5363
 
 
5364
    if (packet) {
 
5365
        /* Make the packet resemble the flow, so that it gets sent to an
 
5366
         * OpenFlow controller properly, so that it looks correct for sFlow,
 
5367
         * and so that flow_extract() will get the correct vlan_tci if it is
 
5368
         * called on 'packet'. */
 
5369
        eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
 
5370
    }
 
5371
 
4714
5372
    return true;
4715
5373
}
4716
5374
 
4765
5423
static odp_port_t
4766
5424
ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4767
5425
{
4768
 
    const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
 
5426
    const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
4769
5427
    return ofport ? ofport->odp_port : ODPP_NONE;
4770
5428
}
4771
5429
 
4800
5458
    }
4801
5459
}
4802
5460
 
4803
 
struct ofproto_dpif *
4804
 
ofproto_dpif_recirc_get_ofproto(const struct dpif_backer *backer,
4805
 
                                uint32_t recirc_id)
4806
 
{
4807
 
    struct dpif_backer_recirc_node *node;
4808
 
 
4809
 
    ovs_mutex_lock(&backer->recirc_mutex);
4810
 
    node = CONTAINER_OF(hmap_first_with_hash(&backer->recirc_map, recirc_id),
4811
 
                        struct dpif_backer_recirc_node, hmap_node);
4812
 
    ovs_mutex_unlock(&backer->recirc_mutex);
4813
 
 
4814
 
    return node ? node->ofproto : NULL;
4815
 
}
4816
 
 
4817
 
uint32_t
4818
 
ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4819
 
{
4820
 
    struct dpif_backer *backer = ofproto->backer;
4821
 
    uint32_t recirc_id = recirc_id_alloc(backer->rid_pool);
4822
 
 
4823
 
    if (recirc_id) {
4824
 
        struct dpif_backer_recirc_node *node = xmalloc(sizeof *node);
4825
 
 
4826
 
        node->recirc_id = recirc_id;
4827
 
        node->ofproto = ofproto;
4828
 
 
4829
 
        ovs_mutex_lock(&backer->recirc_mutex);
4830
 
        hmap_insert(&backer->recirc_map, &node->hmap_node, node->recirc_id);
4831
 
        ovs_mutex_unlock(&backer->recirc_mutex);
4832
 
    }
4833
 
 
4834
 
    return recirc_id;
4835
 
}
4836
 
 
4837
 
void
4838
 
ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4839
 
{
4840
 
    struct dpif_backer *backer = ofproto->backer;
4841
 
    struct dpif_backer_recirc_node *node;
4842
 
 
4843
 
    ovs_mutex_lock(&backer->recirc_mutex);
4844
 
    node = CONTAINER_OF(hmap_first_with_hash(&backer->recirc_map, recirc_id),
4845
 
                        struct dpif_backer_recirc_node, hmap_node);
4846
 
    if (node) {
4847
 
        hmap_remove(&backer->recirc_map, &node->hmap_node);
4848
 
        ovs_mutex_unlock(&backer->recirc_mutex);
4849
 
        recirc_id_free(backer->rid_pool, node->recirc_id);
4850
 
 
4851
 
        if (node->ofproto != ofproto) {
4852
 
            VLOG_ERR("recirc_id %"PRIu32", freed by incorrect ofproto (%s),"
4853
 
                     " expect ofproto (%s)", node->recirc_id, ofproto->up.name,
4854
 
                     node->ofproto->up.name);
4855
 
        }
4856
 
 
4857
 
        /* RCU postpone the free, since other threads may be referring
4858
 
         * to 'node' at same time. */
4859
 
        ovsrcu_postpone(free, node);
4860
 
    } else {
4861
 
        ovs_mutex_unlock(&backer->recirc_mutex);
4862
 
    }
4863
 
}
4864
 
 
4865
5461
int
4866
5462
ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
4867
5463
                               const struct match *match, int priority,
 
5464
                               uint16_t idle_timeout,
4868
5465
                               const struct ofpbuf *ofpacts,
4869
5466
                               struct rule **rulep)
4870
5467
{
4880
5477
    fm.modify_cookie = false;
4881
5478
    fm.table_id = TBL_INTERNAL;
4882
5479
    fm.command = OFPFC_ADD;
4883
 
    fm.idle_timeout = 0;
 
5480
    fm.idle_timeout = idle_timeout;
4884
5481
    fm.hard_timeout = 0;
 
5482
    fm.importance = 0;
4885
5483
    fm.buffer_id = 0;
4886
5484
    fm.out_port = 0;
4887
5485
    fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
4888
 
    fm.ofpacts = ofpbuf_data(ofpacts);
4889
 
    fm.ofpacts_len = ofpbuf_size(ofpacts);
 
5486
    fm.ofpacts = ofpacts->data;
 
5487
    fm.ofpacts_len = ofpacts->size;
4890
5488
 
4891
5489
    error = ofproto_flow_mod(&ofproto->up, &fm);
4892
5490
    if (error) {
4896
5494
        return error;
4897
5495
    }
4898
5496
 
4899
 
    rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
 
5497
    rule = rule_dpif_lookup_in_table(ofproto,
 
5498
                                     ofproto_dpif_get_tables_version(ofproto),
 
5499
                                     TBL_INTERNAL, &fm.match.flow,
4900
5500
                                     &fm.match.wc, false);
4901
5501
    if (rule) {
4902
5502
        *rulep = &rule->up;
4950
5550
    NULL,                       /* get_memory_usage. */
4951
5551
    type_get_memory_usage,
4952
5552
    flush,
4953
 
    get_features,
4954
 
    get_tables,
 
5553
    query_tables,
 
5554
    set_tables_version,
4955
5555
    port_alloc,
4956
5556
    port_construct,
4957
5557
    port_destruct,
4968
5568
    port_poll,
4969
5569
    port_poll_wait,
4970
5570
    port_is_lacp_current,
 
5571
    port_get_lacp_stats,
4971
5572
    NULL,                       /* rule_choose_table */
4972
5573
    rule_alloc,
4973
5574
    rule_construct,
4977
5578
    rule_dealloc,
4978
5579
    rule_get_stats,
4979
5580
    rule_execute,
4980
 
    rule_modify_actions,
4981
5581
    set_frag_handling,
4982
5582
    packet_out,
4983
5583
    set_netflow,
4985
5585
    set_sflow,
4986
5586
    set_ipfix,
4987
5587
    set_cfm,
 
5588
    cfm_status_changed,
4988
5589
    get_cfm_status,
 
5590
    set_lldp,
 
5591
    get_lldp_status,
 
5592
    set_aa,
 
5593
    aa_mapping_set,
 
5594
    aa_mapping_unset,
 
5595
    aa_vlan_get_queued,
 
5596
    aa_vlan_get_queue_size,
4989
5597
    set_bfd,
 
5598
    bfd_status_changed,
4990
5599
    get_bfd_status,
4991
5600
    set_stp,
4992
5601
    get_stp_status,
4993
5602
    set_stp_port,
4994
5603
    get_stp_port_status,
4995
5604
    get_stp_port_stats,
 
5605
    set_rstp,
 
5606
    get_rstp_status,
 
5607
    set_rstp_port,
 
5608
    get_rstp_port_status,
4996
5609
    set_queues,
4997
5610
    bundle_set,
4998
5611
    bundle_remove,
5002
5615
    is_mirror_output_bundle,
5003
5616
    forward_bpdu_changed,
5004
5617
    set_mac_table_config,
 
5618
    set_mcast_snooping,
 
5619
    set_mcast_snooping_port,
5005
5620
    set_realdev,
5006
5621
    NULL,                       /* meter_get_features */
5007
5622
    NULL,                       /* meter_set */
5013
5628
    group_dealloc,              /* group_dealloc */
5014
5629
    group_modify,               /* group_modify */
5015
5630
    group_get_stats,            /* group_get_stats */
 
5631
    get_datapath_version,       /* get_datapath_version */
5016
5632
};