forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetdev.c
1835 lines (1641 loc) · 58.4 KB
/
netdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "netdev.h"
#include <errno.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "coverage.h"
#include "dpif.h"
#include "dp-packet.h"
#include "dynamic-string.h"
#include "fatal-signal.h"
#include "hash.h"
#include "list.h"
#include "netdev-dpdk.h"
#include "netdev-provider.h"
#include "netdev-vport.h"
#include "odp-netlink.h"
#include "openflow/openflow.h"
#include "packets.h"
#include "poll-loop.h"
#include "seq.h"
#include "shash.h"
#include "smap.h"
#include "sset.h"
#include "svec.h"
#include "openvswitch/vlog.h"
#include "flow.h"
VLOG_DEFINE_THIS_MODULE(netdev);
COVERAGE_DEFINE(netdev_received);
COVERAGE_DEFINE(netdev_sent);
COVERAGE_DEFINE(netdev_add_router);
COVERAGE_DEFINE(netdev_get_stats);
struct netdev_saved_flags {
struct netdev *netdev;
struct ovs_list node; /* In struct netdev's saved_flags_list. */
enum netdev_flags saved_flags;
enum netdev_flags saved_values;
};
/* Protects 'netdev_shash' and the mutable members of struct netdev. */
static struct ovs_mutex netdev_mutex = OVS_MUTEX_INITIALIZER;
/* All created network devices. */
static struct shash netdev_shash OVS_GUARDED_BY(netdev_mutex)
= SHASH_INITIALIZER(&netdev_shash);
/* Protects 'netdev_classes' against insertions or deletions.
*
* This is a recursive mutex to allow recursive acquisition when calling into
* providers. For example, netdev_run() calls into provider 'run' functions,
* which might reasonably want to call one of the netdev functions that takes
* netdev_class_mutex. */
static struct ovs_mutex netdev_class_mutex OVS_ACQ_BEFORE(netdev_mutex);
/* Contains 'struct netdev_registered_class'es. */
static struct hmap netdev_classes OVS_GUARDED_BY(netdev_class_mutex)
= HMAP_INITIALIZER(&netdev_classes);
struct netdev_registered_class {
/* In 'netdev_classes', by class->type. */
struct hmap_node hmap_node OVS_GUARDED_BY(netdev_class_mutex);
const struct netdev_class *class OVS_GUARDED_BY(netdev_class_mutex);
/* Number of 'struct netdev's of this class. */
int ref_cnt OVS_GUARDED_BY(netdev_class_mutex);
};
/* This is set pretty low because we probably won't learn anything from the
* additional log messages. */
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
static void restore_all_flags(void *aux OVS_UNUSED);
void update_device_args(struct netdev *, const struct shash *args);
int
netdev_n_txq(const struct netdev *netdev)
{
return netdev->n_txq;
}
int
netdev_n_rxq(const struct netdev *netdev)
{
return netdev->n_rxq;
}
bool
netdev_is_pmd(const struct netdev *netdev)
{
return (!strcmp(netdev->netdev_class->type, "dpdk") ||
!strcmp(netdev->netdev_class->type, "dpdkr") ||
!strcmp(netdev->netdev_class->type, "dpdkvhostcuse") ||
!strcmp(netdev->netdev_class->type, "dpdkvhostuser"));
}
static void
netdev_class_mutex_initialize(void)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
if (ovsthread_once_start(&once)) {
ovs_mutex_init_recursive(&netdev_class_mutex);
ovsthread_once_done(&once);
}
}
static void
netdev_initialize(void)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
if (ovsthread_once_start(&once)) {
netdev_class_mutex_initialize();
fatal_signal_add_hook(restore_all_flags, NULL, NULL, true);
netdev_vport_patch_register();
#ifdef __linux__
netdev_register_provider(&netdev_linux_class);
netdev_register_provider(&netdev_internal_class);
netdev_register_provider(&netdev_tap_class);
netdev_vport_tunnel_register();
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__)
netdev_register_provider(&netdev_tap_class);
netdev_register_provider(&netdev_bsd_class);
#endif
#ifdef _WIN32
netdev_register_provider(&netdev_windows_class);
netdev_register_provider(&netdev_internal_class);
netdev_vport_tunnel_register();
#endif
netdev_dpdk_register();
ovsthread_once_done(&once);
}
}
/* Performs periodic work needed by all the various kinds of netdevs.
*
* If your program opens any netdevs, it must call this function within its
* main poll loop. */
void
netdev_run(void)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
struct netdev_registered_class *rc;
netdev_initialize();
ovs_mutex_lock(&netdev_class_mutex);
HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
if (rc->class->run) {
rc->class->run();
}
}
ovs_mutex_unlock(&netdev_class_mutex);
}
/* Arranges for poll_block() to wake up when netdev_run() needs to be called.
*
* If your program opens any netdevs, it must call this function within its
* main poll loop. */
void
netdev_wait(void)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
struct netdev_registered_class *rc;
ovs_mutex_lock(&netdev_class_mutex);
HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
if (rc->class->wait) {
rc->class->wait();
}
}
ovs_mutex_unlock(&netdev_class_mutex);
}
static struct netdev_registered_class *
netdev_lookup_class(const char *type)
OVS_REQ_RDLOCK(netdev_class_mutex)
{
struct netdev_registered_class *rc;
HMAP_FOR_EACH_WITH_HASH (rc, hmap_node, hash_string(type, 0),
&netdev_classes) {
if (!strcmp(type, rc->class->type)) {
return rc;
}
}
return NULL;
}
/* Initializes and registers a new netdev provider. After successful
* registration, new netdevs of that type can be opened using netdev_open(). */
int
netdev_register_provider(const struct netdev_class *new_class)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
int error;
netdev_class_mutex_initialize();
ovs_mutex_lock(&netdev_class_mutex);
if (netdev_lookup_class(new_class->type)) {
VLOG_WARN("attempted to register duplicate netdev provider: %s",
new_class->type);
error = EEXIST;
} else {
error = new_class->init ? new_class->init() : 0;
if (!error) {
struct netdev_registered_class *rc;
rc = xmalloc(sizeof *rc);
hmap_insert(&netdev_classes, &rc->hmap_node,
hash_string(new_class->type, 0));
rc->class = new_class;
rc->ref_cnt = 0;
} else {
VLOG_ERR("failed to initialize %s network device class: %s",
new_class->type, ovs_strerror(error));
}
}
ovs_mutex_unlock(&netdev_class_mutex);
return error;
}
/* Unregisters a netdev provider. 'type' must have been previously
* registered and not currently be in use by any netdevs. After unregistration
* new netdevs of that type cannot be opened using netdev_open(). */
int
netdev_unregister_provider(const char *type)
OVS_EXCLUDED(netdev_class_mutex, netdev_mutex)
{
struct netdev_registered_class *rc;
int error;
netdev_initialize();
ovs_mutex_lock(&netdev_class_mutex);
rc = netdev_lookup_class(type);
if (!rc) {
VLOG_WARN("attempted to unregister a netdev provider that is not "
"registered: %s", type);
error = EAFNOSUPPORT;
} else {
if (!rc->ref_cnt) {
hmap_remove(&netdev_classes, &rc->hmap_node);
free(rc);
error = 0;
} else {
VLOG_WARN("attempted to unregister in use netdev provider: %s",
type);
error = EBUSY;
}
}
ovs_mutex_unlock(&netdev_class_mutex);
return error;
}
/* Clears 'types' and enumerates the types of all currently registered netdev
* providers into it. The caller must first initialize the sset. */
void
netdev_enumerate_types(struct sset *types)
OVS_EXCLUDED(netdev_mutex)
{
struct netdev_registered_class *rc;
netdev_initialize();
sset_clear(types);
ovs_mutex_lock(&netdev_class_mutex);
HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
sset_add(types, rc->class->type);
}
ovs_mutex_unlock(&netdev_class_mutex);
}
/* Check that the network device name is not the same as any of the registered
* vport providers' dpif_port name (dpif_port is NULL if the vport provider
* does not define it) or the datapath internal port name (e.g. ovs-system).
*
* Returns true if there is a name conflict, false otherwise. */
bool
netdev_is_reserved_name(const char *name)
OVS_EXCLUDED(netdev_mutex)
{
struct netdev_registered_class *rc;
netdev_initialize();
ovs_mutex_lock(&netdev_class_mutex);
HMAP_FOR_EACH (rc, hmap_node, &netdev_classes) {
const char *dpif_port = netdev_vport_class_get_dpif_port(rc->class);
if (dpif_port && !strncmp(name, dpif_port, strlen(dpif_port))) {
ovs_mutex_unlock(&netdev_class_mutex);
return true;
}
}
ovs_mutex_unlock(&netdev_class_mutex);
if (!strncmp(name, "ovs-", 4)) {
struct sset types;
const char *type;
sset_init(&types);
dp_enumerate_types(&types);
SSET_FOR_EACH (type, &types) {
if (!strcmp(name+4, type)) {
sset_destroy(&types);
return true;
}
}
sset_destroy(&types);
}
return false;
}
/* Opens the network device named 'name' (e.g. "eth0") of the specified 'type'
* (e.g. "system") and returns zero if successful, otherwise a positive errno
* value. On success, sets '*netdevp' to the new network device, otherwise to
* null.
*
* Some network devices may need to be configured (with netdev_set_config())
* before they can be used. */
int
netdev_open(const char *name, const char *type, struct netdev **netdevp)
OVS_EXCLUDED(netdev_mutex)
{
struct netdev *netdev;
int error;
netdev_initialize();
ovs_mutex_lock(&netdev_class_mutex);
ovs_mutex_lock(&netdev_mutex);
netdev = shash_find_data(&netdev_shash, name);
if (!netdev) {
struct netdev_registered_class *rc;
rc = netdev_lookup_class(type && type[0] ? type : "system");
if (rc) {
netdev = rc->class->alloc();
if (netdev) {
memset(netdev, 0, sizeof *netdev);
netdev->netdev_class = rc->class;
netdev->name = xstrdup(name);
netdev->change_seq = 1;
netdev->node = shash_add(&netdev_shash, name, netdev);
/* By default enable one tx and rx queue per netdev. */
netdev->n_txq = netdev->netdev_class->send ? 1 : 0;
netdev->n_rxq = netdev->netdev_class->rxq_alloc ? 1 : 0;
list_init(&netdev->saved_flags_list);
error = rc->class->construct(netdev);
if (!error) {
rc->ref_cnt++;
netdev_change_seq_changed(netdev);
} else {
free(netdev->name);
ovs_assert(list_is_empty(&netdev->saved_flags_list));
shash_delete(&netdev_shash, netdev->node);
rc->class->dealloc(netdev);
}
} else {
error = ENOMEM;
}
} else {
VLOG_WARN("could not create netdev %s of unknown type %s",
name, type);
error = EAFNOSUPPORT;
}
} else {
error = 0;
}
if (!error) {
netdev->ref_cnt++;
*netdevp = netdev;
} else {
*netdevp = NULL;
}
ovs_mutex_unlock(&netdev_mutex);
ovs_mutex_unlock(&netdev_class_mutex);
return error;
}
/* Returns a reference to 'netdev_' for the caller to own. Returns null if
* 'netdev_' is null. */
struct netdev *
netdev_ref(const struct netdev *netdev_)
OVS_EXCLUDED(netdev_mutex)
{
struct netdev *netdev = CONST_CAST(struct netdev *, netdev_);
if (netdev) {
ovs_mutex_lock(&netdev_mutex);
ovs_assert(netdev->ref_cnt > 0);
netdev->ref_cnt++;
ovs_mutex_unlock(&netdev_mutex);
}
return netdev;
}
/* Reconfigures the device 'netdev' with 'args'. 'args' may be empty
* or NULL if none are needed. */
int
netdev_set_config(struct netdev *netdev, const struct smap *args, char **errp)
OVS_EXCLUDED(netdev_mutex)
{
if (netdev->netdev_class->set_config) {
const struct smap no_args = SMAP_INITIALIZER(&no_args);
int error;
error = netdev->netdev_class->set_config(netdev,
args ? args : &no_args);
if (error) {
VLOG_WARN_BUF(errp, "%s: could not set configuration (%s)",
netdev_get_name(netdev), ovs_strerror(error));
}
return error;
} else if (args && !smap_is_empty(args)) {
VLOG_WARN_BUF(errp, "%s: arguments provided to device that is not configurable",
netdev_get_name(netdev));
}
return 0;
}
/* Returns the current configuration for 'netdev' in 'args'. The caller must
* have already initialized 'args' with smap_init(). Returns 0 on success, in
* which case 'args' will be filled with 'netdev''s configuration. On failure
* returns a positive errno value, in which case 'args' will be empty.
*
* The caller owns 'args' and its contents and must eventually free them with
* smap_destroy(). */
int
netdev_get_config(const struct netdev *netdev, struct smap *args)
OVS_EXCLUDED(netdev_mutex)
{
int error;
smap_clear(args);
if (netdev->netdev_class->get_config) {
error = netdev->netdev_class->get_config(netdev, args);
if (error) {
smap_clear(args);
}
} else {
error = 0;
}
return error;
}
const struct netdev_tunnel_config *
netdev_get_tunnel_config(const struct netdev *netdev)
OVS_EXCLUDED(netdev_mutex)
{
if (netdev->netdev_class->get_tunnel_config) {
return netdev->netdev_class->get_tunnel_config(netdev);
} else {
return NULL;
}
}
/* Returns the id of the numa node the 'netdev' is on. If the function
* is not implemented, returns NETDEV_NUMA_UNSPEC. */
int
netdev_get_numa_id(const struct netdev *netdev)
{
if (netdev->netdev_class->get_numa_id) {
return netdev->netdev_class->get_numa_id(netdev);
} else {
return NETDEV_NUMA_UNSPEC;
}
}
static void
netdev_unref(struct netdev *dev)
OVS_RELEASES(netdev_mutex)
{
ovs_assert(dev->ref_cnt);
if (!--dev->ref_cnt) {
const struct netdev_class *class = dev->netdev_class;
struct netdev_registered_class *rc;
dev->netdev_class->destruct(dev);
if (dev->node) {
shash_delete(&netdev_shash, dev->node);
}
free(dev->name);
dev->netdev_class->dealloc(dev);
ovs_mutex_unlock(&netdev_mutex);
ovs_mutex_lock(&netdev_class_mutex);
rc = netdev_lookup_class(class->type);
ovs_assert(rc->ref_cnt > 0);
rc->ref_cnt--;
ovs_mutex_unlock(&netdev_class_mutex);
} else {
ovs_mutex_unlock(&netdev_mutex);
}
}
/* Closes and destroys 'netdev'. */
void
netdev_close(struct netdev *netdev)
OVS_EXCLUDED(netdev_mutex)
{
if (netdev) {
ovs_mutex_lock(&netdev_mutex);
netdev_unref(netdev);
}
}
/* Removes 'netdev' from the global shash and unrefs 'netdev'.
*
* This allows handler and revalidator threads to still retain references
* to this netdev while the main thread changes interface configuration.
*
* This function should only be called by the main thread when closing
* netdevs during user configuration changes. Otherwise, netdev_close should be
* used to close netdevs. */
void
netdev_remove(struct netdev *netdev)
{
if (netdev) {
ovs_mutex_lock(&netdev_mutex);
if (netdev->node) {
shash_delete(&netdev_shash, netdev->node);
netdev->node = NULL;
netdev_change_seq_changed(netdev);
}
netdev_unref(netdev);
}
}
/* Parses 'netdev_name_', which is of the form [type@]name into its component
* pieces. 'name' and 'type' must be freed by the caller. */
void
netdev_parse_name(const char *netdev_name_, char **name, char **type)
{
char *netdev_name = xstrdup(netdev_name_);
char *separator;
separator = strchr(netdev_name, '@');
if (separator) {
*separator = '\0';
*type = netdev_name;
*name = xstrdup(separator + 1);
} else {
*name = netdev_name;
*type = xstrdup("system");
}
}
/* Attempts to open a netdev_rxq handle for obtaining packets received on
* 'netdev'. On success, returns 0 and stores a nonnull 'netdev_rxq *' into
* '*rxp'. On failure, returns a positive errno value and stores NULL into
* '*rxp'.
*
* Some kinds of network devices might not support receiving packets. This
* function returns EOPNOTSUPP in that case.*/
int
netdev_rxq_open(struct netdev *netdev, struct netdev_rxq **rxp, int id)
OVS_EXCLUDED(netdev_mutex)
{
int error;
if (netdev->netdev_class->rxq_alloc && id < netdev->n_rxq) {
struct netdev_rxq *rx = netdev->netdev_class->rxq_alloc();
if (rx) {
rx->netdev = netdev;
rx->queue_id = id;
error = netdev->netdev_class->rxq_construct(rx);
if (!error) {
netdev_ref(netdev);
*rxp = rx;
return 0;
}
netdev->netdev_class->rxq_dealloc(rx);
} else {
error = ENOMEM;
}
} else {
error = EOPNOTSUPP;
}
*rxp = NULL;
return error;
}
/* Closes 'rx'. */
void
netdev_rxq_close(struct netdev_rxq *rx)
OVS_EXCLUDED(netdev_mutex)
{
if (rx) {
struct netdev *netdev = rx->netdev;
netdev->netdev_class->rxq_destruct(rx);
netdev->netdev_class->rxq_dealloc(rx);
netdev_close(netdev);
}
}
/* Attempts to receive batch of packets from 'rx'.
*
* Returns EAGAIN immediately if no packet is ready to be received.
*
* Returns EMSGSIZE, and discards the packet, if the received packet is longer
* than 'dp_packet_tailroom(buffer)'.
*
* It is advised that the tailroom of 'buffer' should be
* VLAN_HEADER_LEN bytes longer than the MTU to allow space for an
* out-of-band VLAN header to be added to the packet. At the very least,
* 'buffer' must have at least ETH_TOTAL_MIN bytes of tailroom.
*
* This function may be set to null if it would always return EOPNOTSUPP
* anyhow. */
int
netdev_rxq_recv(struct netdev_rxq *rx, struct dp_packet **buffers, int *cnt)
{
int retval;
retval = rx->netdev->netdev_class->rxq_recv(rx, buffers, cnt);
if (!retval) {
COVERAGE_INC(netdev_received);
}
return retval;
}
/* Arranges for poll_block() to wake up when a packet is ready to be received
* on 'rx'. */
void
netdev_rxq_wait(struct netdev_rxq *rx)
{
rx->netdev->netdev_class->rxq_wait(rx);
}
/* Discards any packets ready to be received on 'rx'. */
int
netdev_rxq_drain(struct netdev_rxq *rx)
{
return (rx->netdev->netdev_class->rxq_drain
? rx->netdev->netdev_class->rxq_drain(rx)
: 0);
}
/* Configures the number of tx queues and rx queues of 'netdev'.
* Return 0 if successful, otherwise a positive errno value.
*
* 'n_rxq' specifies the maximum number of receive queues to create.
* The netdev provider might choose to create less (e.g. if the hardware
* supports only a smaller number). The caller can check how many have been
* actually created by calling 'netdev_n_rxq()'
*
* 'n_txq' specifies the exact number of transmission queues to create.
* If this function returns successfully, the caller can make 'n_txq'
* concurrent calls to netdev_send() (each one with a different 'qid' in the
* range [0..'n_txq'-1]).
*
* On error, the tx queue and rx queue configuration is indeterminant.
* Caller should make decision on whether to restore the previous or
* the default configuration. Also, caller must make sure there is no
* other thread accessing the queues at the same time. */
int
netdev_set_multiq(struct netdev *netdev, unsigned int n_txq,
unsigned int n_rxq)
{
int error;
error = (netdev->netdev_class->set_multiq
? netdev->netdev_class->set_multiq(netdev,
MAX(n_txq, 1),
MAX(n_rxq, 1))
: EOPNOTSUPP);
if (error && error != EOPNOTSUPP) {
VLOG_DBG_RL(&rl, "failed to set tx/rx queue for network device %s:"
"%s", netdev_get_name(netdev), ovs_strerror(error));
}
return error;
}
/* Sends 'buffers' on 'netdev'. Returns 0 if successful (for every packet),
* otherwise a positive errno value. Returns EAGAIN without blocking if
* at least one the packets cannot be queued immediately. Returns EMSGSIZE
* if a partial packet was transmitted or if a packet is too big or too small
* to transmit on the device.
*
* If the function returns a non-zero value, some of the packets might have
* been sent anyway.
*
* To retain ownership of 'buffer' caller can set may_steal to false.
*
* The network device is expected to maintain one or more packet
* transmission queues, so that the caller does not ordinarily have to
* do additional queuing of packets. 'qid' specifies the queue to use
* and can be ignored if the implementation does not support multiple
* queues.
*
* Some network devices may not implement support for this function. In such
* cases this function will always return EOPNOTSUPP. */
int
netdev_send(struct netdev *netdev, int qid, struct dp_packet **buffers,
int cnt, bool may_steal)
{
int error;
error = (netdev->netdev_class->send
? netdev->netdev_class->send(netdev, qid, buffers, cnt, may_steal)
: EOPNOTSUPP);
if (!error) {
COVERAGE_INC(netdev_sent);
}
return error;
}
int
netdev_pop_header(struct netdev *netdev, struct dp_packet **buffers, int cnt)
{
int i;
if (!netdev->netdev_class->pop_header) {
return EOPNOTSUPP;
}
for (i = 0; i < cnt; i++) {
int err;
err = netdev->netdev_class->pop_header(buffers[i]);
if (err) {
dp_packet_clear(buffers[i]);
}
}
return 0;
}
int
netdev_build_header(const struct netdev *netdev, struct ovs_action_push_tnl *data,
const struct flow *tnl_flow)
{
if (netdev->netdev_class->build_header) {
return netdev->netdev_class->build_header(netdev, data, tnl_flow);
}
return EOPNOTSUPP;
}
int
netdev_push_header(const struct netdev *netdev,
struct dp_packet **buffers, int cnt,
const struct ovs_action_push_tnl *data)
{
int i;
if (!netdev->netdev_class->push_header) {
return -EINVAL;
}
for (i = 0; i < cnt; i++) {
netdev->netdev_class->push_header(buffers[i], data);
pkt_metadata_init(&buffers[i]->md, u32_to_odp(data->out_port));
}
return 0;
}
/* Registers with the poll loop to wake up from the next call to poll_block()
* when the packet transmission queue has sufficient room to transmit a packet
* with netdev_send().
*
* The network device is expected to maintain one or more packet
* transmission queues, so that the caller does not ordinarily have to
* do additional queuing of packets. 'qid' specifies the queue to use
* and can be ignored if the implementation does not support multiple
* queues. */
void
netdev_send_wait(struct netdev *netdev, int qid)
{
if (netdev->netdev_class->send_wait) {
netdev->netdev_class->send_wait(netdev, qid);
}
}
/* Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
* otherwise a positive errno value. */
int
netdev_set_etheraddr(struct netdev *netdev, const struct eth_addr mac)
{
return netdev->netdev_class->set_etheraddr(netdev, mac);
}
/* Retrieves 'netdev''s MAC address. If successful, returns 0 and copies the
* the MAC address into 'mac'. On failure, returns a positive errno value and
* clears 'mac' to all-zeros. */
int
netdev_get_etheraddr(const struct netdev *netdev, struct eth_addr *mac)
{
return netdev->netdev_class->get_etheraddr(netdev, mac);
}
/* Returns the name of the network device that 'netdev' represents,
* e.g. "eth0". The caller must not modify or free the returned string. */
const char *
netdev_get_name(const struct netdev *netdev)
{
return netdev->name;
}
/* Retrieves the MTU of 'netdev'. The MTU is the maximum size of transmitted
* (and received) packets, in bytes, not including the hardware header; thus,
* this is typically 1500 bytes for Ethernet devices.
*
* If successful, returns 0 and stores the MTU size in '*mtup'. Returns
* EOPNOTSUPP if 'netdev' does not have an MTU (as e.g. some tunnels do not).
* On other failure, returns a positive errno value. On failure, sets '*mtup'
* to 0. */
int
netdev_get_mtu(const struct netdev *netdev, int *mtup)
{
const struct netdev_class *class = netdev->netdev_class;
int error;
error = class->get_mtu ? class->get_mtu(netdev, mtup) : EOPNOTSUPP;
if (error) {
*mtup = 0;
if (error != EOPNOTSUPP) {
VLOG_DBG_RL(&rl, "failed to retrieve MTU for network device %s: "
"%s", netdev_get_name(netdev), ovs_strerror(error));
}
}
return error;
}
/* Sets the MTU of 'netdev'. The MTU is the maximum size of transmitted
* (and received) packets, in bytes.
*
* If successful, returns 0. Returns EOPNOTSUPP if 'netdev' does not have an
* MTU (as e.g. some tunnels do not). On other failure, returns a positive
* errno value. */
int
netdev_set_mtu(const struct netdev *netdev, int mtu)
{
const struct netdev_class *class = netdev->netdev_class;
int error;
error = class->set_mtu ? class->set_mtu(netdev, mtu) : EOPNOTSUPP;
if (error && error != EOPNOTSUPP) {
VLOG_DBG_RL(&rl, "failed to set MTU for network device %s: %s",
netdev_get_name(netdev), ovs_strerror(error));
}
return error;
}
/* Returns the ifindex of 'netdev', if successful, as a positive number. On
* failure, returns a negative errno value.
*
* The desired semantics of the ifindex value are a combination of those
* specified by POSIX for if_nametoindex() and by SNMP for ifIndex. An ifindex
* value should be unique within a host and remain stable at least until
* reboot. SNMP says an ifindex "ranges between 1 and the value of ifNumber"
* but many systems do not follow this rule anyhow.
*
* Some network devices may not implement support for this function. In such
* cases this function will always return -EOPNOTSUPP.
*/
int
netdev_get_ifindex(const struct netdev *netdev)
{
int (*get_ifindex)(const struct netdev *);
get_ifindex = netdev->netdev_class->get_ifindex;
return get_ifindex ? get_ifindex(netdev) : -EOPNOTSUPP;
}
/* Stores the features supported by 'netdev' into each of '*current',
* '*advertised', '*supported', and '*peer' that are non-null. Each value is a
* bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
* successful, otherwise a positive errno value. On failure, all of the
* passed-in values are set to 0.
*
* Some network devices may not implement support for this function. In such
* cases this function will always return EOPNOTSUPP. */
int
netdev_get_features(const struct netdev *netdev,
enum netdev_features *current,
enum netdev_features *advertised,
enum netdev_features *supported,
enum netdev_features *peer)
{
int (*get_features)(const struct netdev *netdev,
enum netdev_features *current,
enum netdev_features *advertised,
enum netdev_features *supported,
enum netdev_features *peer);
enum netdev_features dummy[4];
int error;
if (!current) {
current = &dummy[0];
}
if (!advertised) {
advertised = &dummy[1];
}
if (!supported) {
supported = &dummy[2];
}
if (!peer) {
peer = &dummy[3];
}
get_features = netdev->netdev_class->get_features;
error = get_features
? get_features(netdev, current, advertised, supported,
peer)
: EOPNOTSUPP;
if (error) {
*current = *advertised = *supported = *peer = 0;
}
return error;
}
/* Returns the maximum speed of a network connection that has the NETDEV_F_*
* bits in 'features', in bits per second. If no bits that indicate a speed
* are set in 'features', returns 'default_bps'. */
uint64_t
netdev_features_to_bps(enum netdev_features features,
uint64_t default_bps)
{
enum {
F_1000000MB = NETDEV_F_1TB_FD,
F_100000MB = NETDEV_F_100GB_FD,
F_40000MB = NETDEV_F_40GB_FD,
F_10000MB = NETDEV_F_10GB_FD,
F_1000MB = NETDEV_F_1GB_HD | NETDEV_F_1GB_FD,
F_100MB = NETDEV_F_100MB_HD | NETDEV_F_100MB_FD,
F_10MB = NETDEV_F_10MB_HD | NETDEV_F_10MB_FD
};
return ( features & F_1000000MB ? UINT64_C(1000000000000)
: features & F_100000MB ? UINT64_C(100000000000)
: features & F_40000MB ? UINT64_C(40000000000)
: features & F_10000MB ? UINT64_C(10000000000)
: features & F_1000MB ? UINT64_C(1000000000)
: features & F_100MB ? UINT64_C(100000000)
: features & F_10MB ? UINT64_C(10000000)
: default_bps);
}
/* Returns true if any of the NETDEV_F_* bits that indicate a full-duplex link
* are set in 'features', otherwise false. */
bool
netdev_features_is_full_duplex(enum netdev_features features)
{
return (features & (NETDEV_F_10MB_FD | NETDEV_F_100MB_FD | NETDEV_F_1GB_FD
| NETDEV_F_10GB_FD | NETDEV_F_40GB_FD
| NETDEV_F_100GB_FD | NETDEV_F_1TB_FD)) != 0;
}
/* Set the features advertised by 'netdev' to 'advertise'. Returns 0 if
* successful, otherwise a positive errno value. */
int
netdev_set_advertisements(struct netdev *netdev,
enum netdev_features advertise)
{
return (netdev->netdev_class->set_advertisements
? netdev->netdev_class->set_advertisements(