service: fix loops to always scan all services
[dpdk.git] / lib / librte_eal / common / rte_service.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <inttypes.h>
37 #include <limits.h>
38 #include <string.h>
39 #include <dirent.h>
40
41 #include <rte_service.h>
42 #include "include/rte_service_component.h"
43
44 #include <rte_eal.h>
45 #include <rte_lcore.h>
46 #include <rte_common.h>
47 #include <rte_debug.h>
48 #include <rte_cycles.h>
49 #include <rte_atomic.h>
50 #include <rte_memory.h>
51 #include <rte_malloc.h>
52
53 #define RTE_SERVICE_NUM_MAX 64
54
55 #define SERVICE_F_REGISTERED    (1 << 0)
56 #define SERVICE_F_STATS_ENABLED (1 << 1)
57
58 /* runstates for services and lcores, denoting if they are active or not */
59 #define RUNSTATE_STOPPED 0
60 #define RUNSTATE_RUNNING 1
61
62 /* internal representation of a service */
63 struct rte_service_spec_impl {
64         /* public part of the struct */
65         struct rte_service_spec spec;
66
67         /* atomic lock that when set indicates a service core is currently
68          * running this service callback. When not set, a core may take the
69          * lock and then run the service callback.
70          */
71         rte_atomic32_t execute_lock;
72
73         /* API set/get-able variables */
74         int32_t runstate;
75         uint8_t internal_flags;
76
77         /* per service statistics */
78         uint32_t num_mapped_cores;
79         uint64_t calls;
80         uint64_t cycles_spent;
81 } __rte_cache_aligned;
82
83 /* the internal values of a service core */
84 struct core_state {
85         /* map of services IDs are run on this core */
86         uint64_t service_mask;
87         uint8_t runstate; /* running or stopped */
88         uint8_t is_service_core; /* set if core is currently a service core */
89
90         /* extreme statistics */
91         uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
92 } __rte_cache_aligned;
93
94 static uint32_t rte_service_count;
95 static struct rte_service_spec_impl *rte_services;
96 static struct core_state *lcore_states;
97 static uint32_t rte_service_library_initialized;
98
99 int32_t rte_service_init(void)
100 {
101         if (rte_service_library_initialized) {
102                 printf("service library init() called, init flag %d\n",
103                         rte_service_library_initialized);
104                 return -EALREADY;
105         }
106
107         rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
108                         sizeof(struct rte_service_spec_impl),
109                         RTE_CACHE_LINE_SIZE);
110         if (!rte_services) {
111                 printf("error allocating rte services array\n");
112                 return -ENOMEM;
113         }
114
115         lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
116                         sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
117         if (!lcore_states) {
118                 printf("error allocating core states array\n");
119                 return -ENOMEM;
120         }
121
122         int i;
123         int count = 0;
124         struct rte_config *cfg = rte_eal_get_configuration();
125         for (i = 0; i < RTE_MAX_LCORE; i++) {
126                 if (lcore_config[i].core_role == ROLE_SERVICE) {
127                         if ((unsigned int)i == cfg->master_lcore)
128                                 continue;
129                         rte_service_lcore_add(i);
130                         count++;
131                 }
132         }
133
134         rte_service_library_initialized = 1;
135         return 0;
136 }
137
138 /* returns 1 if service is registered and has not been unregistered
139  * Returns 0 if service never registered, or has been unregistered
140  */
141 static inline int
142 service_valid(uint32_t id)
143 {
144         return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
145 }
146
147 /* validate ID and retrieve service pointer, or return error value */
148 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do {          \
149         if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))            \
150                 return retval;                                          \
151         service = &rte_services[id];                                    \
152 } while (0)
153
154 /* returns 1 if statistics should be colleced for service
155  * Returns 0 if statistics should not be collected for service
156  */
157 static inline int
158 service_stats_enabled(struct rte_service_spec_impl *impl)
159 {
160         return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
161 }
162
163 static inline int
164 service_mt_safe(struct rte_service_spec_impl *s)
165 {
166         return s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE;
167 }
168
169 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enabled)
170 {
171         struct rte_service_spec_impl *s;
172         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
173
174         if (enabled)
175                 s->internal_flags |= SERVICE_F_STATS_ENABLED;
176         else
177                 s->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
178
179         return 0;
180 }
181
182 uint32_t
183 rte_service_get_count(void)
184 {
185         return rte_service_count;
186 }
187
188 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id)
189 {
190         if (!service_id)
191                 return -EINVAL;
192
193         int i;
194         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
195                 if (service_valid(i) &&
196                                 strcmp(name, rte_services[i].spec.name) == 0) {
197                         *service_id = i;
198                         return 0;
199                 }
200         }
201
202         return -ENODEV;
203 }
204
205 const char *
206 rte_service_get_name(uint32_t id)
207 {
208         struct rte_service_spec_impl *s;
209         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
210         return s->spec.name;
211 }
212
213 int32_t
214 rte_service_probe_capability(uint32_t id, uint32_t capability)
215 {
216         struct rte_service_spec_impl *s;
217         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
218         return s->spec.capabilities & capability;
219 }
220
221 int32_t
222 rte_service_component_register(const struct rte_service_spec *spec,
223                                uint32_t *id_ptr)
224 {
225         uint32_t i;
226         int32_t free_slot = -1;
227
228         if (spec->callback == NULL || strlen(spec->name) == 0)
229                 return -EINVAL;
230
231         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
232                 if (!service_valid(i)) {
233                         free_slot = i;
234                         break;
235                 }
236         }
237
238         if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
239                 return -ENOSPC;
240
241         struct rte_service_spec_impl *s = &rte_services[free_slot];
242         s->spec = *spec;
243         s->internal_flags |= SERVICE_F_REGISTERED;
244
245         rte_smp_wmb();
246         rte_service_count++;
247
248         if (id_ptr)
249                 *id_ptr = free_slot;
250
251         return 0;
252 }
253
254 int32_t
255 rte_service_component_unregister(uint32_t id)
256 {
257         uint32_t i;
258         struct rte_service_spec_impl *s;
259         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
260
261         rte_service_count--;
262         rte_smp_wmb();
263
264         s->internal_flags &= ~(SERVICE_F_REGISTERED);
265
266         /* clear the run-bit in all cores */
267         for (i = 0; i < RTE_MAX_LCORE; i++)
268                 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
269
270         memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
271
272         return 0;
273 }
274
275 int32_t
276 rte_service_runstate_set(uint32_t id, uint32_t runstate)
277 {
278         struct rte_service_spec_impl *s;
279         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
280
281         if (runstate)
282                 s->runstate = RUNSTATE_RUNNING;
283         else
284                 s->runstate = RUNSTATE_STOPPED;
285
286         rte_smp_wmb();
287         return 0;
288 }
289
290 int32_t
291 rte_service_runstate_get(uint32_t id)
292 {
293         struct rte_service_spec_impl *s;
294         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
295
296         return (s->runstate == RUNSTATE_RUNNING) && (s->num_mapped_cores > 0);
297 }
298
299 static inline void
300 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
301                                struct core_state *cs, uint32_t service_idx)
302 {
303         void *userdata = s->spec.callback_userdata;
304
305         if (service_stats_enabled(s)) {
306                 uint64_t start = rte_rdtsc();
307                 s->spec.callback(userdata);
308                 uint64_t end = rte_rdtsc();
309                 s->cycles_spent += end - start;
310                 cs->calls_per_service[service_idx]++;
311                 s->calls++;
312         } else
313                 s->spec.callback(userdata);
314 }
315
316 static int32_t
317 rte_service_runner_func(void *arg)
318 {
319         RTE_SET_USED(arg);
320         uint32_t i;
321         const int lcore = rte_lcore_id();
322         struct core_state *cs = &lcore_states[lcore];
323
324         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
325                 const uint64_t service_mask = cs->service_mask;
326
327                 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
328                         if (!service_valid(i))
329                                 continue;
330                         struct rte_service_spec_impl *s = &rte_services[i];
331                         if (s->runstate != RUNSTATE_RUNNING ||
332                                         !(service_mask & (UINT64_C(1) << i)))
333                                 continue;
334
335                         /* check do we need cmpset, if MT safe or <= 1 core
336                          * mapped, atomic ops are not required.
337                          */
338                         const int use_atomics = (service_mt_safe(s) == 0) &&
339                                                 (s->num_mapped_cores > 1);
340                         if (use_atomics) {
341                                 uint32_t *lock = (uint32_t *)&s->execute_lock;
342                                 if (rte_atomic32_cmpset(lock, 0, 1)) {
343                                         rte_service_runner_do_callback(s, cs, i);
344                                         rte_atomic32_clear(&s->execute_lock);
345                                 }
346                         } else
347                                 rte_service_runner_do_callback(s, cs, i);
348                 }
349
350                 rte_smp_rmb();
351         }
352
353         lcore_config[lcore].state = WAIT;
354
355         return 0;
356 }
357
358 int32_t
359 rte_service_lcore_count(void)
360 {
361         int32_t count = 0;
362         uint32_t i;
363         for (i = 0; i < RTE_MAX_LCORE; i++)
364                 count += lcore_states[i].is_service_core;
365         return count;
366 }
367
368 int32_t
369 rte_service_lcore_list(uint32_t array[], uint32_t n)
370 {
371         uint32_t count = rte_service_lcore_count();
372         if (count > n)
373                 return -ENOMEM;
374
375         if (!array)
376                 return -EINVAL;
377
378         uint32_t i;
379         uint32_t idx = 0;
380         for (i = 0; i < RTE_MAX_LCORE; i++) {
381                 struct core_state *cs = &lcore_states[i];
382                 if (cs->is_service_core) {
383                         array[idx] = i;
384                         idx++;
385                 }
386         }
387
388         return count;
389 }
390
391 int32_t
392 rte_service_lcore_count_services(uint32_t lcore)
393 {
394         if (lcore >= RTE_MAX_LCORE)
395                 return -EINVAL;
396
397         struct core_state *cs = &lcore_states[lcore];
398         if (!cs->is_service_core)
399                 return -ENOTSUP;
400
401         return __builtin_popcountll(cs->service_mask);
402 }
403
404 int32_t
405 rte_service_start_with_defaults(void)
406 {
407         /* create a default mapping from cores to services, then start the
408          * services to make them transparent to unaware applications.
409          */
410         uint32_t i;
411         int ret;
412         uint32_t count = rte_service_get_count();
413
414         int32_t lcore_iter = 0;
415         uint32_t ids[RTE_MAX_LCORE];
416         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
417
418         if (lcore_count == 0)
419                 return -ENOTSUP;
420
421         for (i = 0; (int)i < lcore_count; i++)
422                 rte_service_lcore_start(ids[i]);
423
424         for (i = 0; i < count; i++) {
425                 /* do 1:1 core mapping here, with each service getting
426                  * assigned a single core by default. Adding multiple services
427                  * should multiplex to a single core, or 1:1 if there are the
428                  * same amount of services as service-cores
429                  */
430                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
431                 if (ret)
432                         return -ENODEV;
433
434                 lcore_iter++;
435                 if (lcore_iter >= lcore_count)
436                         lcore_iter = 0;
437
438                 ret = rte_service_runstate_set(i, 1);
439                 if (ret)
440                         return -ENOEXEC;
441         }
442
443         return 0;
444 }
445
446 static int32_t
447 service_update(struct rte_service_spec *service, uint32_t lcore,
448                 uint32_t *set, uint32_t *enabled)
449 {
450         uint32_t i;
451         int32_t sid = -1;
452
453         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
454                 if ((struct rte_service_spec *)&rte_services[i] == service &&
455                                 service_valid(i)) {
456                         sid = i;
457                         break;
458                 }
459         }
460
461         if (sid == -1 || lcore >= RTE_MAX_LCORE)
462                 return -EINVAL;
463
464         if (!lcore_states[lcore].is_service_core)
465                 return -EINVAL;
466
467         uint64_t sid_mask = UINT64_C(1) << sid;
468         if (set) {
469                 if (*set) {
470                         lcore_states[lcore].service_mask |= sid_mask;
471                         rte_services[sid].num_mapped_cores++;
472                 } else {
473                         lcore_states[lcore].service_mask &= ~(sid_mask);
474                         rte_services[sid].num_mapped_cores--;
475                 }
476         }
477
478         if (enabled)
479                 *enabled = (lcore_states[lcore].service_mask & (sid_mask));
480
481         rte_smp_wmb();
482
483         return 0;
484 }
485
486 int32_t
487 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
488 {
489         struct rte_service_spec_impl *s;
490         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
491         uint32_t on = enabled > 0;
492         return service_update(&s->spec, lcore, &on, 0);
493 }
494
495 int32_t
496 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
497 {
498         struct rte_service_spec_impl *s;
499         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
500         uint32_t enabled;
501         int ret = service_update(&s->spec, lcore, 0, &enabled);
502         if (ret == 0)
503                 return enabled;
504         return ret;
505 }
506
507 int32_t rte_service_lcore_reset_all(void)
508 {
509         /* loop over cores, reset all to mask 0 */
510         uint32_t i;
511         for (i = 0; i < RTE_MAX_LCORE; i++) {
512                 lcore_states[i].service_mask = 0;
513                 lcore_states[i].is_service_core = 0;
514                 lcore_states[i].runstate = RUNSTATE_STOPPED;
515         }
516         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
517                 rte_services[i].num_mapped_cores = 0;
518
519         rte_smp_wmb();
520
521         return 0;
522 }
523
524 static void
525 set_lcore_state(uint32_t lcore, int32_t state)
526 {
527         /* mark core state in hugepage backed config */
528         struct rte_config *cfg = rte_eal_get_configuration();
529         cfg->lcore_role[lcore] = state;
530
531         /* mark state in process local lcore_config */
532         lcore_config[lcore].core_role = state;
533
534         /* update per-lcore optimized state tracking */
535         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
536 }
537
538 int32_t
539 rte_service_lcore_add(uint32_t lcore)
540 {
541         if (lcore >= RTE_MAX_LCORE)
542                 return -EINVAL;
543         if (lcore_states[lcore].is_service_core)
544                 return -EALREADY;
545
546         set_lcore_state(lcore, ROLE_SERVICE);
547
548         /* ensure that after adding a core the mask and state are defaults */
549         lcore_states[lcore].service_mask = 0;
550         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
551
552         rte_smp_wmb();
553         return 0;
554 }
555
556 int32_t
557 rte_service_lcore_del(uint32_t lcore)
558 {
559         if (lcore >= RTE_MAX_LCORE)
560                 return -EINVAL;
561
562         struct core_state *cs = &lcore_states[lcore];
563         if (!cs->is_service_core)
564                 return -EINVAL;
565
566         if (cs->runstate != RUNSTATE_STOPPED)
567                 return -EBUSY;
568
569         set_lcore_state(lcore, ROLE_RTE);
570
571         rte_smp_wmb();
572         return 0;
573 }
574
575 int32_t
576 rte_service_lcore_start(uint32_t lcore)
577 {
578         if (lcore >= RTE_MAX_LCORE)
579                 return -EINVAL;
580
581         struct core_state *cs = &lcore_states[lcore];
582         if (!cs->is_service_core)
583                 return -EINVAL;
584
585         if (cs->runstate == RUNSTATE_RUNNING)
586                 return -EALREADY;
587
588         /* set core to run state first, and then launch otherwise it will
589          * return immediately as runstate keeps it in the service poll loop
590          */
591         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
592
593         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
594         /* returns -EBUSY if the core is already launched, 0 on success */
595         return ret;
596 }
597
598 int32_t
599 rte_service_lcore_stop(uint32_t lcore)
600 {
601         if (lcore >= RTE_MAX_LCORE)
602                 return -EINVAL;
603
604         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
605                 return -EALREADY;
606
607         uint32_t i;
608         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
609                 int32_t enabled =
610                         lcore_states[i].service_mask & (UINT64_C(1) << i);
611                 int32_t service_running = rte_services[i].runstate !=
612                                                 RUNSTATE_STOPPED;
613                 int32_t only_core = rte_services[i].num_mapped_cores == 1;
614
615                 /* if the core is mapped, and the service is running, and this
616                  * is the only core that is mapped, the service would cease to
617                  * run if this core stopped, so fail instead.
618                  */
619                 if (enabled && service_running && only_core)
620                         return -EBUSY;
621         }
622
623         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
624
625         return 0;
626 }
627
628 static void
629 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
630                      uint64_t all_cycles, uint32_t reset)
631 {
632         /* avoid divide by zero */
633         if (all_cycles == 0)
634                 all_cycles = 1;
635
636         int calls = 1;
637         if (s->calls != 0)
638                 calls = s->calls;
639
640         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
641                         PRIu64"\tavg: %"PRIu64"\n",
642                         s->spec.name, service_stats_enabled(s), s->calls,
643                         s->cycles_spent, s->cycles_spent / calls);
644
645         if (reset) {
646                 s->cycles_spent = 0;
647                 s->calls = 0;
648         }
649 }
650
651 static void
652 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
653 {
654         uint32_t i;
655         struct core_state *cs = &lcore_states[lcore];
656
657         fprintf(f, "%02d\t", lcore);
658         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
659                 if (!service_valid(i))
660                         continue;
661                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
662                 if (reset)
663                         cs->calls_per_service[i] = 0;
664         }
665         fprintf(f, "\n");
666 }
667
668 int32_t rte_service_dump(FILE *f, uint32_t id)
669 {
670         uint32_t i;
671         int print_one = (id != UINT32_MAX);
672
673         uint64_t total_cycles = 0;
674
675         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
676                 if (!service_valid(i))
677                         continue;
678                 total_cycles += rte_services[i].cycles_spent;
679         }
680
681         /* print only the specified service */
682         if (print_one) {
683                 struct rte_service_spec_impl *s;
684                 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
685                 fprintf(f, "Service %s Summary\n", s->spec.name);
686                 uint32_t reset = 0;
687                 rte_service_dump_one(f, s, total_cycles, reset);
688                 return 0;
689         }
690
691         /* print all services, as UINT32_MAX was passed as id */
692         fprintf(f, "Services Summary\n");
693         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
694                 if (!service_valid(i))
695                         continue;
696                 uint32_t reset = 1;
697                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
698         }
699
700         fprintf(f, "Service Cores Summary\n");
701         for (i = 0; i < RTE_MAX_LCORE; i++) {
702                 if (lcore_config[i].core_role != ROLE_SERVICE)
703                         continue;
704
705                 uint32_t reset = 0;
706                 service_dump_calls_per_lcore(f, i, reset);
707         }
708
709         return 0;
710 }