~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/dpif.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
 
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3
3
 *
4
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
5
 * you may not use this file except in compliance with the License.
70
70
static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
71
71
static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
72
72
 
 
73
/* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
 
74
static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
 
75
 
73
76
/* Rate limit for individual messages going to or from the datapath, output at
74
77
 * DBG level.  This is very high because, if these are enabled, it is because
75
78
 * we really need to see them. */
81
84
static void log_flow_message(const struct dpif *dpif, int error,
82
85
                             const char *operation,
83
86
                             const struct nlattr *key, size_t key_len,
 
87
                             const struct nlattr *mask, size_t mask_len,
84
88
                             const struct dpif_flow_stats *stats,
85
89
                             const struct nlattr *actions, size_t actions_len);
86
90
static void log_operation(const struct dpif *, const char *operation,
96
100
static void
97
101
dp_initialize(void)
98
102
{
99
 
    static int status = -1;
 
103
    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
100
104
 
101
 
    if (status < 0) {
 
105
    if (ovsthread_once_start(&once)) {
102
106
        int i;
103
107
 
104
 
        status = 0;
105
108
        for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
106
109
            dp_register_provider(base_dpif_classes[i]);
107
110
        }
 
111
        ovsthread_once_done(&once);
108
112
    }
109
113
}
110
114
 
111
 
/* Registers a new datapath provider.  After successful registration, new
112
 
 * datapaths of that type can be opened using dpif_open(). */
113
 
int
114
 
dp_register_provider(const struct dpif_class *new_class)
 
115
static int
 
116
dp_register_provider__(const struct dpif_class *new_class)
115
117
{
116
118
    struct registered_dpif_class *registered_class;
117
119
 
136
138
    return 0;
137
139
}
138
140
 
 
141
/* Registers a new datapath provider.  After successful registration, new
 
142
 * datapaths of that type can be opened using dpif_open(). */
 
143
int
 
144
dp_register_provider(const struct dpif_class *new_class)
 
145
{
 
146
    int error;
 
147
 
 
148
    ovs_mutex_lock(&dpif_mutex);
 
149
    error = dp_register_provider__(new_class);
 
150
    ovs_mutex_unlock(&dpif_mutex);
 
151
 
 
152
    return error;
 
153
}
 
154
 
139
155
/* Unregisters a datapath provider.  'type' must have been previously
140
156
 * registered and not currently be in use by any dpifs.  After unregistration
141
157
 * new datapaths of that type cannot be opened using dpif_open(). */
142
 
int
143
 
dp_unregister_provider(const char *type)
 
158
static int
 
159
dp_unregister_provider__(const char *type)
144
160
{
145
161
    struct shash_node *node;
146
162
    struct registered_dpif_class *registered_class;
164
180
    return 0;
165
181
}
166
182
 
 
183
/* Unregisters a datapath provider.  'type' must have been previously
 
184
 * registered and not currently be in use by any dpifs.  After unregistration
 
185
 * new datapaths of that type cannot be opened using dpif_open(). */
 
186
int
 
187
dp_unregister_provider(const char *type)
 
188
{
 
189
    int error;
 
190
 
 
191
    dp_initialize();
 
192
 
 
193
    ovs_mutex_lock(&dpif_mutex);
 
194
    error = dp_unregister_provider__(type);
 
195
    ovs_mutex_unlock(&dpif_mutex);
 
196
 
 
197
    return error;
 
198
}
 
199
 
167
200
/* Blacklists a provider.  Causes future calls of dp_register_provider() with
168
201
 * a dpif_class which implements 'type' to fail. */
169
202
void
170
203
dp_blacklist_provider(const char *type)
171
204
{
 
205
    ovs_mutex_lock(&dpif_mutex);
172
206
    sset_add(&dpif_blacklist, type);
 
207
    ovs_mutex_unlock(&dpif_mutex);
173
208
}
174
209
 
175
210
/* Clears 'types' and enumerates the types of all currently registered datapath
182
217
    dp_initialize();
183
218
    sset_clear(types);
184
219
 
 
220
    ovs_mutex_lock(&dpif_mutex);
185
221
    SHASH_FOR_EACH(node, &dpif_classes) {
186
222
        const struct registered_dpif_class *registered_class = node->data;
187
223
        sset_add(types, registered_class->dpif_class->type);
188
224
    }
 
225
    ovs_mutex_unlock(&dpif_mutex);
 
226
}
 
227
 
 
228
static void
 
229
dp_class_unref(struct registered_dpif_class *rc)
 
230
{
 
231
    ovs_mutex_lock(&dpif_mutex);
 
232
    ovs_assert(rc->refcount);
 
233
    rc->refcount--;
 
234
    ovs_mutex_unlock(&dpif_mutex);
 
235
}
 
236
 
 
237
static struct registered_dpif_class *
 
238
dp_class_lookup(const char *type)
 
239
{
 
240
    struct registered_dpif_class *rc;
 
241
 
 
242
    ovs_mutex_lock(&dpif_mutex);
 
243
    rc = shash_find_data(&dpif_classes, type);
 
244
    if (rc) {
 
245
        rc->refcount++;
 
246
    }
 
247
    ovs_mutex_unlock(&dpif_mutex);
 
248
 
 
249
    return rc;
189
250
}
190
251
 
191
252
/* Clears 'names' and enumerates the names of all known created datapaths with
197
258
int
198
259
dp_enumerate_names(const char *type, struct sset *names)
199
260
{
200
 
    const struct registered_dpif_class *registered_class;
 
261
    struct registered_dpif_class *registered_class;
201
262
    const struct dpif_class *dpif_class;
202
263
    int error;
203
264
 
204
265
    dp_initialize();
205
266
    sset_clear(names);
206
267
 
207
 
    registered_class = shash_find_data(&dpif_classes, type);
 
268
    registered_class = dp_class_lookup(type);
208
269
    if (!registered_class) {
209
270
        VLOG_WARN("could not enumerate unknown type: %s", type);
210
271
        return EAFNOSUPPORT;
212
273
 
213
274
    dpif_class = registered_class->dpif_class;
214
275
    error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
215
 
 
216
276
    if (error) {
217
277
        VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
218
 
                   strerror(error));
 
278
                   ovs_strerror(error));
219
279
    }
 
280
    dp_class_unref(registered_class);
220
281
 
221
282
    return error;
222
283
}
252
313
    dp_initialize();
253
314
 
254
315
    type = dpif_normalize_type(type);
255
 
 
256
 
    registered_class = shash_find_data(&dpif_classes, type);
 
316
    registered_class = dp_class_lookup(type);
257
317
    if (!registered_class) {
258
318
        VLOG_WARN("could not create datapath %s of unknown type %s", name,
259
319
                  type);
265
325
                                               name, create, &dpif);
266
326
    if (!error) {
267
327
        ovs_assert(dpif->dpif_class == registered_class->dpif_class);
268
 
        registered_class->refcount++;
 
328
    } else {
 
329
        dp_class_unref(registered_class);
269
330
    }
270
331
 
271
332
exit:
310
371
        error = dpif_open(name, type, dpifp);
311
372
        if (error) {
312
373
            VLOG_WARN("datapath %s already exists but cannot be opened: %s",
313
 
                      name, strerror(error));
 
374
                      name, ovs_strerror(error));
314
375
        }
315
376
    } else if (error) {
316
 
        VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
 
377
        VLOG_WARN("failed to create datapath %s: %s",
 
378
                  name, ovs_strerror(error));
317
379
    }
318
380
    return error;
319
381
}
324
386
dpif_close(struct dpif *dpif)
325
387
{
326
388
    if (dpif) {
327
 
        struct registered_dpif_class *registered_class;
328
 
 
329
 
        registered_class = shash_find_data(&dpif_classes,
330
 
                dpif->dpif_class->type);
331
 
        ovs_assert(registered_class);
332
 
        ovs_assert(registered_class->refcount);
333
 
 
334
 
        registered_class->refcount--;
 
389
        struct registered_dpif_class *rc;
 
390
 
 
391
        rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
335
392
        dpif_uninit(dpif, true);
 
393
        dp_class_unref(rc);
336
394
    }
337
395
}
338
396
 
419
477
const char *
420
478
dpif_port_open_type(const char *datapath_type, const char *port_type)
421
479
{
422
 
    struct registered_dpif_class *registered_class;
 
480
    struct registered_dpif_class *rc;
423
481
 
424
482
    datapath_type = dpif_normalize_type(datapath_type);
425
483
 
426
 
    registered_class = shash_find_data(&dpif_classes, datapath_type);
427
 
    if (!registered_class
428
 
            || !registered_class->dpif_class->port_open_type) {
429
 
        return port_type;
 
484
    ovs_mutex_lock(&dpif_mutex);
 
485
    rc = shash_find_data(&dpif_classes, datapath_type);
 
486
    if (rc && rc->dpif_class->port_open_type) {
 
487
        port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type);
430
488
    }
 
489
    ovs_mutex_unlock(&dpif_mutex);
431
490
 
432
 
    return registered_class->dpif_class->port_open_type(
433
 
                          registered_class->dpif_class, port_type);
 
491
    return port_type;
434
492
}
435
493
 
436
494
/* Attempts to add 'netdev' as a port on 'dpif'.  If 'port_nop' is
437
 
 * non-null and its value is not UINT32_MAX, then attempts to use the
 
495
 * non-null and its value is not ODPP_NONE, then attempts to use the
438
496
 * value as the port number.
439
497
 *
440
498
 * If successful, returns 0 and sets '*port_nop' to the new port's port
441
499
 * number (if 'port_nop' is non-null).  On failure, returns a positive
442
 
 * errno value and sets '*port_nop' to UINT32_MAX (if 'port_nop' is
 
500
 * errno value and sets '*port_nop' to ODPP_NONE (if 'port_nop' is
443
501
 * non-null). */
444
502
int
445
 
dpif_port_add(struct dpif *dpif, struct netdev *netdev, uint32_t *port_nop)
 
503
dpif_port_add(struct dpif *dpif, struct netdev *netdev, odp_port_t *port_nop)
446
504
{
447
505
    const char *netdev_name = netdev_get_name(netdev);
448
 
    uint32_t port_no = UINT32_MAX;
 
506
    odp_port_t port_no = ODPP_NONE;
449
507
    int error;
450
508
 
451
509
    COVERAGE_INC(dpif_port_add);
460
518
                    dpif_name(dpif), netdev_name, port_no);
461
519
    } else {
462
520
        VLOG_WARN_RL(&error_rl, "%s: failed to add %s as port: %s",
463
 
                     dpif_name(dpif), netdev_name, strerror(error));
464
 
        port_no = UINT32_MAX;
 
521
                     dpif_name(dpif), netdev_name, ovs_strerror(error));
 
