service: use id in get by name function
[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 int32_t
300 rte_service_runner_func(void *arg)
301 {
302         RTE_SET_USED(arg);
303         uint32_t i;
304         const int lcore = rte_lcore_id();
305         struct core_state *cs = &lcore_states[lcore];
306
307         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
308                 const uint64_t service_mask = cs->service_mask;
309                 for (i = 0; i < rte_service_count; i++) {
310                         struct rte_service_spec_impl *s = &rte_services[i];
311                         if (s->runstate != RUNSTATE_RUNNING ||
312                                         !(service_mask & (UINT64_C(1) << i)))
313                                 continue;
314
315                         /* check do we need cmpset, if MT safe or <= 1 core
316                          * mapped, atomic ops are not required.
317                          */
318                         const int need_cmpset = !((service_mt_safe(s) == 0) &&
319                                                 (s->num_mapped_cores > 1));
320                         uint32_t *lock = (uint32_t *)&s->execute_lock;
321
322                         if (need_cmpset || rte_atomic32_cmpset(lock, 0, 1)) {
323                                 void *userdata = s->spec.callback_userdata;
324
325                                 if (service_stats_enabled(s)) {
326                                         uint64_t start = rte_rdtsc();
327                                         s->spec.callback(userdata);
328                                         uint64_t end = rte_rdtsc();
329                                         s->cycles_spent += end - start;
330                                         cs->calls_per_service[i]++;
331                                         s->calls++;
332                                 } else
333                                         s->spec.callback(userdata);
334
335                                 if (need_cmpset)
336                                         rte_atomic32_clear(&s->execute_lock);
337                         }
338                 }
339
340                 rte_smp_rmb();
341         }
342
343         lcore_config[lcore].state = WAIT;
344
345         return 0;
346 }
347
348 int32_t
349 rte_service_lcore_count(void)
350 {
351         int32_t count = 0;
352         uint32_t i;
353         for (i = 0; i < RTE_MAX_LCORE; i++)
354                 count += lcore_states[i].is_service_core;
355         return count;
356 }
357
358 int32_t
359 rte_service_lcore_list(uint32_t array[], uint32_t n)
360 {
361         uint32_t count = rte_service_lcore_count();
362         if (count > n)
363                 return -ENOMEM;
364
365         if (!array)
366                 return -EINVAL;
367
368         uint32_t i;
369         uint32_t idx = 0;
370         for (i = 0; i < RTE_MAX_LCORE; i++) {
371                 struct core_state *cs = &lcore_states[i];
372                 if (cs->is_service_core) {
373                         array[idx] = i;
374                         idx++;
375                 }
376         }
377
378         return count;
379 }
380
381 int32_t
382 rte_service_lcore_count_services(uint32_t lcore)
383 {
384         if (lcore >= RTE_MAX_LCORE)
385                 return -EINVAL;
386
387         struct core_state *cs = &lcore_states[lcore];
388         if (!cs->is_service_core)
389                 return -ENOTSUP;
390
391         return __builtin_popcountll(cs->service_mask);
392 }
393
394 int32_t
395 rte_service_start_with_defaults(void)
396 {
397         /* create a default mapping from cores to services, then start the
398          * services to make them transparent to unaware applications.
399          */
400         uint32_t i;
401         int ret;
402         uint32_t count = rte_service_get_count();
403
404         int32_t lcore_iter = 0;
405         uint32_t ids[RTE_MAX_LCORE];
406         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
407
408         if (lcore_count == 0)
409                 return -ENOTSUP;
410
411         for (i = 0; (int)i < lcore_count; i++)
412                 rte_service_lcore_start(ids[i]);
413
414         for (i = 0; i < count; i++) {
415                 /* do 1:1 core mapping here, with each service getting
416                  * assigned a single core by default. Adding multiple services
417                  * should multiplex to a single core, or 1:1 if there are the
418                  * same amount of services as service-cores
419                  */
420                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
421                 if (ret)
422                         return -ENODEV;
423
424                 lcore_iter++;
425                 if (lcore_iter >= lcore_count)
426                         lcore_iter = 0;
427
428                 ret = rte_service_runstate_set(i, 1);
429                 if (ret)
430                         return -ENOEXEC;
431         }
432
433         return 0;
434 }
435
436 static int32_t
437 service_update(struct rte_service_spec *service, uint32_t lcore,
438                 uint32_t *set, uint32_t *enabled)
439 {
440         uint32_t i;
441         int32_t sid = -1;
442
443         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
444                 if ((struct rte_service_spec *)&rte_services[i] == service &&
445                                 service_valid(i)) {
446                         sid = i;
447                         break;
448                 }
449         }
450
451         if (sid == -1 || lcore >= RTE_MAX_LCORE)
452                 return -EINVAL;
453
454         if (!lcore_states[lcore].is_service_core)
455                 return -EINVAL;
456
457         uint64_t sid_mask = UINT64_C(1) << sid;
458         if (set) {
459                 if (*set) {
460                         lcore_states[lcore].service_mask |= sid_mask;
461                         rte_services[sid].num_mapped_cores++;
462                 } else {
463                         lcore_states[lcore].service_mask &= ~(sid_mask);
464                         rte_services[sid].num_mapped_cores--;
465                 }
466         }
467
468         if (enabled)
469                 *enabled = (lcore_states[lcore].service_mask & (sid_mask));
470
471         rte_smp_wmb();
472
473         return 0;
474 }
475
476 int32_t
477 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
478 {
479         struct rte_service_spec_impl *s;
480         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
481         uint32_t on = enabled > 0;
482         return service_update(&s->spec, lcore, &on, 0);
483 }
484
485 int32_t
486 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
487 {
488         struct rte_service_spec_impl *s;
489         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
490         uint32_t enabled;
491         int ret = service_update(&s->spec, lcore, 0, &enabled);
492         if (ret == 0)
493                 return enabled;
494         return ret;
495 }
496
497 int32_t rte_service_lcore_reset_all(void)
498 {
499         /* loop over cores, reset all to mask 0 */
500         uint32_t i;
501         for (i = 0; i < RTE_MAX_LCORE; i++) {
502                 lcore_states[i].service_mask = 0;
503                 lcore_states[i].is_service_core = 0;
504                 lcore_states[i].runstate = RUNSTATE_STOPPED;
505         }
506         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
507                 rte_services[i].num_mapped_cores = 0;
508
509         rte_smp_wmb();
510
511         return 0;
512 }
513
514 static void
515 set_lcore_state(uint32_t lcore, int32_t state)
516 {
517         /* mark core state in hugepage backed config */
518         struct rte_config *cfg = rte_eal_get_configuration();
519         cfg->lcore_role[lcore] = state;
520
521         /* mark state in process local lcore_config */
522         lcore_config[lcore].core_role = state;
523
524         /* update per-lcore optimized state tracking */
525         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
526 }
527
528 int32_t
529 rte_service_lcore_add(uint32_t lcore)
530 {
531         if (lcore >= RTE_MAX_LCORE)
532                 return -EINVAL;
533         if (lcore_states[lcore].is_service_core)
534                 return -EALREADY;
535
536         set_lcore_state(lcore, ROLE_SERVICE);
537
538         /* ensure that after adding a core the mask and state are defaults */
539         lcore_states[lcore].service_mask = 0;
540         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
541
542         rte_smp_wmb();
543         return 0;
544 }
545
546 int32_t
547 rte_service_lcore_del(uint32_t lcore)
548 {
549         if (lcore >= RTE_MAX_LCORE)
550                 return -EINVAL;
551
552         struct core_state *cs = &lcore_states[lcore];
553         if (!cs->is_service_core)
554                 return -EINVAL;
555
556         if (cs->runstate != RUNSTATE_STOPPED)
557                 return -EBUSY;
558
559         set_lcore_state(lcore, ROLE_RTE);
560
561         rte_smp_wmb();
562         return 0;
563 }
564
565 int32_t
566 rte_service_lcore_start(uint32_t lcore)
567 {
568         if (lcore >= RTE_MAX_LCORE)
569                 return -EINVAL;
570
571         struct core_state *cs = &lcore_states[lcore];
572         if (!cs->is_service_core)
573                 return -EINVAL;
574
575         if (cs->runstate == RUNSTATE_RUNNING)
576                 return -EALREADY;
577
578         /* set core to run state first, and then launch otherwise it will
579          * return immediately as runstate keeps it in the service poll loop
580          */
581         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
582
583         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
584         /* returns -EBUSY if the core is already launched, 0 on success */
585         return ret;
586 }
587
588 int32_t
589 rte_service_lcore_stop(uint32_t lcore)
590 {
591         if (lcore >= RTE_MAX_LCORE)
592                 return -EINVAL;
593
594         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
595                 return -EALREADY;
596
597         uint32_t i;
598         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
599                 int32_t enabled =
600                         lcore_states[i].service_mask & (UINT64_C(1) << i);
601                 int32_t service_running = rte_services[i].runstate !=
602                                                 RUNSTATE_STOPPED;
603                 int32_t only_core = rte_services[i].num_mapped_cores == 1;
604
605                 /* if the core is mapped, and the service is running, and this
606                  * is the only core that is mapped, the service would cease to
607                  * run if this core stopped, so fail instead.
608                  */
609                 if (enabled && service_running && only_core)
610                         return -EBUSY;
611         }
612
613         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
614
615         return 0;
616 }
617
618 static void
619 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
620                      uint64_t all_cycles, uint32_t reset)
621 {
622         /* avoid divide by zero */
623         if (all_cycles == 0)
624                 all_cycles = 1;
625
626         int calls = 1;
627         if (s->calls != 0)
628                 calls = s->calls;
629
630         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
631                         PRIu64"\tavg: %"PRIu64"\n",
632                         s->spec.name, service_stats_enabled(s), s->calls,
633                         s->cycles_spent, s->cycles_spent / calls);
634
635         if (reset) {
636                 s->cycles_spent = 0;
637                 s->calls = 0;
638         }
639 }
640
641 static void
642 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
643 {
644         uint32_t i;
645         struct core_state *cs = &lcore_states[lcore];
646
647         fprintf(f, "%02d\t", lcore);
648         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
649                 if (!service_valid(i))
650                         continue;
651                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
652                 if (reset)
653                         cs->calls_per_service[i] = 0;
654         }
655         fprintf(f, "\n");
656 }
657
658 int32_t rte_service_dump(FILE *f, uint32_t id)
659 {
660         uint32_t i;
661         int print_one = (id != UINT32_MAX);
662
663         uint64_t total_cycles = 0;
664         for (i = 0; i < rte_service_count; i++) {
665                 if (!service_valid(i))
666                         continue;
667                 total_cycles += rte_services[i].cycles_spent;
668         }
669
670         /* print only the specified service */
671         if (print_one) {
672                 struct rte_service_spec_impl *s;
673                 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
674                 fprintf(f, "Service %s Summary\n", s->spec.name);
675                 uint32_t reset = 0;
676                 rte_service_dump_one(f, s, total_cycles, reset);
677                 return 0;
678         }
679
680         /* print all services, as UINT32_MAX was passed as id */
681         fprintf(f, "Services Summary\n");
682         for (i = 0; i < rte_service_count; i++) {
683                 uint32_t reset = 1;
684                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
685         }
686
687         fprintf(f, "Service Cores Summary\n");
688         for (i = 0; i < RTE_MAX_LCORE; i++) {
689                 if (lcore_config[i].core_role != ROLE_SERVICE)
690                         continue;
691
692                 uint32_t reset = 0;
693                 service_dump_calls_per_lcore(f, i, reset);
694         }
695
696         return 0;
697 }