2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, Lei Xia <xiaxlei@gmail.com>
12 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
13 * All rights reserved.
15 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * Author: Lei Xia <xiaxlei@gmail.com>
18 * This is free software. You are permitted to use,
19 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
22 #include <geekos/socket.h>
23 #include <geekos/malloc.h>
24 #include <geekos/ne2k.h>
26 #include <uip/uip_arp.h>
27 #include <geekos/vmm_stubs.h>
28 #include <geekos/debug.h>
29 #include <geekos/timer.h>
32 #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
34 #define MAX_SOCKS 1024
37 struct socket sockets[MAX_SOCKS];
39 void socket_appcall(void);
41 #define UIP_APPCALL socket_appcall
42 #endif /* UIP_APPCALL */
47 static int Packet_Received(struct NE2K_Packet_Info* info, uchar_t *pkt);
48 static void periodic_caller(int timer_id);
50 void init_socket_layer() {
55 for (i = 0; i < MAX_SOCKS; i++) {
56 sockets[i].in_use = 0;
57 sockets[i].send_buf = NULL;
58 sockets[i].recv_buf = NULL;
59 sockets[i].state = CLOSED;
69 Init_Ne2k(&Packet_Received);
71 iflag = Begin_Int_Atomic();
72 Start_Timer(2, periodic_caller);
73 End_Int_Atomic(iflag);
80 void set_ip_addr(uchar_t addr[4]) {
82 uip_ipaddr(ipaddr, addr[0], addr[1], addr[2], addr[3]); /* Local IP address */
83 uip_sethostaddr(ipaddr);
87 static int allocate_socket_fd() {
90 for (i = 0; i < MAX_SOCKS; i++) {
91 if (sockets[i].in_use == 0) {
92 sockets[i].in_use = 1;
93 sockets[i].send_buf = create_ring_buffer(BUF_SIZE);
94 if (sockets[i].send_buf == NULL)
96 sockets[i].recv_buf = create_ring_buffer(BUF_SIZE);
97 if (sockets[i].recv_buf == NULL){
98 free_ring_buffer(sockets[i].send_buf);
108 static int release_socket_fd(int sockfd){
109 if (sockfd >= 0 && sockfd < MAX_SOCKS){
110 sockets[sockfd].in_use = 0;
111 free_ring_buffer(sockets[sockfd].send_buf);
112 free_ring_buffer(sockets[sockfd].recv_buf);
113 sockets[sockfd].send_buf = NULL;
114 sockets[sockfd].recv_buf = NULL;
115 sockets[sockfd].state = CLOSED;
122 struct socket * get_socket_from_fd(int fd) {
123 return &(sockets[fd]);
128 static void periodic_caller(int timer_id) {
130 //handle the periodic calls of uIP
132 //PrintBoth("Timer CALLBACK handler\n");
134 for(i = 0; i < UIP_CONNS; ++i) {
137 //devicedriver_send();
138 PrintBoth("Sending Packet\n");
139 NE2K_Transmit(uip_len);
142 for(i = 0; i < UIP_UDP_CONNS; i++) {
145 //devicedriver_send();
146 NE2K_Transmit(uip_len);
152 int connect(const uchar_t ip_addr[4], ushort_t port) {
154 sockfd = allocate_socket_fd();
155 static uip_ipaddr_t ipaddr;
161 uip_ipaddr(&ipaddr, ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
163 sockets[sockfd].con = uip_connect(&ipaddr, htons(port));
166 if (sockets[sockfd].con == NULL){
167 release_socket_fd(sockfd);
172 PrintBoth("Connection start\n");
173 Wait(&(sockets[sockfd].recv_wait_queue));
176 PrintBoth("Connected\n");
184 int recv(int sockfd, void * buf, uint_t len){
187 struct socket *sock = get_socket_from_fd(sockfd);
190 // here we need some lock mechnism, just disable interrupt may not work properly because recv() will be run as a kernel thread
192 recvlen = rb_read(sock->recv_buf, buf, len);
195 Wait(&(sock->recv_wait_queue));
204 // a series of utilities to handle conncetion states
205 static void connected(int sockfd) {
206 struct socket * sock = get_socket_from_fd(sockfd);
208 PrintBoth("Connected Interrupt\n");
210 sock->state = ESTABLISHED;
212 Wake_Up(&(sock->recv_wait_queue));
215 static void closed(int sockfd){
219 static void acked(int sockfd){
223 static void newdata(int sockfd){
230 dataptr = (char *)uip_appdata;
235 sock = get_socket_from_fd(sockfd);
237 wrlen = rb_write(sock->recv_buf, dataptr, len);
239 if (wrlen < len){ //write error, what should I do?
243 Wake_Up(&(sock->recv_wait_queue));
250 static void send_to_driver(int sockfd) {
252 PrintBoth("Sending data to driver\n");
258 int send(int sockfd, void * buf, uint_t len) {
259 struct socket * sock = get_socket_from_fd(sockfd);
260 //int mss = uip_mss();
261 //int pending_bytes = rb_data_len(sock->send_buf);
262 //int len = (mss < pending_bytes) ? mss: pending_bytes;
264 uchar_t * send_buf = uip_appdata;
266 bytes_read = rb_peek(sock->send_buf, send_buf, len);
268 if (bytes_read == 0) {
269 // no packet for send
273 uip_send(send_buf, len);
280 //get the socket id by the local tcp port
281 static int get_socket_from_port(ushort_t lport) {
285 for (i = 0; i < MAX_SOCKS; i++){
286 if (sockets[i].con->lport == lport) {
296 void socket_appcall(void) {
300 sockfd = get_socket_from_port(uip_conn->lport);
302 PrintBoth("Appcall\n");
309 if (uip_connected()) {
314 if (uip_closed() ||uip_aborted() ||uip_timedout()) {
332 send_to_driver(sockfd);
339 static int Packet_Received(struct NE2K_Packet_Info * info, uchar_t * pkt) {
341 uip_len = info->size;
343 // for (i = 0; i < info->size; i++) {
344 // uip_buf[i] = *(pkt + i);
347 PrintBoth("Packet REceived\n");
349 memcpy(uip_buf, pkt, uip_len);
354 if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
355 PrintBoth("ARP PACKET\n");
361 PrintBoth("Transmitting\n");
362 NE2K_Transmit(uip_len);
367 PrintBoth("Data PACKET\n");
373 PrintBoth("Transmitting\n");
375 NE2K_Transmit(uip_len);