522
        port_no = ODPP_NONE;
465
523
    }
466
524
    if (port_nop) {
467
525
        *port_nop = port_no;
472
530
/* Attempts to remove 'dpif''s port number 'port_no'.  Returns 0 if successful,
473
531
 * otherwise a positive errno value. */
474
532
int
475
 
dpif_port_del(struct dpif *dpif, uint32_t port_no)
 
533
dpif_port_del(struct dpif *dpif, odp_port_t port_no)
476
534
{
477
535
    int error;
478
536
 
517
575
    int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL);
518
576
    if (error != 0 && error != ENOENT && error != ENODEV) {
519
577
        VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s",
520
 
                     dpif_name(dpif), devname, strerror(error));
 
578
                     dpif_name(dpif), devname, ovs_strerror(error));
521
579
    }
522
580
 
523
581
    return !error;
530
588
 * The caller owns the data in 'port' and must free it with
531
589
 * dpif_port_destroy() when it is no longer needed. */
532
590
int
533
 
dpif_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
 
591
dpif_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
534
592
                          struct dpif_port *port)
535
593
{
536
594
    int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
540
598
    } else {
541
599
        memset(port, 0, sizeof *port);
542
600
        VLOG_WARN_RL(&error_rl, "%s: failed to query port %"PRIu32": %s",
543
 
                     dpif_name(dpif), port_no, strerror(error));
 
601
                     dpif_name(dpif), port_no, ovs_strerror(error));
