-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrte_ethdev.c
5827 lines (4829 loc) · 146 KB
/
rte_ethdev.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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2017 Intel Corporation
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <rte_byteorder.h>
#include <rte_log.h>
#include <rte_debug.h>
#include <rte_interrupts.h>
#include <rte_memory.h>
#include <rte_memcpy.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_branch_prediction.h>
#include <rte_common.h>
#include <rte_mempool.h>
#include <rte_malloc.h>
#include <rte_mbuf.h>
#include <rte_errno.h>
#include <rte_spinlock.h>
#include <rte_string_fns.h>
#include <rte_kvargs.h>
#include <rte_class.h>
#include <rte_ether.h>
#include <rte_telemetry.h>
#include "rte_ethdev_trace.h"
#include "rte_ethdev.h"
#include "ethdev_driver.h"
#include "ethdev_profile.h"
#include "ethdev_private.h"
static const char *MZ_RTE_ETH_DEV_DATA = "rte_eth_dev_data";
struct rte_eth_dev rte_eth_devices[RTE_MAX_ETHPORTS];
/* spinlock for eth device callbacks */
static rte_spinlock_t eth_dev_cb_lock = RTE_SPINLOCK_INITIALIZER;
/* spinlock for add/remove rx callbacks */
static rte_spinlock_t eth_dev_rx_cb_lock = RTE_SPINLOCK_INITIALIZER;
/* spinlock for add/remove tx callbacks */
static rte_spinlock_t eth_dev_tx_cb_lock = RTE_SPINLOCK_INITIALIZER;
/* spinlock for shared data allocation */
static rte_spinlock_t eth_dev_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
/* store statistics names and its offset in stats structure */
struct rte_eth_xstats_name_off {
char name[RTE_ETH_XSTATS_NAME_SIZE];
unsigned offset;
};
/* Shared memory between primary and secondary processes. */
static struct {
uint64_t next_owner_id;
rte_spinlock_t ownership_lock;
struct rte_eth_dev_data data[RTE_MAX_ETHPORTS];
} *eth_dev_shared_data;
static const struct rte_eth_xstats_name_off eth_dev_stats_strings[] = {
{"rx_good_packets", offsetof(struct rte_eth_stats, ipackets)},
{"tx_good_packets", offsetof(struct rte_eth_stats, opackets)},
{"rx_good_bytes", offsetof(struct rte_eth_stats, ibytes)},
{"tx_good_bytes", offsetof(struct rte_eth_stats, obytes)},
{"rx_missed_errors", offsetof(struct rte_eth_stats, imissed)},
{"rx_errors", offsetof(struct rte_eth_stats, ierrors)},
{"tx_errors", offsetof(struct rte_eth_stats, oerrors)},
{"rx_mbuf_allocation_errors", offsetof(struct rte_eth_stats,
rx_nombuf)},
};
#define RTE_NB_STATS RTE_DIM(eth_dev_stats_strings)
static const struct rte_eth_xstats_name_off eth_dev_rxq_stats_strings[] = {
{"packets", offsetof(struct rte_eth_stats, q_ipackets)},
{"bytes", offsetof(struct rte_eth_stats, q_ibytes)},
{"errors", offsetof(struct rte_eth_stats, q_errors)},
};
#define RTE_NB_RXQ_STATS RTE_DIM(eth_dev_rxq_stats_strings)
static const struct rte_eth_xstats_name_off eth_dev_txq_stats_strings[] = {
{"packets", offsetof(struct rte_eth_stats, q_opackets)},
{"bytes", offsetof(struct rte_eth_stats, q_obytes)},
};
#define RTE_NB_TXQ_STATS RTE_DIM(eth_dev_txq_stats_strings)
#define RTE_RX_OFFLOAD_BIT2STR(_name) \
{ DEV_RX_OFFLOAD_##_name, #_name }
#define RTE_ETH_RX_OFFLOAD_BIT2STR(_name) \
{ RTE_ETH_RX_OFFLOAD_##_name, #_name }
static const struct {
uint64_t offload;
const char *name;
} eth_dev_rx_offload_names[] = {
RTE_RX_OFFLOAD_BIT2STR(VLAN_STRIP),
RTE_RX_OFFLOAD_BIT2STR(IPV4_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(UDP_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(TCP_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(TCP_LRO),
RTE_RX_OFFLOAD_BIT2STR(QINQ_STRIP),
RTE_RX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(MACSEC_STRIP),
RTE_RX_OFFLOAD_BIT2STR(HEADER_SPLIT),
RTE_RX_OFFLOAD_BIT2STR(VLAN_FILTER),
RTE_RX_OFFLOAD_BIT2STR(VLAN_EXTEND),
RTE_RX_OFFLOAD_BIT2STR(JUMBO_FRAME),
RTE_RX_OFFLOAD_BIT2STR(SCATTER),
RTE_RX_OFFLOAD_BIT2STR(TIMESTAMP),
RTE_RX_OFFLOAD_BIT2STR(SECURITY),
RTE_RX_OFFLOAD_BIT2STR(KEEP_CRC),
RTE_RX_OFFLOAD_BIT2STR(SCTP_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
RTE_RX_OFFLOAD_BIT2STR(RSS_HASH),
RTE_ETH_RX_OFFLOAD_BIT2STR(BUFFER_SPLIT),
};
#undef RTE_RX_OFFLOAD_BIT2STR
#undef RTE_ETH_RX_OFFLOAD_BIT2STR
#define RTE_TX_OFFLOAD_BIT2STR(_name) \
{ DEV_TX_OFFLOAD_##_name, #_name }
static const struct {
uint64_t offload;
const char *name;
} eth_dev_tx_offload_names[] = {
RTE_TX_OFFLOAD_BIT2STR(VLAN_INSERT),
RTE_TX_OFFLOAD_BIT2STR(IPV4_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(UDP_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(TCP_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(SCTP_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(TCP_TSO),
RTE_TX_OFFLOAD_BIT2STR(UDP_TSO),
RTE_TX_OFFLOAD_BIT2STR(OUTER_IPV4_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(QINQ_INSERT),
RTE_TX_OFFLOAD_BIT2STR(VXLAN_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(GRE_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(IPIP_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(GENEVE_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(MACSEC_INSERT),
RTE_TX_OFFLOAD_BIT2STR(MT_LOCKFREE),
RTE_TX_OFFLOAD_BIT2STR(MULTI_SEGS),
RTE_TX_OFFLOAD_BIT2STR(MBUF_FAST_FREE),
RTE_TX_OFFLOAD_BIT2STR(SECURITY),
RTE_TX_OFFLOAD_BIT2STR(UDP_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(IP_TNL_TSO),
RTE_TX_OFFLOAD_BIT2STR(OUTER_UDP_CKSUM),
RTE_TX_OFFLOAD_BIT2STR(SEND_ON_TIMESTAMP),
};
#undef RTE_TX_OFFLOAD_BIT2STR
/**
* The user application callback description.
*
* It contains callback address to be registered by user application,
* the pointer to the parameters for callback, and the event type.
*/
struct rte_eth_dev_callback {
TAILQ_ENTRY(rte_eth_dev_callback) next; /**< Callbacks list */
rte_eth_dev_cb_fn cb_fn; /**< Callback address */
void *cb_arg; /**< Parameter for callback */
void *ret_param; /**< Return parameter */
enum rte_eth_event_type event; /**< Interrupt event type */
uint32_t active; /**< Callback is executing */
};
enum {
STAT_QMAP_TX = 0,
STAT_QMAP_RX
};
int
rte_eth_iterator_init(struct rte_dev_iterator *iter, const char *devargs_str)
{
int ret;
struct rte_devargs devargs = {.args = NULL};
const char *bus_param_key;
char *bus_str = NULL;
char *cls_str = NULL;
int str_size;
memset(iter, 0, sizeof(*iter));
/*
* The devargs string may use various syntaxes:
* - 0000:08:00.0,representor=[1-3]
* - pci:0000:06:00.0,representor=[0,5]
* - class=eth,mac=00:11:22:33:44:55
* A new syntax is in development (not yet supported):
* - bus=X,paramX=x/class=Y,paramY=y/driver=Z,paramZ=z
*/
/*
* Handle pure class filter (i.e. without any bus-level argument),
* from future new syntax.
* rte_devargs_parse() is not yet supporting the new syntax,
* that's why this simple case is temporarily parsed here.
*/
#define iter_anybus_str "class=eth,"
if (strncmp(devargs_str, iter_anybus_str,
strlen(iter_anybus_str)) == 0) {
iter->cls_str = devargs_str + strlen(iter_anybus_str);
goto end;
}
/* Split bus, device and parameters. */
ret = rte_devargs_parse(&devargs, devargs_str);
if (ret != 0)
goto error;
/*
* Assume parameters of old syntax can match only at ethdev level.
* Extra parameters will be ignored, thanks to "+" prefix.
*/
str_size = strlen(devargs.args) + 2;
cls_str = malloc(str_size);
if (cls_str == NULL) {
ret = -ENOMEM;
goto error;
}
ret = snprintf(cls_str, str_size, "+%s", devargs.args);
if (ret != str_size - 1) {
ret = -EINVAL;
goto error;
}
iter->cls_str = cls_str;
free(devargs.args); /* allocated by rte_devargs_parse() */
devargs.args = NULL;
iter->bus = devargs.bus;
if (iter->bus->dev_iterate == NULL) {
ret = -ENOTSUP;
goto error;
}
/* Convert bus args to new syntax for use with new API dev_iterate. */
if (strcmp(iter->bus->name, "vdev") == 0) {
bus_param_key = "name";
} else if (strcmp(iter->bus->name, "pci") == 0) {
bus_param_key = "addr";
} else {
ret = -ENOTSUP;
goto error;
}
str_size = strlen(bus_param_key) + strlen(devargs.name) + 2;
bus_str = malloc(str_size);
if (bus_str == NULL) {
ret = -ENOMEM;
goto error;
}
ret = snprintf(bus_str, str_size, "%s=%s",
bus_param_key, devargs.name);
if (ret != str_size - 1) {
ret = -EINVAL;
goto error;
}
iter->bus_str = bus_str;
end:
iter->cls = rte_class_find_by_name("eth");
return 0;
error:
if (ret == -ENOTSUP)
RTE_ETHDEV_LOG(ERR, "Bus %s does not support iterating.\n",
iter->bus->name);
free(devargs.args);
free(bus_str);
free(cls_str);
return ret;
}
uint16_t
rte_eth_iterator_next(struct rte_dev_iterator *iter)
{
if (iter->cls == NULL) /* invalid ethdev iterator */
return RTE_MAX_ETHPORTS;
do { /* loop to try all matching rte_device */
/* If not pure ethdev filter and */
if (iter->bus != NULL &&
/* not in middle of rte_eth_dev iteration, */
iter->class_device == NULL) {
/* get next rte_device to try. */
iter->device = iter->bus->dev_iterate(
iter->device, iter->bus_str, iter);
if (iter->device == NULL)
break; /* no more rte_device candidate */
}
/* A device is matching bus part, need to check ethdev part. */
iter->class_device = iter->cls->dev_iterate(
iter->class_device, iter->cls_str, iter);
if (iter->class_device != NULL)
return eth_dev_to_id(iter->class_device); /* match */
} while (iter->bus != NULL); /* need to try next rte_device */
/* No more ethdev port to iterate. */
rte_eth_iterator_cleanup(iter);
return RTE_MAX_ETHPORTS;
}
void
rte_eth_iterator_cleanup(struct rte_dev_iterator *iter)
{
if (iter->bus_str == NULL)
return; /* nothing to free in pure class filter */
free(RTE_CAST_FIELD(iter, bus_str, char *)); /* workaround const */
free(RTE_CAST_FIELD(iter, cls_str, char *)); /* workaround const */
memset(iter, 0, sizeof(*iter));
}
uint16_t
rte_eth_find_next(uint16_t port_id)
{
while (port_id < RTE_MAX_ETHPORTS &&
rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED)
port_id++;
if (port_id >= RTE_MAX_ETHPORTS)
return RTE_MAX_ETHPORTS;
return port_id;
}
/*
* Macro to iterate over all valid ports for internal usage.
* Note: RTE_ETH_FOREACH_DEV is different because filtering owned ports.
*/
#define RTE_ETH_FOREACH_VALID_DEV(port_id) \
for (port_id = rte_eth_find_next(0); \
port_id < RTE_MAX_ETHPORTS; \
port_id = rte_eth_find_next(port_id + 1))
uint16_t
rte_eth_find_next_of(uint16_t port_id, const struct rte_device *parent)
{
port_id = rte_eth_find_next(port_id);
while (port_id < RTE_MAX_ETHPORTS &&
rte_eth_devices[port_id].device != parent)
port_id = rte_eth_find_next(port_id + 1);
return port_id;
}
uint16_t
rte_eth_find_next_sibling(uint16_t port_id, uint16_t ref_port_id)
{
RTE_ETH_VALID_PORTID_OR_ERR_RET(ref_port_id, RTE_MAX_ETHPORTS);
return rte_eth_find_next_of(port_id,
rte_eth_devices[ref_port_id].device);
}
static void
eth_dev_shared_data_prepare(void)
{
const unsigned flags = 0;
const struct rte_memzone *mz;
rte_spinlock_lock(ð_dev_shared_data_lock);
if (eth_dev_shared_data == NULL) {
if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
/* Allocate port data and ownership shared memory. */
mz = rte_memzone_reserve(MZ_RTE_ETH_DEV_DATA,
sizeof(*eth_dev_shared_data),
rte_socket_id(), flags);
} else
mz = rte_memzone_lookup(MZ_RTE_ETH_DEV_DATA);
if (mz == NULL)
rte_panic("Cannot allocate ethdev shared data\n");
eth_dev_shared_data = mz->addr;
if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
eth_dev_shared_data->next_owner_id =
RTE_ETH_DEV_NO_OWNER + 1;
rte_spinlock_init(ð_dev_shared_data->ownership_lock);
memset(eth_dev_shared_data->data, 0,
sizeof(eth_dev_shared_data->data));
}
}
rte_spinlock_unlock(ð_dev_shared_data_lock);
}
static bool
eth_dev_is_allocated(const struct rte_eth_dev *ethdev)
{
return ethdev->data->name[0] != '\0';
}
static struct rte_eth_dev *
eth_dev_allocated(const char *name)
{
uint16_t i;
RTE_BUILD_BUG_ON(RTE_MAX_ETHPORTS >= UINT16_MAX);
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
if (rte_eth_devices[i].data != NULL &&
strcmp(rte_eth_devices[i].data->name, name) == 0)
return &rte_eth_devices[i];
}
return NULL;
}
struct rte_eth_dev *
rte_eth_dev_allocated(const char *name)
{
struct rte_eth_dev *ethdev;
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
ethdev = eth_dev_allocated(name);
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return ethdev;
}
static uint16_t
eth_dev_find_free_port(void)
{
uint16_t i;
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
/* Using shared name field to find a free port. */
if (eth_dev_shared_data->data[i].name[0] == '\0') {
RTE_ASSERT(rte_eth_devices[i].state ==
RTE_ETH_DEV_UNUSED);
return i;
}
}
return RTE_MAX_ETHPORTS;
}
static struct rte_eth_dev *
eth_dev_get(uint16_t port_id)
{
struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
eth_dev->data = ð_dev_shared_data->data[port_id];
return eth_dev;
}
struct rte_eth_dev *
rte_eth_dev_allocate(const char *name)
{
uint16_t port_id;
struct rte_eth_dev *eth_dev = NULL;
size_t name_len;
name_len = strnlen(name, RTE_ETH_NAME_MAX_LEN);
if (name_len == 0) {
RTE_ETHDEV_LOG(ERR, "Zero length Ethernet device name\n");
return NULL;
}
if (name_len >= RTE_ETH_NAME_MAX_LEN) {
RTE_ETHDEV_LOG(ERR, "Ethernet device name is too long\n");
return NULL;
}
eth_dev_shared_data_prepare();
/* Synchronize port creation between primary and secondary threads. */
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
if (eth_dev_allocated(name) != NULL) {
RTE_ETHDEV_LOG(ERR,
"Ethernet device with name %s already allocated\n",
name);
goto unlock;
}
port_id = eth_dev_find_free_port();
if (port_id == RTE_MAX_ETHPORTS) {
RTE_ETHDEV_LOG(ERR,
"Reached maximum number of Ethernet ports\n");
goto unlock;
}
eth_dev = eth_dev_get(port_id);
strlcpy(eth_dev->data->name, name, sizeof(eth_dev->data->name));
eth_dev->data->port_id = port_id;
eth_dev->data->mtu = RTE_ETHER_MTU;
pthread_mutex_init(ð_dev->data->flow_ops_mutex, NULL);
unlock:
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return eth_dev;
}
/*
* Attach to a port already registered by the primary process, which
* makes sure that the same device would have the same port id both
* in the primary and secondary process.
*/
struct rte_eth_dev *
rte_eth_dev_attach_secondary(const char *name)
{
uint16_t i;
struct rte_eth_dev *eth_dev = NULL;
eth_dev_shared_data_prepare();
/* Synchronize port attachment to primary port creation and release. */
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
if (strcmp(eth_dev_shared_data->data[i].name, name) == 0)
break;
}
if (i == RTE_MAX_ETHPORTS) {
RTE_ETHDEV_LOG(ERR,
"Device %s is not driven by the primary process\n",
name);
} else {
eth_dev = eth_dev_get(i);
RTE_ASSERT(eth_dev->data->port_id == i);
}
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return eth_dev;
}
int
rte_eth_dev_release_port(struct rte_eth_dev *eth_dev)
{
if (eth_dev == NULL)
return -EINVAL;
eth_dev_shared_data_prepare();
if (eth_dev->state != RTE_ETH_DEV_UNUSED)
rte_eth_dev_callback_process(eth_dev,
RTE_ETH_EVENT_DESTROY, NULL);
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
eth_dev->state = RTE_ETH_DEV_UNUSED;
eth_dev->device = NULL;
eth_dev->process_private = NULL;
eth_dev->intr_handle = NULL;
eth_dev->rx_pkt_burst = NULL;
eth_dev->tx_pkt_burst = NULL;
eth_dev->tx_pkt_prepare = NULL;
eth_dev->rx_queue_count = NULL;
eth_dev->rx_descriptor_done = NULL;
eth_dev->rx_descriptor_status = NULL;
eth_dev->tx_descriptor_status = NULL;
eth_dev->dev_ops = NULL;
if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
rte_free(eth_dev->data->rx_queues);
rte_free(eth_dev->data->tx_queues);
rte_free(eth_dev->data->mac_addrs);
rte_free(eth_dev->data->hash_mac_addrs);
rte_free(eth_dev->data->dev_private);
pthread_mutex_destroy(ð_dev->data->flow_ops_mutex);
memset(eth_dev->data, 0, sizeof(struct rte_eth_dev_data));
}
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return 0;
}
int
rte_eth_dev_is_valid_port(uint16_t port_id)
{
if (port_id >= RTE_MAX_ETHPORTS ||
(rte_eth_devices[port_id].state == RTE_ETH_DEV_UNUSED))
return 0;
else
return 1;
}
static int
eth_is_valid_owner_id(uint64_t owner_id)
{
if (owner_id == RTE_ETH_DEV_NO_OWNER ||
eth_dev_shared_data->next_owner_id <= owner_id)
return 0;
return 1;
}
uint64_t
rte_eth_find_next_owned_by(uint16_t port_id, const uint64_t owner_id)
{
port_id = rte_eth_find_next(port_id);
while (port_id < RTE_MAX_ETHPORTS &&
rte_eth_devices[port_id].data->owner.id != owner_id)
port_id = rte_eth_find_next(port_id + 1);
return port_id;
}
int
rte_eth_dev_owner_new(uint64_t *owner_id)
{
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
*owner_id = eth_dev_shared_data->next_owner_id++;
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return 0;
}
static int
eth_dev_owner_set(const uint16_t port_id, const uint64_t old_owner_id,
const struct rte_eth_dev_owner *new_owner)
{
struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
struct rte_eth_dev_owner *port_owner;
if (port_id >= RTE_MAX_ETHPORTS || !eth_dev_is_allocated(ethdev)) {
RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
port_id);
return -ENODEV;
}
if (!eth_is_valid_owner_id(new_owner->id) &&
!eth_is_valid_owner_id(old_owner_id)) {
RTE_ETHDEV_LOG(ERR,
"Invalid owner old_id=%016"PRIx64" new_id=%016"PRIx64"\n",
old_owner_id, new_owner->id);
return -EINVAL;
}
port_owner = &rte_eth_devices[port_id].data->owner;
if (port_owner->id != old_owner_id) {
RTE_ETHDEV_LOG(ERR,
"Cannot set owner to port %u already owned by %s_%016"PRIX64"\n",
port_id, port_owner->name, port_owner->id);
return -EPERM;
}
/* can not truncate (same structure) */
strlcpy(port_owner->name, new_owner->name, RTE_ETH_MAX_OWNER_NAME_LEN);
port_owner->id = new_owner->id;
RTE_ETHDEV_LOG(DEBUG, "Port %u owner is %s_%016"PRIx64"\n",
port_id, new_owner->name, new_owner->id);
return 0;
}
int
rte_eth_dev_owner_set(const uint16_t port_id,
const struct rte_eth_dev_owner *owner)
{
int ret;
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
ret = eth_dev_owner_set(port_id, RTE_ETH_DEV_NO_OWNER, owner);
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return ret;
}
int
rte_eth_dev_owner_unset(const uint16_t port_id, const uint64_t owner_id)
{
const struct rte_eth_dev_owner new_owner = (struct rte_eth_dev_owner)
{.id = RTE_ETH_DEV_NO_OWNER, .name = ""};
int ret;
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
ret = eth_dev_owner_set(port_id, owner_id, &new_owner);
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return ret;
}
int
rte_eth_dev_owner_delete(const uint64_t owner_id)
{
uint16_t port_id;
int ret = 0;
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
if (eth_is_valid_owner_id(owner_id)) {
for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
if (rte_eth_devices[port_id].data->owner.id == owner_id)
memset(&rte_eth_devices[port_id].data->owner, 0,
sizeof(struct rte_eth_dev_owner));
RTE_ETHDEV_LOG(NOTICE,
"All port owners owned by %016"PRIx64" identifier have removed\n",
owner_id);
} else {
RTE_ETHDEV_LOG(ERR,
"Invalid owner id=%016"PRIx64"\n",
owner_id);
ret = -EINVAL;
}
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return ret;
}
int
rte_eth_dev_owner_get(const uint16_t port_id, struct rte_eth_dev_owner *owner)
{
int ret = 0;
struct rte_eth_dev *ethdev = &rte_eth_devices[port_id];
eth_dev_shared_data_prepare();
rte_spinlock_lock(ð_dev_shared_data->ownership_lock);
if (port_id >= RTE_MAX_ETHPORTS || !eth_dev_is_allocated(ethdev)) {
RTE_ETHDEV_LOG(ERR, "Port id %"PRIu16" is not allocated\n",
port_id);
ret = -ENODEV;
} else {
rte_memcpy(owner, ðdev->data->owner, sizeof(*owner));
}
rte_spinlock_unlock(ð_dev_shared_data->ownership_lock);
return ret;
}
int
rte_eth_dev_socket_id(uint16_t port_id)
{
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -1);
return rte_eth_devices[port_id].data->numa_node;
}
void *
rte_eth_dev_get_sec_ctx(uint16_t port_id)
{
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
return rte_eth_devices[port_id].security_ctx;
}
uint16_t
rte_eth_dev_count_avail(void)
{
uint16_t p;
uint16_t count;
count = 0;
RTE_ETH_FOREACH_DEV(p)
count++;
return count;
}
uint16_t
rte_eth_dev_count_total(void)
{
uint16_t port, count = 0;
RTE_ETH_FOREACH_VALID_DEV(port)
count++;
return count;
}
int
rte_eth_dev_get_name_by_port(uint16_t port_id, char *name)
{
char *tmp;
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
if (name == NULL) {
RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
return -EINVAL;
}
/* shouldn't check 'rte_eth_devices[i].data',
* because it might be overwritten by VDEV PMD */
tmp = eth_dev_shared_data->data[port_id].name;
strcpy(name, tmp);
return 0;
}
int
rte_eth_dev_get_port_by_name(const char *name, uint16_t *port_id)
{
uint16_t pid;
if (name == NULL) {
RTE_ETHDEV_LOG(ERR, "Null pointer is specified\n");
return -EINVAL;
}
RTE_ETH_FOREACH_VALID_DEV(pid)
if (!strcmp(name, eth_dev_shared_data->data[pid].name)) {
*port_id = pid;
return 0;
}
return -ENODEV;
}
static int
eth_err(uint16_t port_id, int ret)
{
if (ret == 0)
return 0;
if (rte_eth_dev_is_removed(port_id))
return -EIO;
return ret;
}
static int
eth_dev_rx_queue_config(struct rte_eth_dev *dev, uint16_t nb_queues)
{
uint16_t old_nb_queues = dev->data->nb_rx_queues;
void **rxq;
unsigned i;
if (dev->data->rx_queues == NULL && nb_queues != 0) { /* first time configuration */
dev->data->rx_queues = rte_zmalloc("ethdev->rx_queues",
sizeof(dev->data->rx_queues[0]) * nb_queues,
RTE_CACHE_LINE_SIZE);
if (dev->data->rx_queues == NULL) {
dev->data->nb_rx_queues = 0;
return -(ENOMEM);
}
} else if (dev->data->rx_queues != NULL && nb_queues != 0) { /* re-configure */
RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
rxq = dev->data->rx_queues;
for (i = nb_queues; i < old_nb_queues; i++)
(*dev->dev_ops->rx_queue_release)(rxq[i]);
rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
RTE_CACHE_LINE_SIZE);
if (rxq == NULL)
return -(ENOMEM);
if (nb_queues > old_nb_queues) {
uint16_t new_qs = nb_queues - old_nb_queues;
memset(rxq + old_nb_queues, 0,
sizeof(rxq[0]) * new_qs);
}
dev->data->rx_queues = rxq;
} else if (dev->data->rx_queues != NULL && nb_queues == 0) {
RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_release, -ENOTSUP);
rxq = dev->data->rx_queues;
for (i = nb_queues; i < old_nb_queues; i++)
(*dev->dev_ops->rx_queue_release)(rxq[i]);
rte_free(dev->data->rx_queues);
dev->data->rx_queues = NULL;
}
dev->data->nb_rx_queues = nb_queues;
return 0;
}
static int
eth_dev_validate_rx_queue(const struct rte_eth_dev *dev, uint16_t rx_queue_id)
{
uint16_t port_id;
if (rx_queue_id >= dev->data->nb_rx_queues) {
port_id = dev->data->port_id;
RTE_ETHDEV_LOG(ERR,
"Invalid Rx queue_id=%u of device with port_id=%u\n",
rx_queue_id, port_id);
return -EINVAL;
}
if (dev->data->rx_queues[rx_queue_id] == NULL) {
port_id = dev->data->port_id;
RTE_ETHDEV_LOG(ERR,
"Queue %u of device with port_id=%u has not been setup\n",
rx_queue_id, port_id);
return -EINVAL;
}
return 0;
}
static int
eth_dev_validate_tx_queue(const struct rte_eth_dev *dev, uint16_t tx_queue_id)
{
uint16_t port_id;
if (tx_queue_id >= dev->data->nb_tx_queues) {
port_id = dev->data->port_id;
RTE_ETHDEV_LOG(ERR,
"Invalid Tx queue_id=%u of device with port_id=%u\n",
tx_queue_id, port_id);
return -EINVAL;
}
if (dev->data->tx_queues[tx_queue_id] == NULL) {
port_id = dev->data->port_id;
RTE_ETHDEV_LOG(ERR,
"Queue %u of device with port_id=%u has not been setup\n",
tx_queue_id, port_id);
return -EINVAL;
}
return 0;
}
int
rte_eth_dev_rx_queue_start(uint16_t port_id, uint16_t rx_queue_id)
{
struct rte_eth_dev *dev;
int ret;
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
dev = &rte_eth_devices[port_id];
if (!dev->data->dev_started) {
RTE_ETHDEV_LOG(ERR,
"Port %u must be started before start any queue\n",
port_id);
return -EINVAL;
}
ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
if (ret != 0)
return ret;
RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_start, -ENOTSUP);
if (rte_eth_dev_is_rx_hairpin_queue(dev, rx_queue_id)) {
RTE_ETHDEV_LOG(INFO,
"Can't start Rx hairpin queue %"PRIu16" of device with port_id=%"PRIu16"\n",
rx_queue_id, port_id);
return -EINVAL;
}
if (dev->data->rx_queue_state[rx_queue_id] != RTE_ETH_QUEUE_STATE_STOPPED) {
RTE_ETHDEV_LOG(INFO,
"Queue %"PRIu16" of device with port_id=%"PRIu16" already started\n",
rx_queue_id, port_id);
return 0;
}
return eth_err(port_id, dev->dev_ops->rx_queue_start(dev,
rx_queue_id));
}
int
rte_eth_dev_rx_queue_stop(uint16_t port_id, uint16_t rx_queue_id)
{
struct rte_eth_dev *dev;
int ret;
RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
dev = &rte_eth_devices[port_id];
ret = eth_dev_validate_rx_queue(dev, rx_queue_id);
if (ret != 0)
return ret;
RTE_FUNC_PTR_OR_ERR_RET(*dev->dev_ops->rx_queue_stop, -ENOTSUP);