PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

servers/lwip/tcp.c

http://www.minix3.org/
C | 1205 lines | 882 code | 214 blank | 109 comment | 184 complexity | 9a7a68fd9791172d0d3abda2754ad2ff MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <minix/sysutil.h>
  4. #include <sys/ioc_net.h>
  5. #include <net/gen/in.h>
  6. #include <net/gen/tcp.h>
  7. #include <net/gen/tcp_io.h>
  8. #include <lwip/tcp.h>
  9. #include <lwip/tcp_impl.h>
  10. #include <lwip/ip_addr.h>
  11. #include "socket.h"
  12. #include "proto.h"
  13. #define TCP_BUF_SIZE (32 << 10)
  14. #define sock_alloc_buf(s) debug_malloc(s)
  15. #define sock_free_buf(x) debug_free(x)
  16. static int do_tcp_debug;
  17. #if 0
  18. #define debug_tcp_print(str, ...) printf("LWIP %s:%d : " str "\n", \
  19. __func__, __LINE__, ##__VA_ARGS__)
  20. #else
  21. #define debug_tcp_print(...) debug_print(__VA_ARGS__)
  22. #endif
  23. struct wbuf {
  24. unsigned len;
  25. unsigned written;
  26. unsigned unacked;
  27. unsigned rem_len;
  28. struct wbuf * next;
  29. char data[];
  30. };
  31. struct wbuf_chain {
  32. struct wbuf * head;
  33. struct wbuf * tail;
  34. struct wbuf * unsent; /* points to the first buffer that contains unsent
  35. data. It may point anywhere between head and
  36. tail */
  37. };
  38. static void tcp_error_callback(void *arg, err_t err)
  39. {
  40. int perr;
  41. struct socket * sock = (struct socket *) arg;
  42. debug_tcp_print("socket num %ld err %d", get_sock_num(sock), err);
  43. switch (err) {
  44. case ERR_RST:
  45. perr = ECONNREFUSED;
  46. break;
  47. case ERR_CLSD:
  48. perr = EPIPE;
  49. break;
  50. case ERR_CONN:
  51. perr = ENOTCONN;
  52. break;
  53. default:
  54. perr = EIO;
  55. }
  56. if (sock->flags & SOCK_FLG_OP_PENDING) {
  57. sock_revive(sock, perr);
  58. sock->flags &= ~SOCK_FLG_OP_PENDING;
  59. } else if (sock_select_set(sock))
  60. sock_select_notify(sock);
  61. /*
  62. * When error callback is called the tcb either does not exist anymore
  63. * or is going to be deallocated soon after. We must not use the pcb
  64. * anymore
  65. */
  66. sock->pcb = NULL;
  67. }
  68. static int tcp_fill_new_socket(struct socket * sock, struct tcp_pcb * pcb)
  69. {
  70. struct wbuf_chain * wc;
  71. if (!(wc = malloc(sizeof(struct wbuf_chain))))
  72. return ENOMEM;
  73. wc-> head = wc->tail = wc->unsent = NULL;
  74. sock->buf = wc;
  75. sock->buf_size = 0;
  76. sock->pcb = pcb;
  77. tcp_arg(pcb, sock);
  78. tcp_err(pcb, tcp_error_callback);
  79. tcp_nagle_disable(pcb);
  80. return OK;
  81. }
  82. static int tcp_op_open(struct socket * sock, __unused message * m)
  83. {
  84. struct tcp_pcb * pcb;
  85. int ret;
  86. debug_tcp_print("socket num %ld", get_sock_num(sock));
  87. if (!(pcb = tcp_new()))
  88. return ENOMEM;
  89. debug_tcp_print("new tcp pcb %p\n", pcb);
  90. if ((ret = tcp_fill_new_socket(sock, pcb) != OK))
  91. tcp_abandon(pcb, 0);
  92. return ret;
  93. }
  94. static void tcp_recv_free(__unused void * data)
  95. {
  96. pbuf_free((struct pbuf *) data);
  97. }
  98. static void tcp_backlog_free(void * data)
  99. {
  100. tcp_abort((struct tcp_pcb *) data);
  101. }
  102. static void free_wbuf_chain(struct wbuf_chain * wc)
  103. {
  104. struct wbuf * wb;
  105. assert(wc != NULL);
  106. wb = wc->head;
  107. while (wb) {
  108. struct wbuf * w = wb;
  109. debug_tcp_print("freeing wbuf %p", wb);
  110. wb = wb->next;
  111. debug_free(w);
  112. }
  113. debug_free(wc);
  114. }
  115. static void tcp_op_close(struct socket * sock, __unused message * m)
  116. {
  117. debug_tcp_print("socket num %ld", get_sock_num(sock));
  118. if (sock->flags & SOCK_FLG_OP_LISTENING)
  119. sock_dequeue_data_all(sock, tcp_backlog_free);
  120. else
  121. sock_dequeue_data_all(sock, tcp_recv_free);
  122. debug_tcp_print("dequed RX data");
  123. if (sock->pcb) {
  124. int err;
  125. /* we are not able to handle any callback anymore */
  126. tcp_arg((struct tcp_pcb *)sock->pcb, NULL);
  127. tcp_err((struct tcp_pcb *)sock->pcb, NULL);
  128. tcp_sent((struct tcp_pcb *)sock->pcb, NULL);
  129. tcp_recv((struct tcp_pcb *)sock->pcb, NULL);
  130. err = tcp_close(sock->pcb);
  131. assert(err == ERR_OK);
  132. sock->pcb = NULL;
  133. }
  134. debug_tcp_print("freed pcb");
  135. if (sock->buf) {
  136. free_wbuf_chain((struct wbuf_chain *) sock->buf);
  137. sock->buf = NULL;
  138. }
  139. debug_tcp_print("freed TX data");
  140. sock_reply(sock, OK);
  141. debug_tcp_print("socket unused");
  142. /* mark it as unused */
  143. sock->ops = NULL;
  144. }
  145. __unused static void print_tcp_payload(unsigned char * buf, int len)
  146. {
  147. int i;
  148. printf("LWIP tcp payload (%d) :\n", len);
  149. for (i = 0; i < len; i++, buf++) {
  150. printf("%02x ", buf[0]);
  151. if (i % 8 == 7)
  152. kputc('\n');
  153. }
  154. kputc('\n');
  155. }
  156. static int read_from_tcp(struct socket * sock, message * m)
  157. {
  158. unsigned rem_buf, written = 0;
  159. struct pbuf * p;
  160. assert(!(sock->flags & SOCK_FLG_OP_LISTENING) && sock->recv_head);
  161. rem_buf = m->COUNT;
  162. debug_tcp_print("socket num %ld recv buff sz %d", get_sock_num(sock), rem_buf);
  163. p = (struct pbuf *)sock->recv_head->data;
  164. while (rem_buf) {
  165. int err;
  166. if (rem_buf >= p->len) {
  167. struct pbuf * np;
  168. /*
  169. * FIXME perhaps copy this to a local buffer and do a
  170. * single copy to user then
  171. */
  172. #if 0
  173. print_tcp_payload(p->payload, p->len);
  174. #endif
  175. err = copy_to_user(m->m_source, p->payload, p->len,
  176. (cp_grant_id_t) m->IO_GRANT, written);
  177. if (err != OK)
  178. goto cp_error;
  179. sock->recv_data_size -= p->len;
  180. debug_tcp_print("whole pbuf copied (%d bytes)", p->len);
  181. rem_buf -= p->len;
  182. written += p->len;
  183. if ((np = p->next)) {
  184. pbuf_ref(np);
  185. if (pbuf_free(p) != 1)
  186. panic("LWIP : pbuf_free != 1");
  187. /*
  188. * Mark where we are going to continue if an
  189. * error occurs
  190. */
  191. sock->recv_head->data = np;
  192. p = np;
  193. } else {
  194. sock_dequeue_data(sock);
  195. pbuf_free(p);
  196. if (sock->recv_head)
  197. p = (struct pbuf *)sock->recv_head->data;
  198. else
  199. break;
  200. }
  201. if (rem_buf == 0)
  202. break;
  203. } else {
  204. /*
  205. * It must be PBUF_RAM for us to be able to shift the
  206. * payload pointer
  207. */
  208. assert(p->type == PBUF_RAM);
  209. #if 0
  210. print_tcp_payload(p->payload, rem_buf);
  211. #endif
  212. err = copy_to_user(m->m_source, p->payload, rem_buf,
  213. (cp_grant_id_t) m->IO_GRANT, written);
  214. if (err != OK)
  215. goto cp_error;
  216. sock->recv_data_size -= rem_buf;
  217. debug_tcp_print("partial pbuf copied (%d bytes)", rem_buf);
  218. /*
  219. * The whole pbuf hasn't been copied out, we only shift
  220. * the payload pointer to remember where to continue
  221. * next time
  222. */
  223. pbuf_header(p, -rem_buf);
  224. written += rem_buf;
  225. break;
  226. }
  227. }
  228. debug_tcp_print("%d bytes written to userspace", written);
  229. //printf("%d wr, queue %d\n", written, sock->recv_data_size);
  230. tcp_recved((struct tcp_pcb *) sock->pcb, written);
  231. return written;
  232. cp_error:
  233. if (written) {
  234. debug_tcp_print("%d bytes written to userspace", written);
  235. return written;
  236. } else
  237. return EFAULT;
  238. }
  239. static void tcp_op_read(struct socket * sock, message * m)
  240. {
  241. debug_tcp_print("socket num %ld", get_sock_num(sock));
  242. if (!sock->pcb || ((struct tcp_pcb *) sock->pcb)->state !=
  243. ESTABLISHED) {
  244. debug_tcp_print("Connection not established\n");
  245. sock_reply(sock, ENOTCONN);
  246. return;
  247. }
  248. if (sock->recv_head) {
  249. /* data available receive immeditely */
  250. int ret = read_from_tcp(sock, m);
  251. debug_tcp_print("read op finished");
  252. sock_reply(sock, ret);
  253. } else {
  254. if (sock->flags & SOCK_FLG_CLOSED) {
  255. printf("socket %ld already closed!!! call from %d\n",
  256. get_sock_num(sock), m->USER_ENDPT);
  257. do_tcp_debug = 1;
  258. sock_reply(sock, 0);
  259. return;
  260. }
  261. /* operation is being processed */
  262. debug_tcp_print("no data to read, suspending");
  263. sock_reply(sock, SUSPEND);
  264. sock->flags |= SOCK_FLG_OP_PENDING | SOCK_FLG_OP_READING;
  265. }
  266. }
  267. static struct wbuf * wbuf_add(struct socket * sock, unsigned sz)
  268. {
  269. struct wbuf * wbuf;
  270. struct wbuf_chain * wc = (struct wbuf_chain *)sock->buf;
  271. assert(wc);
  272. wbuf = debug_malloc(sizeof(struct wbuf) + sz);
  273. if (!wbuf)
  274. return NULL;
  275. wbuf->len = sz;
  276. wbuf->written = wbuf->unacked = 0;
  277. wbuf->next = NULL;
  278. if (wc->head == NULL)
  279. wc->head = wc->tail = wbuf;
  280. else {
  281. wc->tail->next = wbuf;
  282. wc->tail = wbuf;
  283. }
  284. sock->buf_size += sz;
  285. debug_tcp_print("buffer %p size %d\n", wbuf, sock->buf_size);
  286. return wbuf;
  287. }
  288. static struct wbuf * wbuf_ack_sent(struct socket * sock, unsigned sz)
  289. {
  290. struct wbuf_chain * wc = (struct wbuf_chain *) sock->buf;
  291. struct wbuf ** wb;
  292. wb = &wc->head;
  293. while (sz && *wb) {
  294. if ((*wb)->unacked <= sz) {
  295. struct wbuf * w;
  296. assert((*wb)->rem_len == 0);
  297. w = *wb;
  298. *wb = w->next;
  299. sock->buf_size -= w->len;
  300. sz -= w->unacked;
  301. debug_tcp_print("whole buffer acked (%d / %d), removed",
  302. w->unacked, w->len);
  303. debug_free(w);
  304. } else {
  305. (*wb)->unacked -= sz;
  306. (*wb)->written += sz;
  307. debug_tcp_print("acked %d / %d bytes", sz, (*wb)->len);
  308. sz = 0;
  309. }
  310. }
  311. /* did we write out more than we had? */
  312. assert(sz == 0);
  313. if (wc->head == NULL)
  314. wc->tail = NULL;
  315. debug_tcp_print("buffer size %d\n", sock->buf_size);
  316. return wc->head;
  317. }
  318. static void tcp_op_write(struct socket * sock, message * m)
  319. {
  320. int ret;
  321. struct wbuf * wbuf;
  322. unsigned snd_buf_len, usr_buf_len;
  323. u8_t flgs = 0;
  324. if (!sock->pcb) {
  325. sock_reply(sock, ENOTCONN);
  326. return;
  327. }
  328. usr_buf_len = m->COUNT;
  329. debug_tcp_print("socket num %ld data size %d",
  330. get_sock_num(sock), usr_buf_len);
  331. /*
  332. * Let at most one buffer grow beyond TCP_BUF_SIZE. This is to minimize
  333. * small writes from userspace if only a few bytes were sent before
  334. */
  335. if (sock->buf_size >= TCP_BUF_SIZE) {
  336. /* FIXME do not block for now */
  337. debug_tcp_print("WARNING : tcp buffers too large, cannot allocate more");
  338. sock_reply(sock, ENOMEM);
  339. return;
  340. }
  341. /*
  342. * Never let the allocated buffers grow more than to 2xTCP_BUF_SIZE and
  343. * never copy more than space available
  344. */
  345. usr_buf_len = (usr_buf_len > TCP_BUF_SIZE ? TCP_BUF_SIZE : usr_buf_len);
  346. wbuf = wbuf_add(sock, usr_buf_len);
  347. debug_tcp_print("new wbuf for %d bytes", wbuf->len);
  348. if (!wbuf) {
  349. debug_tcp_print("cannot allocate new buffer of %d bytes", usr_buf_len);
  350. sock_reply(sock, ENOMEM);
  351. }
  352. if ((ret = copy_from_user(m->m_source, wbuf->data, usr_buf_len,
  353. (cp_grant_id_t) m->IO_GRANT, 0)) != OK) {
  354. sock_reply(sock, ret);
  355. return;
  356. }
  357. wbuf->written = 0;
  358. wbuf->rem_len = usr_buf_len;
  359. /*
  360. * If a writing operation is already in progress, we just enqueue the
  361. * data and quit.
  362. */
  363. if (sock->flags & SOCK_FLG_OP_WRITING) {
  364. struct wbuf_chain * wc = (struct wbuf_chain *)sock->buf;
  365. /*
  366. * We are adding a buffer with unsent data. If we don't have any other
  367. * unsent data, set the pointer to this buffer.
  368. */
  369. if (wc->unsent == NULL) {
  370. wc->unsent = wbuf;
  371. debug_tcp_print("unsent %p remains %d\n", wbuf, wbuf->rem_len);
  372. }
  373. debug_tcp_print("returns %d\n", usr_buf_len);
  374. sock_reply(sock, usr_buf_len);
  375. /*
  376. * We cannot accept new operations (write). We set the flag
  377. * after sending reply not to revive only. We could deadlock.
  378. */
  379. if (sock->buf_size >= TCP_BUF_SIZE)
  380. sock->flags |= SOCK_FLG_OP_PENDING;
  381. return;
  382. }
  383. /*
  384. * Start sending data if the operation is not in progress yet. The
  385. * current buffer is the nly one we have, we cannot send more.
  386. */
  387. snd_buf_len = tcp_sndbuf((struct tcp_pcb *)sock->pcb);
  388. debug_tcp_print("tcp can accept %d bytes", snd_buf_len);
  389. wbuf->unacked = (snd_buf_len < wbuf->rem_len ? snd_buf_len : wbuf->rem_len);
  390. wbuf->rem_len -= wbuf->unacked;
  391. if (wbuf->rem_len) {
  392. flgs = TCP_WRITE_FLAG_MORE;
  393. /*
  394. * Remember that this buffer has some data which we didn't pass
  395. * to tcp yet.
  396. */
  397. ((struct wbuf_chain *)sock->buf)->unsent = wbuf;
  398. debug_tcp_print("unsent %p remains %d\n", wbuf, wbuf->rem_len);
  399. }
  400. ret = tcp_write((struct tcp_pcb *)sock->pcb, wbuf->data,
  401. wbuf->unacked, flgs);
  402. tcp_output((struct tcp_pcb *)sock->pcb);
  403. debug_tcp_print("%d bytes to tcp", wbuf->unacked);
  404. if (ret == ERR_OK) {
  405. /*
  406. * Operation is being processed, no need to remember the message
  407. * in this case, we are going to reply immediatly
  408. */
  409. debug_tcp_print("returns %d\n", usr_buf_len);
  410. sock_reply(sock, usr_buf_len);
  411. sock->flags |= SOCK_FLG_OP_WRITING;
  412. if (sock->buf_size >= TCP_BUF_SIZE)
  413. sock->flags |= SOCK_FLG_OP_PENDING;
  414. } else
  415. sock_reply(sock, EIO);
  416. }
  417. static void tcp_set_conf(struct socket * sock, message * m)
  418. {
  419. int err;
  420. nwio_tcpconf_t tconf;
  421. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  422. debug_tcp_print("socket num %ld", get_sock_num(sock));
  423. assert(pcb);
  424. err = copy_from_user(m->m_source, &tconf, sizeof(tconf),
  425. (cp_grant_id_t) m->IO_GRANT, 0);
  426. if (err != OK)
  427. sock_reply(sock, err);
  428. debug_tcp_print("tconf.nwtc_flags = 0x%lx", tconf.nwtc_flags);
  429. debug_tcp_print("tconf.nwtc_remaddr = 0x%x",
  430. (unsigned int) tconf.nwtc_remaddr);
  431. debug_tcp_print("tconf.nwtc_remport = 0x%x", ntohs(tconf.nwtc_remport));
  432. debug_tcp_print("tconf.nwtc_locaddr = 0x%x",
  433. (unsigned int) tconf.nwtc_locaddr);
  434. debug_tcp_print("tconf.nwtc_locport = 0x%x", ntohs(tconf.nwtc_locport));
  435. sock->usr_flags = tconf.nwtc_flags;
  436. if (sock->usr_flags & NWTC_SET_RA)
  437. pcb->remote_ip.addr = tconf.nwtc_remaddr;
  438. if (sock->usr_flags & NWTC_SET_RP)
  439. pcb->remote_port = ntohs(tconf.nwtc_remport);
  440. if (sock->usr_flags & NWTC_LP_SET) {
  441. /* FIXME the user library can only bind to ANY anyway */
  442. if (tcp_bind(pcb, IP_ADDR_ANY, ntohs(tconf.nwtc_locport)) == ERR_USE) {
  443. sock_reply(sock, EADDRINUSE);
  444. return;
  445. }
  446. }
  447. sock_reply(sock, OK);
  448. }
  449. static void tcp_get_conf(struct socket * sock, message * m)
  450. {
  451. int err;
  452. nwio_tcpconf_t tconf;
  453. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  454. debug_tcp_print("socket num %ld", get_sock_num(sock));
  455. assert(pcb);
  456. tconf.nwtc_locaddr = pcb->local_ip.addr;
  457. tconf.nwtc_locport = htons(pcb->local_port);
  458. tconf.nwtc_remaddr = pcb->remote_ip.addr;
  459. tconf.nwtc_remport = htons(pcb->remote_port);
  460. tconf.nwtc_flags = sock->usr_flags;
  461. debug_tcp_print("tconf.nwtc_flags = 0x%lx", tconf.nwtc_flags);
  462. debug_tcp_print("tconf.nwtc_remaddr = 0x%x",
  463. (unsigned int) tconf.nwtc_remaddr);
  464. debug_tcp_print("tconf.nwtc_remport = 0x%x", ntohs(tconf.nwtc_remport));
  465. debug_tcp_print("tconf.nwtc_locaddr = 0x%x",
  466. (unsigned int) tconf.nwtc_locaddr);
  467. debug_tcp_print("tconf.nwtc_locport = 0x%x", ntohs(tconf.nwtc_locport));
  468. if ((unsigned) m->COUNT < sizeof(tconf)) {
  469. sock_reply(sock, EINVAL);
  470. return;
  471. }
  472. err = copy_to_user(m->m_source, &tconf, sizeof(tconf),
  473. (cp_grant_id_t) m->IO_GRANT, 0);
  474. if (err != OK)
  475. sock_reply(sock, err);
  476. sock_reply(sock, OK);
  477. }
  478. static int enqueue_rcv_data(struct socket * sock, struct pbuf * pbuf)
  479. {
  480. /* Do not enqueue more data than allowed */
  481. if (0 && sock->recv_data_size > 4 * TCP_BUF_SIZE)
  482. return ERR_MEM;
  483. if (sock_enqueue_data(sock, pbuf, pbuf->tot_len) != OK) {
  484. debug_tcp_print("data enqueueing failed");
  485. return ERR_MEM;
  486. }
  487. debug_tcp_print("enqueued %d bytes", pbuf->tot_len);
  488. //printf("enqueued %d bytes, queue %d\n", pbuf->tot_len, sock->recv_data_size);
  489. return ERR_OK;
  490. }
  491. static err_t tcp_recv_callback(void *arg,
  492. struct tcp_pcb *tpcb,
  493. struct pbuf *pbuf,
  494. err_t err)
  495. {
  496. int ret, enqueued = 0;
  497. struct socket * sock = (struct socket *) arg;
  498. debug_tcp_print("socket num %ld", get_sock_num(sock));
  499. if (sock->pcb == NULL) {
  500. if (sock_select_set(sock))
  501. sock_select_notify(sock);
  502. return ERR_OK;
  503. }
  504. assert((struct tcp_pcb *) sock->pcb == tpcb);
  505. if (err != ERR_OK)
  506. return ERR_OK;
  507. if (!pbuf) {
  508. debug_tcp_print("tcp stream closed on the remote side");
  509. // sock->flags |= SOCK_FLG_CLOSED;
  510. /* wake up the reader and report EOF */
  511. if (sock->flags & SOCK_FLG_OP_PENDING &&
  512. sock->flags & SOCK_FLG_OP_READING &&
  513. !(sock->flags & SOCK_FLG_OP_REVIVING)) {
  514. sock_revive(sock, 0);
  515. sock->flags &= ~(SOCK_FLG_OP_PENDING |
  516. SOCK_FLG_OP_READING);
  517. }
  518. #if 0
  519. /* if there are any undelivered data, drop them */
  520. sock_dequeue_data_all(sock, tcp_recv_free);
  521. tcp_abandon(tpcb, 0);
  522. sock->pcb = NULL;
  523. #endif
  524. return ERR_OK;
  525. }
  526. /*
  527. * FIXME we always enqueue the data first. If the head is empty and read
  528. * operation is pending we could try to deliver immeditaly without
  529. * enqueueing
  530. */
  531. if (enqueue_rcv_data(sock, pbuf) == ERR_OK)
  532. enqueued = 1;
  533. /*
  534. * Deliver data if there is a pending read operation, otherwise notify
  535. * select if the socket is being monitored
  536. */
  537. if (sock->flags & SOCK_FLG_OP_PENDING) {
  538. if (sock->flags & SOCK_FLG_OP_READING) {
  539. ret = read_from_tcp(sock, &sock->mess);
  540. debug_tcp_print("read op finished");
  541. sock_revive(sock, ret);
  542. sock->flags &= ~(SOCK_FLG_OP_PENDING |
  543. SOCK_FLG_OP_READING);
  544. }
  545. } else if (!(sock->flags & SOCK_FLG_OP_WRITING) &&
  546. sock_select_rw_set(sock))
  547. sock_select_notify(sock);
  548. /* perhaps we have deliverd some data to user, try to enqueue again */
  549. if (!enqueued) {
  550. return enqueue_rcv_data(sock, pbuf);
  551. } else
  552. return ERR_OK;
  553. }
  554. static err_t tcp_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len)
  555. {
  556. struct socket * sock = (struct socket *) arg;
  557. struct wbuf * wbuf;
  558. struct wbuf_chain * wc = (struct wbuf_chain *) sock->buf;
  559. unsigned snd_buf_len;
  560. int ret;
  561. debug_tcp_print("socket num %ld", get_sock_num(sock));
  562. /* an error might have had happen */
  563. if (sock->pcb == NULL) {
  564. if (sock_select_set(sock))
  565. sock_select_notify(sock);
  566. return ERR_OK;
  567. }
  568. assert((struct tcp_pcb *)sock->pcb == tpcb);
  569. /* operation must have been canceled, do not send any other data */
  570. if (!sock->flags & SOCK_FLG_OP_PENDING)
  571. return ERR_OK;
  572. wbuf = wbuf_ack_sent(sock, len);
  573. if (wbuf == NULL) {
  574. debug_tcp_print("all data acked, nothing more to send");
  575. sock->flags &= ~SOCK_FLG_OP_WRITING;
  576. if (!(sock->flags & SOCK_FLG_OP_READING))
  577. sock->flags &= ~SOCK_FLG_OP_PENDING;
  578. /* no reviving, we must notify. Write and read possible */
  579. if (sock_select_rw_set(sock))
  580. sock_select_notify(sock);
  581. return ERR_OK;
  582. }
  583. /* we have just freed some space, write will be accepted */
  584. if (sock->buf_size < TCP_BUF_SIZE && sock_select_rw_set(sock)) {
  585. if (!(sock->flags & SOCK_FLG_OP_READING)) {
  586. sock->flags &= ~SOCK_FLG_OP_PENDING;
  587. sock_select_notify(sock);
  588. }
  589. }
  590. /*
  591. * Check if there is some space for new data, there should be, we just
  592. * got a confirmation that some data reached the other end of the
  593. * connection
  594. */
  595. snd_buf_len = tcp_sndbuf(tpcb);
  596. assert(snd_buf_len > 0);
  597. debug_tcp_print("tcp can accept %d bytes", snd_buf_len);
  598. if (!wc->unsent) {
  599. debug_tcp_print("nothing to send");
  600. return ERR_OK;
  601. }
  602. wbuf = wc->unsent;
  603. while (wbuf) {
  604. unsigned towrite;
  605. u8_t flgs = 0;
  606. towrite = (snd_buf_len < wbuf->rem_len ?
  607. snd_buf_len : wbuf->rem_len);
  608. wbuf->rem_len -= towrite;
  609. debug_tcp_print("data to send, sending %d", towrite);
  610. if (wbuf->rem_len || wbuf->next)
  611. flgs = TCP_WRITE_FLAG_MORE;
  612. ret = tcp_write(tpcb, wbuf->data + wbuf->written + wbuf->unacked,
  613. towrite, flgs);
  614. debug_tcp_print("%d bytes to tcp", towrite);
  615. /* tcp_output() is called once we return from this callback */
  616. if (ret != ERR_OK) {
  617. debug_print("tcp_write() failed (%d), written %d"
  618. , ret, wbuf->written);
  619. sock->flags &= ~(SOCK_FLG_OP_PENDING | SOCK_FLG_OP_WRITING);
  620. /* no reviving, we must notify. Write and read possible */
  621. if (sock_select_rw_set(sock))
  622. sock_select_notify(sock);
  623. return ERR_OK;
  624. }
  625. wbuf->unacked += towrite;
  626. snd_buf_len -= towrite;
  627. debug_tcp_print("tcp still accepts %d bytes\n", snd_buf_len);
  628. if (snd_buf_len) {
  629. assert(wbuf->rem_len == 0);
  630. wbuf = wbuf->next;
  631. wc->unsent = wbuf;
  632. if (wbuf)
  633. debug_tcp_print("unsent %p remains %d\n",
  634. wbuf, wbuf->rem_len);
  635. else {
  636. debug_tcp_print("nothing to send");
  637. }
  638. } else
  639. break;
  640. }
  641. return ERR_OK;
  642. }
  643. static err_t tcp_connected_callback(void *arg,
  644. struct tcp_pcb *tpcb,
  645. __unused err_t err)
  646. {
  647. struct socket * sock = (struct socket *) arg;
  648. debug_tcp_print("socket num %ld err %d", get_sock_num(sock), err);
  649. if (sock->pcb == NULL) {
  650. if (sock_select_set(sock))
  651. sock_select_notify(sock);
  652. return ERR_OK;
  653. }
  654. assert((struct tcp_pcb *)sock->pcb == tpcb);
  655. tcp_sent(tpcb, tcp_sent_callback);
  656. tcp_recv(tpcb, tcp_recv_callback);
  657. sock_revive(sock, OK);
  658. sock->flags &= ~(SOCK_FLG_OP_PENDING | SOCK_FLG_OP_CONNECTING);
  659. /* revive does the sock_select_notify() for us */
  660. return ERR_OK;
  661. }
  662. static void tcp_op_connect(struct socket * sock)
  663. {
  664. ip_addr_t remaddr;
  665. struct tcp_pcb * pcb;
  666. err_t err;
  667. debug_tcp_print("socket num %ld", get_sock_num(sock));
  668. /*
  669. * Connecting is going to send some packets. Unless an immediate error
  670. * occurs this operation is going to block
  671. */
  672. sock_reply(sock, SUSPEND);
  673. sock->flags |= SOCK_FLG_OP_PENDING | SOCK_FLG_OP_CONNECTING;
  674. /* try to connect now */
  675. pcb = (struct tcp_pcb *) sock->pcb;
  676. remaddr = pcb->remote_ip;
  677. err = tcp_connect(pcb, &remaddr, pcb->remote_port,
  678. tcp_connected_callback);
  679. if (err == ERR_VAL)
  680. panic("Wrong tcp_connect arguments");
  681. if (err != ERR_OK)
  682. panic("Other tcp_connect error %d\n", err);
  683. }
  684. static int tcp_do_accept(struct socket * listen_sock,
  685. message * m,
  686. struct tcp_pcb * newpcb)
  687. {
  688. struct socket * newsock;
  689. unsigned sock_num;
  690. int ret;
  691. debug_tcp_print("socket num %ld", get_sock_num(listen_sock));
  692. if ((ret = copy_from_user(m->m_source, &sock_num, sizeof(sock_num),
  693. (cp_grant_id_t) m->IO_GRANT, 0)) != OK)
  694. return EFAULT;
  695. if (!is_valid_sock_num(sock_num))
  696. return EBADF;
  697. newsock = get_sock(sock_num);
  698. assert(newsock->pcb); /* because of previous open() */
  699. /* we really want to forget about this socket */
  700. tcp_err((struct tcp_pcb *)newsock->pcb, NULL);
  701. tcp_abandon((struct tcp_pcb *)newsock->pcb, 0);
  702. tcp_arg(newpcb, newsock);
  703. tcp_err(newpcb, tcp_error_callback);
  704. tcp_sent(newpcb, tcp_sent_callback);
  705. tcp_recv(newpcb, tcp_recv_callback);
  706. tcp_nagle_disable(newpcb);
  707. tcp_accepted(((struct tcp_pcb *)(listen_sock->pcb)));
  708. newsock->pcb = newpcb;
  709. debug_tcp_print("Accepted new connection using socket %d\n", sock_num);
  710. return OK;
  711. }
  712. static err_t tcp_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
  713. {
  714. struct socket * sock = (struct socket *) arg;
  715. debug_tcp_print("socket num %ld", get_sock_num(sock));
  716. assert(err == ERR_OK && newpcb);
  717. assert(sock->flags & SOCK_FLG_OP_LISTENING);
  718. if (sock->flags & SOCK_FLG_OP_PENDING) {
  719. int ret;
  720. ret = tcp_do_accept(sock, &sock->mess, newpcb);
  721. sock_revive(sock, ret);
  722. sock->flags &= ~SOCK_FLG_OP_PENDING;
  723. if (ret == OK) {
  724. return ERR_OK;
  725. }
  726. /* in case of an error fall through */
  727. }
  728. /* If we cannot accept rightaway we enqueue the connection for later */
  729. debug_tcp_print("Enqueue connection sock %ld pcb %p\n",
  730. get_sock_num(sock), newpcb);
  731. if (sock_enqueue_data(sock, newpcb, 1) != OK) {
  732. tcp_abort(newpcb);
  733. return ERR_ABRT;
  734. }
  735. if (sock_select_read_set(sock))
  736. sock_select_notify(sock);
  737. return ERR_OK;
  738. }
  739. static void tcp_op_listen(struct socket * sock, message * m)
  740. {
  741. int backlog, err;
  742. struct tcp_pcb * new_pcb;
  743. debug_tcp_print("socket num %ld", get_sock_num(sock));
  744. err = copy_from_user(m->m_source, &backlog, sizeof(backlog),
  745. (cp_grant_id_t) m->IO_GRANT, 0);
  746. new_pcb = tcp_listen_with_backlog((struct tcp_pcb *) sock->pcb,
  747. (u8_t) backlog);
  748. debug_tcp_print("listening pcb %p", new_pcb);
  749. if (!new_pcb) {
  750. debug_tcp_print("Cannot listen on socket %ld", get_sock_num(sock));
  751. sock_reply(sock, EGENERIC);
  752. return;
  753. }
  754. /* advertise that this socket is willing to accept connections */
  755. tcp_accept(new_pcb, tcp_accept_callback);
  756. sock->flags |= SOCK_FLG_OP_LISTENING;
  757. sock->pcb = new_pcb;
  758. sock_reply(sock, OK);
  759. }
  760. static void tcp_op_accept(struct socket * sock, message * m)
  761. {
  762. debug_tcp_print("socket num %ld", get_sock_num(sock));
  763. if (!(sock->flags & SOCK_FLG_OP_LISTENING)) {
  764. debug_tcp_print("socket %ld does not listen\n", get_sock_num(sock));
  765. sock_reply(sock, EINVAL);
  766. return;
  767. }
  768. /* there is a connection ready to be accepted */
  769. if (sock->recv_head) {
  770. int ret;
  771. struct tcp_pcb * pcb;
  772. pcb = (struct tcp_pcb *) sock->recv_head->data;
  773. assert(pcb);
  774. ret = tcp_do_accept(sock, m, pcb);
  775. sock_reply(sock, ret);
  776. if (ret == OK)
  777. sock_dequeue_data(sock);
  778. return;
  779. }
  780. debug_tcp_print("no ready connection, suspending\n");
  781. sock_reply(sock, SUSPEND);
  782. sock->flags |= SOCK_FLG_OP_PENDING;
  783. }
  784. static void tcp_op_shutdown_tx(struct socket * sock)
  785. {
  786. err_t err;
  787. debug_tcp_print("socket num %ld", get_sock_num(sock));
  788. err = tcp_shutdown((struct tcp_pcb *) sock->pcb, 0, 1);
  789. switch (err) {
  790. case ERR_OK:
  791. sock_reply(sock, OK);
  792. break;
  793. case ERR_CONN:
  794. sock_reply(sock, ENOTCONN);
  795. break;
  796. default:
  797. sock_reply(sock, EGENERIC);
  798. }
  799. }
  800. static void tcp_op_get_cookie(struct socket * sock, message * m)
  801. {
  802. tcp_cookie_t cookie;
  803. unsigned sock_num;
  804. assert(sizeof(cookie) >= sizeof(sock));
  805. sock_num = get_sock_num(sock);
  806. memcpy(&cookie, &sock_num, sizeof(sock_num));
  807. if (copy_to_user(m->m_source, &cookie, sizeof(sock),
  808. (cp_grant_id_t) m->IO_GRANT, 0) == OK)
  809. sock_reply(sock, OK);
  810. else
  811. sock_reply(sock, EFAULT);
  812. }
  813. static void tcp_get_opt(struct socket * sock, message * m)
  814. {
  815. int err;
  816. nwio_tcpopt_t tcpopt;
  817. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  818. debug_tcp_print("socket num %ld", get_sock_num(sock));
  819. assert(pcb);
  820. if ((unsigned) m->COUNT < sizeof(tcpopt)) {
  821. sock_reply(sock, EINVAL);
  822. return;
  823. }
  824. /* FIXME : not used by the userspace library */
  825. tcpopt.nwto_flags = 0;
  826. err = copy_to_user(m->m_source, &tcpopt, sizeof(tcpopt),
  827. (cp_grant_id_t) m->IO_GRANT, 0);
  828. if (err != OK)
  829. sock_reply(sock, err);
  830. sock_reply(sock, OK);
  831. }
  832. static void tcp_set_opt(struct socket * sock, message * m)
  833. {
  834. int err;
  835. nwio_tcpopt_t tcpopt;
  836. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  837. debug_tcp_print("socket num %ld", get_sock_num(sock));
  838. assert(pcb);
  839. err = copy_from_user(m->m_source, &tcpopt, sizeof(tcpopt),
  840. (cp_grant_id_t) m->IO_GRANT, 0);
  841. if (err != OK)
  842. sock_reply(sock, err);
  843. /* FIXME : The userspace library does not use this */
  844. sock_reply(sock, OK);
  845. }
  846. static void tcp_op_ioctl(struct socket * sock, message * m)
  847. {
  848. if (!sock->pcb) {
  849. sock_reply(sock, ENOTCONN);
  850. return;
  851. }
  852. debug_tcp_print("socket num %ld req %c %d %d",
  853. get_sock_num(sock),
  854. (m->REQUEST >> 8) & 0xff,
  855. m->REQUEST & 0xff,
  856. (m->REQUEST >> 16) & _IOCPARM_MASK);
  857. switch (m->REQUEST) {
  858. case NWIOGTCPCONF:
  859. tcp_get_conf(sock, m);
  860. break;
  861. case NWIOSTCPCONF:
  862. tcp_set_conf(sock, m);
  863. break;
  864. case NWIOTCPCONN:
  865. tcp_op_connect(sock);
  866. break;
  867. case NWIOTCPLISTENQ:
  868. tcp_op_listen(sock, m);
  869. break;
  870. case NWIOGTCPCOOKIE:
  871. tcp_op_get_cookie(sock, m);
  872. break;
  873. case NWIOTCPACCEPTTO:
  874. tcp_op_accept(sock, m);
  875. break;
  876. case NWIOTCPSHUTDOWN:
  877. tcp_op_shutdown_tx(sock);
  878. break;
  879. case NWIOGTCPOPT:
  880. tcp_get_opt(sock, m);
  881. break;
  882. case NWIOSTCPOPT:
  883. tcp_set_opt(sock, m);
  884. break;
  885. default:
  886. sock_reply(sock, EBADIOCTL);
  887. return;
  888. }
  889. }
  890. static void tcp_op_select(struct socket * sock, __unused message * m)
  891. {
  892. int retsel = 0, sel;
  893. sel = m->USER_ENDPT;
  894. debug_tcp_print("socket num %ld 0x%x", get_sock_num(sock), sel);
  895. /* in this case any operation would block, no error */
  896. if (sock->flags & SOCK_FLG_OP_PENDING) {
  897. debug_tcp_print("SOCK_FLG_OP_PENDING");
  898. if (sel & SEL_NOTIFY) {
  899. if (sel & SEL_RD) {
  900. sock->flags |= SOCK_FLG_SEL_READ;
  901. debug_tcp_print("monitor read");
  902. }
  903. if (sel & SEL_WR) {
  904. sock->flags |= SOCK_FLG_SEL_WRITE;
  905. debug_tcp_print("monitor write");
  906. }
  907. if (sel & SEL_ERR)
  908. sock->flags |= SOCK_FLG_SEL_ERROR;
  909. }
  910. send_reply(m, 0);
  911. return;
  912. }
  913. if (sel & SEL_RD) {
  914. /*
  915. * If recv_head is not NULL we can either read or accept a
  916. * connection which is the same for select()
  917. */
  918. if (sock->pcb) {
  919. if (sock->recv_head &&
  920. !(sock->flags & SOCK_FLG_OP_WRITING))
  921. retsel |= SEL_RD;
  922. else if (!(sock->flags & SOCK_FLG_OP_LISTENING) &&
  923. ((struct tcp_pcb *) sock->pcb)->state != ESTABLISHED)
  924. retsel |= SEL_RD;
  925. else if (sel & SEL_NOTIFY) {
  926. sock->flags |= SOCK_FLG_SEL_READ;
  927. debug_tcp_print("monitor read");
  928. }
  929. } else
  930. retsel |= SEL_RD; /* not connected read does not block */
  931. }
  932. if (sel & SEL_WR) {
  933. if (sock->pcb) {
  934. if (((struct tcp_pcb *) sock->pcb)->state == ESTABLISHED)
  935. retsel |= SEL_WR;
  936. else if (sel & SEL_NOTIFY) {
  937. sock->flags |= SOCK_FLG_SEL_WRITE;
  938. debug_tcp_print("monitor write");
  939. }
  940. } else
  941. retsel |= SEL_WR; /* not connected write does not block */
  942. }
  943. if (retsel & SEL_RD) {
  944. debug_tcp_print("read won't block");
  945. }
  946. if (retsel & SEL_WR) {
  947. debug_tcp_print("write won't block");
  948. }
  949. /* we only monitor if errors will happen in the future */
  950. if (sel & SEL_ERR && sel & SEL_NOTIFY)
  951. sock->flags |= SOCK_FLG_SEL_ERROR;
  952. send_reply(m, retsel);
  953. }
  954. static void tcp_op_select_reply(struct socket * sock, message * m)
  955. {
  956. assert(sock->select_ep != NONE);
  957. debug_tcp_print("socket num %ld", get_sock_num(sock));
  958. if (sock->flags & (SOCK_FLG_OP_PENDING | SOCK_FLG_OP_REVIVING)) {
  959. debug_tcp_print("WARNING socket still blocking!");
  960. return;
  961. }
  962. if (sock->flags & SOCK_FLG_SEL_READ) {
  963. if (sock->pcb == NULL || (sock->recv_head &&
  964. !(sock->flags & SOCK_FLG_OP_WRITING)) ||
  965. (!(sock->flags & SOCK_FLG_OP_LISTENING) &&
  966. ((struct tcp_pcb *) sock->pcb)->state !=
  967. ESTABLISHED)) {
  968. m->DEV_SEL_OPS |= SEL_RD;
  969. debug_tcp_print("read won't block");
  970. }
  971. }
  972. if (sock->flags & SOCK_FLG_SEL_WRITE &&
  973. (sock->pcb == NULL ||
  974. ((struct tcp_pcb *) sock->pcb)->state ==
  975. ESTABLISHED)) {
  976. m->DEV_SEL_OPS |= SEL_WR;
  977. debug_tcp_print("write won't block");
  978. }
  979. if (m->DEV_SEL_OPS)
  980. sock->flags &= ~(SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_READ |
  981. SOCK_FLG_SEL_ERROR);
  982. }
  983. struct sock_ops sock_tcp_ops = {
  984. .open = tcp_op_open,
  985. .close = tcp_op_close,
  986. .read = tcp_op_read,
  987. .write = tcp_op_write,
  988. .ioctl = tcp_op_ioctl,
  989. .select = tcp_op_select,
  990. .select_reply = tcp_op_select_reply
  991. };