forked from iqiyi/dpvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl.c
1624 lines (1385 loc) · 50.5 KB
/
ctrl.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
/*
* DPVS is a software load balancer (Virtual Server) based on DPDK.
*
* Copyright (C) 2021 iQIYI (www.iqiyi.com).
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <sys/socket.h>
#include <fcntl.h>
#include <sys/un.h>
#include <unistd.h>
#include <libgen.h>
#include <string.h>
#include <assert.h>
#include "ctrl.h"
#include "netif.h"
#include "mempool.h"
#include "parser/parser.h"
#include "scheduler.h"
/////////////////////////////////// lcore msg ///////////////////////////////////////////
#define MSG_MAX_LCORE_SUPPORTED 64
uint64_t slave_lcore_mask; /* bit-wise enabled lcores */
uint8_t slave_lcore_nb; /* slave lcore number */
lcoreid_t master_lcore; /* master lcore id */
struct dpvs_mempool *msg_pool; /* memory pool for msg */
#define MSG_TIMEOUT_US 2000
static int g_msg_timeout = MSG_TIMEOUT_US;
static uint8_t g_msg_prio = MSG_PRIO_LOW;
const char* dpvs_sockopts_name[] = {
DPVSMSG_SOCKOPT_ENUM(ENUM_STRING)
};
#define DPVS_MT_BITS 8
#define DPVS_MT_LEN (1 << DPVS_MT_BITS)
#define DPVS_MT_MASK (DPVS_MT_LEN - 1)
#define DPVS_MSG_RING_SIZE_DEF 4096
#define DPVS_MSG_RING_SIZE_MIN 256
#define DPVS_MSG_RING_SIZE_MAX 524288
static uint32_t msg_ring_size = DPVS_MSG_RING_SIZE_DEF;
/* per-lcore msg-type array */
typedef struct list_head msg_type_array_t[DPVS_MT_LEN];
typedef rte_rwlock_t msg_type_lock_t[DPVS_MT_LEN];
msg_type_array_t mt_array[DPVS_MAX_LCORE];
msg_type_lock_t mt_lock[DPVS_MAX_LCORE];
/* multicast msg hlist, to collect reply msg from slaves (used on master lcore only) */
#define DPVS_MC_HLIST_BITS 8
#define DPVS_MC_HLIST_LEN (1 << DPVS_MC_HLIST_BITS)
#define DPVS_MC_HLIST_MASK (DPVS_MC_HLIST_LEN - 1)
struct list_head mc_wait_hlist[DPVS_MC_HLIST_LEN];
/* per-lcore msg queue */
struct rte_ring *msg_ring[DPVS_MAX_LCORE];
#ifdef CONFIG_MSG_DEBUG
rte_atomic64_t n_msg_allc;
rte_atomic64_t n_msg_free;
rte_atomic32_t n_msg_using;
inline static void msg_debug_init(void)
{
rte_atomic64_init(&n_msg_allc);
rte_atomic64_init(&n_msg_free);
rte_atomic32_init(&n_msg_using);
}
inline static void msg_debug_alloc(void)
{
rte_atomic64_inc(&n_msg_allc);
rte_atomic32_inc(&n_msg_using);
}
inline static void msg_debug_free(void)
{
rte_atomic64_inc(&n_msg_free);
rte_atomic32_dec(&n_msg_using);
}
inline static void msg_debug_dump(void)
{
uint64_t allc;
allc = rte_atomic64_read(&n_msg_allc);
if (likely(allc % 100000))
return;
RTE_LOG(INFO, MSGMGR, "%s: allocated=%ld, freed=%ld, processing=%d\n",
__func__, allc, rte_atomic64_read(&n_msg_free),
rte_atomic32_read(&n_msg_using));
}
inline static int msg_memory_stats(char *buf, int len)
{
snprintf(buf, len,
"allocated:%ld, freed:%ld, processing:%d",
rte_atomic64_read(&n_msg_allc),
rte_atomic64_read(&n_msg_free),
rte_atomic32_read(&n_msg_using));
return strlen(buf);
}
#else
inline static void msg_debug_init(void) {}
inline static void msg_debug_alloc(void) {}
inline static void msg_debug_free(void) {}
inline static void msg_debug_dump(void) {}
inline static int msg_memory_stats(char *buf, int len) { return 0; }
#endif
static inline int mc_queue_hashkey(msgid_t type, uint32_t seq)
{
return (((uint32_t)type) ^ seq) & DPVS_MC_HLIST_MASK;
}
/* only be called on master lcore, thus no lock needed */
static inline void mc_queue_hash(struct dpvs_multicast_queue *mcq)
{
int hashkey;
if (unlikely(!mcq))
return;
hashkey = mc_queue_hashkey(mcq->type, mcq->seq);
list_add_tail(&mcq->list, &mc_wait_hlist[hashkey]);
}
static inline void mc_queue_unhash(struct dpvs_multicast_queue *mcq)
{
if (unlikely(!mcq))
return;
list_del_init(&mcq->list);
}
static inline struct dpvs_multicast_queue* mc_queue_get(msgid_t type, uint32_t seq)
{
int hashkey;
struct dpvs_multicast_queue *mcq;
assert(rte_lcore_id() == master_lcore);
hashkey = mc_queue_hashkey(type, seq);
list_for_each_entry(mcq, &mc_wait_hlist[hashkey], list) {
if (mcq->type == type && mcq->seq == seq) {
return mcq;
}
}
return NULL;
}
static inline int mt_hashkey(msgid_t type)
{
return type & DPVS_MT_MASK;
}
static struct dpvs_msg_type* msg_type_get(msgid_t type, lcoreid_t cid)
{
int hashkey = mt_hashkey(type);
struct dpvs_msg_type *mt;
if (unlikely(cid >= DPVS_MAX_LCORE))
return NULL;
rte_rwlock_read_lock(&mt_lock[cid][hashkey]);
list_for_each_entry(mt, &mt_array[cid][hashkey], list) {
if (type == mt->type /*&& mode == mt->mode*/) {
rte_atomic32_inc(&mt->refcnt);
rte_rwlock_read_unlock(&mt_lock[cid][hashkey]);
return mt;
}
}
rte_rwlock_read_unlock(&mt_lock[cid][hashkey]);
return NULL;
}
static inline void msg_type_put(struct dpvs_msg_type *mt)
{
if (unlikely(NULL == mt))
return;
rte_atomic32_dec(&mt->refcnt);
}
int msg_type_register(const struct dpvs_msg_type *msg_type)
{
int hashkey;
struct dpvs_msg_type *mt;
if (unlikely(NULL == msg_type || msg_type->cid >= DPVS_MAX_LCORE)) {
RTE_LOG(WARNING, MSGMGR, "%s: invalid args !\n", __func__);
return EDPVS_INVAL;
}
hashkey = mt_hashkey(msg_type->type);
mt = msg_type_get(msg_type->type, /*msg_type->mode, */msg_type->cid);
if (NULL != mt) {
RTE_LOG(WARNING, MSGMGR, "%s: msg type %d mode %s already registered\n",
__func__, mt->type, mt->mode == DPVS_MSG_UNICAST ? "UNICAST" : "MULTICAST");
msg_type_put(mt);
return EDPVS_EXIST;
}
mt = rte_zmalloc("msg_type", sizeof(struct dpvs_msg_type), RTE_CACHE_LINE_SIZE);
if (unlikely(NULL == mt)) {
RTE_LOG(ERR, MSGMGR, "%s: no memory !\n", __func__);
return EDPVS_NOMEM;
}
memcpy(mt, msg_type, sizeof(struct dpvs_msg_type));
rte_atomic32_set(&mt->refcnt, 0);
rte_rwlock_write_lock(&mt_lock[msg_type->cid][hashkey]);
list_add_tail(&mt->list, &mt_array[msg_type->cid][hashkey]);
rte_rwlock_write_unlock(&mt_lock[msg_type->cid][hashkey]);
return EDPVS_OK;
}
int msg_type_unregister(const struct dpvs_msg_type *msg_type)
{
int hashkey;
struct dpvs_msg_type *mt;
if (unlikely(NULL == msg_type) || msg_type->cid >= DPVS_MAX_LCORE) {
RTE_LOG(WARNING, MSGMGR, "%s: invalid args !\n", __func__);
return EDPVS_INVAL;
}
hashkey = mt_hashkey(msg_type->type);
mt = msg_type_get(msg_type->type, /*msg_type->mode, */msg_type->cid);
if (NULL == mt) {
RTE_LOG(WARNING, MSGMGR, "%s: msg type %d mode %s not yet registered\n",
__func__, msg_type->type, msg_type->mode == DPVS_MSG_UNICAST ? "UNINCAST" : "MULTICAST");
return EDPVS_NOTEXIST;
}
rte_rwlock_write_lock(&mt_lock[msg_type->cid][hashkey]);
list_del_init(&mt->list);
rte_rwlock_write_unlock(&mt_lock[msg_type->cid][hashkey]);
msg_type_put(mt);
DPVS_WAIT_WHILE(rte_atomic32_read(&mt->refcnt) > 0);
rte_free(mt);
return EDPVS_OK;
}
inline static int default_mc_msg_cb(__rte_unused struct dpvs_multicast_queue *mcq)
{
return EDPVS_OK;
}
int msg_type_mc_register(const struct dpvs_msg_type *msg_type)
{
lcoreid_t cid;
struct dpvs_msg_type mt;
int ret = EDPVS_OK;
if (unlikely(NULL == msg_type))
return EDPVS_INVAL;
memset(&mt, 0, sizeof(mt));
mt.type = msg_type->type;
mt.mode = DPVS_MSG_MULTICAST;
for (cid = 0; cid < DPVS_MAX_LCORE; cid++) {
if (cid == master_lcore) {
mt.cid = cid;
mt.prio = msg_type->prio;
mt.unicast_msg_cb = NULL;
if (msg_type->multicast_msg_cb)
mt.multicast_msg_cb = msg_type->multicast_msg_cb;
else /* if no multicast callback given, then a default one is used, which do nothing now */
mt.multicast_msg_cb = default_mc_msg_cb;
} else if (slave_lcore_mask & (1L << cid)) {
mt.cid = cid;
mt.prio = MSG_PRIO_IGN; /* multi reply msg should always be sent */
mt.unicast_msg_cb = msg_type->unicast_msg_cb;
mt.multicast_msg_cb = NULL;
} else
continue;
ret = msg_type_register(&mt);
if (unlikely(ret < 0)) {
RTE_LOG(ERR, MSGMGR, "%s: fail to register multicast msg on lcore %d\n", __func__, cid);
return ret;
}
}
return EDPVS_OK;
}
int msg_type_mc_unregister(const struct dpvs_msg_type *msg_type)
{
lcoreid_t cid;
struct dpvs_msg_type mt;
int ret = EDPVS_OK;
if (unlikely(NULL == msg_type))
return EDPVS_INVAL;
memset(&mt, 0, sizeof(mt));
mt.type = msg_type->type;
mt.mode = DPVS_MSG_MULTICAST;
for (cid = 0; cid < DPVS_MAX_LCORE; cid++) {
if (cid == master_lcore) {
mt.cid = cid;
mt.prio = msg_type->prio;
mt.unicast_msg_cb = NULL;
if (msg_type->multicast_msg_cb)
mt.multicast_msg_cb = msg_type->multicast_msg_cb;
else
mt.multicast_msg_cb = default_mc_msg_cb;
} else if (slave_lcore_mask & (1L << cid)) {
mt.cid = cid;
mt.prio = MSG_PRIO_IGN;
mt.unicast_msg_cb = msg_type->unicast_msg_cb;
mt.multicast_msg_cb = NULL;
} else
continue;
ret = msg_type_unregister(&mt);
if (unlikely(ret < 0)) {
RTE_LOG(ERR, MSGMGR, "%s: fail to unregister mulitcast msg on lcore %d\n",
__func__, cid);
return ret;
}
}
return EDPVS_OK;
}
void* msg_reply_alloc(int size)
{
return dpvs_mempool_get(msg_pool, size);
}
void msg_reply_free(void *mptr)
{
return dpvs_mempool_put(msg_pool, mptr);
}
struct dpvs_msg* msg_make(msgid_t type, uint32_t seq,
msg_mode_t mode,
lcoreid_t cid,
uint32_t len, const void *data)
{
int total_len;
struct dpvs_msg *msg;
total_len = sizeof(struct dpvs_msg) + len;
msg = dpvs_mempool_get(msg_pool, total_len);
if (unlikely(NULL == msg))
return NULL;
memset(msg, 0, total_len);
rte_spinlock_init(&msg->lock);
msg->type = type;
msg->seq = seq;
msg->mode = mode;
msg->cid = cid;
msg->len = len;
if (len && data)
rte_memcpy(msg->data, data, len);
msg->reply.data = NULL;
msg->reply.len = 0;
rte_atomic16_init(&msg->refcnt);
rte_atomic16_inc(&msg->refcnt);
msg_debug_alloc();
msg_debug_dump();
return msg;
}
int msg_destroy(struct dpvs_msg **pmsg)
{
struct dpvs_msg *msg;
if (unlikely(!pmsg || !(*pmsg)))
return EDPVS_INVAL;
msg = *pmsg;
if (unlikely(rte_atomic16_read(&msg->refcnt) == 0)) {
char buf[1024];
msg_dump(msg, buf, sizeof(buf));
RTE_LOG(ERR, MSGMGR, "%s: bad msg refcnt at destroy:\n%s", __func__, buf);
assert(0);
}
if (!rte_atomic16_dec_and_test(&msg->refcnt)) {
*pmsg = NULL;
return EDPVS_OK;
}
/* i'm the only one hold the msg, free it now */
if (msg->mode == DPVS_MSG_MULTICAST) {
struct dpvs_msg *cur, *next;
struct dpvs_multicast_queue *mcq;
assert(rte_lcore_id() == master_lcore);
mcq = mc_queue_get(msg->type, msg->seq);
if (likely(mcq != NULL)) {
list_for_each_entry_safe(cur, next, &mcq->mq, mq_node) {
list_del_init(&cur->mq_node);
add_msg_flags(cur, DPVS_MSG_F_STATE_FIN); /* in case slaves reply with blockable msg */
msg_destroy(&cur);
}
mc_queue_unhash(mcq);
dpvs_mempool_put(msg_pool, mcq);
} else {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, deleting multicast msg not found in queue:"
"type=%d, seq=%d\n", __func__, msg, msg->type, msg->seq);
}
}
if (msg->reply.data) {
assert(msg->reply.len != 0);
msg_reply_free(msg->reply.data);
msg->reply.len = 0;
}
dpvs_mempool_put(msg_pool, msg);
/*
* Be careful:
* pmsg MUST NOT pointer to mcq->org_msg when seting *pmsg = NULL, bacause mcq is freed now.
* In that special case, if the space of freed mcq is newly allocted as a struct msg by other lcore,
* set *pmsg = NULL here may cause step on the memory of the newly allocted msg.
* PS: the offset of member org_msg in struct dpvs_multicast_queue is 32, and it's size is 8B.
* the offset of member flags in struct dpvs_msg is 32, and it's size is 4.
* the offset of member refcnt in struct dpvs_msg is 36, and it's size is 2.
*/
*pmsg = NULL;
msg_debug_free();
return EDPVS_OK;
}
static int msg_master_process(int step);
/* "msg" must be produced by "msg_make" */
int msg_send(struct dpvs_msg *msg, lcoreid_t cid, uint32_t flags, struct dpvs_msg_reply **reply)
{
struct dpvs_msg_type *mt;
int res;
int step = 1;
uint32_t tflags;
uint64_t start, delay;
if (unlikely(msg == NULL))
return EDPVS_INVAL;
add_msg_flags(msg, flags);
if (unlikely(!((cid == master_lcore) || (slave_lcore_mask & (1L << cid))))) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, invalid args\n", __func__, msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
return EDPVS_INVAL;
}
mt = msg_type_get(msg->type, cid);
if (unlikely(!mt)) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, msg type %d not registered\n",
__func__, msg, msg->type);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
return EDPVS_NOTEXIST;
}
if (mt->prio > g_msg_prio) {
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg_type_put(mt);
return EDPVS_DISABLED;
}
msg_type_put(mt);
/* two lcores will be using the msg now, increase its refcnt */
rte_atomic16_inc(&msg->refcnt);
res = rte_ring_enqueue(msg_ring[cid], msg);
if (unlikely(-EDQUOT == res)) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, msg ring of lcore %d quota exceeded\n",
__func__, msg, cid);
} else if (unlikely(-ENOBUFS == res)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, msg ring of lcore %d is full\n", __func__, msg, res);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
rte_atomic16_dec(&msg->refcnt); /* not enqueued, free manually */
return EDPVS_DPDKAPIFAIL;
} else if (res) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, unkown error %d for rte_ring_enqueue\n",
__func__, msg, res);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
rte_atomic16_dec(&msg->refcnt); /* not enqueued, free manually */
return EDPVS_DPDKAPIFAIL;
}
if (flags & DPVS_MSG_F_ASYNC)
return EDPVS_OK;
/* blockable msg, wait here until done or timeout */
add_msg_flags(msg, DPVS_MSG_F_STATE_SEND);
start = rte_get_timer_cycles();
delay = (uint64_t)g_msg_timeout * g_cycles_per_sec / 1000000;
while(!(test_msg_flags(msg, (DPVS_MSG_F_STATE_FIN | DPVS_MSG_F_STATE_DROP)))) {
if (start + delay < rte_get_timer_cycles()) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, uc_msg(type:%d, cid:%d->%d, flags=%d) timeout"
"(%d us), drop...\n", __func__, msg, msg->type, msg->cid, cid,
get_msg_flags(msg), g_msg_timeout);
add_msg_flags(msg, DPVS_MSG_F_TIMEOUT);
return EDPVS_MSG_DROP;
}
/* to avoid dead lock when one send a blockable msg to itself */
if (rte_lcore_id() == master_lcore)
msg_master_process(step);
else
msg_slave_process(step);
step *= 2;
}
if (reply)
*reply = &msg->reply;
tflags = get_msg_flags(msg);
if (tflags & DPVS_MSG_F_CALLBACK_FAIL)
return EDPVS_MSG_FAIL;
else if (tflags & DPVS_MSG_F_STATE_FIN)
return EDPVS_OK;
else
return EDPVS_MSG_DROP;
}
/* "msg" must be produced by "msg_make" */
int multicast_msg_send(struct dpvs_msg *msg, uint32_t flags, struct dpvs_multicast_queue **reply)
{
struct dpvs_msg *new_msg;
struct dpvs_multicast_queue *mcq;
uint32_t tflags;
uint64_t start, delay;
int ii, ret;
if (unlikely(msg == NULL))
return EDPVS_INVAL;
add_msg_flags(msg, flags);
if (unlikely(DPVS_MSG_MULTICAST != msg->mode || master_lcore != msg->cid)) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, invalid multicast msg\n", __func__, msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg->mode = DPVS_MSG_UNICAST; /* do not free msg queue */
return EDPVS_INVAL;
}
/* multicast msg of identical type and seq cannot coexist */
if (unlikely(mc_queue_get(msg->type, msg->seq) != NULL)) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, repeated sequence number for multicast msg: "
"type %d, seq %d\n", __func__, msg, msg->type, msg->seq);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg->mode = DPVS_MSG_UNICAST; /* do not free msg queue */
return EDPVS_BUSY;
}
/* send unicast msgs from master to all alive slaves */
rte_atomic16_inc(&msg->refcnt);
for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
if (slave_lcore_mask & (1UL << ii)) {
new_msg = msg_make(msg->type, msg->seq, DPVS_MSG_UNICAST, msg->cid, msg->len, msg->data);
if (unlikely(!new_msg)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, msg make fail\n", __func__, msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
rte_atomic16_dec(&msg->refcnt);
return EDPVS_NOMEM;
}
/* must send F_ASYNC msg as mcq has not allocated */
ret = msg_send(new_msg, ii, DPVS_MSG_F_ASYNC, NULL);
if (ret < 0) { /* nonblock msg not equeued */
if (ret != EDPVS_DISABLED)
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, new_msg@%p, msg send fail\n",
__func__, msg, new_msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
rte_atomic16_dec(&msg->refcnt);
msg_destroy(&new_msg);
return ret;
}
msg_destroy(&new_msg);
}
}
mcq = dpvs_mempool_get(msg_pool, sizeof(struct dpvs_multicast_queue));
if (unlikely(!mcq)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, no memory for mcq\n", __func__, msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
rte_atomic16_dec(&msg->refcnt);
return EDPVS_NOMEM;
}
mcq->type = msg->type;
mcq->seq = msg->seq;
mcq->mask = slave_lcore_mask;
mcq->org_msg = msg; /* save original msg */
INIT_LIST_HEAD(&mcq->mq);
/* hash mcq so that reply msg can be collected in msg_master_process */
mc_queue_hash(mcq);
if (flags & DPVS_MSG_F_ASYNC)
return EDPVS_OK;
/* blockable msg wait here until done or timeout */
add_msg_flags(msg, DPVS_MSG_F_STATE_SEND);
start = rte_get_timer_cycles();
delay = (uint64_t)g_msg_timeout * g_cycles_per_sec / 1000000;
while(!(test_msg_flags(msg, (DPVS_MSG_F_STATE_FIN | DPVS_MSG_F_STATE_DROP)))) {
if (start + delay < rte_get_timer_cycles()) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, mcq(type:%d, cid:%d->slaves) timeout"
"(%d us), drop...\n", __func__, msg,
msg->type, msg->cid, g_msg_timeout);
add_msg_flags(msg, DPVS_MSG_F_TIMEOUT);
/* just in case slave send reply fail.
* it's safe here, because msg is used on master lcore only. */
msg_destroy(&msg);
return EDPVS_MSG_DROP;
}
msg_master_process(slave_lcore_nb * 1.5); /* to avoid dead lock if send msg to myself */
}
if (reply)
*reply = mcq; /* here, mcq store all slave's reply msg */
tflags = get_msg_flags(msg);
if (tflags & DPVS_MSG_F_CALLBACK_FAIL)
return EDPVS_MSG_FAIL;
else if (tflags & DPVS_MSG_F_STATE_FIN)
return EDPVS_OK;
else
return EDPVS_MSG_DROP;
}
/* both unicast msg and multicast msg can be recieved on master lcore */
static int msg_master_process(int step)
{
int n = 0;
struct dpvs_msg *msg, *orig_msg;
struct dpvs_msg_type *msg_type;
struct dpvs_multicast_queue *mcq;
/* dequeue msg from ring on the master lcore and process it */
while (((step <= 0) || ((step > 0) && (++n <= step))) &&
(0 == rte_ring_dequeue(msg_ring[master_lcore], (void **)&msg))) {
add_msg_flags(msg, DPVS_MSG_F_STATE_RECV);
msg_type = msg_type_get(msg->type, master_lcore);
if (!msg_type) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, unregistered msg type %d on master\n",
__func__, msg, msg->type);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg_destroy(&msg);
continue;
}
if (DPVS_MSG_UNICAST == msg_type->mode) { /* unicast msg */
if (likely(msg_type->unicast_msg_cb != NULL)) {
if (msg_type->unicast_msg_cb(msg) < 0) {
add_msg_flags(msg, DPVS_MSG_F_CALLBACK_FAIL);
#ifdef CONFIG_MSG_DEBUG
RTE_LOG(INFO, MSGMGR, "%s:msg@%p, uc msg_type %d callback failed on master\n",
__func__, msg, msg->type);
#endif
}
}
add_msg_flags(msg, DPVS_MSG_F_STATE_FIN);
msg_destroy(&msg);
} else { /* multicast msg */
mcq = mc_queue_get(msg->type, msg->seq);
if (!mcq) {
/* probably previous msg timeout */
RTE_LOG(INFO, MSGMGR, "%s:msg@%p, multicast reply msg <type:%d, seq:%d> from"
" lcore %d missed\n", __func__, msg, msg->type, msg->seq, msg->cid);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg_destroy(&msg);
msg_type_put(msg_type);
continue;
}
assert(msg_type->multicast_msg_cb != NULL);
if (mcq->mask & (1UL << msg->cid)) { /* you are the msg i'm waiting */
list_add_tail(&msg->mq_node, &mcq->mq);
add_msg_flags(msg, DPVS_MSG_F_STATE_QUEUE);/* set QUEUE flag for slave's reply msg */
mcq->mask &= ~(1UL << msg->cid);
if (test_msg_flags(msg, DPVS_MSG_F_CALLBACK_FAIL)) /* callback on slave failed */
add_msg_flags(mcq->org_msg, DPVS_MSG_F_CALLBACK_FAIL);
if (unlikely(0 == mcq->mask)) { /* okay, all slave reply msg arrived */
if (msg_type->multicast_msg_cb(mcq) < 0) {
add_msg_flags(mcq->org_msg, DPVS_MSG_F_CALLBACK_FAIL);/* callback on master failed */
#ifdef CONFIG_MSG_DEBUG
RTE_LOG(INFO, MSGMGR, "%s:msg@%p, mc msg_type %d callback failed on master\n",
__func__, mcq->org_msg, msg->type);
#endif
}
add_msg_flags(mcq->org_msg, DPVS_MSG_F_STATE_FIN);
orig_msg = mcq->org_msg;
msg_destroy(&orig_msg);
}
msg_type_put(msg_type);
continue;
}
/* probably previous msg timeout and new msg of this type sent */
RTE_LOG(INFO, MSGMGR, "%s:msg@%p, multicast reply msg <type:%d, seq:%d> from"
" lcore %d repeated\n", __func__, msg, msg->type, msg->seq, msg->cid);
assert(msg->mode == DPVS_MSG_UNICAST);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg_destroy(&msg); /* sorry, you are late */
}
msg_type_put(msg_type);
}
return EDPVS_OK;
}
/* only unicast msg can be recieved on slave lcore */
int msg_slave_process(int step)
{
int n = 0;
struct dpvs_msg *msg, *xmsg;
struct dpvs_msg_type *msg_type;
lcoreid_t cid;
int ret = EDPVS_OK;
cid = rte_lcore_id();
if (unlikely(cid == master_lcore)) {
RTE_LOG(ERR, MSGMGR, "%s is called on master lcore!\n", __func__);
return EDPVS_NONEALCORE;
}
/* dequeue msg from ring on the lcore and process it */
while (0 == rte_ring_dequeue(msg_ring[cid], (void **)&msg)) {
add_msg_flags(msg, DPVS_MSG_F_STATE_RECV);
msg_type = NULL;
if (unlikely(DPVS_MSG_MULTICAST == msg->mode)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, multicast msg recieved on slave lcore!\n",
__func__, msg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
goto cont;
}
msg_type = msg_type_get(msg->type, cid);
if (!msg_type) {
RTE_LOG(WARNING, MSGMGR, "%s:msg@%p, unregistered msg type %d on lcore %d\n",
__func__, msg, msg->type, cid);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
goto cont;
}
if (likely(msg_type->unicast_msg_cb != NULL)) {
if (msg_type->unicast_msg_cb(msg) < 0) {
add_msg_flags(msg, DPVS_MSG_F_CALLBACK_FAIL);
#ifdef CONFIG_MSG_DEBUG
RTE_LOG(INFO, MSGMGR, "%s:msg@%p, msg_type %d callback failed on lcore %d\n",
__func__, msg, msg->type, cid);
#endif
}
}
/* send reply msg to master for multicast msg */
if (DPVS_MSG_MULTICAST == msg_type->mode) {
/* FIXME:
* What if fail here? The result is master lcore would never get the reply msg from slaves.
* - For blockable msg, no problem exists because the multicast_wait_hlist for it would be freed
* when timeout, making all its repsonse slave msg freed or invalid.
* - Nonblockable msg is difficult to end itself, and all this type msg sending afterwards would fail.
* Fortunately, chances of error happending here is very slim, and nonblockable mulitcast msg is
* rarely used. we just log an error and continue if fail here.
* */
xmsg = msg_make(msg->type, msg->seq, DPVS_MSG_UNICAST, cid, msg->reply.len,
msg->reply.data);
if (unlikely(!xmsg)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p, no memory for msg_make\n", __func__, msg);
ret = EDPVS_NOMEM;
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
goto cont;
}
add_msg_flags(xmsg, DPVS_MSG_F_CALLBACK_FAIL & get_msg_flags(msg));
if (msg_send(xmsg, master_lcore, DPVS_MSG_F_ASYNC, NULL)) {
RTE_LOG(ERR, MSGMGR, "%s:msg@%p,xmsg@%p, xmsg send failed!\n",
__func__, msg, xmsg);
add_msg_flags(msg, DPVS_MSG_F_STATE_DROP);
msg_destroy(&xmsg);
goto cont;
}
msg_destroy(&xmsg);
}
add_msg_flags(msg, DPVS_MSG_F_STATE_FIN);
cont:
if (likely(msg_type != NULL))
msg_type_put(msg_type);
msg_destroy(&msg);
if (step > 0 && ++n >= step)
break;
}
return ret;
}
/* for debug */
int msg_dump(const struct dpvs_msg *msg, char *buf, int len)
{
int curlen;
char mstats[256];
if (!msg || !buf || !len)
return 0;
memset(buf, 0, len);
snprintf(buf, len, " ptr:%p, type:%d, seq:%d, mode:%d, cid:%d, flags:0x%x, refcnt:%d\n ",
msg, msg->type, msg->seq, msg->mode, msg->cid, msg->flags, rte_atomic16_read(&msg->refcnt));
if (msg->len) {
curlen = strlen(buf);
if (len - curlen <= 0)
return strlen(buf);
snprintf(buf + curlen, len - curlen, "len:%d, data:0x%lx\n ", msg->len, (uint64_t)msg->data);
}
if (msg->reply.len) {
curlen = strlen(buf);
if (len - curlen <= 0)
return strlen(buf);
snprintf(buf + curlen, len - curlen, "reply.len:%d, reply.data:0x%lx\n ",
msg->reply.len, (uint64_t)msg->reply.data);
}
if (msg_memory_stats(mstats, sizeof(mstats))) {
curlen = strlen(buf);
if (len - curlen <= 0)
return strlen(buf);
snprintf(buf + curlen, len - curlen, "%s\n ", mstats);
}
return strlen(buf);
}
int msg_type_table_print(char *buf, int len)
{
char line[256];
int ii, jj, rem_len;
struct dpvs_msg_type *mt;
if (!buf || !len)
return EDPVS_INVAL;
memset(buf, 0, len);
for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
if (ii != master_lcore && !(slave_lcore_mask & (1L << ii)))
continue;
for (jj = 0; jj < DPVS_MT_LEN; jj++) {
rte_rwlock_read_lock(&mt_lock[ii][jj]);
list_for_each_entry(mt, &mt_array[ii][jj], list) {
memset(line, 0, sizeof(line));
snprintf(line, sizeof(line), "mt_array[%-2d][%-2d] type %-8d mode %-12s"
" unicast_cb %p multicast_cb %p\n", ii, jj, mt->type,
mt->mode == DPVS_MSG_UNICAST ? "UNICAST" : "MULITICAST",
mt->unicast_msg_cb, mt->multicast_msg_cb);
rem_len = len - strlen(buf) - 1;
if (strlen(line) > rem_len) {
RTE_LOG(WARNING, MSGMGR, "%s: buffer not enough\n", __func__);
rte_rwlock_read_unlock(&mt_lock[ii][jj]);
return EDPVS_INVAL;
}
strncat(buf, line, rem_len);
}
rte_rwlock_read_unlock(&mt_lock[ii][jj]);
}
}
return EDPVS_OK;
}
/**************************** built-in msg *******************************/
static int msg_type_reg_cb(struct dpvs_msg *msg)
{
int ret;
struct dpvs_msg_type *mt;
if (unlikely(NULL == msg || sizeof(struct dpvs_msg_type) != msg->len)) {
RTE_LOG(WARNING, MSGMGR, "%s: bad msg data for MSG_TYPE_REG\n", __func__);
return EDPVS_INVAL;
}
mt = (struct dpvs_msg_type *)msg->data;
ret = msg_type_register(mt);
if (ret < 0) {
RTE_LOG(WARNING, MSGMGR, "%s: fail to request to register new msg type\n", __func__);
return ret;
}
return EDPVS_OK;
}
static int msg_type_unreg_cb(struct dpvs_msg *msg)
{
int ret;
struct dpvs_msg_type *mt;
if (unlikely(NULL == msg || sizeof(struct dpvs_msg_type) != msg->len)) {
RTE_LOG(WARNING, MSGMGR, "%s: bad msg data for MSG_TYPE_UNREG\n", __func__);
return EDPVS_INVAL;
}
mt = (struct dpvs_msg_type *)msg->data;
ret = msg_type_unregister(mt);
if (ret < 0) {
RTE_LOG(WARNING, MSGMGR, "%s: fail to request to unregister new msg type\n", __func__);
return ret;
}
return EDPVS_OK;
}
static int register_built_in_msg(void)
{
int ii, tret, ret = EDPVS_OK;
struct dpvs_msg_type mt;
/* register msg-type on all enabled lcores */
memset(&mt, 0, sizeof(mt));
mt.type = MSG_TYPE_REG;
mt.mode = DPVS_MSG_UNICAST;
mt.prio = MSG_PRIO_HIGH;
mt.unicast_msg_cb = msg_type_reg_cb;
for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
if ((ii != master_lcore) && !(slave_lcore_mask & (1UL<<ii)))
continue;
mt.cid = ii;
if (unlikely((tret = msg_type_register(&mt)) < 0 )) {
RTE_LOG(WARNING, MSGMGR, "%s: fail to register msg-register msg\n", __func__);
ret = tret;
}
}
/* unregister msg-type on all enabled lcores */
memset(&mt, 0, sizeof(mt));
mt.type = MSG_TYPE_UNREG;
mt.mode = DPVS_MSG_UNICAST;
mt.prio = MSG_PRIO_HIGH;
mt.unicast_msg_cb = msg_type_unreg_cb;
for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
if ((ii != master_lcore) && !(slave_lcore_mask & (1UL<<ii)))
continue;
mt.cid = ii;
if (unlikely((tret = msg_type_register(&mt)))) {
RTE_LOG(WARNING, MSGMGR, "%s: fail to register msg-unregister mgs\n", __func__);
ret = tret;
}
}
/* master_xmit_msg msg-type on all slave lcores */
if (unlikely(tret = netif_register_master_xmit_msg()))
ret = tret;
return ret;
}
static int unregister_built_in_msg(void)
{
int ii, tret, ret = EDPVS_OK;
struct dpvs_msg_type mt;
/* msg register msg-type */
mt.type = MSG_TYPE_REG;
mt.mode = DPVS_MSG_UNICAST;
mt.prio = MSG_PRIO_HIGH;
mt.unicast_msg_cb = msg_type_reg_cb;
for (ii = 0; ii < DPVS_MAX_LCORE; ii++) {
if ((ii != master_lcore) && !(slave_lcore_mask & (1UL<<ii)))
continue;
mt.cid = ii;
if (unlikely((tret = msg_type_unregister(&mt)) < 0)) {
RTE_LOG(WARNING, MSGMGR, "%s: fail to unregister msg-register msg\n", __func__);
ret = tret;
}
}
/* msg unregister msg-type */
memset(&mt, 0, sizeof(mt));
mt.type = MSG_TYPE_UNREG;
mt.mode = DPVS_MSG_UNICAST;
mt.prio = MSG_PRIO_HIGH;
mt.unicast_msg_cb = msg_type_unreg_cb;