forked from sdnfv/openNetVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonvm_nflib.c
executable file
·1413 lines (1170 loc) · 51 KB
/
onvm_nflib.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
/*********************************************************************
* openNetVM
* https://sdnfv.github.io
*
* BSD LICENSE
*
* Copyright(c)
* 2015-2019 George Washington University
* 2015-2019 University of California Riverside
* 2016-2019 Hewlett Packard Enterprise Development LP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************/
/******************************************************************************
onvm_nflib.c
File containing all functions of the NF API
******************************************************************************/
/***************************Standard C library********************************/
#include <getopt.h>
#include <signal.h>
/******************************DPDK libraries*********************************/
#include "rte_malloc.h"
/*****************************Internal headers********************************/
#include "onvm_includes.h"
#include "onvm_nflib.h"
#include "onvm_sc_common.h"
/**********************************Macros*************************************/
// Possible NF packet consuming modes
#define NF_MODE_UNKNOWN 0
#define NF_MODE_SINGLE 1
#define NF_MODE_RING 2
#define ONVM_NO_CALLBACK NULL
/******************************Global Variables*******************************/
// Shared data for host port information
struct port_info *ports;
// Shared data for core information
struct core_status *cores;
// ring used for NF -> mgr messages (like startup & shutdown)
static struct rte_ring *mgr_msg_queue;
// Shared data from server. We update statistics here
struct onvm_nf *nfs;
// Shared data from manager, has information used for nf_side tx
uint16_t **services;
uint16_t *nf_per_service_count;
// Shared pool for all NFs info
static struct rte_mempool *nf_init_cfg_mp;
// Shared pool for mgr <--> NF messages
static struct rte_mempool *nf_msg_pool;
// Global NF context to manage signal termination
static struct onvm_nf_local_ctx *main_nf_local_ctx;
// Global NF specific signal handler
static handle_signal_func global_nf_signal_handler = NULL;
// Shared data for default service chain
struct onvm_service_chain *default_chain;
/* Shared data for onvm config */
struct onvm_configuration *onvm_config;
/* Flag to check if shared core mutex sleep/wakeup is enabled */
uint8_t ONVM_NF_SHARE_CORES;
/***********************Internal Functions Prototypes*************************/
/*
* Function that initialize a nf tx info data structure.
*
* Input : onvm_nf_init_cfg struct pointer
* Output : the data structure initialized
*
*/
static void
onvm_nflib_nf_tx_mgr_init(struct onvm_nf *nf);
/*
* Function printing an explanation of command line instruction for a NF.
*
* Input : name of the executable containing the NF
*
*/
static void
onvm_nflib_usage(const char *progname);
/*
* Function that parses the global arguments common to all NFs.
*
* Input : the number of arguments (following C standard library convention)
* an array of strings representing these arguments
* Output : an error code
*
*/
static int
onvm_nflib_parse_args(int argc, char *argv[], struct onvm_nf_init_cfg *nf_init_cfg);
/*
* Check if there are packets in this NF's RX Queue and process them
*/
static inline uint16_t
onvm_nflib_dequeue_packets(void **pkts, struct onvm_nf_local_ctx *nf_local_ctx,
nf_pkt_handler_fn handler) __attribute__((always_inline));
/*
* Check if there is a message available for this NF and process it
*/
static inline void
onvm_nflib_dequeue_messages(struct onvm_nf_local_ctx *nf_local_ctx) __attribute__((always_inline));
/*
* Terminate the children spawned by the NF
*
* Input: Info struct corresponding to this NF
*/
static void
onvm_nflib_terminate_children(struct onvm_nf *nf);
/*
* Set this NF's status to not running and release memory
*
* Input: pointer to context struct for this NF
*/
static void
onvm_nflib_cleanup(struct onvm_nf_local_ctx *nf_local_ctx);
/*
* Entry point of a spawned child NF
*/
static void *
onvm_nflib_start_child(void *arg);
/*
* Check if the NF info struct is valid
*/
static int
onvm_nflib_is_scale_info_valid(struct onvm_nf_scale_info *scale_info);
/*
* Initialize dpdk as a secondary proc
*
* Input: arc, argv args
*/
static int
onvm_nflib_dpdk_init(int argc, char *argv[]);
/*
* Lookup the structs shared with manager
*
*/
static int
onvm_nflib_lookup_shared_structs(void);
/*
* Parse the custom onvm config shared with manager
*
*/
static void
onvm_nflib_parse_config(struct onvm_configuration *onvm_config);
/*
* Entry point of the NF main loop
*
* Input: void pointer, points to the onvm_nf_local_ctx struct
*/
void *
onvm_nflib_thread_main_loop(void *arg);
/*
* Function to initalize the shared core support
*
* Input : Number of NF instances
*/
static void
init_shared_core_mode_info(uint16_t instance_id);
/*
* Signal handler to catch SIGINT/SIGTERM.
*
* Input : int corresponding to the signal catched
*
*/
void
onvm_nflib_handle_signal(int signal);
/************************************API**************************************/
struct onvm_nf_local_ctx *
onvm_nflib_init_nf_local_ctx(void) {
struct onvm_nf_local_ctx *nf_local_ctx;
nf_local_ctx = (struct onvm_nf_local_ctx*)calloc(1, sizeof(struct onvm_nf_local_ctx));
if (nf_local_ctx == NULL)
rte_exit(EXIT_FAILURE, "Failed to allocate memory for NF context\n");
rte_atomic16_init(&nf_local_ctx->keep_running);
rte_atomic16_set(&nf_local_ctx->keep_running, 1);
rte_atomic16_init(&nf_local_ctx->nf_init_finished);
rte_atomic16_set(&nf_local_ctx->nf_init_finished, 0);
rte_atomic16_init(&nf_local_ctx->nf_stopped);
rte_atomic16_set(&nf_local_ctx->nf_stopped, 0);
return nf_local_ctx;
}
struct onvm_nf_function_table *
onvm_nflib_init_nf_function_table(void) {
struct onvm_nf_function_table *nf_function_table;
nf_function_table = (struct onvm_nf_function_table*)calloc(1, sizeof(struct onvm_nf_function_table));
if (nf_function_table == NULL)
rte_exit(EXIT_FAILURE, "Failed to allocate memory for NF context\n");
return nf_function_table;
}
int
onvm_nflib_request_lpm(struct lpm_request *lpm_req) {
struct onvm_nf_msg *request_message;
int ret;
ret = rte_mempool_get(nf_msg_pool, (void **) (&request_message));
if (ret != 0) return ret;
request_message->msg_type = MSG_REQUEST_LPM_REGION;
request_message->msg_data = lpm_req;
ret = rte_ring_enqueue(mgr_msg_queue, request_message);
if (ret < 0) {
rte_mempool_put(nf_msg_pool, request_message);
return ret;
}
lpm_req->status = NF_WAITING_FOR_LPM;
for (; lpm_req->status == (uint16_t) NF_WAITING_FOR_LPM;) {
sleep(1);
}
rte_mempool_put(nf_msg_pool, request_message);
return lpm_req->status;
}
int
onvm_nflib_request_ft(struct rte_hash_parameters *ipv4_hash_params) {
struct onvm_nf_msg *request_message;
struct ft_request *ft_req;
int ret;
ft_req = (struct ft_request *) rte_malloc(NULL, sizeof(struct ft_request), 0);
if (!ft_req) {
return -1;
}
ret = rte_mempool_get(nf_msg_pool, (void **) (&request_message));
if (ret != 0) {
rte_mempool_put(nf_msg_pool, request_message);
return ret;
}
ft_req->ipv4_hash_params = ipv4_hash_params;
request_message->msg_type = MSG_REQUEST_FT;
request_message->msg_data = ft_req;
ret = rte_ring_enqueue(mgr_msg_queue, request_message);
if (ret < 0) {
rte_mempool_put(nf_msg_pool, request_message);
return ret;
}
ft_req->status = NF_WAITING_FOR_FT;
for (; ft_req->status == (uint16_t) NF_WAITING_FOR_FT;) {
sleep(1);
}
rte_mempool_put(nf_msg_pool, request_message);
return ft_req->status;
}
int
onvm_nflib_start_signal_handler(struct onvm_nf_local_ctx *nf_local_ctx, handle_signal_func nf_signal_handler) {
/* Signal handling is global thus save global context */
main_nf_local_ctx = nf_local_ctx;
global_nf_signal_handler = nf_signal_handler;
printf("[Press Ctrl-C to quit ...]\n");
signal(SIGINT, onvm_nflib_handle_signal);
signal(SIGTERM, onvm_nflib_handle_signal);
return 0;
}
int
onvm_nflib_init(int argc, char *argv[], const char *nf_tag, struct onvm_nf_local_ctx *nf_local_ctx,
struct onvm_nf_function_table *nf_function_table) {
struct onvm_nf_init_cfg *nf_init_cfg;
int ret, retval_eal, retval_parse, retval_final;
int use_config = 0;
/* Check to see if a config file should be used */
if (strcmp(argv[1], "-F") == 0) {
use_config = 1;
cJSON *config = onvm_config_parse_file(argv[2]);
if (config == NULL) {
printf("Could not parse config file\n");
return -1;
}
if (onvm_config_create_nf_arg_list(config, &argc, &argv) < 0) {
printf("Could not create arg list\n");
cJSON_Delete(config);
return -1;
}
cJSON_Delete(config);
printf("LOADED CONFIG SUCCESFULLY\n");
}
retval_eal = onvm_nflib_dpdk_init(argc, argv);
if (retval_eal < 0)
return retval_eal;
/* Modify argc and argv to conform to getopt rules for parse_nflib_args */
argc -= retval_eal;
argv += retval_eal;
/* Reset getopt global variables opterr and optind to their default values */
opterr = 0;
optind = 1;
/* Lookup the info shared or created by the manager */
onvm_nflib_lookup_shared_structs();
/* Initialize the info struct */
nf_init_cfg = onvm_nflib_init_nf_init_cfg(nf_tag);
if ((retval_parse = onvm_nflib_parse_args(argc, argv, nf_init_cfg)) < 0)
rte_exit(EXIT_FAILURE, "Invalid command-line arguments\n");
/* Reset getopt global variables opterr and optind to their default values */
opterr = 0;
optind = 1;
/*
* Calculate the offset that the nf will use to modify argc and argv for its
* getopt call. This is the sum of the number of arguments parsed by
* rte_eal_init and parse_nflib_args. This will be decremented by 1 to assure
* getopt is looking at the correct index since optind is incremented by 1 each
* time "--" is parsed.
* This is the value that will be returned if initialization succeeds.
*/
retval_final = (retval_eal + retval_parse) - 1;
if ((ret = onvm_nflib_start_nf(nf_local_ctx, nf_init_cfg)) < 0)
return ret;
/* Save the nf specifc function table */
nf_local_ctx->nf->function_table = nf_function_table;
// Set to 3 because that is the bare minimum number of arguments, the config file will increase this number
if (use_config) {
return 3;
}
return retval_final;
}
int
onvm_nflib_start_nf(struct onvm_nf_local_ctx *nf_local_ctx, struct onvm_nf_init_cfg *nf_init_cfg) {
struct onvm_nf_msg *startup_msg;
struct onvm_nf *nf;
int i;
/* Block signals, ensure only the parent signal handler gets the signal */
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
if (pthread_sigmask(SIG_BLOCK, &mask, NULL) != 0) {
printf("Could not set pthread sigmask\n");
return -1;
}
if (!rte_atomic16_read(&nf_local_ctx->keep_running)) {
return ONVM_SIGNAL_TERMINATION;
}
/* Put this NF's info struct onto queue for manager to process startup */
if (rte_mempool_get(nf_msg_pool, (void **)(&startup_msg)) != 0) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg); // give back memory
rte_exit(EXIT_FAILURE, "Cannot create startup msg");
}
/* Tell the manager we're ready to recieve packets */
startup_msg->msg_type = MSG_NF_STARTING;
startup_msg->msg_data = nf_init_cfg;
if (rte_ring_enqueue(mgr_msg_queue, startup_msg) < 0) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg); // give back mermory
rte_mempool_put(nf_msg_pool, startup_msg);
rte_exit(EXIT_FAILURE, "Cannot send nf_init_cfg to manager");
}
/* Wait for a NF id to be assigned by the manager */
RTE_LOG(INFO, APP, "Waiting for manager to assign an ID...\n");
for (; nf_init_cfg->status == (uint16_t)NF_WAITING_FOR_ID;) {
sleep(1);
if (!rte_atomic16_read(&nf_local_ctx->keep_running)) {
/* Wait because we sent a message to the onvm_mgr */
for (i = 0; i < NF_TERM_INIT_ITER_TIMES && nf_init_cfg->status != NF_STARTING; i++) {
printf("Waiting for onvm_mgr to recieve the message before shutting down\n");
sleep(NF_TERM_WAIT_TIME);
}
/* Mark init as finished, even though we're exiting onvm_nflib_stop will do proper cleanup */
if (nf_init_cfg->status == NF_STARTING) {
nf_local_ctx->nf = &nfs[nf_init_cfg->instance_id];
rte_atomic16_set(&nf_local_ctx->nf_init_finished, 1);
}
return ONVM_SIGNAL_TERMINATION;
}
}
/* This NF is trying to declare an ID already in use. */
if (nf_init_cfg->status == NF_ID_CONFLICT) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("%s", "Selected ID already in use. Exiting...\n");
return -NF_ID_CONFLICT;
} else if (nf_init_cfg->status == NF_SERVICE_MAX) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("Service ID must be less than %d\n", MAX_SERVICES);
return -NF_SERVICE_MAX;
} else if (nf_init_cfg->status == NF_SERVICE_COUNT_MAX) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("Maximum amount of NF's per service spawned, must be less than %d", MAX_NFS_PER_SERVICE);
return -NF_SERVICE_COUNT_MAX;
} else if (nf_init_cfg->status == NF_NO_IDS) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("There are no ids available for this NF\n");
return -NF_NO_IDS;
} else if (nf_init_cfg->status == NF_NO_CORES) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("There are no cores available for this NF\n");
return -NF_NO_CORES;
} else if (nf_init_cfg->status == NF_NO_DEDICATED_CORES) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("There is no space to assign a dedicated core, or the selected core has NFs running\n");
return -NF_NO_DEDICATED_CORES;
} else if (nf_init_cfg->status == NF_CORE_OUT_OF_RANGE) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("Requested core is not enabled or not in range\n");
return -NF_CORE_OUT_OF_RANGE;
} else if (nf_init_cfg->status == NF_CORE_BUSY) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("Requested core is busy\n");
return -NF_CORE_BUSY;
} else if (nf_init_cfg->status != NF_STARTING) {
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
printf("Error occurred during manager initialization\n");
return -1;
}
nf = &nfs[nf_init_cfg->instance_id];
/* Mark init as finished, sig handler/onvm_nflib_stop will now do proper cleanup */
if (rte_atomic16_read(&nf_local_ctx->nf_init_finished) == 0) {
nf_local_ctx->nf = nf;
rte_atomic16_set(&nf_local_ctx->nf_init_finished, 1);
}
/* Init finished free the bootstrap struct */
rte_mempool_put(nf_init_cfg_mp, nf_init_cfg);
/* Initialize empty NF's tx manager */
onvm_nflib_nf_tx_mgr_init(nf);
/* Set the parent id to none */
nf->thread_info.parent = 0;
rte_atomic16_init(&nf->thread_info.children_cnt);
rte_atomic16_set(&nf->thread_info.children_cnt, 0);
/* In case this instance_id is reused, clear all function pointers */
nf->function_table = NULL;
if (ONVM_NF_SHARE_CORES) {
RTE_LOG(INFO, APP, "Shared CPU support enabled\n");
init_shared_core_mode_info(nf->instance_id);
}
RTE_LOG(INFO, APP, "Using Instance ID %d\n", nf->instance_id);
RTE_LOG(INFO, APP, "Using Service ID %d\n", nf->service_id);
RTE_LOG(INFO, APP, "Running on core %d\n", nf->thread_info.core);
if (nf->flags.time_to_live)
RTE_LOG(INFO, APP, "Time to live set to %u\n", nf->flags.time_to_live);
if (nf->flags.pkt_limit)
RTE_LOG(INFO, APP, "Packet limit (rx) set to %u\n", nf->flags.pkt_limit);
/*
* Allow this for cases when there is not enough cores and using
* the shared core mode is not an option
*/
if (ONVM_CHECK_BIT(nf->flags.init_options, SHARE_CORE_BIT) && !ONVM_NF_SHARE_CORES)
RTE_LOG(WARNING, APP, "Requested shared core allocation but shared core mode is NOT "
"enabled, this will hurt performance, proceed with caution\n");
RTE_LOG(INFO, APP, "Finished Process Init.\n");
return 0;
}
int
onvm_nflib_run(struct onvm_nf_local_ctx *nf_local_ctx) {
int ret;
pthread_t main_loop_thread;
if ((ret = pthread_create(&main_loop_thread, NULL, onvm_nflib_thread_main_loop, (void *)nf_local_ctx)) < 0) {
rte_exit(EXIT_FAILURE, "Failed to spawn main loop thread, error %d", ret);
}
if ((ret = pthread_join(main_loop_thread, NULL)) < 0) {
rte_exit(EXIT_FAILURE, "Failed to join with main loop thread, error %d", ret);
}
return 0;
}
void *
onvm_nflib_thread_main_loop(void *arg) {
struct rte_mbuf *pkts[PACKET_READ_SIZE];
struct onvm_nf_local_ctx *nf_local_ctx;
struct onvm_nf *nf;
uint16_t nb_pkts_added;
uint64_t start_time;
int ret;
nf_local_ctx = (struct onvm_nf_local_ctx *)arg;
nf = nf_local_ctx->nf;
onvm_threading_core_affinitize(nf->thread_info.core);
printf("Sending NF_READY message to manager...\n");
ret = onvm_nflib_nf_ready(nf);
if (ret != 0)
rte_exit(EXIT_FAILURE, "Unable to message manager\n");
/* Run the setup function (this might send pkts so done after the state change) */
if (nf->function_table->setup != NULL)
nf->function_table->setup(nf_local_ctx);
start_time = rte_get_tsc_cycles();
for (;rte_atomic16_read(&nf_local_ctx->keep_running) && rte_atomic16_read(&main_nf_local_ctx->keep_running);) {
/* Possibly sleep if in shared core mode, otherwise continue */
if (ONVM_NF_SHARE_CORES) {
if (unlikely(rte_ring_count(nf->rx_q) == 0) && likely(rte_ring_count(nf->msg_q) == 0)) {
rte_atomic16_set(nf->shared_core.sleep_state, 1);
sem_wait(nf->shared_core.nf_mutex);
}
}
nb_pkts_added =
onvm_nflib_dequeue_packets((void **)pkts, nf_local_ctx, nf->function_table->pkt_handler);
if (likely(nb_pkts_added > 0)) {
onvm_pkt_process_tx_batch(nf->nf_tx_mgr, pkts, nb_pkts_added, nf);
}
/* Flush the packet buffers */
onvm_pkt_enqueue_tx_thread(nf->nf_tx_mgr->to_tx_buf, nf);
onvm_pkt_flush_all_nfs(nf->nf_tx_mgr, nf);
onvm_nflib_dequeue_messages(nf_local_ctx);
if (nf->function_table->user_actions != ONVM_NO_CALLBACK) {
rte_atomic16_set(&nf_local_ctx->keep_running,
!(*nf->function_table->user_actions)(nf_local_ctx) &&
rte_atomic16_read(&nf_local_ctx->keep_running));
}
if (nf->flags.time_to_live && unlikely((rte_get_tsc_cycles() - start_time) *
TIME_TTL_MULTIPLIER / rte_get_timer_hz() >= nf->flags.time_to_live)) {
printf("Time to live exceeded, shutting down\n");
rte_atomic16_set(&nf_local_ctx->keep_running, 0);
}
if (nf->flags.pkt_limit && unlikely(nf->stats.rx >= (uint64_t)nf->flags.pkt_limit *
PKT_TTL_MULTIPLIER)) {
printf("Packet limit exceeded, shutting down\n");
rte_atomic16_set(&nf_local_ctx->keep_running, 0);
}
}
return NULL;
}
int
onvm_nflib_return_pkt(struct onvm_nf *nf, struct rte_mbuf *pkt) {
return onvm_nflib_return_pkt_bulk(nf, &pkt, 1);
}
int
onvm_nflib_return_pkt_bulk(struct onvm_nf *nf, struct rte_mbuf **pkts, uint16_t count) {
unsigned int i;
if (pkts == NULL || count == 0)
return -1;
if (unlikely(rte_ring_enqueue_bulk(nf->tx_q, (void **)pkts, count, NULL) == 0)) {
nf->stats.tx_drop += count;
for (i = 0; i < count; i++) {
rte_pktmbuf_free(pkts[i]);
}
return -ENOBUFS;
} else {
nf->stats.tx_returned += count;
}
return 0;
}
int
onvm_nflib_nf_ready(struct onvm_nf *nf) {
struct onvm_nf_msg *startup_msg;
int ret;
/* Put this NF's info struct onto queue for manager to process startup */
ret = rte_mempool_get(nf_msg_pool, (void **)(&startup_msg));
if (ret != 0)
return ret;
startup_msg->msg_type = MSG_NF_READY;
startup_msg->msg_data = nf;
ret = rte_ring_enqueue(mgr_msg_queue, startup_msg);
if (ret < 0) {
rte_mempool_put(nf_msg_pool, startup_msg);
return ret;
}
/* Don't start running before the onvm_mgr handshake is finished */
while (nf->status != NF_RUNNING) {
sleep(1);
}
return 0;
}
int
onvm_nflib_handle_msg(struct onvm_nf_msg *msg, struct onvm_nf_local_ctx *nf_local_ctx) {
switch (msg->msg_type) {
case MSG_STOP:
RTE_LOG(INFO, APP, "Shutting down...\n");
rte_atomic16_set(&nf_local_ctx->keep_running, 0);
break;
case MSG_SCALE:
RTE_LOG(INFO, APP, "Received scale message...\n");
onvm_nflib_scale((struct onvm_nf_scale_info*)msg->msg_data);
break;
case MSG_FROM_NF:
RTE_LOG(INFO, APP, "Received MSG from other NF\n");
if (nf_local_ctx->nf->function_table->msg_handler != NULL) {
nf_local_ctx->nf->function_table->msg_handler(msg->msg_data, nf_local_ctx);
}
break;
case MSG_CHANGE_CORE:
RTE_LOG(INFO, APP, "Received relocation message...\n");
RTE_LOG(INFO, APP, "Moving NF to core %d\n", *(uint16_t *)msg->msg_data);
nf_local_ctx->nf->thread_info.core = *(uint16_t *)msg->msg_data;
onvm_threading_core_affinitize(nf_local_ctx->nf->thread_info.core);
rte_free(msg->msg_data);
break;
case MSG_NOOP:
default:
break;
}
return 0;
}
int
onvm_nflib_send_msg_to_nf(uint16_t dest, void *msg_data) {
int ret;
struct onvm_nf_msg *msg;
ret = rte_mempool_get(nf_msg_pool, (void**)(&msg));
if (ret != 0) {
RTE_LOG(INFO, APP, "Oh the huge manatee! Unable to allocate msg from pool :(\n");
return ret;
}
msg->msg_type = MSG_FROM_NF;
msg->msg_data = msg_data;
return rte_ring_enqueue(nfs[dest].msg_q, (void*)msg);
}
void
onvm_nflib_stop(struct onvm_nf_local_ctx *nf_local_ctx) {
if (nf_local_ctx == NULL || nf_local_ctx->nf == NULL || rte_atomic16_read(&nf_local_ctx->nf_stopped) != 0) {
return;
}
/* Ensure we only call nflib_stop once */
rte_atomic16_set(&nf_local_ctx->nf_stopped, 1);
/* Terminate children */
onvm_nflib_terminate_children(nf_local_ctx->nf);
/* Print statistics summary */
onvm_nflib_stats_summary_output(nf_local_ctx->nf->instance_id);
/* Stop and free */
onvm_nflib_cleanup(nf_local_ctx);
}
struct onvm_configuration *
onvm_nflib_get_onvm_config(void) {
return onvm_config;
}
int
onvm_nflib_scale(struct onvm_nf_scale_info *scale_info) {
int ret;
pthread_t app_thread;
if (onvm_nflib_is_scale_info_valid(scale_info) < 0) {
RTE_LOG(INFO, APP, "Scale info invalid\n");
return -1;
}
rte_atomic16_inc(&nfs[scale_info->parent->instance_id].thread_info.children_cnt);
/* Careful, this is required for shared core scaling TODO: resolve */
if (ONVM_NF_SHARE_CORES)
sleep(1);
ret = pthread_create(&app_thread, NULL, &onvm_nflib_start_child, scale_info);
if (ret < 0) {
rte_atomic16_dec(&nfs[scale_info->parent->instance_id].thread_info.children_cnt);
RTE_LOG(INFO, APP, "Failed to create child thread\n");
return -1;
}
ret = pthread_detach(app_thread);
if (ret < 0) {
RTE_LOG(INFO, APP, "Failed to detach child thread\n");
return -1;
}
return 0;
}
struct onvm_nf_init_cfg *
onvm_nflib_init_nf_init_cfg(const char *tag) {
void *mempool_data;
struct onvm_nf_init_cfg *nf_init_cfg;
if (rte_mempool_get(nf_init_cfg_mp, &mempool_data) < 0) {
rte_exit(EXIT_FAILURE, "Failed to get nf nf_init_cfg memory\n");
}
if (mempool_data == NULL) {
rte_exit(EXIT_FAILURE, "Client Info struct not allocated\n");
}
nf_init_cfg = (struct onvm_nf_init_cfg *)mempool_data;
nf_init_cfg->instance_id = NF_NO_ID;
nf_init_cfg->core = rte_lcore_id();
nf_init_cfg->init_options = 0;
nf_init_cfg->status = NF_WAITING_FOR_ID;
/* Allocate memory for the tag so that onvm_mgr can access it */
nf_init_cfg->tag = rte_malloc("nf_tag", TAG_SIZE, 0);
strncpy(nf_init_cfg->tag, tag, TAG_SIZE);
/* In case provided tag was longer than TAG_SIZE */
nf_init_cfg->tag[TAG_SIZE - 1] = '\0';
/* TTL and packet limit disabled by default */
nf_init_cfg->time_to_live = 0;
nf_init_cfg->pkt_limit = 0;
return nf_init_cfg;
}
struct onvm_nf_init_cfg *
onvm_nflib_inherit_parent_init_cfg(struct onvm_nf *parent) {
struct onvm_nf_init_cfg *nf_init_cfg;
nf_init_cfg = onvm_nflib_init_nf_init_cfg(parent->tag);
nf_init_cfg->service_id = parent->service_id;
nf_init_cfg->core = parent->thread_info.core;
nf_init_cfg->init_options = parent->flags.init_options;
nf_init_cfg->time_to_live = parent->flags.time_to_live;
nf_init_cfg->pkt_limit = parent->flags.pkt_limit;
return nf_init_cfg;
}
struct onvm_nf_scale_info *
onvm_nflib_get_empty_scaling_config(struct onvm_nf *parent) {
struct onvm_nf_scale_info *scale_info;
scale_info = rte_calloc("nf_scale_info", 1, sizeof(struct onvm_nf_scale_info), 0);
if (scale_info == NULL) {
RTE_LOG(ERR, APP, "Can't allocate scale info struct\n");
}
scale_info->nf_init_cfg = onvm_nflib_init_nf_init_cfg(parent->tag);
scale_info->parent = parent;
return scale_info;
}
struct onvm_nf_scale_info *
onvm_nflib_inherit_parent_config(struct onvm_nf *parent, void *data) {
struct onvm_nf_scale_info *scale_info;
scale_info = rte_calloc("nf_scale_info", 1, sizeof(struct onvm_nf_scale_info), 0);
if (scale_info == NULL) {
RTE_LOG(ERR, APP, "Can't allocate scale info struct\n");
}
scale_info->nf_init_cfg = onvm_nflib_inherit_parent_init_cfg(parent);
scale_info->parent = parent;
scale_info->data = data;
scale_info->function_table = parent->function_table;
return scale_info;
}
struct onvm_service_chain *
onvm_nflib_get_default_chain(void) {
return default_chain;
}
/******************************Helper functions*******************************/
static int
onvm_nflib_dpdk_init(int argc, char *argv[]) {
int retval_eal = 0;
if ((retval_eal = rte_eal_init(argc, argv)) < 0)
return -1;
return retval_eal;
}
static int
onvm_nflib_lookup_shared_structs(void) {
const struct rte_memzone *mz_nf;
const struct rte_memzone *mz_port;
const struct rte_memzone *mz_cores;
const struct rte_memzone *mz_scp;
const struct rte_memzone *mz_services;
const struct rte_memzone *mz_nf_per_service;
const struct rte_memzone *mz_onvm_config;
struct rte_mempool *mp;
struct onvm_service_chain **scp;
/* Lookup mempool for nf_init_cfg struct */
nf_init_cfg_mp = rte_mempool_lookup(_NF_MEMPOOL_NAME);
if (nf_init_cfg_mp == NULL)
rte_exit(EXIT_FAILURE, "No NF Info mempool - bye\n");
/* Lookup mempool for NF messages */
nf_msg_pool = rte_mempool_lookup(_NF_MSG_POOL_NAME);
if (nf_msg_pool == NULL)
rte_exit(EXIT_FAILURE, "No NF Message mempool - bye\n");
mp = rte_mempool_lookup(PKTMBUF_POOL_NAME);
if (mp == NULL)
rte_exit(EXIT_FAILURE, "Cannot get mempool for mbufs\n");
/* Lookup mempool for NF structs */
mz_nf = rte_memzone_lookup(MZ_NF_INFO);
if (mz_nf == NULL)
rte_exit(EXIT_FAILURE, "Cannot get NF structure mempool\n");
nfs = mz_nf->addr;
mz_services = rte_memzone_lookup(MZ_SERVICES_INFO);
if (mz_services == NULL) {
rte_exit(EXIT_FAILURE, "Cannot get service information\n");
}
services = mz_services->addr;
mz_nf_per_service = rte_memzone_lookup(MZ_NF_PER_SERVICE_INFO);
if (mz_nf_per_service == NULL) {
rte_exit(EXIT_FAILURE, "Cannot get NF per service information\n");
}
nf_per_service_count = mz_nf_per_service->addr;
mz_port = rte_memzone_lookup(MZ_PORT_INFO);
if (mz_port == NULL)
rte_exit(EXIT_FAILURE, "Cannot get port info structure\n");
ports = mz_port->addr;
mz_cores = rte_memzone_lookup(MZ_CORES_STATUS);
if (mz_cores == NULL)
rte_exit(EXIT_FAILURE, "Cannot get core status structure\n");
cores = mz_cores->addr;
mz_onvm_config = rte_memzone_lookup(MZ_ONVM_CONFIG);
if (mz_onvm_config == NULL)
rte_exit(EXIT_FAILURE, "Cannot get onvm config\n");
onvm_config = mz_onvm_config->addr;
onvm_nflib_parse_config(onvm_config);
mz_scp = rte_memzone_lookup(MZ_SCP_INFO);
if (mz_scp == NULL)
rte_exit(EXIT_FAILURE, "Cannot get service chain info structre\n");
scp = mz_scp->addr;
default_chain = *scp;
onvm_sc_print(default_chain);
mgr_msg_queue = rte_ring_lookup(_MGR_MSG_QUEUE_NAME);
if (mgr_msg_queue == NULL)
rte_exit(EXIT_FAILURE, "Cannot get mgr message ring");
return 0;
}
static void
onvm_nflib_parse_config(struct onvm_configuration *config) {
ONVM_NF_SHARE_CORES = config->flags.ONVM_NF_SHARE_CORES;
}
static inline uint16_t
onvm_nflib_dequeue_packets(void **pkts, struct onvm_nf_local_ctx *nf_local_ctx, nf_pkt_handler_fn handler) {
struct onvm_nf *nf;
struct onvm_pkt_meta *meta;
uint16_t i, nb_pkts;
struct packet_buf tx_buf;
int ret_act;
nf = nf_local_ctx->nf;
/* Dequeue all packets in ring up to max possible. */
nb_pkts = rte_ring_dequeue_burst(nf->rx_q, pkts, PACKET_READ_SIZE, NULL);
if (unlikely(nb_pkts == 0)) {
return 0;
}
tx_buf.count = 0;
/* Give each packet to the user proccessing function */
for (i = 0; i < nb_pkts; i++) {
meta = onvm_get_pkt_meta((struct rte_mbuf *)pkts[i]);
ret_act = (*handler)((struct rte_mbuf *)pkts[i], meta, nf_local_ctx);
/* NF returns 0 to return packets or 1 to buffer */
if (likely(ret_act == 0)) {
tx_buf.buffer[tx_buf.count++] = pkts[i];
} else {
nf->stats.tx_buffer++;
}
}
if (ONVM_NF_HANDLE_TX) {
return nb_pkts;
}
onvm_pkt_enqueue_tx_thread(&tx_buf, nf);
return 0;
}
static inline void
onvm_nflib_dequeue_messages(struct onvm_nf_local_ctx *nf_local_ctx) {
struct onvm_nf_msg *msg;