1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, Lei Xia <xiaxlei@gmail.com> */
3 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
5 #include <geekos/socket.h>
6 #include <geekos/malloc.h>
7 #include <geekos/ne2k.h>
12 #include <uip/uip_arp.h>
14 #include <geekos/vmm_stubs.h>
15 #include <geekos/debug.h>
16 #include <geekos/timer.h>
18 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
20 #define MAX_SOCKS 1024
23 struct socket sockets[MAX_SOCKS];
25 void socket_appcall(void);
27 #define UIP_APPCALL socket_appcall
28 #endif /* UIP_APPCALL */
31 static int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt);
32 static void periodic_caller(int timer_id, void * arg);
34 void init_socket_layer() {
40 for (i = 0; i < MAX_SOCKS; i++) {
41 sockets[i].in_use = 0;
42 sockets[i].send_buf = NULL;
43 sockets[i].recv_buf = NULL;
44 sockets[i].state = CLOSED;
52 Init_Ne2k(&Packet_Received);
54 iflag = Begin_Int_Atomic();
55 Start_Timer(2, periodic_caller, NULL);
56 End_Int_Atomic(iflag);
59 void set_ip_addr(uchar_t addr[4]) {
61 uip_ipaddr(ipaddr, addr[0], addr[1], addr[2], addr[3]); /* Local IP address */
62 uip_sethostaddr(ipaddr);
65 static int allocate_socket_fd() {
68 for (i = 0; i < MAX_SOCKS; i++) {
69 if (sockets[i].in_use == 0) {
70 sockets[i].in_use = 1;
71 sockets[i].send_buf = create_ring_buffer(BUF_SIZE);
72 if (sockets[i].send_buf == NULL)
74 sockets[i].recv_buf = create_ring_buffer(BUF_SIZE);
75 if (sockets[i].recv_buf == NULL){
76 free_ring_buffer(sockets[i].send_buf);
86 static int release_socket_fd(int sockfd){
87 if (sockfd >= 0 && sockfd < MAX_SOCKS){
88 sockets[sockfd].in_use = 0;
89 free_ring_buffer(sockets[sockfd].send_buf);
90 free_ring_buffer(sockets[sockfd].recv_buf);
91 sockets[sockfd].send_buf = NULL;
92 sockets[sockfd].recv_buf = NULL;
93 sockets[sockfd].state = CLOSED;
100 struct socket * get_socket_from_fd(int fd) {
101 return &(sockets[fd]);
105 static void periodic_caller(int timer_id, void * arg) {
107 //handle the periodic calls of uIP
109 //PrintBoth("Timer CALLBACK handler\n");
111 for(i = 0; i < UIP_CONNS; ++i) {
114 //devicedriver_send();
115 PrintBoth("Sending Packet\n");
116 NE2K_Transmit(uip_len);
119 for(i = 0; i < UIP_UDP_CONNS; i++) {
122 //devicedriver_send();
123 NE2K_Transmit(uip_len);
129 int connect(const uchar_t ip_addr[4], ushort_t port) {
131 sockfd = allocate_socket_fd();
132 static uip_ipaddr_t ipaddr;
138 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
140 sockets[sockfd].con = uip_connect(&ipaddr, htons(port));
143 if (sockets[sockfd].con == NULL){
144 release_socket_fd(sockfd);
149 PrintBoth("Connection start\n");
150 Wait(&(sockets[sockfd].recv_wait_queue));
153 PrintBoth("Connected\n");
161 int recv(int sockfd, void * buf, uint_t len){
164 struct socket *sock = get_socket_from_fd(sockfd);
167 // here we need some lock mechnism, just disable interrupt may not work properly because recv() will be run as a kernel thread
169 recvlen = rb_read(sock->recv_buf, buf, len);
172 Wait(&(sock->recv_wait_queue));
181 // a series of utilities to handle conncetion states
182 static void connected(int sockfd) {
183 struct socket * sock = get_socket_from_fd(sockfd);
185 PrintBoth("Connected Interrupt\n");
187 sock->state = ESTABLISHED;
189 Wake_Up(&(sock->recv_wait_queue));
192 static void closed(int sockfd){
196 static void acked(int sockfd){
200 static void newdata(int sockfd){
207 dataptr = (char *)uip_appdata;
212 sock = get_socket_from_fd(sockfd);
214 wrlen = rb_write(sock->recv_buf, dataptr, len);
216 if (wrlen < len){ //write error, what should I do?
220 Wake_Up(&(sock->recv_wait_queue));
227 static void send_to_driver(int sockfd) {
229 PrintBoth("Sending data to driver\n");
235 int send(int sockfd, void * buf, uint_t len) {
236 struct socket * sock = get_socket_from_fd(sockfd);
237 //int mss = uip_mss();
238 //int pending_bytes = rb_data_len(sock->send_buf);
239 //int len = (mss < pending_bytes) ? mss: pending_bytes;
241 uchar_t * send_buf = uip_appdata;
243 bytes_read = rb_peek(sock->send_buf, send_buf, len);
245 if (bytes_read == 0) {
246 // no packet for send
250 uip_send(send_buf, len);
257 //get the socket id by the local tcp port
258 static int get_socket_from_port(ushort_t lport) {
262 for (i = 0; i < MAX_SOCKS; i++){
263 if (sockets[i].con->lport == lport) {
273 void socket_appcall(void) {
277 sockfd = get_socket_from_port(uip_conn->lport);
279 PrintBoth("Appcall\n");
286 if (uip_connected()) {
291 if (uip_closed() ||uip_aborted() ||uip_timedout()) {
309 send_to_driver(sockfd);
316 static int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
318 uip_len = info->size;
320 // for (i = 0; i < info->size; i++) {
321 // uip_buf[i] = *(pkt + i);
324 PrintBoth("Packet REceived\n");
326 memcpy(uip_buf, pkt, uip_len);
331 if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
332 PrintBoth("ARP PACKET\n");
338 PrintBoth("Transmitting\n");
339 NE2K_Transmit(uip_len);
344 PrintBoth("Data PACKET\n");
350 PrintBoth("Transmitting\n");
352 NE2K_Transmit(uip_len);