1 /* 2 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 27 #include <string.h> 28 #include <sys/socket.h> 29 #include <rte_malloc.h> 30 31 #include "ff_ipc.h" 32 33 int 34 sysctl(int *name, unsigned namelen, void *old, 35 size_t *oldlenp, const void *new, size_t newlen) 36 { 37 struct ff_msg *msg, *retmsg = NULL; 38 char *extra_buf = NULL; 39 size_t total_len; 40 41 if (old != NULL && oldlenp == NULL) { 42 errno = EINVAL; 43 return -1; 44 } 45 46 msg = ff_ipc_msg_alloc(); 47 if (msg == NULL) { 48 errno = ENOMEM; 49 return -1; 50 } 51 52 size_t oldlen = 0; 53 if (old && oldlenp) { 54 oldlen = *oldlenp; 55 } 56 57 total_len = namelen + oldlen + newlen; 58 if (total_len > msg->buf_len) { 59 extra_buf = rte_malloc(NULL, total_len, 0); 60 if (extra_buf == NULL) { 61 errno = ENOMEM; 62 ff_ipc_msg_free(msg); 63 return -1; 64 } 65 msg->buf_addr = extra_buf; 66 msg->buf_len = total_len; 67 } 68 69 char *buf_addr = msg->buf_addr; 70 71 msg->msg_type = FF_SYSCTL; 72 msg->sysctl.name = (int *)buf_addr; 73 msg->sysctl.namelen = namelen; 74 memcpy(msg->sysctl.name, name, namelen*sizeof(int)); 75 76 buf_addr += namelen*sizeof(int); 77 78 if (new != NULL && newlen != 0) { 79 msg->sysctl.new = buf_addr; 80 msg->sysctl.newlen = newlen; 81 memcpy(msg->sysctl.new, new, newlen); 82 83 buf_addr += newlen; 84 } else { 85 msg->sysctl.new = NULL; 86 msg->sysctl.newlen = 0; 87 } 88 89 if (oldlenp != NULL) { 90 msg->sysctl.oldlenp = (size_t *)buf_addr; 91 memcpy(msg->sysctl.oldlenp, oldlenp, sizeof(size_t)); 92 buf_addr += sizeof(size_t); 93 94 if (old != NULL) { 95 msg->sysctl.old = (void *)buf_addr; 96 memcpy(msg->sysctl.old, old, *oldlenp); 97 buf_addr += *oldlenp; 98 } else { 99 msg->sysctl.old = NULL; 100 } 101 } else { 102 msg->sysctl.oldlenp = NULL; 103 msg->sysctl.old = NULL; 104 } 105 106 int ret = ff_ipc_send(msg); 107 if (ret < 0) { 108 errno = EPIPE; 109 ff_ipc_msg_free(msg); 110 if (extra_buf) { 111 rte_free(extra_buf); 112 } 113 return -1; 114 } 115 116 do { 117 if (retmsg != NULL) { 118 ff_ipc_msg_free(retmsg); 119 } 120 ret = ff_ipc_recv(&retmsg, msg->msg_type); 121 if (ret < 0) { 122 errno = EPIPE; 123 ff_ipc_msg_free(msg); 124 if (extra_buf) { 125 rte_free(extra_buf); 126 } 127 return -1; 128 } 129 } while (msg != retmsg); 130 131 if (retmsg->result == 0) { 132 ret = 0; 133 if (oldlenp && retmsg->sysctl.oldlenp) { 134 *oldlenp = *retmsg->sysctl.oldlenp; 135 } 136 137 if (old && retmsg->sysctl.old && oldlenp) { 138 memcpy(old, retmsg->sysctl.old, *oldlenp); 139 } 140 } else { 141 ret = -1; 142 errno = retmsg->result; 143 } 144 145 ff_ipc_msg_free(msg); 146 if (extra_buf) { 147 rte_free(extra_buf); 148 } 149 150 return ret; 151 } 152