forked from veos-sxarr-NEC/veosinfo_source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
veosinfo.c
5279 lines (4884 loc) · 145 KB
/
veosinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2017-2018 NEC Corporation
* This file is part of the VEOS information library.
*
* The VEOS information library is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* The VEOS information library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the VEOS information library; if not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file veosinfo.c
* @brief Handles and manages the RPM source request and get required
* information from VEOS and VE sysfs
*
* @internal
* @author RPM command
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <stdint.h>
#include <sys/types.h>
#include <libudev.h>
#include <elf.h>
#include "veosinfo.h"
#include "ve_sock.h"
#include "veos_RPM.pb-c.h"
#include "veosinfo_log.h"
#include "veosinfo_internal.h"
#include "veosinfo_comm.h"
/**
* @brief This function will check the value of VE node passed as environment
* variable is a valid VE node or not
*
* @param envrn[in] VE node passed by user as environment variable in
* "VE_NODE_NUMBER" with command
*
* @return 0 on success and -1 of failure
*/
int ve_match_envrn(char *envrn)
{
FILE *fp = NULL;
int nodeid = -1;
int retval = -1;
int nread = 0;
int status = -1;
int sock_fd = -1;
char sock_name[VE_PATH_MAX] = {0};
char *node_status_path = NULL;
const char ve_sysfs_path[PATH_MAX] = {0};
VE_RPMLIB_TRACE("Entering");
if (!envrn) {
VE_RPMLIB_ERR("Wrong argument received: envrn = %p", envrn);
errno = EINVAL;
goto hndl_return;
}
nodeid = atoi(envrn);
/* Get sysfs path corresponding to given VE node */
retval = ve_sysfs_path_info(nodeid, ve_sysfs_path);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to get sysfs path: %s",
strerror(errno));
goto hndl_return;
}
retval = -1;
node_status_path =
(char *)malloc(strlen(ve_sysfs_path) + VE_FILE_NAME);
if (!node_status_path) {
VE_RPMLIB_ERR("Memory allocation to node status path failed: %s",
strerror(errno));
goto hndl_return;
}
/* Get sysfs path to read 'os_state' of given VE node
*/
sprintf(node_status_path, "%s/os_state", ve_sysfs_path);
/* Open the "os_state" file to get the state of VE node
*/
fp = fopen(node_status_path, "r");
if (!fp) {
VE_RPMLIB_ERR("Open file '%s' failed: %s",
node_status_path, strerror(errno));
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Open node status file %s successfully.",
node_status_path);
/* Read the VE node status information
*/
nread = fscanf(fp, "%d", &status);
if (nread == EOF || nread != 1) {
VE_RPMLIB_ERR("Failed to read: %s", strerror(errno));
fclose(fp);
goto hndl_return2;
}
fclose(fp);
if (status) {
VE_RPMLIB_ERR("Given node %s is not online", envrn);
goto hndl_return2;
}
sprintf(sock_name, "%s/veos%d.sock", VE_SOC_PATH, nodeid);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_name);
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
sock_name, strerror(errno));
} else if (-2 == sock_fd) {
VE_RPMLIB_ERR("Given node %s is offline", envrn);
close(sock_fd);
}
else {
close(sock_fd);
retval = 0;
VE_RPMLIB_DEBUG("Given node is online: %s", envrn);
}
errno = 0;
hndl_return2:
free(node_status_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function will create socket path corresponding to given VE node
*
* @param nodeid[in] VE node number
*
* @return Socket path corresponding to given node on success and
* NULL on failure
*/
char *ve_create_sockpath(int nodeid)
{
char *sock_path = NULL;
VE_RPMLIB_TRACE("Entering");
sock_path = (char *) malloc(sizeof(char) * VE_PATH_MAX);
if (!sock_path) {
VE_RPMLIB_ERR("Socket path memory allocation failed: %s",
strerror(errno));
goto hndl_return;
}
sprintf(sock_path, "%s/veos%d.sock", VE_SOC_PATH, nodeid);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return sock_path;
}
/**
* @brief This function will populate total number of online VE nodes
* and node name
*
* @param online_node_count[out] Total number of nodes,
* on which VEOS is running
* @param nodeid[out] Node number of all online VE nodes
*
* @return 0 on success and -1 of failure
*/
int ve_get_nos(unsigned int *online_node_count, int *nodeid)
{
unsigned int node_count = 0;
unsigned int count = 0;
int retval = -1;
int sock_fd = -1;
char sock_name[VE_PATH_MAX] = {0};
struct ve_nodeinfo ve_nodeinfo_req = { {0} };
VE_RPMLIB_TRACE("Entering");
if (!nodeid || !online_node_count) {
VE_RPMLIB_ERR("Wrong argument received:node = %p," \
" online_node_count = %p", nodeid,
online_node_count);
errno = EINVAL;
goto hndl_return;
}
/* Get the installed VE nodes */
if (-1 == ve_node_info(&ve_nodeinfo_req)) {
VE_RPMLIB_ERR("Failed to get VE node information: %s",
strerror(errno));
goto hndl_return;
}
for (count = 0; count < ve_nodeinfo_req.total_node_count; count++) {
VE_RPMLIB_DEBUG("Check for node_count = %d and node = %d",
count, ve_nodeinfo_req.nodeid[count]);
/* Check for node status online and offline
*/
if (!ve_nodeinfo_req.status[count]) {
sprintf(sock_name, "%s/veos%d.sock",
VE_SOC_PATH, ve_nodeinfo_req.nodeid[count]);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_name);
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_DEBUG("Failed to create socket: %s, error: %s",
sock_name, strerror(errno));
errno = 0;
} else if (-2 == sock_fd) {
VE_RPMLIB_DEBUG("Node %d is offline",
ve_nodeinfo_req.nodeid[count]);
close(sock_fd);
errno = 0;
} else {
close(sock_fd);
nodeid[node_count] = ve_nodeinfo_req.nodeid[count];
VE_RPMLIB_DEBUG("%d node is online.",
nodeid[node_count]);
node_count++;
}
}
}
/* If, there is no online node on VE */
if (!node_count) {
errno = ENOENT;
goto hndl_return;
}
*online_node_count = node_count;
retval = 0;
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function populates the VE architecture details
*
* @param nodeid[in] VE node number, corresponding to which architecture
* information is required
* @param ve_archinfo_req[out] Structure to store architecture information
*
* @return 0 on success and -1 on error
*/
int ve_arch_info(int nodeid, struct ve_archinfo *ve_archinfo_req)
{
int retval = -1;
VE_RPMLIB_TRACE("Entering");
if (!ve_archinfo_req) {
VE_RPMLIB_ERR("Wrong argument received: ve_archinfo_req = %p",
ve_archinfo_req);
errno = EINVAL;
goto hndl_return;
}
/* Populate VE architecture details
*/
strncpy(ve_archinfo_req->machine, VE_MACHINE, VE_FILE_NAME);
strncpy(ve_archinfo_req->processor, VE_PROCESSOR, 257);
strncpy(ve_archinfo_req->hw_platform, VE_HW_PLATFORM, 257);
retval = 0;
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function populates online and offline VE nodes's details
*
* @param ve_nodeinfo_req [out] Structure to populate VE node information
*
* This field consists of total number of VE nodes, total
* cores per node and status corresponding to each VE node
*
* @return 0 on success and -1 on error
*/
int ve_node_info(struct ve_nodeinfo *ve_nodeinfo_req)
{
FILE *fp = NULL;
int numcore = 0;
int node_count = 0;
int retval = -1;
int nread = 0;
char *node_status_path = NULL;
const char ve_sysfs_path[PATH_MAX] = {0};
VE_RPMLIB_TRACE("Entering");
if (!ve_nodeinfo_req) {
VE_RPMLIB_ERR("Wrong argument received: ve_nodeinfo_req = %p",
ve_nodeinfo_req);
errno = EINVAL;
goto hndl_return;
}
/* Get the VE node number and total number of VE nodes in system */
if (0 > get_ve_node(ve_nodeinfo_req->nodeid,
&ve_nodeinfo_req->total_node_count)) {
VE_RPMLIB_ERR("Failed to get VE node number: %s",
strerror(errno));
goto hndl_return;
}
node_status_path =
(char *)malloc(VE_PATH_MAX + VE_FILE_NAME);
if (!node_status_path) {
VE_RPMLIB_ERR("Memory allocation to node status path failed: %s",
strerror(errno));
goto hndl_return;
}
for (node_count = 0; node_count < ve_nodeinfo_req->total_node_count;
node_count++) {
VE_RPMLIB_DEBUG("Check for node_count = %d and node = %d",
node_count, ve_nodeinfo_req->nodeid[node_count]);
memset(ve_sysfs_path, '\0', PATH_MAX);
/* Get sysfs path corresponding to given VE node */
retval = ve_sysfs_path_info(ve_nodeinfo_req->nodeid[node_count],
ve_sysfs_path);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to get VE sysfs path: %s",
strerror(errno));
goto hndl_return2;
}
retval = -1;
/* Open VE specific sysfs directory, to get the node specific
* information
*/
memset(node_status_path, '\0', (VE_PATH_MAX + VE_FILE_NAME));
sprintf(node_status_path, "%s/os_state", ve_sysfs_path);
/* Open the "os_state" file corresponding to node,
* to get the state information
*/
fp = fopen(node_status_path, "r");
if (!fp) {
VE_RPMLIB_ERR("Open file '%s' failed: %s",
node_status_path, strerror(errno));
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Open node status file %s successfully.",
node_status_path);
/* Read the VE node status information
*/
nread = 0;
nread = fscanf(fp, "%d", &ve_nodeinfo_req->status[node_count]);
if (nread == EOF || nread != 1) {
VE_RPMLIB_ERR("Failed to read file (%s): %s",
node_status_path, strerror(errno));
fclose(fp);
goto hndl_return2;
}
fclose(fp);
VE_RPMLIB_DEBUG("Reading status = %d",
ve_nodeinfo_req->status[node_count]);
/* Get the cores corresponding to given node
*/
numcore = 0;
if (-1 == ve_core_info(ve_nodeinfo_req->nodeid[node_count],
&numcore)) {
VE_RPMLIB_ERR("Failed to get CPU cores: %s",
strerror(errno));
goto hndl_return;
}
/* Populate the structure with cores value
*/
ve_nodeinfo_req->cores[node_count] = numcore;
VE_RPMLIB_DEBUG("node = %d, node_status = %d, cores = %d",
ve_nodeinfo_req->nodeid[node_count],
ve_nodeinfo_req->status[node_count],
ve_nodeinfo_req->cores[node_count]);
}
retval = 0;
VE_RPMLIB_DEBUG("Total_node_count = %d",
ve_nodeinfo_req->total_node_count);
hndl_return2:
free(node_status_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief Fetch the resource limit.
*
* @param ve_rlim resource limit
*/
void get_ve_rlimit(struct rlimit *ve_rlim)
{
int resource = 0;
VE_RPMLIB_TRACE("Entering");
while (resource < RLIM_NLIMITS) {
switch (resource) {
case RLIMIT_CPU:
case RLIMIT_AS:
case RLIMIT_CORE:
case RLIMIT_DATA:
case RLIMIT_SIGPENDING:
case RLIMIT_RSS:
case RLIMIT_FSIZE:
case RLIMIT_LOCKS:
case RLIMIT_MEMLOCK:
case RLIMIT_MSGQUEUE:
case RLIMIT_NOFILE:
case RLIMIT_NPROC:
case RLIMIT_RTTIME:
getrlimit(resource, ve_rlim);
ve_rlim++;
resource++;
break;
default:
ve_rlim++;
resource++;
break;
}
}
VE_RPMLIB_TRACE("Exiting");
}
/**
* @brief This function will check status (online/offline) of given VE node.
*
* @param node_num[in] VE node number to check status.
*
* @return 0 on success and -1 on failure.
*/
int ve_check_node_status(int node_num)
{
int retval = -1;
char *ve_sock_name = NULL;
VE_RPMLIB_TRACE("Entering");
errno = 0;
/* Create the socket path corresponding to received VE node
*/
ve_sock_name = ve_create_sockpath(node_num);
if (!ve_sock_name) {
VE_RPMLIB_ERR("Failed to create socket path for VE: %s",
strerror(errno));
goto hndl_return;
}
/* Create the socket connection corresponding to socket path
*/
if (0 > velib_sock(ve_sock_name)) {
VE_RPMLIB_ERR("Failed to create socket [%s]: %s",
ve_sock_name, strerror(errno));
goto hndl_return_sock;
}
retval = 0;
hndl_return_sock:
free(ve_sock_name);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function will create a new VE process on given VE node
*
* @param nodeid[in] Create process on given node number
* @param pid[in] Create process of given PID at VE
* @param flag[in] Identifier as specified in "enum create_task_flag"
* @param numa_num[in] NUMA node number
* @param membind_flag[in] Flag to indicate memory policy.
* @param set[in] CPU mask for dummy VE process.
*
* @return PID of created process on success and -1 on failure
*/
int ve_create_process(int nodeid, int pid, int flag, int numa_num,
int membind_flag, cpu_set_t *set)
{
int retval = -1;
int sock_fd = -1;
int pack_msg_len = -1;
void *pack_buf_send = NULL;
uint8_t *unpack_buf_recv = NULL;
char *ve_sock_name = NULL;
VelibConnect *res = NULL;
ProtobufCBinaryData subreq = {0};
VelibConnect request = VELIB_CONNECT__INIT;
char *ve_dev_filename = NULL;
struct velib_create_process ve_create_proc = {0};
struct stat sb = {0};
int fd = -1;
VE_RPMLIB_TRACE("Entering");
errno = 0;
/* Create the socket path corresponding to received VE node
*/
ve_sock_name = ve_create_sockpath(nodeid);
if (!ve_sock_name) {
VE_RPMLIB_ERR("Failed to create socket path for VE: %s",
strerror(errno));
goto hndl_return;
}
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(ve_sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
ve_sock_name, strerror(errno));
goto hndl_return_sock;
} else if (-2 == sock_fd)
goto abort;
ve_dev_filename = (char *)malloc(sizeof(char) * VE_FILE_NAME);
if (!ve_dev_filename) {
VE_RPMLIB_ERR("Memory allocation failed: %s",
strerror(errno));
goto hndl_return_sock;
}
sprintf(ve_dev_filename, "%s/%s%d", DEV_PATH, VE_DEVICE_NAME, nodeid);
fd = open(ve_dev_filename, O_RDWR);
if (fd < 0) {
VE_RPMLIB_ERR("Couldn't open file (%s): %s",
ve_dev_filename, strerror(errno));
goto hndl_return1;
}
retval = fstat(fd, &sb);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to get file status(%s): %s",
ve_dev_filename, strerror(errno));
close(fd);
goto hndl_return1;
}
if (SET_TASK_LIMIT == flag) {
memset(ve_create_proc.ve_rlim, -1,
sizeof(ve_create_proc.ve_rlim));
get_ve_rlimit(ve_create_proc.ve_rlim);
}
ve_create_proc.vedl_fd = fd;
ve_create_proc.flag = flag;
ve_create_proc.numa_num = numa_num;
if (!membind_flag)
ve_create_proc.membind_flag = MPOL_BIND;
else
ve_create_proc.membind_flag = MPOL_DEFAULT;
VE_RPMLIB_DEBUG("flag:%d, fd:%d, numa: %d, membind_flag: %d",
ve_create_proc.flag, ve_create_proc.vedl_fd,
ve_create_proc.numa_num, ve_create_proc.membind_flag);
if (set) {
memcpy(&ve_create_proc.set, set, sizeof(ve_create_proc.set));
ve_create_proc.cpu_mask_flag = true;
VE_RPMLIB_DEBUG("CPU count in mask: %d",
CPU_COUNT(&ve_create_proc.set));
} else {
ve_create_proc.cpu_mask_flag = false;
}
ve_create_proc.ppid = getppid();
subreq.data = (uint8_t *)&ve_create_proc;
subreq.len = sizeof(struct velib_create_process);
request.cmd_str = RPM_QUERY;
request.has_subcmd_str = true;
request.subcmd_str = VE_CREATE_PROCESS;
request.has_rpm_pid = true;
request.rpm_pid = getpid();
request.has_rpm_msg = true;
request.rpm_msg = subreq;
request.has_ve_pid = true;
request.ve_pid = pid;
/* Get the request message size to send to VEOS */
pack_msg_len = velib_connect__get_packed_size(&request);
if (0 >= pack_msg_len) {
VE_RPMLIB_ERR("Failed to get size to pack message");
fprintf(stderr, "Failed to get size to pack message\n");
goto abort;
}
pack_buf_send = malloc(pack_msg_len);
if (!pack_buf_send) {
VE_RPMLIB_ERR("Memory allocation failed: %s", strerror(errno));
goto hndl_return1;
}
memset(pack_buf_send, '\0', pack_msg_len);
/* Pack the message to send to VEOS */
retval = velib_connect__pack(&request, pack_buf_send);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to pack message");
fprintf(stderr, "Failed to pack message\n");
goto abort;
}
/* Send the IPC message to VEOS and wait for the acknowledgement
* from VEOS
*/
retval = velib_send_cmd(sock_fd, pack_buf_send, pack_msg_len);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to send message: %d bytes written",
retval);
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Send data successfully to VEOS and" \
" waiting to receive....");
unpack_buf_recv = malloc(MAX_PROTO_MSG_SIZE);
if (!unpack_buf_recv) {
VE_RPMLIB_ERR("Memory allocation failed: %s",
strerror(errno));
goto hndl_return2;
}
memset(unpack_buf_recv, '\0', MAX_PROTO_MSG_SIZE);
/* Receive the IPC message from VEOS
*/
retval = velib_recv_cmd(sock_fd, unpack_buf_recv, MAX_PROTO_MSG_SIZE);
VE_RPMLIB_DEBUG("Data received successfully from VEOS, now verify it.");
/* Unpack the data received from VEOS */
res = velib_connect__unpack(NULL, retval,
(const uint8_t *)(unpack_buf_recv));
if (!res) {
VE_RPMLIB_ERR("Failed to unpack message: %d", retval);
fprintf(stderr, "Failed to unpack message\n");
goto abort;
}
retval = res->rpm_retval;
/* Check if the desired return value is received
*/
if (0 > retval) {
VE_RPMLIB_ERR("Received message verification failed.");
errno = -(retval);
goto hndl_return3;
}
/* Function will return success, if expected return value is
* received from VEOS
*/
VE_RPMLIB_DEBUG("Received message from VEOS and retval = %d", retval);
goto hndl_return3;
abort:
close(sock_fd);
abort();
hndl_return3:
velib_connect__free_unpacked(res, NULL);
free(unpack_buf_recv);
hndl_return2:
free(pack_buf_send);
hndl_return1:
free(ve_dev_filename);
close(sock_fd);
hndl_return_sock:
free(ve_sock_name);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function will check that pid is running on given VE node or not
*
* @param nodeid[in] VE node number
*
* @param pid[in] Process ID
*
* @return 0 or 1 on success and -1 on failure. 0 to indicate a valid VE PID
* and 1 indicates process not exists on specified node
*/
int ve_check_pid(int nodeid, int pid)
{
int retval = -1;
int sock_fd = -1;
int pack_msg_len = -1;
void *pack_buf_send = NULL;
uint8_t *unpack_buf_recv = NULL;
char *ve_sock_name = NULL;
VelibConnect *res = NULL;
VelibConnect request = VELIB_CONNECT__INIT;
VE_RPMLIB_TRACE("Entering");
errno = 0;
/* Create the socket path corresponding to received VE node
*/
ve_sock_name = ve_create_sockpath(nodeid);
if (!ve_sock_name) {
VE_RPMLIB_ERR("Failed to create socket path for VE: %s",
strerror(errno));
goto hndl_return;
}
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(ve_sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
ve_sock_name, strerror(errno));
goto hndl_return_sock;
} else if (-2 == sock_fd)
goto abort;
request.cmd_str = RPM_QUERY;
request.has_subcmd_str = true;
request.subcmd_str = VE_CHECKPID;
request.has_rpm_pid = true;
request.rpm_pid = getpid();
request.has_ve_pid = true;
request.ve_pid = pid;
/* Get the request message size to send to VEOS */
pack_msg_len = velib_connect__get_packed_size(&request);
if (0 >= pack_msg_len) {
VE_RPMLIB_ERR("Failed to get size to pack message");
fprintf(stderr, "Failed to get size to pack message\n");
goto abort;
}
pack_buf_send = malloc(pack_msg_len);
if (!pack_buf_send) {
VE_RPMLIB_ERR("Memory allocation failed: %s", strerror(errno));
goto hndl_return1;
}
VE_RPMLIB_DEBUG("pack_msg_len = %d", pack_msg_len);
memset(pack_buf_send, '\0', pack_msg_len);
/* Pack the message to send to VEOS */
retval = velib_connect__pack(&request, pack_buf_send);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to pack message");
fprintf(stderr, "Failed to pack message\n");
goto abort;
}
/* Send the IPC message to VEOS and wait for the acknowledgement
* from VEOS
*/
retval = velib_send_cmd(sock_fd, pack_buf_send, pack_msg_len);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to send message: %d bytes written",
retval);
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Send data successfully to VEOS and" \
" waiting to receive....");
unpack_buf_recv = malloc(MAX_PROTO_MSG_SIZE);
if (!unpack_buf_recv) {
VE_RPMLIB_ERR("Memory allocation failed: %s", strerror(errno));
goto hndl_return2;
}
memset(unpack_buf_recv, '\0', MAX_PROTO_MSG_SIZE);
/* Receive the IPC message from VEOS
*/
retval = velib_recv_cmd(sock_fd, unpack_buf_recv, MAX_PROTO_MSG_SIZE);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to receive message: %s", strerror(errno));
goto hndl_return3;
}
VE_RPMLIB_DEBUG("Data received successfully from VEOS, now verify it.");
/* Unpack the data received from VEOS */
res = velib_connect__unpack(NULL, retval,
(const uint8_t *)(unpack_buf_recv));
if (!res) {
VE_RPMLIB_ERR("Failed to unpack message: %d", retval);
fprintf(stderr, "Failed to unpack message\n");
goto abort;
}
retval = res->rpm_retval;
if (0 != retval) {
VE_RPMLIB_ERR("Received return value from veos= %d", retval);
errno = -(retval);
}
VE_RPMLIB_DEBUG("Received message from VEOS and retval = %d", retval);
goto hndl_return4;
abort:
close(sock_fd);
abort();
hndl_return4:
velib_connect__free_unpacked(res, NULL);
hndl_return3:
free(unpack_buf_recv);
hndl_return2:
free(pack_buf_send);
hndl_return1:
close(sock_fd);
hndl_return_sock:
free(ve_sock_name);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function populates the memory information of given VE node
*
* @param nodeid[in] VE node number
* @param ve_meminfo_req[out] Structure to store memory information
* of given VE node
*
* @return 0 on success and -1 on failure
*/
int ve_mem_info(int nodeid, struct ve_meminfo *ve_meminfo_req)
{
int retval = -1;
int sock_fd = -1;
int pack_msg_len = -1;
void *pack_buf_send = NULL;
uint8_t *unpack_buf_recv = NULL;
char *ve_sock_name = NULL;
struct velib_meminfo lib_meminfo = {0};
VelibConnect *res = NULL;
VelibConnect request = VELIB_CONNECT__INIT;
VE_RPMLIB_TRACE("Entering");
errno = 0;
if (!ve_meminfo_req) {
VE_RPMLIB_ERR("Wrong argument received: ve_meminfo_req = %p",
ve_meminfo_req);
errno = EINVAL;
goto hndl_return;
}
/* Create the socket path corresponding to received VE node
*/
ve_sock_name = ve_create_sockpath(nodeid);
if (!ve_sock_name) {
VE_RPMLIB_ERR("Failed to create socket path for VE: %s",
strerror(errno));
goto hndl_return;
}
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(ve_sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
ve_sock_name, strerror(errno));
goto hndl_return_sock;
} else if (-2 == sock_fd)
goto abort;
request.cmd_str = RPM_QUERY;
request.has_subcmd_str = true;
request.subcmd_str = VE_MEM_INFO;
request.has_rpm_pid = true;
request.rpm_pid = getpid();
/* Get the request message size to send to VEOS */
pack_msg_len = velib_connect__get_packed_size(&request);
if (0 >= pack_msg_len) {
VE_RPMLIB_ERR("Failed to get size to pack message");
fprintf(stderr, "Failed to get size to pack message\n");
goto abort;
}
pack_buf_send = malloc(pack_msg_len);
if (!pack_buf_send) {
VE_RPMLIB_ERR("Memory allocation failed: %s", strerror(errno));
goto hndl_return1;
}
VE_RPMLIB_DEBUG("pack_msg_len = %d", pack_msg_len);
memset(pack_buf_send, '\0', pack_msg_len);
/* Pack the message to send to VEOS */
retval = velib_connect__pack(&request, pack_buf_send);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to pack message");
fprintf(stderr, "Failed to pack message\n");
goto abort;
}
/* Send the IPC message to VEOS and wait for the acknowledgement
* from VEOS
*/
retval = velib_send_cmd(sock_fd, pack_buf_send, pack_msg_len);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to send message: %d bytes written",
retval);
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Send data successfully to VEOS and" \
" waiting to receive....");
unpack_buf_recv = malloc(MAX_PROTO_MSG_SIZE);
if (!unpack_buf_recv) {
VE_RPMLIB_ERR("Memory allocation failed: %s",
strerror(errno));
goto hndl_return2;
}
memset(unpack_buf_recv, '\0', MAX_PROTO_MSG_SIZE);
/* Receive the IPC message from VEOS
*/
retval = velib_recv_cmd(sock_fd, unpack_buf_recv, MAX_PROTO_MSG_SIZE);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to receive message: %s",
strerror(errno));
goto hndl_return3;
}
VE_RPMLIB_DEBUG("Data received successfully from VEOS, now verify it.");
/* Unpack the data received from VEOS */
res = velib_connect__unpack(NULL, retval,
(const uint8_t *)(unpack_buf_recv));
if (!res) {
VE_RPMLIB_ERR("Failed to unpack message: %d", retval);
fprintf(stderr, "Failed to unpack message\n");
goto abort;
}
retval = res->rpm_retval;
/* Check if the desired return value is received
*/
if (0 != retval) {
VE_RPMLIB_ERR("Received message verification failed.");
errno = -(retval);
goto hndl_return4;
}
memcpy(&lib_meminfo, res->rpm_msg.data, res->rpm_msg.len);
VE_RPMLIB_DEBUG("Received message from VEOS and retval = %d", retval);
/* Populate the argument used to store the memory information with
* the values received from VEOS
*/
memset(ve_meminfo_req, '\0', sizeof(struct ve_meminfo));
ve_meminfo_req->kb_main_total = lib_meminfo.kb_main_total / VKB;
ve_meminfo_req->kb_main_used = lib_meminfo.kb_main_used / VKB;
ve_meminfo_req->kb_main_free = lib_meminfo.kb_main_free / VKB;
ve_meminfo_req->kb_main_shared = lib_meminfo.kb_main_shared / VKB;
ve_meminfo_req->kb_hugepage_used = lib_meminfo.kb_hugepage_used / VKB;
VE_RPMLIB_DEBUG("Received message from VEOS and values are " \
"as follows:kb_main_total = %lu, kb_main_used " \
"= %lu, kb_main_free = %lu, kb_main_shared = %lu, " \
"kb_hugepage_used=%lu",
ve_meminfo_req->kb_main_total,
ve_meminfo_req->kb_main_used,
ve_meminfo_req->kb_main_free,
ve_meminfo_req->kb_main_shared,
ve_meminfo_req->kb_hugepage_used);
goto hndl_return4;
abort:
close(sock_fd);
abort();
hndl_return4:
velib_connect__free_unpacked(res, NULL);
hndl_return3:
free(unpack_buf_recv);
hndl_return2:
free(pack_buf_send);
hndl_return1:
close(sock_fd);
hndl_return_sock:
free(ve_sock_name);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function is used to get uptime information.
*
* @param nodeid[in] VE node number
* @param uptime_secs[out] Value of uptime
*
* @return 0 on success and -1 on failure
*/
int ve_uptime_info(int nodeid, double *uptime_secs)
{
int retval = -1;
struct ve_statinfo ve_statinfo_req = { {0} };
VE_RPMLIB_TRACE("Entering");
if (!uptime_secs) {
VE_RPMLIB_ERR("Wrong argument received: uptime_secs = %p",
uptime_secs);
errno = EINVAL;