544
602
    }
545
603
    return error;
546
604
}
569
627
        VLOG_RL(&error_rl,
570
628
                error == ENOENT || error == ENODEV ? VLL_DBG : VLL_WARN,
571
629
                "%s: failed to query port %s: %s",
572
 
                dpif_name(dpif), devname, strerror(error));
 
630
                dpif_name(dpif), devname, ovs_strerror(error));
573
631
    }
574
632
    return error;
575
633
}
576
634
 
577
635
/* Returns one greater than the maximum port number accepted in flow
578
636
 * actions. */
579
 
int
 
637
odp_port_t
580
638
dpif_get_max_ports(const struct dpif *dpif)
581
639
{
582
640
    return dpif->dpif_class->get_max_ports(dpif);
586
644
 * as the OVS_USERSPACE_ATTR_PID attribute's value, for use in flows whose
587
645
 * packets arrived on port 'port_no'.
588
646
 *
589
 
 * A 'port_no' of UINT32_MAX is a special case: it returns a reserved PID, not
 
647
 * A 'port_no' of ODPP_NONE is a special case: it returns a reserved PID, not
590
648
 * allocated to any port, that the client may use for special purposes.
591
649
 *
592
650
 * The return value is only meaningful when DPIF_UC_ACTION has been enabled in
595
653
 * update all of the flows that it installed that contain
596
654
 * OVS_ACTION_ATTR_USERSPACE actions. */
597
655
uint32_t
598
 
dpif_port_get_pid(const struct dpif *dpif, uint32_t port_no)
 
656
dpif_port_get_pid(const struct dpif *dpif, odp_port_t port_no)
599
657
{
600
658
    return (dpif->dpif_class->port_get_pid
601
659
            ? (dpif->dpif_class->port_get_pid)(dpif, port_no)
607
665
 * result is null-terminated.  On failure, returns a positive errno value and
608
666
 * makes 'name' the empty string. */
609
667
int
610
 
dpif_port_get_name(struct dpif *dpif, uint32_t port_no,
 
668
dpif_port_get_name(struct dpif *dpif, odp_port_t port_no,
611
669
                   char *name, size_t name_size)
612
670
{
613
671
    struct dpif_port port;
808
866
            actions = NULL;
809
867
            actions_len = 0;
810
868
        }
811
 
        log_flow_message(dpif, error, "flow_get", key, key_len, stats,
812
 
                         actions, actions_len);
 
869
        log_flow_message(dpif, error, "flow_get", key, key_len,
 
870
                         NULL, 0, stats, actions, actions_len);
813
871
    }
814
872
    return error;
815
873
}
832
890
}
833
891
 
834
892
/* Adds or modifies a flow in 'dpif'.  The flow is specified by the Netlink
835
 
 * attributes with types OVS_KEY_ATTR_* in the 'key_len' bytes starting at
836
 
 * 'key'.  The associated actions are specified by the Netlink attributes with
837
 
 * types OVS_ACTION_ATTR_* in the 'actions_len' bytes starting at 'actions'.
 
893
 * attribute OVS_FLOW_ATTR_KEY with types OVS_KEY_ATTR_* in the 'key_len' bytes
 
894
 * starting at 'key', and OVS_FLOW_ATTR_MASK with types of OVS_KEY_ATTR_* in the
 
895
 * 'mask_len' bytes starting at 'mask'. The associated actions are specified by
 
896
 * the Netlink attributes with types OVS_ACTION_ATTR_* in the 'actions_len'
 
897
 * bytes starting at 'actions'.
838
898
 *
839
899
 * - If the flow's key does not exist in 'dpif', then the flow will be added if
840
900
 *   'flags' includes DPIF_FP_CREATE.  Otherwise the operation will fail with
854
914
int
855
915
dpif_flow_put(struct dpif *dpif, enum dpif_flow_put_flags flags,
856
916
              const struct nlattr *key, size_t key_len,
 
917
              const struct nlattr *mask, size_t mask_len,
857
918
              const struct nlattr *actions, size_t actions_len,
858
919
              struct dpif_flow_stats *stats)
859
920
{
862
923
    put.flags = flags;
863
924
    put.key = key;
864
925
    put.key_len = key_len;
 
926
    put.mask = mask;
 
927
    put.mask_len = mask_len;
865
928
    put.actions = actions;
866
929
    put.actions_len = actions_len;
867
930
    put.stats = stats;
937
1000
bool
938
1001
dpif_flow_dump_next(struct dpif_flow_dump *dump,
939
1002
                    const struct nlattr **key, size_t *key_len,
 
1003
                    const struct nlattr **mask, size_t *mask_len,
940
1004
                    const struct nlattr **actions, size_t *actions_len,
941
1005
                    const struct dpif_flow_stats **stats)
942
1006
{
946
1010
    if (!error) {
947
1011
        error = dpif->dpif_class->flow_dump_next(dpif, dump->state,
948
1012
                                                 key, key_len,
 
1013
                                                 mask, mask_len,
949
1014
                                                 actions, actions_len,
950
1015
                                                 stats);
951
1016
        if (error) {
957
1022
            *key = NULL;
958
1023
            *key_len = 0;
959
1024
        }
 
1025
        if (mask) {
 
1026
            *mask = NULL;
 
1027
            *mask_len = 0;
 
1028
        }
960
1029
        if (actions) {
961
1030
            *actions = NULL;
962
1031
            *actions_len = 0;
971
1040
        } else if (should_log_flow_message(error)) {
972
1041
            log_flow_message(dpif, error, "flow_dump",
973
1042
                             key ? *key : NULL, key ? *key_len : 0,
 
1043
                             mask ? *mask : NULL, mask ? *mask_len : 0,
974
1044
                             stats ? *stats : NULL, actions ? *actions : NULL,
975
1045
                             actions ? *actions_len : 0);
976
1046
        }
1243
1313
                     dpif_name(dpif), operation, ofperr_get_name(error));
1244
1314
    } else {
1245
1315
        VLOG_WARN_RL(&error_rl, "%s: %s failed (%s)",
1246
 
                     dpif_name(dpif), operation, strerror(error));
 
1316
                     dpif_name(dpif), operation, ovs_strerror(error));
1247
1317
    }
1248
1318
}
1249
1319
 
1250
1320
static enum vlog_level
1251
1321
flow_message_log_level(int error)
1252
1322
{
1253
 
    return error ? VLL_WARN : VLL_DBG;
 
1323
    /* If flows arrive in a batch, userspace may push down multiple
 
1324
     * unique flow definitions that overlap when wildcards are applied.
 
1325
     * Kernels that support flow wildcarding will reject these flows as
 
1326
     * duplicates (EEXIST), so lower the log level to debug for these
 
1327
     * types of messages. */
 
1328
    return (error && error != EEXIST) ? VLL_WARN : VLL_DBG;
1254
1329
}
1255
1330
 
