service: return integer service id from register
[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(struct rte_service_spec *service,
170                                   int32_t enabled)
171 {
172         struct rte_service_spec_impl *impl =
173                 (struct rte_service_spec_impl *)service;
174         if (!impl)
175                 return -EINVAL;
176
177         if (enabled)
178                 impl->internal_flags |= SERVICE_F_STATS_ENABLED;
179         else
180                 impl->internal_flags &= ~(SERVICE_F_STATS_ENABLED);
181
182         return 0;
183 }
184
185 uint32_t
186 rte_service_get_count(void)
187 {
188         return rte_service_count;
189 }
190
191 struct rte_service_spec *
192 rte_service_get_by_id(uint32_t id)
193 {
194         struct rte_service_spec *service = NULL;
195         if (id < rte_service_count)
196                 service = (struct rte_service_spec *)&rte_services[id];
197
198         return service;
199 }
200
201 struct rte_service_spec *rte_service_get_by_name(const char *name)
202 {
203         struct rte_service_spec *service = NULL;
204         int i;
205         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
206                 if (service_valid(i) &&
207                                 strcmp(name, rte_services[i].spec.name) == 0) {
208                         service = (struct rte_service_spec *)&rte_services[i];
209                         break;
210                 }
211         }
212
213         return service;
214 }
215
216 const char *
217 rte_service_get_name(uint32_t id)
218 {
219         struct rte_service_spec_impl *s;
220         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
221         return s->spec.name;
222 }
223
224 int32_t
225 rte_service_probe_capability(uint32_t id, uint32_t capability)
226 {
227         struct rte_service_spec_impl *s;
228         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
229         return s->spec.capabilities & capability;
230 }
231
232 int32_t
233 rte_service_is_running(const struct rte_service_spec *spec)
234 {
235         const struct rte_service_spec_impl *impl =
236                 (const struct rte_service_spec_impl *)spec;
237         if (!impl)
238                 return -EINVAL;
239
240         return (impl->runstate == RUNSTATE_RUNNING) &&
241                 (impl->num_mapped_cores > 0);
242 }
243
244 int32_t
245 rte_service_component_register(const struct rte_service_spec *spec,
246                                uint32_t *id_ptr)
247 {
248         uint32_t i;
249         int32_t free_slot = -1;
250
251         if (spec->callback == NULL || strlen(spec->name) == 0)
252                 return -EINVAL;
253
254         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
255                 if (!service_valid(i)) {
256                         free_slot = i;
257                         break;
258                 }
259         }
260
261         if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
262                 return -ENOSPC;
263
264         struct rte_service_spec_impl *s = &rte_services[free_slot];
265         s->spec = *spec;
266         s->internal_flags |= SERVICE_F_REGISTERED;
267
268         rte_smp_wmb();
269         rte_service_count++;
270
271         if (id_ptr)
272                 *id_ptr = free_slot;
273
274         return 0;
275 }
276
277 int32_t
278 rte_service_unregister(struct rte_service_spec *spec)
279 {
280         struct rte_service_spec_impl *s = NULL;
281         struct rte_service_spec_impl *spec_impl =
282                 (struct rte_service_spec_impl *)spec;
283
284         uint32_t i;
285         uint32_t service_id;
286         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
287                 if (&rte_services[i] == spec_impl) {
288                         s = spec_impl;
289                         service_id = i;
290                         break;
291                 }
292         }
293
294         if (!s)
295                 return -EINVAL;
296
297         rte_service_count--;
298         rte_smp_wmb();
299
300         s->internal_flags &= ~(SERVICE_F_REGISTERED);
301
302         for (i = 0; i < RTE_MAX_LCORE; i++)
303                 lcore_states[i].service_mask &= ~(UINT64_C(1) << service_id);
304
305         memset(&rte_services[service_id], 0,
306                         sizeof(struct rte_service_spec_impl));
307
308         return 0;
309 }
310
311 int32_t
312 rte_service_start(struct rte_service_spec *service)
313 {
314         struct rte_service_spec_impl *s =
315                 (struct rte_service_spec_impl *)service;
316         s->runstate = RUNSTATE_RUNNING;
317         rte_smp_wmb();
318         return 0;
319 }
320
321 int32_t
322 rte_service_stop(struct rte_service_spec *service)
323 {
324         struct rte_service_spec_impl *s =
325                 (struct rte_service_spec_impl *)service;
326         s->runstate = RUNSTATE_STOPPED;
327         rte_smp_wmb();
328         return 0;
329 }
330
331 static int32_t
332 rte_service_runner_func(void *arg)
333 {
334         RTE_SET_USED(arg);
335         uint32_t i;
336         const int lcore = rte_lcore_id();
337         struct core_state *cs = &lcore_states[lcore];
338
339         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
340                 const uint64_t service_mask = cs->service_mask;
341                 for (i = 0; i < rte_service_count; i++) {
342                         struct rte_service_spec_impl *s = &rte_services[i];
343                         if (s->runstate != RUNSTATE_RUNNING ||
344                                         !(service_mask & (UINT64_C(1) << i)))
345                                 continue;
346
347                         /* check do we need cmpset, if MT safe or <= 1 core
348                          * mapped, atomic ops are not required.
349                          */
350                         const int need_cmpset = !((service_mt_safe(s) == 0) &&
351                                                 (s->num_mapped_cores > 1));
352                         uint32_t *lock = (uint32_t *)&s->execute_lock;
353
354                         if (need_cmpset || rte_atomic32_cmpset(lock, 0, 1)) {
355                                 void *userdata = s->spec.callback_userdata;
356
357                                 if (service_stats_enabled(s)) {
358                                         uint64_t start = rte_rdtsc();
359                                         s->spec.callback(userdata);
360                                         uint64_t end = rte_rdtsc();
361                                         s->cycles_spent += end - start;
362                                         cs->calls_per_service[i]++;
363                                         s->calls++;
364                                 } else
365                                         s->spec.callback(userdata);
366
367                                 if (need_cmpset)
368                                         rte_atomic32_clear(&s->execute_lock);
369                         }
370                 }
371
372                 rte_smp_rmb();
373         }
374
375         lcore_config[lcore].state = WAIT;
376
377         return 0;
378 }
379
380 int32_t
381 rte_service_lcore_count(void)
382 {
383         int32_t count = 0;
384         uint32_t i;
385         for (i = 0; i < RTE_MAX_LCORE; i++)
386                 count += lcore_states[i].is_service_core;
387         return count;
388 }
389
390 int32_t
391 rte_service_lcore_list(uint32_t array[], uint32_t n)
392 {
393         uint32_t count = rte_service_lcore_count();
394         if (count > n)
395                 return -ENOMEM;
396
397         if (!array)
398                 return -EINVAL;
399
400         uint32_t i;
401         uint32_t idx = 0;
402         for (i = 0; i < RTE_MAX_LCORE; i++) {
403                 struct core_state *cs = &lcore_states[i];
404                 if (cs->is_service_core) {
405                         array[idx] = i;
406                         idx++;
407                 }
408         }
409
410         return count;
411 }
412
413 int32_t
414 rte_service_lcore_count_services(uint32_t lcore)
415 {
416         if (lcore >= RTE_MAX_LCORE)
417                 return -EINVAL;
418
419         struct core_state *cs = &lcore_states[lcore];
420         if (!cs->is_service_core)
421                 return -ENOTSUP;
422
423         return __builtin_popcountll(cs->service_mask);
424 }
425
426 int32_t
427 rte_service_start_with_defaults(void)
428 {
429         /* create a default mapping from cores to services, then start the
430          * services to make them transparent to unaware applications.
431          */
432         uint32_t i;
433         int ret;
434         uint32_t count = rte_service_get_count();
435
436         int32_t lcore_iter = 0;
437         uint32_t ids[RTE_MAX_LCORE];
438         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
439
440         if (lcore_count == 0)
441                 return -ENOTSUP;
442
443         for (i = 0; (int)i < lcore_count; i++)
444                 rte_service_lcore_start(ids[i]);
445
446         for (i = 0; i < count; i++) {
447                 struct rte_service_spec *s = rte_service_get_by_id(i);
448                 if (!s)
449                         return -EINVAL;
450
451                 /* do 1:1 core mapping here, with each service getting
452                  * assigned a single core by default. Adding multiple services
453                  * should multiplex to a single core, or 1:1 if there are the
454                  * same amount of services as service-cores
455                  */
456                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
457                 if (ret)
458                         return -ENODEV;
459
460                 lcore_iter++;
461                 if (lcore_iter >= lcore_count)
462                         lcore_iter = 0;
463
464                 ret = rte_service_start(s);
465                 if (ret)
466                         return -ENOEXEC;
467         }
468
469         return 0;
470 }
471
472 static int32_t
473 service_update(struct rte_service_spec *service, uint32_t lcore,
474                 uint32_t *set, uint32_t *enabled)
475 {
476         uint32_t i;
477         int32_t sid = -1;
478
479         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
480                 if ((struct rte_service_spec *)&rte_services[i] == service &&
481                                 service_valid(i)) {
482                         sid = i;
483                         break;
484                 }
485         }
486
487         if (sid == -1 || lcore >= RTE_MAX_LCORE)
488                 return -EINVAL;
489
490         if (!lcore_states[lcore].is_service_core)
491                 return -EINVAL;
492
493         uint64_t sid_mask = UINT64_C(1) << sid;
494         if (set) {
495                 if (*set) {
496                         lcore_states[lcore].service_mask |= sid_mask;
497                         rte_services[sid].num_mapped_cores++;
498                 } else {
499                         lcore_states[lcore].service_mask &= ~(sid_mask);
500                         rte_services[sid].num_mapped_cores--;
501                 }
502         }
503
504         if (enabled)
505                 *enabled = (lcore_states[lcore].service_mask & (sid_mask));
506
507         rte_smp_wmb();
508
509         return 0;
510 }
511
512 int32_t
513 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
514 {
515         struct rte_service_spec_impl *s;
516         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
517         uint32_t on = enabled > 0;
518         return service_update(&s->spec, lcore, &on, 0);
519 }
520
521 int32_t
522 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
523 {
524         struct rte_service_spec_impl *s;
525         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
526         uint32_t enabled;
527         int ret = service_update(&s->spec, lcore, 0, &enabled);
528         if (ret == 0)
529                 return enabled;
530         return ret;
531 }
532
533 int32_t rte_service_lcore_reset_all(void)
534 {
535         /* loop over cores, reset all to mask 0 */
536         uint32_t i;
537         for (i = 0; i < RTE_MAX_LCORE; i++) {
538                 lcore_states[i].service_mask = 0;
539                 lcore_states[i].is_service_core = 0;
540                 lcore_states[i].runstate = RUNSTATE_STOPPED;
541         }
542         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
543                 rte_services[i].num_mapped_cores = 0;
544
545         rte_smp_wmb();
546
547         return 0;
548 }
549
550 static void
551 set_lcore_state(uint32_t lcore, int32_t state)
552 {
553         /* mark core state in hugepage backed config */
554         struct rte_config *cfg = rte_eal_get_configuration();
555         cfg->lcore_role[lcore] = state;
556
557         /* mark state in process local lcore_config */
558         lcore_config[lcore].core_role = state;
559
560         /* update per-lcore optimized state tracking */
561         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
562 }
563
564 int32_t
565 rte_service_lcore_add(uint32_t lcore)
566 {
567         if (lcore >= RTE_MAX_LCORE)
568                 return -EINVAL;
569         if (lcore_states[lcore].is_service_core)
570                 return -EALREADY;
571
572         set_lcore_state(lcore, ROLE_SERVICE);
573
574         /* ensure that after adding a core the mask and state are defaults */
575         lcore_states[lcore].service_mask = 0;
576         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
577
578         rte_smp_wmb();
579         return 0;
580 }
581
582 int32_t
583 rte_service_lcore_del(uint32_t lcore)
584 {
585         if (lcore >= RTE_MAX_LCORE)
586                 return -EINVAL;
587
588         struct core_state *cs = &lcore_states[lcore];
589         if (!cs->is_service_core)
590                 return -EINVAL;
591
592         if (cs->runstate != RUNSTATE_STOPPED)
593                 return -EBUSY;
594
595         set_lcore_state(lcore, ROLE_RTE);
596
597         rte_smp_wmb();
598         return 0;
599 }
600
601 int32_t
602 rte_service_lcore_start(uint32_t lcore)
603 {
604         if (lcore >= RTE_MAX_LCORE)
605                 return -EINVAL;
606
607         struct core_state *cs = &lcore_states[lcore];
608         if (!cs->is_service_core)
609                 return -EINVAL;
610
611         if (cs->runstate == RUNSTATE_RUNNING)
612                 return -EALREADY;
613
614         /* set core to run state first, and then launch otherwise it will
615          * return immediately as runstate keeps it in the service poll loop
616          */
617         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
618
619         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
620         /* returns -EBUSY if the core is already launched, 0 on success */
621         return ret;
622 }
623
624 int32_t
625 rte_service_lcore_stop(uint32_t lcore)
626 {
627         if (lcore >= RTE_MAX_LCORE)
628                 return -EINVAL;
629
630         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
631                 return -EALREADY;
632
633         uint32_t i;
634         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
635                 int32_t enabled =
636                         lcore_states[i].service_mask & (UINT64_C(1) << i);
637                 int32_t service_running = rte_services[i].runstate !=
638                                                 RUNSTATE_STOPPED;
639                 int32_t only_core = rte_services[i].num_mapped_cores == 1;
640
641                 /* if the core is mapped, and the service is running, and this
642                  * is the only core that is mapped, the service would cease to
643                  * run if this core stopped, so fail instead.
644                  */
645                 if (enabled && service_running && only_core)
646                         return -EBUSY;
647         }
648
649         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
650
651         return 0;
652 }
653
654 static void
655 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
656                      uint64_t all_cycles, uint32_t reset)
657 {
658         /* avoid divide by zero */
659         if (all_cycles == 0)
660                 all_cycles = 1;
661
662         int calls = 1;
663         if (s->calls != 0)
664                 calls = s->calls;
665
666         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
667                         PRIu64"\tavg: %"PRIu64"\n",
668                         s->spec.name, service_stats_enabled(s), s->calls,
669                         s->cycles_spent, s->cycles_spent / calls);
670
671         if (reset) {
672                 s->cycles_spent = 0;
673                 s->calls = 0;
674         }
675 }
676
677 static void
678 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
679 {
680         uint32_t i;
681         struct core_state *cs = &lcore_states[lcore];
682
683         fprintf(f, "%02d\t", lcore);
684         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
685                 if (!service_valid(i))
686                         continue;
687                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
688                 if (reset)
689                         cs->calls_per_service[i] = 0;
690         }
691         fprintf(f, "\n");
692 }
693
694 int32_t rte_service_dump(FILE *f, struct rte_service_spec *service)
695 {
696         uint32_t i;
697
698         uint64_t total_cycles = 0;
699         for (i = 0; i < rte_service_count; i++) {
700                 if (!service_valid(i))
701                         continue;
702                 total_cycles += rte_services[i].cycles_spent;
703         }
704
705         if (service) {
706                 struct rte_service_spec_impl *s =
707                         (struct rte_service_spec_impl *)service;
708                 fprintf(f, "Service %s Summary\n", s->spec.name);
709                 uint32_t reset = 0;
710                 rte_service_dump_one(f, s, total_cycles, reset);
711                 return 0;
712         }
713
714         fprintf(f, "Services Summary\n");
715         for (i = 0; i < rte_service_count; i++) {
716                 uint32_t reset = 1;
717                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
718         }
719
720         fprintf(f, "Service Cores Summary\n");
721         for (i = 0; i < RTE_MAX_LCORE; i++) {
722                 if (lcore_config[i].core_role != ROLE_SERVICE)
723                         continue;
724
725                 uint32_t reset = 0;
726                 service_dump_calls_per_lcore(f, i, reset);
727         }
728
729         return 0;
730 }