PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/lwip/tcp.c

http://www.minix3.org/
C | 1206 lines | 883 code | 214 blank | 109 comment | 185 complexity | 8136d59b0e6ea7cd0ff2d7e317b43742 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 <minix/netsock.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_reply(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_close(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, int blk)
  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. if (!blk) {
  262. debug_tcp_print("reading would block -> EAGAIN");
  263. sock_reply(sock, EAGAIN);
  264. return;
  265. }
  266. /* operation is being processed */
  267. debug_tcp_print("no data to read, suspending");
  268. sock->flags |= SOCK_FLG_OP_PENDING | SOCK_FLG_OP_READING;
  269. }
  270. }
  271. static struct wbuf * wbuf_add(struct socket * sock, unsigned sz)
  272. {
  273. struct wbuf * wbuf;
  274. struct wbuf_chain * wc = (struct wbuf_chain *)sock->buf;
  275. assert(wc);
  276. wbuf = debug_malloc(sizeof(struct wbuf) + sz);
  277. if (!wbuf)
  278. return NULL;
  279. wbuf->len = sz;
  280. wbuf->written = wbuf->unacked = 0;
  281. wbuf->next = NULL;
  282. if (wc->head == NULL)
  283. wc->head = wc->tail = wbuf;
  284. else {
  285. wc->tail->next = wbuf;
  286. wc->tail = wbuf;
  287. }
  288. sock->buf_size += sz;
  289. debug_tcp_print("buffer %p size %d\n", wbuf, sock->buf_size);
  290. return wbuf;
  291. }
  292. static struct wbuf * wbuf_ack_sent(struct socket * sock, unsigned sz)
  293. {
  294. struct wbuf_chain * wc = (struct wbuf_chain *) sock->buf;
  295. struct wbuf ** wb;
  296. wb = &wc->head;
  297. while (sz && *wb) {
  298. if ((*wb)->unacked <= sz) {
  299. struct wbuf * w;
  300. assert((*wb)->rem_len == 0);
  301. w = *wb;
  302. *wb = w->next;
  303. sock->buf_size -= w->len;
  304. sz -= w->unacked;
  305. debug_tcp_print("whole buffer acked (%d / %d), removed",
  306. w->unacked, w->len);
  307. debug_free(w);
  308. } else {
  309. (*wb)->unacked -= sz;
  310. (*wb)->written += sz;
  311. debug_tcp_print("acked %d / %d bytes", sz, (*wb)->len);
  312. sz = 0;
  313. }
  314. }
  315. /* did we write out more than we had? */
  316. assert(sz == 0);
  317. if (wc->head == NULL)
  318. wc->tail = NULL;
  319. debug_tcp_print("buffer size %d\n", sock->buf_size);
  320. return wc->head;
  321. }
  322. static void tcp_op_write(struct socket * sock, message * m, __unused int blk)
  323. {
  324. int ret;
  325. struct wbuf * wbuf;
  326. unsigned snd_buf_len, usr_buf_len;
  327. u8_t flgs = 0;
  328. if (!sock->pcb) {
  329. sock_reply(sock, ENOTCONN);
  330. return;
  331. }
  332. usr_buf_len = m->COUNT;
  333. debug_tcp_print("socket num %ld data size %d",
  334. get_sock_num(sock), usr_buf_len);
  335. /*
  336. * Let at most one buffer grow beyond TCP_BUF_SIZE. This is to minimize
  337. * small writes from userspace if only a few bytes were sent before
  338. */
  339. if (sock->buf_size >= TCP_BUF_SIZE) {
  340. /* FIXME do not block for now */
  341. debug_tcp_print("WARNING : tcp buffers too large, cannot allocate more");
  342. sock_reply(sock, ENOMEM);
  343. return;
  344. }
  345. /*
  346. * Never let the allocated buffers grow more than to 2xTCP_BUF_SIZE and
  347. * never copy more than space available
  348. */
  349. usr_buf_len = (usr_buf_len > TCP_BUF_SIZE ? TCP_BUF_SIZE : usr_buf_len);
  350. wbuf = wbuf_add(sock, usr_buf_len);
  351. debug_tcp_print("new wbuf for %d bytes", wbuf->len);
  352. if (!wbuf) {
  353. debug_tcp_print("cannot allocate new buffer of %d bytes", usr_buf_len);
  354. sock_reply(sock, ENOMEM);
  355. }
  356. if ((ret = copy_from_user(m->m_source, wbuf->data, usr_buf_len,
  357. (cp_grant_id_t) m->IO_GRANT, 0)) != OK) {
  358. sock_reply(sock, ret);
  359. return;
  360. }
  361. wbuf->written = 0;
  362. wbuf->rem_len = usr_buf_len;
  363. /*
  364. * If a writing operation is already in progress, we just enqueue the
  365. * data and quit.
  366. */
  367. if (sock->flags & SOCK_FLG_OP_WRITING) {
  368. struct wbuf_chain * wc = (struct wbuf_chain *)sock->buf;
  369. /*
  370. * We are adding a buffer with unsent data. If we don't have any other
  371. * unsent data, set the pointer to this buffer.
  372. */
  373. if (wc->unsent == NULL) {
  374. wc->unsent = wbuf;
  375. debug_tcp_print("unsent %p remains %d\n", wbuf, wbuf->rem_len);
  376. }
  377. debug_tcp_print("returns %d\n", usr_buf_len);
  378. sock_reply(sock, usr_buf_len);
  379. /*
  380. * We cannot accept new operations (write). We set the flag
  381. * after sending reply not to revive only. We could deadlock.
  382. */
  383. if (sock->buf_size >= TCP_BUF_SIZE)
  384. sock->flags |= SOCK_FLG_OP_PENDING;
  385. return;
  386. }
  387. /*
  388. * Start sending data if the operation is not in progress yet. The
  389. * current buffer is the nly one we have, we cannot send more.
  390. */
  391. snd_buf_len = tcp_sndbuf((struct tcp_pcb *)sock->pcb);
  392. debug_tcp_print("tcp can accept %d bytes", snd_buf_len);
  393. wbuf->unacked = (snd_buf_len < wbuf->rem_len ? snd_buf_len : wbuf->rem_len);
  394. wbuf->rem_len -= wbuf->unacked;
  395. if (wbuf->rem_len) {
  396. flgs = TCP_WRITE_FLAG_MORE;
  397. /*
  398. * Remember that this buffer has some data which we didn't pass
  399. * to tcp yet.
  400. */
  401. ((struct wbuf_chain *)sock->buf)->unsent = wbuf;
  402. debug_tcp_print("unsent %p remains %d\n", wbuf, wbuf->rem_len);
  403. }
  404. ret = tcp_write((struct tcp_pcb *)sock->pcb, wbuf->data,
  405. wbuf->unacked, flgs);
  406. tcp_output((struct tcp_pcb *)sock->pcb);
  407. debug_tcp_print("%d bytes to tcp", wbuf->unacked);
  408. if (ret == ERR_OK) {
  409. /*
  410. * Operation is being processed, no need to remember the message
  411. * in this case, we are going to reply immediatly
  412. */
  413. debug_tcp_print("returns %d\n", usr_buf_len);
  414. sock_reply(sock, usr_buf_len);
  415. sock->flags |= SOCK_FLG_OP_WRITING;
  416. if (sock->buf_size >= TCP_BUF_SIZE)
  417. sock->flags |= SOCK_FLG_OP_PENDING;
  418. } else
  419. sock_reply(sock, EIO);
  420. }
  421. static void tcp_set_conf(struct socket * sock, message * m)
  422. {
  423. int err;
  424. nwio_tcpconf_t tconf;
  425. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  426. debug_tcp_print("socket num %ld", get_sock_num(sock));
  427. assert(pcb);
  428. err = copy_from_user(m->m_source, &tconf, sizeof(tconf),
  429. (cp_grant_id_t) m->IO_GRANT, 0);
  430. if (err != OK)
  431. sock_reply(sock, err);
  432. debug_tcp_print("tconf.nwtc_flags = 0x%lx", tconf.nwtc_flags);
  433. debug_tcp_print("tconf.nwtc_remaddr = 0x%x",
  434. (unsigned int) tconf.nwtc_remaddr);
  435. debug_tcp_print("tconf.nwtc_remport = 0x%x", ntohs(tconf.nwtc_remport));
  436. debug_tcp_print("tconf.nwtc_locaddr = 0x%x",
  437. (unsigned int) tconf.nwtc_locaddr);
  438. debug_tcp_print("tconf.nwtc_locport = 0x%x", ntohs(tconf.nwtc_locport));
  439. sock->usr_flags = tconf.nwtc_flags;
  440. if (sock->usr_flags & NWTC_SET_RA)
  441. pcb->remote_ip.addr = tconf.nwtc_remaddr;
  442. if (sock->usr_flags & NWTC_SET_RP)
  443. pcb->remote_port = ntohs(tconf.nwtc_remport);
  444. if (sock->usr_flags & NWTC_LP_SET) {
  445. /* FIXME the user library can only bind to ANY anyway */
  446. if (tcp_bind(pcb, IP_ADDR_ANY, ntohs(tconf.nwtc_locport)) == ERR_USE) {
  447. sock_reply(sock, EADDRINUSE);
  448. return;
  449. }
  450. }
  451. sock_reply(sock, OK);
  452. }
  453. static void tcp_get_conf(struct socket * sock, message * m)
  454. {
  455. int err;
  456. nwio_tcpconf_t tconf;
  457. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  458. debug_tcp_print("socket num %ld", get_sock_num(sock));
  459. assert(pcb);
  460. tconf.nwtc_locaddr = pcb->local_ip.addr;
  461. tconf.nwtc_locport = htons(pcb->local_port);
  462. tconf.nwtc_remaddr = pcb->remote_ip.addr;
  463. tconf.nwtc_remport = htons(pcb->remote_port);
  464. tconf.nwtc_flags = sock->usr_flags;
  465. debug_tcp_print("tconf.nwtc_flags = 0x%lx", tconf.nwtc_flags);
  466. debug_tcp_print("tconf.nwtc_remaddr = 0x%x",
  467. (unsigned int) tconf.nwtc_remaddr);
  468. debug_tcp_print("tconf.nwtc_remport = 0x%x", ntohs(tconf.nwtc_remport));
  469. debug_tcp_print("tconf.nwtc_locaddr = 0x%x",
  470. (unsigned int) tconf.nwtc_locaddr);
  471. debug_tcp_print("tconf.nwtc_locport = 0x%x", ntohs(tconf.nwtc_locport));
  472. if ((unsigned) m->COUNT < sizeof(tconf)) {
  473. sock_reply(sock, EINVAL);
  474. return;
  475. }
  476. err = copy_to_user(m->m_source, &tconf, sizeof(tconf),
  477. (cp_grant_id_t) m->IO_GRANT, 0);
  478. if (err != OK)
  479. sock_reply(sock, err);
  480. sock_reply(sock, OK);
  481. }
  482. static int enqueue_rcv_data(struct socket * sock, struct pbuf * pbuf)
  483. {
  484. /* Do not enqueue more data than allowed */
  485. if (0 && sock->recv_data_size > 4 * TCP_BUF_SIZE)
  486. return ERR_MEM;
  487. if (sock_enqueue_data(sock, pbuf, pbuf->tot_len) != OK) {
  488. debug_tcp_print("data enqueueing failed");
  489. return ERR_MEM;
  490. }
  491. debug_tcp_print("enqueued %d bytes", pbuf->tot_len);
  492. //printf("enqueued %d bytes, queue %d\n", pbuf->tot_len, sock->recv_data_size);
  493. return ERR_OK;
  494. }
  495. static err_t tcp_recv_callback(void *arg,
  496. struct tcp_pcb *tpcb,
  497. struct pbuf *pbuf,
  498. err_t err)
  499. {
  500. int ret, enqueued = 0;
  501. struct socket * sock = (struct socket *) arg;
  502. debug_tcp_print("socket num %ld", get_sock_num(sock));
  503. if (sock->pcb == NULL) {
  504. if (sock_select_set(sock))
  505. sock_select_notify(sock);
  506. return ERR_OK;
  507. }
  508. assert((struct tcp_pcb *) sock->pcb == tpcb);
  509. if (err != ERR_OK)
  510. return ERR_OK;
  511. if (!pbuf) {
  512. debug_tcp_print("tcp stream closed on the remote side");
  513. // sock->flags |= SOCK_FLG_CLOSED;
  514. /* wake up the reader and report EOF */
  515. if (sock->flags & SOCK_FLG_OP_PENDING &&
  516. sock->flags & SOCK_FLG_OP_READING) {
  517. sock_reply(sock, 0);
  518. sock->flags &= ~(SOCK_FLG_OP_PENDING |
  519. SOCK_FLG_OP_READING);
  520. }
  521. #if 0
  522. /* if there are any undelivered data, drop them */
  523. sock_dequeue_data_all(sock, tcp_recv_free);
  524. tcp_abandon(tpcb, 0);
  525. sock->pcb = NULL;
  526. #endif
  527. return ERR_OK;
  528. }
  529. /*
  530. * FIXME we always enqueue the data first. If the head is empty and read
  531. * operation is pending we could try to deliver immeditaly without
  532. * enqueueing
  533. */
  534. if (enqueue_rcv_data(sock, pbuf) == ERR_OK)
  535. enqueued = 1;
  536. /*
  537. * Deliver data if there is a pending read operation, otherwise notify
  538. * select if the socket is being monitored
  539. */
  540. if (sock->flags & SOCK_FLG_OP_PENDING) {
  541. if (sock->flags & SOCK_FLG_OP_READING) {
  542. ret = read_from_tcp(sock, &sock->mess);
  543. debug_tcp_print("read op finished");
  544. sock_reply(sock, ret);
  545. sock->flags &= ~(SOCK_FLG_OP_PENDING |
  546. SOCK_FLG_OP_READING);
  547. }
  548. } else if (!(sock->flags & SOCK_FLG_OP_WRITING) &&
  549. sock_select_rw_set(sock))
  550. sock_select_notify(sock);
  551. /* perhaps we have deliverd some data to user, try to enqueue again */
  552. if (!enqueued) {
  553. return enqueue_rcv_data(sock, pbuf);
  554. } else
  555. return ERR_OK;
  556. }
  557. static err_t tcp_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len)
  558. {
  559. struct socket * sock = (struct socket *) arg;
  560. struct wbuf * wbuf;
  561. struct wbuf_chain * wc = (struct wbuf_chain *) sock->buf;
  562. unsigned snd_buf_len;
  563. int ret;
  564. debug_tcp_print("socket num %ld", get_sock_num(sock));
  565. /* an error might have had happen */
  566. if (sock->pcb == NULL) {
  567. if (sock_select_set(sock))
  568. sock_select_notify(sock);
  569. return ERR_OK;
  570. }
  571. assert((struct tcp_pcb *)sock->pcb == tpcb);
  572. /* operation must have been canceled, do not send any other data */
  573. if (!sock->flags & SOCK_FLG_OP_PENDING)
  574. return ERR_OK;
  575. wbuf = wbuf_ack_sent(sock, len);
  576. if (wbuf == NULL) {
  577. debug_tcp_print("all data acked, nothing more to send");
  578. sock->flags &= ~SOCK_FLG_OP_WRITING;
  579. if (!(sock->flags & SOCK_FLG_OP_READING))
  580. sock->flags &= ~SOCK_FLG_OP_PENDING;
  581. /* no reviving, we must notify. Write and read possible */
  582. if (sock_select_rw_set(sock))
  583. sock_select_notify(sock);
  584. return ERR_OK;
  585. }
  586. /* we have just freed some space, write will be accepted */
  587. if (sock->buf_size < TCP_BUF_SIZE && sock_select_rw_set(sock)) {
  588. if (!(sock->flags & SOCK_FLG_OP_READING)) {
  589. sock->flags &= ~SOCK_FLG_OP_PENDING;
  590. sock_select_notify(sock);
  591. }
  592. }
  593. /*
  594. * Check if there is some space for new data, there should be, we just
  595. * got a confirmation that some data reached the other end of the
  596. * connection
  597. */
  598. snd_buf_len = tcp_sndbuf(tpcb);
  599. assert(snd_buf_len > 0);
  600. debug_tcp_print("tcp can accept %d bytes", snd_buf_len);
  601. if (!wc->unsent) {
  602. debug_tcp_print("nothing to send");
  603. return ERR_OK;
  604. }
  605. wbuf = wc->unsent;
  606. while (wbuf) {
  607. unsigned towrite;
  608. u8_t flgs = 0;
  609. towrite = (snd_buf_len < wbuf->rem_len ?
  610. snd_buf_len : wbuf->rem_len);
  611. wbuf->rem_len -= towrite;
  612. debug_tcp_print("data to send, sending %d", towrite);
  613. if (wbuf->rem_len || wbuf->next)
  614. flgs = TCP_WRITE_FLAG_MORE;
  615. ret = tcp_write(tpcb, wbuf->data + wbuf->written + wbuf->unacked,
  616. towrite, flgs);
  617. debug_tcp_print("%d bytes to tcp", towrite);
  618. /* tcp_output() is called once we return from this callback */
  619. if (ret != ERR_OK) {
  620. debug_print("tcp_write() failed (%d), written %d"
  621. , ret, wbuf->written);
  622. sock->flags &= ~(SOCK_FLG_OP_PENDING | SOCK_FLG_OP_WRITING);
  623. /* no reviving, we must notify. Write and read possible */
  624. if (sock_select_rw_set(sock))
  625. sock_select_notify(sock);
  626. return ERR_OK;
  627. }
  628. wbuf->unacked += towrite;
  629. snd_buf_len -= towrite;
  630. debug_tcp_print("tcp still accepts %d bytes\n", snd_buf_len);
  631. if (snd_buf_len) {
  632. assert(wbuf->rem_len == 0);
  633. wbuf = wbuf->next;
  634. wc->unsent = wbuf;
  635. if (wbuf)
  636. debug_tcp_print("unsent %p remains %d\n",
  637. wbuf, wbuf->rem_len);
  638. else {
  639. debug_tcp_print("nothing to send");
  640. }
  641. } else
  642. break;
  643. }
  644. return ERR_OK;
  645. }
  646. static err_t tcp_connected_callback(void *arg,
  647. struct tcp_pcb *tpcb,
  648. __unused err_t err)
  649. {
  650. struct socket * sock = (struct socket *) arg;
  651. debug_tcp_print("socket num %ld err %d", get_sock_num(sock), err);
  652. if (sock->pcb == NULL) {
  653. if (sock_select_set(sock))
  654. sock_select_notify(sock);
  655. return ERR_OK;
  656. }
  657. assert((struct tcp_pcb *)sock->pcb == tpcb);
  658. tcp_sent(tpcb, tcp_sent_callback);
  659. tcp_recv(tpcb, tcp_recv_callback);
  660. sock_reply(sock, OK);
  661. sock->flags &= ~(SOCK_FLG_OP_PENDING | SOCK_FLG_OP_CONNECTING);
  662. /* revive does the sock_select_notify() for us */
  663. return ERR_OK;
  664. }
  665. static void tcp_op_connect(struct socket * sock)
  666. {
  667. ip_addr_t remaddr;
  668. struct tcp_pcb * pcb;
  669. err_t err;
  670. debug_tcp_print("socket num %ld", get_sock_num(sock));
  671. /*
  672. * Connecting is going to send some packets. Unless an immediate error
  673. * occurs this operation is going to block
  674. */
  675. sock->flags |= SOCK_FLG_OP_PENDING | SOCK_FLG_OP_CONNECTING;
  676. /* try to connect now */
  677. pcb = (struct tcp_pcb *) sock->pcb;
  678. remaddr = pcb->remote_ip;
  679. err = tcp_connect(pcb, &remaddr, pcb->remote_port,
  680. tcp_connected_callback);
  681. if (err == ERR_VAL)
  682. panic("Wrong tcp_connect arguments");
  683. if (err != ERR_OK)
  684. panic("Other tcp_connect error %d\n", err);
  685. }
  686. static int tcp_do_accept(struct socket * listen_sock,
  687. message * m,
  688. struct tcp_pcb * newpcb)
  689. {
  690. struct socket * newsock;
  691. unsigned sock_num;
  692. int ret;
  693. debug_tcp_print("socket num %ld", get_sock_num(listen_sock));
  694. if ((ret = copy_from_user(m->m_source, &sock_num, sizeof(sock_num),
  695. (cp_grant_id_t) m->IO_GRANT, 0)) != OK)
  696. return EFAULT;
  697. if (!is_valid_sock_num(sock_num))
  698. return EBADF;
  699. newsock = get_sock(sock_num);
  700. assert(newsock->pcb); /* because of previous open() */
  701. /* we really want to forget about this socket */
  702. tcp_err((struct tcp_pcb *)newsock->pcb, NULL);
  703. tcp_abandon((struct tcp_pcb *)newsock->pcb, 0);
  704. tcp_arg(newpcb, newsock);
  705. tcp_err(newpcb, tcp_error_callback);
  706. tcp_sent(newpcb, tcp_sent_callback);
  707. tcp_recv(newpcb, tcp_recv_callback);
  708. tcp_nagle_disable(newpcb);
  709. tcp_accepted(((struct tcp_pcb *)(listen_sock->pcb)));
  710. newsock->pcb = newpcb;
  711. debug_tcp_print("Accepted new connection using socket %d\n", sock_num);
  712. return OK;
  713. }
  714. static err_t tcp_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
  715. {
  716. struct socket * sock = (struct socket *) arg;
  717. debug_tcp_print("socket num %ld", get_sock_num(sock));
  718. assert(err == ERR_OK && newpcb);
  719. assert(sock->flags & SOCK_FLG_OP_LISTENING);
  720. if (sock->flags & SOCK_FLG_OP_PENDING) {
  721. int ret;
  722. ret = tcp_do_accept(sock, &sock->mess, newpcb);
  723. sock_reply(sock, ret);
  724. sock->flags &= ~SOCK_FLG_OP_PENDING;
  725. if (ret == OK) {
  726. return ERR_OK;
  727. }
  728. /* in case of an error fall through */
  729. }
  730. /* If we cannot accept rightaway we enqueue the connection for later */
  731. debug_tcp_print("Enqueue connection sock %ld pcb %p\n",
  732. get_sock_num(sock), newpcb);
  733. if (sock_enqueue_data(sock, newpcb, 1) != OK) {
  734. tcp_abort(newpcb);
  735. return ERR_ABRT;
  736. }
  737. if (sock_select_read_set(sock))
  738. sock_select_notify(sock);
  739. return ERR_OK;
  740. }
  741. static void tcp_op_listen(struct socket * sock, message * m)
  742. {
  743. int backlog, err;
  744. struct tcp_pcb * new_pcb;
  745. debug_tcp_print("socket num %ld", get_sock_num(sock));
  746. err = copy_from_user(m->m_source, &backlog, sizeof(backlog),
  747. (cp_grant_id_t) m->IO_GRANT, 0);
  748. new_pcb = tcp_listen_with_backlog((struct tcp_pcb *) sock->pcb,
  749. (u8_t) backlog);
  750. debug_tcp_print("listening pcb %p", new_pcb);
  751. if (!new_pcb) {
  752. debug_tcp_print("Cannot listen on socket %ld", get_sock_num(sock));
  753. sock_reply(sock, EGENERIC);
  754. return;
  755. }
  756. /* advertise that this socket is willing to accept connections */
  757. tcp_accept(new_pcb, tcp_accept_callback);
  758. sock->flags |= SOCK_FLG_OP_LISTENING;
  759. sock->pcb = new_pcb;
  760. sock_reply(sock, OK);
  761. }
  762. static void tcp_op_accept(struct socket * sock, message * m)
  763. {
  764. debug_tcp_print("socket num %ld", get_sock_num(sock));
  765. if (!(sock->flags & SOCK_FLG_OP_LISTENING)) {
  766. debug_tcp_print("socket %ld does not listen\n", get_sock_num(sock));
  767. sock_reply(sock, EINVAL);
  768. return;
  769. }
  770. /* there is a connection ready to be accepted */
  771. if (sock->recv_head) {
  772. int ret;
  773. struct tcp_pcb * pcb;
  774. pcb = (struct tcp_pcb *) sock->recv_head->data;
  775. assert(pcb);
  776. ret = tcp_do_accept(sock, m, pcb);
  777. sock_reply(sock, ret);
  778. if (ret == OK)
  779. sock_dequeue_data(sock);
  780. return;
  781. }
  782. debug_tcp_print("no ready connection, suspending\n");
  783. sock->flags |= SOCK_FLG_OP_PENDING;
  784. }
  785. static void tcp_op_shutdown_tx(struct socket * sock)
  786. {
  787. err_t err;
  788. debug_tcp_print("socket num %ld", get_sock_num(sock));
  789. err = tcp_shutdown((struct tcp_pcb *) sock->pcb, 0, 1);
  790. switch (err) {
  791. case ERR_OK:
  792. sock_reply(sock, OK);
  793. break;
  794. case ERR_CONN:
  795. sock_reply(sock, ENOTCONN);
  796. break;
  797. default:
  798. sock_reply(sock, EGENERIC);
  799. }
  800. }
  801. static void tcp_op_get_cookie(struct socket * sock, message * m)
  802. {
  803. tcp_cookie_t cookie;
  804. unsigned sock_num;
  805. assert(sizeof(cookie) >= sizeof(sock));
  806. sock_num = get_sock_num(sock);
  807. memcpy(&cookie, &sock_num, sizeof(sock_num));
  808. if (copy_to_user(m->m_source, &cookie, sizeof(sock),
  809. (cp_grant_id_t) m->IO_GRANT, 0) == OK)
  810. sock_reply(sock, OK);
  811. else
  812. sock_reply(sock, EFAULT);
  813. }
  814. static void tcp_get_opt(struct socket * sock, message * m)
  815. {
  816. int err;
  817. nwio_tcpopt_t tcpopt;
  818. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  819. debug_tcp_print("socket num %ld", get_sock_num(sock));
  820. assert(pcb);
  821. if ((unsigned) m->COUNT < sizeof(tcpopt)) {
  822. sock_reply(sock, EINVAL);
  823. return;
  824. }
  825. /* FIXME : not used by the userspace library */
  826. tcpopt.nwto_flags = 0;
  827. err = copy_to_user(m->m_source, &tcpopt, sizeof(tcpopt),
  828. (cp_grant_id_t) m->IO_GRANT, 0);
  829. if (err != OK)
  830. sock_reply(sock, err);
  831. sock_reply(sock, OK);
  832. }
  833. static void tcp_set_opt(struct socket * sock, message * m)
  834. {
  835. int err;
  836. nwio_tcpopt_t tcpopt;
  837. struct tcp_pcb * pcb = (struct tcp_pcb *) sock->pcb;
  838. debug_tcp_print("socket num %ld", get_sock_num(sock));
  839. assert(pcb);
  840. err = copy_from_user(m->m_source, &tcpopt, sizeof(tcpopt),
  841. (cp_grant_id_t) m->IO_GRANT, 0);
  842. if (err != OK)
  843. sock_reply(sock, err);
  844. /* FIXME : The userspace library does not use this */
  845. sock_reply(sock, OK);
  846. }
  847. static void tcp_op_ioctl(struct socket * sock, message * m, __unused int blk)
  848. {
  849. if (!sock->pcb) {
  850. sock_reply(sock, ENOTCONN);
  851. return;
  852. }
  853. debug_tcp_print("socket num %ld req %c %d %d",
  854. get_sock_num(sock),
  855. (m->REQUEST >> 8) & 0xff,
  856. m->REQUEST & 0xff,
  857. (m->REQUEST >> 16) & _IOCPARM_MASK);
  858. switch (m->REQUEST) {
  859. case NWIOGTCPCONF:
  860. tcp_get_conf(sock, m);
  861. break;
  862. case NWIOSTCPCONF:
  863. tcp_set_conf(sock, m);
  864. break;
  865. case NWIOTCPCONN:
  866. tcp_op_connect(sock);
  867. break;
  868. case NWIOTCPLISTENQ:
  869. tcp_op_listen(sock, m);
  870. break;
  871. case NWIOGTCPCOOKIE:
  872. tcp_op_get_cookie(sock, m);
  873. break;
  874. case NWIOTCPACCEPTTO:
  875. tcp_op_accept(sock, m);
  876. break;
  877. case NWIOTCPSHUTDOWN:
  878. tcp_op_shutdown_tx(sock);
  879. break;
  880. case NWIOGTCPOPT:
  881. tcp_get_opt(sock, m);
  882. break;
  883. case NWIOSTCPOPT:
  884. tcp_set_opt(sock, m);
  885. break;
  886. default:
  887. sock_reply(sock, EBADIOCTL);
  888. return;
  889. }
  890. }
  891. static void tcp_op_select(struct socket * sock, __unused message * m)
  892. {
  893. int retsel = 0, sel;
  894. sel = m->USER_ENDPT;
  895. debug_tcp_print("socket num %ld 0x%x", get_sock_num(sock), sel);
  896. /* in this case any operation would block, no error */
  897. if (sock->flags & SOCK_FLG_OP_PENDING) {
  898. debug_tcp_print("SOCK_FLG_OP_PENDING");
  899. if (sel & SEL_NOTIFY) {
  900. if (sel & SEL_RD) {
  901. sock->flags |= SOCK_FLG_SEL_READ;
  902. debug_tcp_print("monitor read");
  903. }
  904. if (sel & SEL_WR) {
  905. sock->flags |= SOCK_FLG_SEL_WRITE;
  906. debug_tcp_print("monitor write");
  907. }
  908. if (sel & SEL_ERR)
  909. sock->flags |= SOCK_FLG_SEL_ERROR;
  910. }
  911. sock_reply_select(sock, 0);
  912. return;
  913. }
  914. if (sel & SEL_RD) {
  915. /*
  916. * If recv_head is not NULL we can either read or accept a
  917. * connection which is the same for select()
  918. */
  919. if (sock->pcb) {
  920. if (sock->recv_head &&
  921. !(sock->flags & SOCK_FLG_OP_WRITING))
  922. retsel |= SEL_RD;
  923. else if (!(sock->flags & SOCK_FLG_OP_LISTENING) &&
  924. ((struct tcp_pcb *) sock->pcb)->state != ESTABLISHED)
  925. retsel |= SEL_RD;
  926. else if (sel & SEL_NOTIFY) {
  927. sock->flags |= SOCK_FLG_SEL_READ;
  928. debug_tcp_print("monitor read");
  929. }
  930. } else
  931. retsel |= SEL_RD; /* not connected read does not block */
  932. }
  933. if (sel & SEL_WR) {
  934. if (sock->pcb) {
  935. if (((struct tcp_pcb *) sock->pcb)->state == ESTABLISHED)
  936. retsel |= SEL_WR;
  937. else if (sel & SEL_NOTIFY) {
  938. sock->flags |= SOCK_FLG_SEL_WRITE;
  939. debug_tcp_print("monitor write");
  940. }
  941. } else
  942. retsel |= SEL_WR; /* not connected write does not block */
  943. }
  944. if (retsel & SEL_RD) {
  945. debug_tcp_print("read won't block");
  946. }
  947. if (retsel & SEL_WR) {
  948. debug_tcp_print("write won't block");
  949. }
  950. /* we only monitor if errors will happen in the future */
  951. if (sel & SEL_ERR && sel & SEL_NOTIFY)
  952. sock->flags |= SOCK_FLG_SEL_ERROR;
  953. sock_reply_select(sock, retsel);
  954. }
  955. static void tcp_op_select_reply(struct socket * sock, message * m)
  956. {
  957. assert(sock->select_ep != NONE);
  958. debug_tcp_print("socket num %ld", get_sock_num(sock));
  959. if (sock->flags & SOCK_FLG_OP_PENDING) {
  960. debug_tcp_print("WARNING socket still blocking!");
  961. return;
  962. }
  963. if (sock->flags & SOCK_FLG_SEL_READ) {
  964. if (sock->pcb == NULL || (sock->recv_head &&
  965. !(sock->flags & SOCK_FLG_OP_WRITING)) ||
  966. (!(sock->flags & SOCK_FLG_OP_LISTENING) &&
  967. ((struct tcp_pcb *) sock->pcb)->state !=
  968. ESTABLISHED)) {
  969. m->DEV_SEL_OPS |= SEL_RD;
  970. debug_tcp_print("read won't block");
  971. }
  972. }
  973. if (sock->flags & SOCK_FLG_SEL_WRITE &&
  974. (sock->pcb == NULL ||
  975. ((struct tcp_pcb *) sock->pcb)->state ==
  976. ESTABLISHED)) {
  977. m->DEV_SEL_OPS |= SEL_WR;
  978. debug_tcp_print("write won't block");
  979. }
  980. if (m->DEV_SEL_OPS)
  981. sock->flags &= ~(SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_READ |
  982. SOCK_FLG_SEL_ERROR);
  983. }
  984. struct sock_ops sock_tcp_ops = {
  985. .open = tcp_op_open,
  986. .close = tcp_op_close,
  987. .read = tcp_op_read,
  988. .write = tcp_op_write,
  989. .ioctl = tcp_op_ioctl,
  990. .select = tcp_op_select,
  991. .select_reply = tcp_op_select_reply
  992. };