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