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 #define FREE_FF_MSG(m) do { \ 34 if (m->original_buf) { \ 35 rte_free(m->buf_addr); \ 36 m->buf_addr = m->original_buf; \ 37 m->buf_len = m->original_buf_len; \ 38 m->original_buf = NULL; \ 39 } \ 40 ff_ipc_msg_free(m); \ 41 } while (0); 42 43 int 44 sysctl(int *name, unsigned namelen, void *old, 45 size_t *oldlenp, const void *new, size_t newlen) 46 { 47 struct ff_msg *msg, *retmsg = NULL; 48 char *extra_buf = NULL; 49 size_t total_len; 50 51 if (old != NULL && oldlenp == NULL) { 52 errno = EINVAL; 53 return -1; 54 } 55 56 msg = ff_ipc_msg_alloc(); 57 if (msg == NULL) { 58 errno = ENOMEM; 59 return -1; 60 } 61 62 size_t oldlen = 0; 63 if (old && oldlenp) { 64 oldlen = *oldlenp; 65 } 66 67 total_len = namelen + oldlen + newlen; 68 if (total_len > msg->buf_len) { 69 extra_buf = rte_malloc(NULL, total_len, 0); 70 if (extra_buf == NULL) { 71 errno = ENOMEM; 72 ff_ipc_msg_free(msg); 73 return -1; 74 } 75 msg->original_buf = msg->buf_addr; 76 msg->original_buf_len = msg->buf_len; 77 msg->buf_addr = extra_buf; 78 msg->buf_len = total_len; 79 } 80 81 char *buf_addr = msg->buf_addr; 82 83 msg->msg_type = FF_SYSCTL; 84 msg->sysctl.name = (int *)buf_addr; 85 msg->sysctl.namelen = namelen; 86 memcpy(msg->sysctl.name, name, namelen * sizeof(int)); 87 88 buf_addr += namelen * sizeof(int); 89 90 if (new != NULL && newlen != 0) { 91 msg->sysctl.new = buf_addr; 92 msg->sysctl.newlen = newlen; 93 memcpy(msg->sysctl.new, new, newlen); 94 95 buf_addr += newlen; 96 } else { 97 msg->sysctl.new = NULL; 98 msg->sysctl.newlen = 0; 99 } 100 101 if (oldlenp != NULL) { 102 msg->sysctl.oldlenp = (size_t *)buf_addr; 103 memcpy(msg->sysctl.oldlenp, oldlenp, sizeof(size_t)); 104 buf_addr += sizeof(size_t); 105 106 if (old != NULL) { 107 msg->sysctl.old = (void *)buf_addr; 108 memcpy(msg->sysctl.old, old, *oldlenp); 109 buf_addr += *oldlenp; 110 } else { 111 msg->sysctl.old = NULL; 112 } 113 } else { 114 msg->sysctl.oldlenp = NULL; 115 msg->sysctl.old = NULL; 116 } 117 118 int ret = ff_ipc_send(msg); 119 if (ret < 0) { 120 errno = EPIPE; 121 goto error; 122 } 123 124 do { 125 if (retmsg != NULL) { 126 FREE_FF_MSG(retmsg) 127 } 128 ret = ff_ipc_recv(&retmsg, msg->msg_type); 129 if (ret < 0) { 130 errno = EPIPE; 131 return -1; 132 } 133 } while (msg != retmsg); 134 135 if (retmsg->result == 0) { 136 ret = 0; 137 if (oldlenp && retmsg->sysctl.oldlenp) { 138 *oldlenp = *retmsg->sysctl.oldlenp; 139 } 140 141 if (old && retmsg->sysctl.old && oldlenp) { 142 memcpy(old, retmsg->sysctl.old, *oldlenp); 143 } 144 } else { 145 ret = -1; 146 errno = retmsg->result; 147 } 148 149 error: 150 FREE_FF_MSG(msg) 151 152 return ret; 153 } 154