Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | /* |
| 31 | * bcmolt_tr_ud_driver.c |
| 32 | * |
| 33 | * Unix Domain socket based transport. |
| 34 | * |
| 35 | */ |
| 36 | #include <stdio.h> |
| 37 | #include <unistd.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/un.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | #include <bcmolt_tr_mux.h> |
| 43 | #include <bcmolt_tr_ud_driver.h> |
| 44 | #include <bcmolt_tr_ud.h> |
| 45 | |
| 46 | #define BCMTR_MEM_PACKETS 10000 |
| 47 | #define BCMTR_MEM_OVERHEAD 1024 |
| 48 | #define BCMTR_MIN_RCVBUF_SIZE (BCMTR_MEM_PACKETS * BCMTR_MEM_OVERHEAD) |
| 49 | |
| 50 | char socket_path[] = SOCKET_PATH; |
| 51 | |
| 52 | typedef struct ud_conn_info { |
| 53 | bcmtrmux_channel channel[BCMTR_MAX_OLTS]; |
| 54 | int sock; |
| 55 | } ud_conn_info; |
| 56 | |
| 57 | static bcmos_task rx_thread; |
| 58 | static int fd; |
| 59 | static ud_conn_info *conn_info[BCMTRMUX_MAX_CHANNELS]; |
| 60 | static int ii; |
| 61 | |
| 62 | static void _bcmtr_ud_recv(bcmolt_devid device, bcmos_buf *buf, bcmtrmux_channel channel, void *data) |
| 63 | { |
| 64 | uint32_t data_length = bcmos_buf_length(buf); |
| 65 | int kk; |
| 66 | |
| 67 | /* Find the socket to respond on... */ |
| 68 | for (kk=0; kk<sizeof(conn_info)/sizeof(conn_info[0]); kk++) |
| 69 | { |
| 70 | if (conn_info[kk]->channel[device] == channel) |
| 71 | { |
| 72 | int len = 0; |
| 73 | bcmos_printf("Sending to user application for index %d...", kk); |
| 74 | len = send(conn_info[kk]->sock, bcmos_buf_data(buf), data_length, 0); |
| 75 | if (len < 0) |
| 76 | { |
| 77 | bcmos_printf("Failed to send respose to user application - what now?\n"); |
| 78 | } |
| 79 | bcmos_printf("Sent %d bytes\n", len); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static int bcmtr_ud_rx_handler(long arg) |
| 86 | { |
| 87 | int rc, jj; |
| 88 | bcmos_buf *buf; |
| 89 | struct timeval tv; |
| 90 | fd_set read_set; |
| 91 | |
| 92 | while (1) |
| 93 | { |
| 94 | int count; |
| 95 | FD_ZERO(&read_set); |
| 96 | for (jj=0; jj<sizeof(conn_info)/sizeof(conn_info[0]); jj++) |
| 97 | { |
| 98 | if (conn_info[jj]) |
| 99 | { |
| 100 | FD_SET(conn_info[jj]->sock, &read_set); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | tv.tv_sec = 0; tv.tv_usec = 100000; |
| 105 | |
| 106 | count = select (FD_SETSIZE, &read_set, NULL, NULL, &tv); |
| 107 | if (count < 0) |
| 108 | break; |
| 109 | |
| 110 | for (jj = 0; jj < sizeof(conn_info)/sizeof(conn_info[0]) ; jj++) |
| 111 | { |
| 112 | if (conn_info[jj] && FD_ISSET(conn_info[jj]->sock, &read_set)) |
| 113 | { |
| 114 | buf = bcmos_buf_alloc(BCMTR_MAX_MTU_SIZE); |
| 115 | if (!buf) |
| 116 | { |
| 117 | return BCM_ERR_NOMEM; |
| 118 | } |
| 119 | memset(buf, 0 , sizeof(buf)); |
| 120 | rc=read(conn_info[jj]->sock, bcmos_buf_data(buf), buf->size); |
| 121 | buf->len = rc; |
| 122 | buf->data+=BCMUD_TXPREFIX_LEN; |
| 123 | if (rc < 0 ) { |
| 124 | perror("read"); |
| 125 | exit(-1); |
| 126 | } |
| 127 | else if (rc == 0) { |
| 128 | close(conn_info[jj]->sock); |
| 129 | bcmos_free(conn_info[jj]); |
| 130 | conn_info[jj]=0; |
| 131 | bcmos_printf("Closing %d\n", jj); |
| 132 | } |
| 133 | else { |
| 134 | bcmolt_devid device = 0; /* How do we get this? |
| 135 | Need to stuff it at the begining of the message. */ |
| 136 | bcmtrmux_rx_from_host(device, conn_info[jj]->channel[device], buf); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | return BCM_ERR_OK; |
| 142 | } |
| 143 | |
| 144 | static int next_conn_info(bcmolt_devid device) |
| 145 | { |
| 146 | int jj=0; |
| 147 | while (jj<sizeof(conn_info)/sizeof(conn_info[0])) |
| 148 | { |
| 149 | jj++; ii++; |
| 150 | if (ii>sizeof(conn_info)/sizeof(conn_info[0])) ii=0; |
| 151 | if (!conn_info[ii]) return 0; |
| 152 | } |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | bcmos_errno bcmtr_ud_init(void) |
| 157 | { |
| 158 | bcmos_errno err = BCM_ERR_OK; |
| 159 | struct sockaddr_un addr; |
| 160 | bcmos_task_parm parm = { |
| 161 | .priority = TASK_PRIORITY_TRANSPORT_RX, |
| 162 | .stack_size = BCMTR_RX_THREAD_STACK, |
| 163 | .handler = bcmtr_ud_rx_handler, |
| 164 | .name = "ud handler" |
| 165 | }; |
| 166 | |
| 167 | ii=0; |
| 168 | |
| 169 | if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
| 170 | perror("socket error"); |
| 171 | exit(-1); |
| 172 | } |
| 173 | |
| 174 | memset(&addr, 0, sizeof(addr)); |
| 175 | addr.sun_family = AF_UNIX; |
| 176 | BUG_ON(sizeof(socket_path)>=sizeof(addr.sun_path)); |
| 177 | memcpy(addr.sun_path, socket_path, sizeof(socket_path)); |
| 178 | |
| 179 | if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { |
| 180 | perror("bind error"); |
| 181 | exit(-1); |
| 182 | } |
| 183 | |
| 184 | if (listen(fd, 5) == -1) { |
| 185 | perror("listen error"); |
| 186 | exit(-1); |
| 187 | } |
| 188 | |
| 189 | err = bcmos_task_create(&rx_thread, &parm); |
| 190 | |
| 191 | if (err) return err; |
| 192 | |
| 193 | while (1) { |
| 194 | bcmolt_devid device = 0; /* How do we get this? Need to stuff it at the begining of the message. */ |
| 195 | int s; |
| 196 | ud_conn_info *ci; |
| 197 | bcmos_errno rc; |
| 198 | bcmtrmux_channel channel = BCMTRMUX_CHANNEL_AUTO_ASSIGN; |
| 199 | |
| 200 | if ( (s = accept(fd, NULL, NULL)) == -1) { |
| 201 | perror("accept error"); |
| 202 | sleep(1); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | ci = bcmos_calloc(sizeof(*ci)); |
| 207 | if (!ci) |
| 208 | { |
| 209 | BCMOS_TRACE_RETURN(BCM_ERR_NOMEM, "No memory\n"); |
| 210 | } |
| 211 | rc = bcmtrmux_channel_register(device, &channel, _bcmtr_ud_recv, conn_info); |
| 212 | if (rc) |
| 213 | { |
| 214 | bcmos_printf("%s: can't register channel for device %u. rc=%d\n", __FUNCTION__, device, (int)rc); |
| 215 | return rc;; |
| 216 | } |
| 217 | ci->sock = s; |
| 218 | ci->channel[device]=channel; |
| 219 | conn_info[ii] = ci; |
| 220 | |
| 221 | bcmos_printf("Accepting %d-%d\n", ii, conn_info[ii]->sock); |
| 222 | |
| 223 | if (next_conn_info(device) < 0) return -1; |
| 224 | } |
| 225 | return BCM_ERR_OK; |
| 226 | } |
| 227 | |
| 228 | void bcmtr_ud_exit(void) |
| 229 | { |
| 230 | |
| 231 | } |