i40evf: fix MAC deletion when stopping
[dpdk.git] / examples / vm_power_manager / channel_monitor.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/epoll.h>
43 #include <sys/queue.h>
44
45 #include <rte_config.h>
46 #include <rte_log.h>
47 #include <rte_memory.h>
48 #include <rte_malloc.h>
49 #include <rte_atomic.h>
50
51
52 #include "channel_monitor.h"
53 #include "channel_commands.h"
54 #include "channel_manager.h"
55 #include "power_manager.h"
56
57 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
58
59 #define MAX_EVENTS 256
60
61
62 static volatile unsigned run_loop = 1;
63 static int global_event_fd;
64 static struct epoll_event *global_events_list;
65
66 void channel_monitor_exit(void)
67 {
68         run_loop = 0;
69         rte_free(global_events_list);
70 }
71
72 static int
73 process_request(struct channel_packet *pkt, struct channel_info *chan_info)
74 {
75         uint64_t core_mask;
76
77         if (chan_info == NULL)
78                 return -1;
79
80         if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
81                         CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
82                 return -1;
83
84         if (pkt->command == CPU_POWER) {
85                 core_mask = get_pcpus_mask(chan_info, pkt->resource_id);
86                 if (core_mask == 0) {
87                         RTE_LOG(ERR, CHANNEL_MONITOR, "Error get physical CPU mask for "
88                                 "channel '%s' using vCPU(%u)\n", chan_info->channel_path,
89                                 (unsigned)pkt->unit);
90                         return -1;
91                 }
92                 if (__builtin_popcountll(core_mask) == 1) {
93
94                         unsigned core_num = __builtin_ffsll(core_mask) - 1;
95
96                         switch (pkt->unit) {
97                         case(CPU_POWER_SCALE_MIN):
98                                         power_manager_scale_core_min(core_num);
99                         break;
100                         case(CPU_POWER_SCALE_MAX):
101                                         power_manager_scale_core_max(core_num);
102                         break;
103                         case(CPU_POWER_SCALE_DOWN):
104                                         power_manager_scale_core_down(core_num);
105                         break;
106                         case(CPU_POWER_SCALE_UP):
107                                         power_manager_scale_core_up(core_num);
108                         break;
109                         default:
110                                 break;
111                         }
112                 } else {
113                         switch (pkt->unit) {
114                         case(CPU_POWER_SCALE_MIN):
115                                         power_manager_scale_mask_min(core_mask);
116                         break;
117                         case(CPU_POWER_SCALE_MAX):
118                                         power_manager_scale_mask_max(core_mask);
119                         break;
120                         case(CPU_POWER_SCALE_DOWN):
121                                         power_manager_scale_mask_down(core_mask);
122                         break;
123                         case(CPU_POWER_SCALE_UP):
124                                         power_manager_scale_mask_up(core_mask);
125                         break;
126                         default:
127                                 break;
128                         }
129
130                 }
131         }
132         /* Return is not checked as channel status may have been set to DISABLED
133          * from management thread
134          */
135         rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
136                         CHANNEL_MGR_CHANNEL_CONNECTED);
137         return 0;
138
139 }
140
141 int
142 add_channel_to_monitor(struct channel_info **chan_info)
143 {
144         struct channel_info *info = *chan_info;
145         struct epoll_event event;
146
147         event.events = EPOLLIN;
148         event.data.ptr = info;
149         if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
150                 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
151                                 "to epoll\n", info->channel_path);
152                 return -1;
153         }
154         return 0;
155 }
156
157 int
158 remove_channel_from_monitor(struct channel_info *chan_info)
159 {
160         if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL, chan_info->fd, NULL) < 0) {
161                 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
162                                 "from epoll\n", chan_info->channel_path);
163                 return -1;
164         }
165         return 0;
166 }
167
168 int
169 channel_monitor_init(void)
170 {
171         global_event_fd = epoll_create1(0);
172         if (global_event_fd == 0) {
173                 RTE_LOG(ERR, CHANNEL_MONITOR, "Error creating epoll context with "
174                                 "error %s\n", strerror(errno));
175                 return -1;
176         }
177         global_events_list = rte_malloc("epoll_events", sizeof(*global_events_list)
178                         * MAX_EVENTS, RTE_CACHE_LINE_SIZE);
179         if (global_events_list == NULL) {
180                 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
181                                 "epoll events\n");
182                 return -1;
183         }
184         return 0;
185 }
186
187 void
188 run_channel_monitor(void)
189 {
190         while (run_loop) {
191                 int n_events, i;
192
193                 n_events = epoll_wait(global_event_fd, global_events_list,
194                                 MAX_EVENTS, 1);
195                 if (!run_loop)
196                         break;
197                 for (i = 0; i < n_events; i++) {
198                         struct channel_info *chan_info = (struct channel_info *)
199                                         global_events_list[i].data.ptr;
200                         if ((global_events_list[i].events & EPOLLERR) ||
201                                         (global_events_list[i].events & EPOLLHUP)) {
202                                 RTE_LOG(DEBUG, CHANNEL_MONITOR, "Remote closed connection for "
203                                                 "channel '%s'\n", chan_info->channel_path);
204                                 remove_channel(&chan_info);
205                                 continue;
206                         }
207                         if (global_events_list[i].events & EPOLLIN) {
208
209                                 int n_bytes, err = 0;
210                                 struct channel_packet pkt;
211                                 void *buffer = &pkt;
212                                 int buffer_len = sizeof(pkt);
213
214                                 while (buffer_len > 0) {
215                                         n_bytes = read(chan_info->fd, buffer, buffer_len);
216                                         if (n_bytes == buffer_len)
217                                                 break;
218                                         if (n_bytes == -1) {
219                                                 err = errno;
220                                                 RTE_LOG(DEBUG, CHANNEL_MONITOR, "Received error on "
221                                                                 "channel '%s' read: %s\n",
222                                                                 chan_info->channel_path, strerror(err));
223                                                 remove_channel(&chan_info);
224                                                 break;
225                                         }
226                                         buffer = (char *)buffer + n_bytes;
227                                         buffer_len -= n_bytes;
228                                 }
229                                 if (!err)
230                                         process_request(&pkt, chan_info);
231                         }
232                 }
233         }
234 }