2
* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
2
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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);
73
/* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
74
static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
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,
97
101
dp_initialize(void)
99
static int status = -1;
103
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
105
if (ovsthread_once_start(&once)) {
105
108
for (i = 0; i < ARRAY_SIZE(base_dpif_classes); i++) {
106
109
dp_register_provider(base_dpif_classes[i]);
111
ovsthread_once_done(&once);
111
/* Registers a new datapath provider. After successful registration, new
112
* datapaths of that type can be opened using dpif_open(). */
114
dp_register_provider(const struct dpif_class *new_class)
116
dp_register_provider__(const struct dpif_class *new_class)
116
118
struct registered_dpif_class *registered_class;
141
/* Registers a new datapath provider. After successful registration, new
142
* datapaths of that type can be opened using dpif_open(). */
144
dp_register_provider(const struct dpif_class *new_class)
148
ovs_mutex_lock(&dpif_mutex);
149
error = dp_register_provider__(new_class);
150
ovs_mutex_unlock(&dpif_mutex);
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(). */
143
dp_unregister_provider(const char *type)
159
dp_unregister_provider__(const char *type)
145
161
struct shash_node *node;
146
162
struct registered_dpif_class *registered_class;
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(). */
187
dp_unregister_provider(const char *type)
193
ovs_mutex_lock(&dpif_mutex);
194
error = dp_unregister_provider__(type);
195
ovs_mutex_unlock(&dpif_mutex);
167
200
/* Blacklists a provider. Causes future calls of dp_register_provider() with
168
201
* a dpif_class which implements 'type' to fail. */
170
203
dp_blacklist_provider(const char *type)
205
ovs_mutex_lock(&dpif_mutex);
172
206
sset_add(&dpif_blacklist, type);
207
ovs_mutex_unlock(&dpif_mutex);
175
210
/* Clears 'types' and enumerates the types of all currently registered datapath
183
218
sset_clear(types);
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);
225
ovs_mutex_unlock(&dpif_mutex);
229
dp_class_unref(struct registered_dpif_class *rc)
231
ovs_mutex_lock(&dpif_mutex);
232
ovs_assert(rc->refcount);
234
ovs_mutex_unlock(&dpif_mutex);
237
static struct registered_dpif_class *
238
dp_class_lookup(const char *type)
240
struct registered_dpif_class *rc;
242
ovs_mutex_lock(&dpif_mutex);
243
rc = shash_find_data(&dpif_classes, type);
247
ovs_mutex_unlock(&dpif_mutex);
191
252
/* Clears 'names' and enumerates the names of all known created datapaths with
198
259
dp_enumerate_names(const char *type, struct sset *names)
200
const struct registered_dpif_class *registered_class;
261
struct registered_dpif_class *registered_class;
201
262
const struct dpif_class *dpif_class;
205
266
sset_clear(names);
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;
213
274
dpif_class = registered_class->dpif_class;
214
275
error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
217
277
VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
278
ovs_strerror(error));
280
dp_class_unref(registered_class);
254
315
type = dpif_normalize_type(type);
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,
310
371
error = dpif_open(name, type, dpifp);
312
373
VLOG_WARN("datapath %s already exists but cannot be opened: %s",
313
name, strerror(error));
374
name, ovs_strerror(error));
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));
324
386
dpif_close(struct dpif *dpif)
327
struct registered_dpif_class *registered_class;
329
registered_class = shash_find_data(&dpif_classes,
330
dpif->dpif_class->type);
331
ovs_assert(registered_class);
332
ovs_assert(registered_class->refcount);
334
registered_class->refcount--;
389
struct registered_dpif_class *rc;
391
rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
335
392
dpif_uninit(dpif, true);
420
478
dpif_port_open_type(const char *datapath_type, const char *port_type)
422
struct registered_dpif_class *registered_class;
480
struct registered_dpif_class *rc;
424
482
datapath_type = dpif_normalize_type(datapath_type);
426
registered_class = shash_find_data(&dpif_classes, datapath_type);
427
if (!registered_class
428
|| !registered_class->dpif_class->port_open_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);
489
ovs_mutex_unlock(&dpif_mutex);
432
return registered_class->dpif_class->port_open_type(
433
registered_class->dpif_class, port_type);
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.
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
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)
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;
451
509
COVERAGE_INC(dpif_port_add);
460
518
dpif_name(dpif), netdev_name, port_no);
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));
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. */
475
dpif_port_del(struct dpif *dpif, uint32_t port_no)
533
dpif_port_del(struct dpif *dpif, odp_port_t port_no)
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));
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. */
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)
536
594
int error = dpif->dpif_class->port_query_by_number(dpif, port_no, port);
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));
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));
577
635
/* Returns one greater than the maximum port number accepted in flow
580
638
dpif_get_max_ports(const struct dpif *dpif)
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'.
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.
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. */
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)
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. */
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)
613
671
struct dpif_port port;
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);
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'.
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
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)
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)
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);
1243
1313
dpif_name(dpif), operation, ofperr_get_name(error));
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));
1250
1320
static enum vlog_level
1251
1321
flow_message_log_level(int error)
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;
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)
1274
1350
ds_put_format(&ds, "%s ", operation);
1276
ds_put_format(&ds, "(%s) ", strerror(error));
1352
ds_put_format(&ds, "(%s) ", ovs_strerror(error));
1278
odp_flow_key_format(key, key_len, &ds);
1354
odp_flow_format(key, key_len, mask, mask_len, &ds);
1280
1356
ds_put_cstr(&ds, ", ");
1281
1357
dpif_flow_stats_format(stats, &ds);
1307
1383
ds_put_cstr(&s, "[zero]");
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);
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);
1336
1412
ds_put_format(&ds, "%s: execute ", dpif_name(dpif));
1337
1413
format_odp_actions(&ds, execute->actions, execute->actions_len);
1339
ds_put_format(&ds, " failed (%s)", strerror(error));
1415
ds_put_format(&ds, " failed (%s)", ovs_strerror(error));
1341
1417
ds_put_format(&ds, " on packet %s", packet);
1342
1418
vlog(THIS_MODULE, error ? VLL_WARN : VLL_DBG, "%s", ds_cstr(&ds));