1256
1331
static bool
1263
1338
static void
1264
1339
log_flow_message(const struct dpif *dpif, int error, const char *operation,
1265
1340
                 const struct nlattr *key, size_t key_len,
 
1341
                 const struct nlattr *mask, size_t mask_len,
1266
1342
                 const struct dpif_flow_stats *stats,
1267
1343
                 const struct nlattr *actions, size_t actions_len)
1268
1344
{
1273
1349
    }
1274
1350
    ds_put_format(&ds, "%s ", operation);
1275
1351
    if (error) {
1276
 
        ds_put_format(&ds, "(%s) ", strerror(error));
 
1352
        ds_put_format(&ds, "(%s) ", ovs_strerror(error));
1277
1353
    }
1278
 
    odp_flow_key_format(key, key_len, &ds);
 
1354
    odp_flow_format(key, key_len, mask, mask_len, &ds);
1279
1355
    if (stats) {
1280
1356
        ds_put_cstr(&ds, ", ");
1281
1357
        dpif_flow_stats_format(stats, &ds);
1307
1383
            ds_put_cstr(&s, "[zero]");
1308
1384
        }
1309
1385
        log_flow_message(dpif, error, ds_cstr(&s),
1310
 
                         put->key, put->key_len, put->stats,
1311
 
                         put->actions, put->actions_len);
 
1386
                         put->key, put->key_len, put->mask, put->mask_len,
 
1387
                         put->stats, put->actions, put->actions_len);
1312
1388
        ds_destroy(&s);
1313
1389
    }
1314
1390
}
1319
1395
{
1320
1396
    if (should_log_flow_message(error)) {
1321
1397
        log_flow_message(dpif, error, "flow_del", del->key, del->key_len,
1322
 
                         !error ? del->stats : NULL, NULL, 0);
 
1398
                         NULL, 0, !error ? del->stats : NULL, NULL, 0);
1323
1399
    }
1324
1400
}
1325
1401
 
1336
1412
        ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
1337
1413
        format_odp_actions(&ds, execute->actions, execute->actions_len);
1338
1414
        if (error) {
1339
 
            ds_put_format(&ds, " failed (%s)", strerror(error));
 
1415
            ds_put_format(&ds, " failed (%s)", ovs_strerror(error));
1340
1416
        }
1341
1417
        ds_put_format(&ds, " on packet %s", packet);
1342
1418
        vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));