service: don't walk out of bounds when checking services
[dpdk.git] / lib / librte_eal / common / rte_service.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <inttypes.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include <rte_compat.h>
12 #include <rte_service.h>
13 #include "include/rte_service_component.h"
14
15 #include <rte_eal.h>
16 #include <rte_lcore.h>
17 #include <rte_common.h>
18 #include <rte_debug.h>
19 #include <rte_cycles.h>
20 #include <rte_atomic.h>
21 #include <rte_memory.h>
22 #include <rte_malloc.h>
23
24 #include "eal_private.h"
25
26 #define RTE_SERVICE_NUM_MAX 64
27
28 #define SERVICE_F_REGISTERED    (1 << 0)
29 #define SERVICE_F_STATS_ENABLED (1 << 1)
30 #define SERVICE_F_START_CHECK   (1 << 2)
31
32 /* runstates for services and lcores, denoting if they are active or not */
33 #define RUNSTATE_STOPPED 0
34 #define RUNSTATE_RUNNING 1
35
36 /* internal representation of a service */
37 struct rte_service_spec_impl {
38         /* public part of the struct */
39         struct rte_service_spec spec;
40
41         /* atomic lock that when set indicates a service core is currently
42          * running this service callback. When not set, a core may take the
43          * lock and then run the service callback.
44          */
45         rte_atomic32_t execute_lock;
46
47         /* API set/get-able variables */
48         int8_t app_runstate;
49         int8_t comp_runstate;
50         uint8_t internal_flags;
51
52         /* per service statistics */
53         rte_atomic32_t num_mapped_cores;
54         uint64_t calls;
55         uint64_t cycles_spent;
56 } __rte_cache_aligned;
57
58 /* the internal values of a service core */
59 struct core_state {
60         /* map of services IDs are run on this core */
61         uint64_t service_mask;
62         uint8_t runstate; /* running or stopped */
63         uint8_t is_service_core; /* set if core is currently a service core */
64         uint8_t service_active_on_lcore[RTE_SERVICE_NUM_MAX];
65         uint64_t loops;
66         uint64_t calls_per_service[RTE_SERVICE_NUM_MAX];
67 } __rte_cache_aligned;
68
69 static uint32_t rte_service_count;
70 static struct rte_service_spec_impl *rte_services;
71 static struct core_state *lcore_states;
72 static uint32_t rte_service_library_initialized;
73
74 int32_t
75 rte_service_init(void)
76 {
77         if (rte_service_library_initialized) {
78                 RTE_LOG(NOTICE, EAL,
79                         "service library init() called, init flag %d\n",
80                         rte_service_library_initialized);
81                 return -EALREADY;
82         }
83
84         rte_services = rte_calloc("rte_services", RTE_SERVICE_NUM_MAX,
85                         sizeof(struct rte_service_spec_impl),
86                         RTE_CACHE_LINE_SIZE);
87         if (!rte_services) {
88                 RTE_LOG(ERR, EAL, "error allocating rte services array\n");
89                 goto fail_mem;
90         }
91
92         lcore_states = rte_calloc("rte_service_core_states", RTE_MAX_LCORE,
93                         sizeof(struct core_state), RTE_CACHE_LINE_SIZE);
94         if (!lcore_states) {
95                 RTE_LOG(ERR, EAL, "error allocating core states array\n");
96                 goto fail_mem;
97         }
98
99         int i;
100         int count = 0;
101         struct rte_config *cfg = rte_eal_get_configuration();
102         for (i = 0; i < RTE_MAX_LCORE; i++) {
103                 if (lcore_config[i].core_role == ROLE_SERVICE) {
104                         if ((unsigned int)i == cfg->master_lcore)
105                                 continue;
106                         rte_service_lcore_add(i);
107                         count++;
108                 }
109         }
110
111         rte_service_library_initialized = 1;
112         return 0;
113 fail_mem:
114         rte_free(rte_services);
115         rte_free(lcore_states);
116         return -ENOMEM;
117 }
118
119 void
120 rte_service_finalize(void)
121 {
122         if (!rte_service_library_initialized)
123                 return;
124
125         rte_free(rte_services);
126         rte_free(lcore_states);
127
128         rte_service_library_initialized = 0;
129 }
130
131 /* returns 1 if service is registered and has not been unregistered
132  * Returns 0 if service never registered, or has been unregistered
133  */
134 static inline int
135 service_valid(uint32_t id)
136 {
137         return !!(rte_services[id].internal_flags & SERVICE_F_REGISTERED);
138 }
139
140 static struct rte_service_spec_impl *
141 service_get(uint32_t id)
142 {
143         return &rte_services[id];
144 }
145
146 /* validate ID and retrieve service pointer, or return error value */
147 #define SERVICE_VALID_GET_OR_ERR_RET(id, service, retval) do {          \
148         if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))            \
149                 return retval;                                          \
150         service = &rte_services[id];                                    \
151 } while (0)
152
153 /* returns 1 if statistics should be collected for service
154  * Returns 0 if statistics should not be collected for service
155  */
156 static inline int
157 service_stats_enabled(struct rte_service_spec_impl *impl)
158 {
159         return !!(impl->internal_flags & SERVICE_F_STATS_ENABLED);
160 }
161
162 static inline int
163 service_mt_safe(struct rte_service_spec_impl *s)
164 {
165         return !!(s->spec.capabilities & RTE_SERVICE_CAP_MT_SAFE);
166 }
167
168 int32_t
169 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 int32_t
183 rte_service_set_runstate_mapped_check(uint32_t id, int32_t enabled)
184 {
185         struct rte_service_spec_impl *s;
186         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
187
188         if (enabled)
189                 s->internal_flags |= SERVICE_F_START_CHECK;
190         else
191                 s->internal_flags &= ~(SERVICE_F_START_CHECK);
192
193         return 0;
194 }
195
196 uint32_t
197 rte_service_get_count(void)
198 {
199         return rte_service_count;
200 }
201
202 int32_t
203 rte_service_get_by_name(const char *name, uint32_t *service_id)
204 {
205         if (!service_id)
206                 return -EINVAL;
207
208         int i;
209         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
210                 if (service_valid(i) &&
211                                 strcmp(name, rte_services[i].spec.name) == 0) {
212                         *service_id = i;
213                         return 0;
214                 }
215         }
216
217         return -ENODEV;
218 }
219
220 const char *
221 rte_service_get_name(uint32_t id)
222 {
223         struct rte_service_spec_impl *s;
224         SERVICE_VALID_GET_OR_ERR_RET(id, s, 0);
225         return s->spec.name;
226 }
227
228 int32_t
229 rte_service_probe_capability(uint32_t id, uint32_t capability)
230 {
231         struct rte_service_spec_impl *s;
232         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
233         return !!(s->spec.capabilities & capability);
234 }
235
236 int32_t
237 rte_service_component_register(const struct rte_service_spec *spec,
238                                uint32_t *id_ptr)
239 {
240         uint32_t i;
241         int32_t free_slot = -1;
242
243         if (spec->callback == NULL || strlen(spec->name) == 0)
244                 return -EINVAL;
245
246         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
247                 if (!service_valid(i)) {
248                         free_slot = i;
249                         break;
250                 }
251         }
252
253         if ((free_slot < 0) || (i == RTE_SERVICE_NUM_MAX))
254                 return -ENOSPC;
255
256         struct rte_service_spec_impl *s = &rte_services[free_slot];
257         s->spec = *spec;
258         s->internal_flags |= SERVICE_F_REGISTERED | SERVICE_F_START_CHECK;
259
260         rte_smp_wmb();
261         rte_service_count++;
262
263         if (id_ptr)
264                 *id_ptr = free_slot;
265
266         return 0;
267 }
268
269 int32_t
270 rte_service_component_unregister(uint32_t id)
271 {
272         uint32_t i;
273         struct rte_service_spec_impl *s;
274         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
275
276         rte_service_count--;
277         rte_smp_wmb();
278
279         s->internal_flags &= ~(SERVICE_F_REGISTERED);
280
281         /* clear the run-bit in all cores */
282         for (i = 0; i < RTE_MAX_LCORE; i++)
283                 lcore_states[i].service_mask &= ~(UINT64_C(1) << id);
284
285         memset(&rte_services[id], 0, sizeof(struct rte_service_spec_impl));
286
287         return 0;
288 }
289
290 int32_t
291 rte_service_component_runstate_set(uint32_t id, uint32_t runstate)
292 {
293         struct rte_service_spec_impl *s;
294         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
295
296         if (runstate)
297                 s->comp_runstate = RUNSTATE_RUNNING;
298         else
299                 s->comp_runstate = RUNSTATE_STOPPED;
300
301         rte_smp_wmb();
302         return 0;
303 }
304
305 int32_t
306 rte_service_runstate_set(uint32_t id, uint32_t runstate)
307 {
308         struct rte_service_spec_impl *s;
309         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
310
311         if (runstate)
312                 s->app_runstate = RUNSTATE_RUNNING;
313         else
314                 s->app_runstate = RUNSTATE_STOPPED;
315
316         rte_smp_wmb();
317         return 0;
318 }
319
320 int32_t
321 rte_service_runstate_get(uint32_t id)
322 {
323         struct rte_service_spec_impl *s;
324         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
325         rte_smp_rmb();
326
327         int check_disabled = !(s->internal_flags & SERVICE_F_START_CHECK);
328         int lcore_mapped = (rte_atomic32_read(&s->num_mapped_cores) > 0);
329
330         return (s->app_runstate == RUNSTATE_RUNNING) &&
331                 (s->comp_runstate == RUNSTATE_RUNNING) &&
332                 (check_disabled | lcore_mapped);
333 }
334
335 static inline void
336 rte_service_runner_do_callback(struct rte_service_spec_impl *s,
337                                struct core_state *cs, uint32_t service_idx)
338 {
339         void *userdata = s->spec.callback_userdata;
340
341         if (service_stats_enabled(s)) {
342                 uint64_t start = rte_rdtsc();
343                 s->spec.callback(userdata);
344                 uint64_t end = rte_rdtsc();
345                 s->cycles_spent += end - start;
346                 cs->calls_per_service[service_idx]++;
347                 s->calls++;
348         } else
349                 s->spec.callback(userdata);
350 }
351
352
353 /* Expects the service 's' is valid. */
354 static int32_t
355 service_run(uint32_t i, struct core_state *cs, uint64_t service_mask,
356             struct rte_service_spec_impl *s)
357 {
358         if (!s)
359                 return -EINVAL;
360
361         if (s->comp_runstate != RUNSTATE_RUNNING ||
362                         s->app_runstate != RUNSTATE_RUNNING ||
363                         !(service_mask & (UINT64_C(1) << i))) {
364                 cs->service_active_on_lcore[i] = 0;
365                 return -ENOEXEC;
366         }
367
368         cs->service_active_on_lcore[i] = 1;
369
370         /* check do we need cmpset, if MT safe or <= 1 core
371          * mapped, atomic ops are not required.
372          */
373         const int use_atomics = (service_mt_safe(s) == 0) &&
374                                 (rte_atomic32_read(&s->num_mapped_cores) > 1);
375         if (use_atomics) {
376                 if (!rte_atomic32_cmpset((uint32_t *)&s->execute_lock, 0, 1))
377                         return -EBUSY;
378
379                 rte_service_runner_do_callback(s, cs, i);
380                 rte_atomic32_clear(&s->execute_lock);
381         } else
382                 rte_service_runner_do_callback(s, cs, i);
383
384         return 0;
385 }
386
387 int32_t
388 rte_service_may_be_active(uint32_t id)
389 {
390         uint32_t ids[RTE_MAX_LCORE] = {0};
391         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
392         int i;
393
394         if (id >= RTE_SERVICE_NUM_MAX || !service_valid(id))
395                 return -EINVAL;
396
397         for (i = 0; i < lcore_count; i++) {
398                 if (lcore_states[i].service_active_on_lcore[id])
399                         return 1;
400         }
401
402         return 0;
403 }
404
405 int32_t
406 rte_service_run_iter_on_app_lcore(uint32_t id, uint32_t serialize_mt_unsafe)
407 {
408         struct core_state *cs = &lcore_states[rte_lcore_id()];
409         struct rte_service_spec_impl *s;
410
411         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
412
413         /* Atomically add this core to the mapped cores first, then examine if
414          * we can run the service. This avoids a race condition between
415          * checking the value, and atomically adding to the mapped count.
416          */
417         if (serialize_mt_unsafe)
418                 rte_atomic32_inc(&s->num_mapped_cores);
419
420         if (service_mt_safe(s) == 0 &&
421                         rte_atomic32_read(&s->num_mapped_cores) > 1) {
422                 if (serialize_mt_unsafe)
423                         rte_atomic32_dec(&s->num_mapped_cores);
424                 return -EBUSY;
425         }
426
427         int ret = service_run(id, cs, UINT64_MAX, s);
428
429         if (serialize_mt_unsafe)
430                 rte_atomic32_dec(&s->num_mapped_cores);
431
432         return ret;
433 }
434
435 static int32_t
436 rte_service_runner_func(void *arg)
437 {
438         RTE_SET_USED(arg);
439         uint32_t i;
440         const int lcore = rte_lcore_id();
441         struct core_state *cs = &lcore_states[lcore];
442
443         while (lcore_states[lcore].runstate == RUNSTATE_RUNNING) {
444                 const uint64_t service_mask = cs->service_mask;
445
446                 for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
447                         if (!service_valid(i))
448                                 continue;
449                         /* return value ignored as no change to code flow */
450                         service_run(i, cs, service_mask, service_get(i));
451                 }
452
453                 cs->loops++;
454
455                 rte_smp_rmb();
456         }
457
458         lcore_config[lcore].state = WAIT;
459
460         return 0;
461 }
462
463 int32_t
464 rte_service_lcore_count(void)
465 {
466         int32_t count = 0;
467         uint32_t i;
468         for (i = 0; i < RTE_MAX_LCORE; i++)
469                 count += lcore_states[i].is_service_core;
470         return count;
471 }
472
473 int32_t
474 rte_service_lcore_list(uint32_t array[], uint32_t n)
475 {
476         uint32_t count = rte_service_lcore_count();
477         if (count > n)
478                 return -ENOMEM;
479
480         if (!array)
481                 return -EINVAL;
482
483         uint32_t i;
484         uint32_t idx = 0;
485         for (i = 0; i < RTE_MAX_LCORE; i++) {
486                 struct core_state *cs = &lcore_states[i];
487                 if (cs->is_service_core) {
488                         array[idx] = i;
489                         idx++;
490                 }
491         }
492
493         return count;
494 }
495
496 int32_t
497 rte_service_lcore_count_services(uint32_t lcore)
498 {
499         if (lcore >= RTE_MAX_LCORE)
500                 return -EINVAL;
501
502         struct core_state *cs = &lcore_states[lcore];
503         if (!cs->is_service_core)
504                 return -ENOTSUP;
505
506         return __builtin_popcountll(cs->service_mask);
507 }
508
509 int32_t
510 rte_service_start_with_defaults(void)
511 {
512         /* create a default mapping from cores to services, then start the
513          * services to make them transparent to unaware applications.
514          */
515         uint32_t i;
516         int ret;
517         uint32_t count = rte_service_get_count();
518
519         int32_t lcore_iter = 0;
520         uint32_t ids[RTE_MAX_LCORE] = {0};
521         int32_t lcore_count = rte_service_lcore_list(ids, RTE_MAX_LCORE);
522
523         if (lcore_count == 0)
524                 return -ENOTSUP;
525
526         for (i = 0; (int)i < lcore_count; i++)
527                 rte_service_lcore_start(ids[i]);
528
529         for (i = 0; i < count; i++) {
530                 /* do 1:1 core mapping here, with each service getting
531                  * assigned a single core by default. Adding multiple services
532                  * should multiplex to a single core, or 1:1 if there are the
533                  * same amount of services as service-cores
534                  */
535                 ret = rte_service_map_lcore_set(i, ids[lcore_iter], 1);
536                 if (ret)
537                         return -ENODEV;
538
539                 lcore_iter++;
540                 if (lcore_iter >= lcore_count)
541                         lcore_iter = 0;
542
543                 ret = rte_service_runstate_set(i, 1);
544                 if (ret)
545                         return -ENOEXEC;
546         }
547
548         return 0;
549 }
550
551 static int32_t
552 service_update(struct rte_service_spec *service, uint32_t lcore,
553                 uint32_t *set, uint32_t *enabled)
554 {
555         uint32_t i;
556         int32_t sid = -1;
557
558         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
559                 if ((struct rte_service_spec *)&rte_services[i] == service &&
560                                 service_valid(i)) {
561                         sid = i;
562                         break;
563                 }
564         }
565
566         if (sid == -1 || lcore >= RTE_MAX_LCORE)
567                 return -EINVAL;
568
569         if (!lcore_states[lcore].is_service_core)
570                 return -EINVAL;
571
572         uint64_t sid_mask = UINT64_C(1) << sid;
573         if (set) {
574                 uint64_t lcore_mapped = lcore_states[lcore].service_mask &
575                         sid_mask;
576
577                 if (*set && !lcore_mapped) {
578                         lcore_states[lcore].service_mask |= sid_mask;
579                         rte_atomic32_inc(&rte_services[sid].num_mapped_cores);
580                 }
581                 if (!*set && lcore_mapped) {
582                         lcore_states[lcore].service_mask &= ~(sid_mask);
583                         rte_atomic32_dec(&rte_services[sid].num_mapped_cores);
584                 }
585         }
586
587         if (enabled)
588                 *enabled = !!(lcore_states[lcore].service_mask & (sid_mask));
589
590         rte_smp_wmb();
591
592         return 0;
593 }
594
595 int32_t
596 rte_service_map_lcore_set(uint32_t id, uint32_t lcore, uint32_t enabled)
597 {
598         struct rte_service_spec_impl *s;
599         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
600         uint32_t on = enabled > 0;
601         return service_update(&s->spec, lcore, &on, 0);
602 }
603
604 int32_t
605 rte_service_map_lcore_get(uint32_t id, uint32_t lcore)
606 {
607         struct rte_service_spec_impl *s;
608         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
609         uint32_t enabled;
610         int ret = service_update(&s->spec, lcore, 0, &enabled);
611         if (ret == 0)
612                 return enabled;
613         return ret;
614 }
615
616 static void
617 set_lcore_state(uint32_t lcore, int32_t state)
618 {
619         /* mark core state in hugepage backed config */
620         struct rte_config *cfg = rte_eal_get_configuration();
621         cfg->lcore_role[lcore] = state;
622
623         /* mark state in process local lcore_config */
624         lcore_config[lcore].core_role = state;
625
626         /* update per-lcore optimized state tracking */
627         lcore_states[lcore].is_service_core = (state == ROLE_SERVICE);
628 }
629
630 int32_t
631 rte_service_lcore_reset_all(void)
632 {
633         /* loop over cores, reset all to mask 0 */
634         uint32_t i;
635         for (i = 0; i < RTE_MAX_LCORE; i++) {
636                 if (lcore_states[i].is_service_core) {
637                         lcore_states[i].service_mask = 0;
638                         set_lcore_state(i, ROLE_RTE);
639                         lcore_states[i].runstate = RUNSTATE_STOPPED;
640                 }
641         }
642         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++)
643                 rte_atomic32_set(&rte_services[i].num_mapped_cores, 0);
644
645         rte_smp_wmb();
646
647         return 0;
648 }
649
650 int32_t
651 rte_service_lcore_add(uint32_t lcore)
652 {
653         if (lcore >= RTE_MAX_LCORE)
654                 return -EINVAL;
655         if (lcore_states[lcore].is_service_core)
656                 return -EALREADY;
657
658         set_lcore_state(lcore, ROLE_SERVICE);
659
660         /* ensure that after adding a core the mask and state are defaults */
661         lcore_states[lcore].service_mask = 0;
662         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
663
664         rte_smp_wmb();
665
666         return rte_eal_wait_lcore(lcore);
667 }
668
669 int32_t
670 rte_service_lcore_del(uint32_t lcore)
671 {
672         if (lcore >= RTE_MAX_LCORE)
673                 return -EINVAL;
674
675         struct core_state *cs = &lcore_states[lcore];
676         if (!cs->is_service_core)
677                 return -EINVAL;
678
679         if (cs->runstate != RUNSTATE_STOPPED)
680                 return -EBUSY;
681
682         set_lcore_state(lcore, ROLE_RTE);
683
684         rte_smp_wmb();
685         return 0;
686 }
687
688 int32_t
689 rte_service_lcore_start(uint32_t lcore)
690 {
691         if (lcore >= RTE_MAX_LCORE)
692                 return -EINVAL;
693
694         struct core_state *cs = &lcore_states[lcore];
695         if (!cs->is_service_core)
696                 return -EINVAL;
697
698         if (cs->runstate == RUNSTATE_RUNNING)
699                 return -EALREADY;
700
701         /* set core to run state first, and then launch otherwise it will
702          * return immediately as runstate keeps it in the service poll loop
703          */
704         lcore_states[lcore].runstate = RUNSTATE_RUNNING;
705
706         int ret = rte_eal_remote_launch(rte_service_runner_func, 0, lcore);
707         /* returns -EBUSY if the core is already launched, 0 on success */
708         return ret;
709 }
710
711 int32_t
712 rte_service_lcore_stop(uint32_t lcore)
713 {
714         if (lcore >= RTE_MAX_LCORE)
715                 return -EINVAL;
716
717         if (lcore_states[lcore].runstate == RUNSTATE_STOPPED)
718                 return -EALREADY;
719
720         uint32_t i;
721         uint64_t service_mask = lcore_states[lcore].service_mask;
722         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
723                 int32_t enabled = service_mask & (UINT64_C(1) << i);
724                 int32_t service_running = rte_service_runstate_get(i);
725                 int32_t only_core = (1 ==
726                         rte_atomic32_read(&rte_services[i].num_mapped_cores));
727
728                 /* if the core is mapped, and the service is running, and this
729                  * is the only core that is mapped, the service would cease to
730                  * run if this core stopped, so fail instead.
731                  */
732                 if (enabled && service_running && only_core)
733                         return -EBUSY;
734         }
735
736         lcore_states[lcore].runstate = RUNSTATE_STOPPED;
737
738         return 0;
739 }
740
741 int32_t
742 rte_service_attr_get(uint32_t id, uint32_t attr_id, uint64_t *attr_value)
743 {
744         struct rte_service_spec_impl *s;
745         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
746
747         if (!attr_value)
748                 return -EINVAL;
749
750         switch (attr_id) {
751         case RTE_SERVICE_ATTR_CYCLES:
752                 *attr_value = s->cycles_spent;
753                 return 0;
754         case RTE_SERVICE_ATTR_CALL_COUNT:
755                 *attr_value = s->calls;
756                 return 0;
757         default:
758                 return -EINVAL;
759         }
760 }
761
762 int32_t
763 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
764                            uint64_t *attr_value)
765 {
766         struct core_state *cs;
767
768         if (lcore >= RTE_MAX_LCORE || !attr_value)
769                 return -EINVAL;
770
771         cs = &lcore_states[lcore];
772         if (!cs->is_service_core)
773                 return -ENOTSUP;
774
775         switch (attr_id) {
776         case RTE_SERVICE_LCORE_ATTR_LOOPS:
777                 *attr_value = cs->loops;
778                 return 0;
779         default:
780                 return -EINVAL;
781         }
782 }
783
784 static void
785 rte_service_dump_one(FILE *f, struct rte_service_spec_impl *s,
786                      uint64_t all_cycles, uint32_t reset)
787 {
788         /* avoid divide by zero */
789         if (all_cycles == 0)
790                 all_cycles = 1;
791
792         int calls = 1;
793         if (s->calls != 0)
794                 calls = s->calls;
795
796         if (reset) {
797                 s->cycles_spent = 0;
798                 s->calls = 0;
799                 return;
800         }
801
802         if (f == NULL)
803                 return;
804
805         fprintf(f, "  %s: stats %d\tcalls %"PRIu64"\tcycles %"
806                         PRIu64"\tavg: %"PRIu64"\n",
807                         s->spec.name, service_stats_enabled(s), s->calls,
808                         s->cycles_spent, s->cycles_spent / calls);
809 }
810
811 int32_t
812 rte_service_attr_reset_all(uint32_t id)
813 {
814         struct rte_service_spec_impl *s;
815         SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
816
817         int reset = 1;
818         rte_service_dump_one(NULL, s, 0, reset);
819         return 0;
820 }
821
822 int32_t
823 rte_service_lcore_attr_reset_all(uint32_t lcore)
824 {
825         struct core_state *cs;
826
827         if (lcore >= RTE_MAX_LCORE)
828                 return -EINVAL;
829
830         cs = &lcore_states[lcore];
831         if (!cs->is_service_core)
832                 return -ENOTSUP;
833
834         cs->loops = 0;
835
836         return 0;
837 }
838
839 static void
840 service_dump_calls_per_lcore(FILE *f, uint32_t lcore, uint32_t reset)
841 {
842         uint32_t i;
843         struct core_state *cs = &lcore_states[lcore];
844
845         fprintf(f, "%02d\t", lcore);
846         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
847                 if (!service_valid(i))
848                         continue;
849                 fprintf(f, "%"PRIu64"\t", cs->calls_per_service[i]);
850                 if (reset)
851                         cs->calls_per_service[i] = 0;
852         }
853         fprintf(f, "\n");
854 }
855
856 int32_t
857 rte_service_dump(FILE *f, uint32_t id)
858 {
859         uint32_t i;
860         int print_one = (id != UINT32_MAX);
861
862         uint64_t total_cycles = 0;
863
864         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
865                 if (!service_valid(i))
866                         continue;
867                 total_cycles += rte_services[i].cycles_spent;
868         }
869
870         /* print only the specified service */
871         if (print_one) {
872                 struct rte_service_spec_impl *s;
873                 SERVICE_VALID_GET_OR_ERR_RET(id, s, -EINVAL);
874                 fprintf(f, "Service %s Summary\n", s->spec.name);
875                 uint32_t reset = 0;
876                 rte_service_dump_one(f, s, total_cycles, reset);
877                 return 0;
878         }
879
880         /* print all services, as UINT32_MAX was passed as id */
881         fprintf(f, "Services Summary\n");
882         for (i = 0; i < RTE_SERVICE_NUM_MAX; i++) {
883                 if (!service_valid(i))
884                         continue;
885                 uint32_t reset = 0;
886                 rte_service_dump_one(f, &rte_services[i], total_cycles, reset);
887         }
888
889         fprintf(f, "Service Cores Summary\n");
890         for (i = 0; i < RTE_MAX_LCORE; i++) {
891                 if (lcore_config[i].core_role != ROLE_SERVICE)
892                         continue;
893
894                 uint32_t reset = 0;
895                 service_dump_calls_per_lcore(f, i, reset);
896         }
897
898         return 0;
899 }