2 * Palacios VNET Host Bridge
6 #include <linux/spinlock.h>
7 #include <linux/seq_file.h>
8 #include <linux/proc_fs.h>
9 #include <asm/uaccess.h>
10 #include <linux/inet.h>
11 #include <linux/kthread.h>
13 #include <linux/netdevice.h>
16 #include <linux/net.h>
17 #include <linux/string.h>
18 #include <linux/preempt.h>
19 #include <linux/sched.h>
22 #include <vnet/vnet.h>
23 #include <vnet/vnet_hashtable.h>
24 #include "palacios-vnet.h"
28 #define VNET_SERVER_PORT 9000
30 #define VNET_YIELD_TIME_USEC 1000
37 struct sockaddr_in sock_addr;
38 vnet_brg_proto_t sock_proto;
40 struct nic_statistics stats;
44 struct list_head node;
48 struct vnet_brg_state {
53 struct list_head link_list;
54 struct hashtable *ip2link;
58 struct socket * serv_sock;
59 struct sockaddr_in serv_addr;
60 vnet_brg_proto_t serv_proto;
62 struct task_struct * serv_thread;
64 void * brg_data; /* private data from vnet_core */
66 struct vnet_brg_stats stats;
70 static struct vnet_brg_state vnet_brg_s;
73 int vnet_brg_stats(struct vnet_brg_stats * stats){
74 memcpy(stats, &(vnet_brg_s.stats), sizeof(*stats));
79 static inline struct vnet_link * _link_by_ip(uint32_t ip) {
80 return (struct vnet_link *)vnet_htable_search(vnet_brg_s.ip2link, (addr_t)&ip);
83 static inline struct vnet_link * _link_by_idx(int idx) {
84 struct vnet_link * link = NULL;
86 list_for_each_entry(link, &(vnet_brg_s.link_list), node) {
88 if (link->idx == idx) {
96 static void _delete_link(struct vnet_link * link){
99 link->sock->ops->release(link->sock);
101 spin_lock_irqsave(&(vnet_brg_s.lock), flags);
102 list_del(&(link->node));
103 vnet_htable_remove(vnet_brg_s.ip2link, (addr_t)&(link->dst_ip), 0);
104 vnet_brg_s.num_links --;
105 spin_unlock_irqrestore(&(vnet_brg_s.lock), flags);
107 INFO("VNET Bridge: Link deleted, ip 0x%x, port: %d, idx: %d\n",
116 void vnet_brg_delete_link(uint32_t idx){
117 struct vnet_link * link = _link_by_idx(idx);
124 static void deinit_links_list(void){
125 struct vnet_link * link, * tmp_link;
127 list_for_each_entry_safe(link, tmp_link, &(vnet_brg_s.link_list), node) {
132 static uint32_t _create_link(struct vnet_link * link) {
137 switch(link->sock_proto){
139 protocol = IPPROTO_UDP;
142 protocol = IPPROTO_TCP;
146 WARNING("Unsupported VNET Server Protocol\n");
150 if ((err = sock_create(AF_INET, SOCK_DGRAM, protocol, &link->sock)) < 0) {
151 WARNING("Could not create socket for VNET Link, error %d\n", err);
155 memset(&link->sock_addr, 0, sizeof(struct sockaddr));
157 link->sock_addr.sin_family = AF_INET;
158 link->sock_addr.sin_addr.s_addr = link->dst_ip;
159 link->sock_addr.sin_port = htons(link->dst_port);
161 if ((err = link->sock->ops->connect(link->sock, (struct sockaddr *)&(link->sock_addr), sizeof(struct sockaddr), 0) < 0)) {
162 WARNING("Could not connect to remote VNET Server, error %d\n", err);
166 spin_lock_irqsave(&(vnet_brg_s.lock), flags);
167 list_add(&(link->node), &(vnet_brg_s.link_list));
168 vnet_brg_s.num_links ++;
169 link->idx = ++ vnet_brg_s.link_idx;
170 vnet_htable_insert(vnet_brg_s.ip2link, (addr_t)&(link->dst_ip), (addr_t)link);
171 spin_unlock_irqrestore(&(vnet_brg_s.lock), flags);
173 INFO("VNET Bridge: Link created, ip 0x%x, port: %d, idx: %d, link: %p, protocol: %s\n",
178 ((link->sock_proto==UDP)?"UDP":"TCP"));
184 uint32_t vnet_brg_add_link(uint32_t ip, uint16_t port, vnet_brg_proto_t proto){
185 struct vnet_link * new_link = NULL;
188 new_link = kmalloc(sizeof(struct vnet_link), GFP_KERNEL);
192 memset(new_link, 0, sizeof(struct vnet_link));
194 new_link->dst_ip = ip;
195 new_link->dst_port = port;
196 new_link->sock_proto = proto;
198 idx = _create_link(new_link);
200 WARNING("Could not create link\n");
209 int vnet_brg_link_stats(uint32_t link_idx, struct nic_statistics * stats){
210 struct vnet_link * link;
212 link = _link_by_idx(link_idx);
217 memcpy(stats, &(link->stats), sizeof(*stats));
224 _udp_send(struct socket * sock,
225 struct sockaddr_in * addr,
226 unsigned char * buf, int len) {
233 if (sock->sk == NULL) {
242 msg.msg_namelen = sizeof(struct sockaddr_in);
243 msg.msg_control = NULL;
244 msg.msg_controllen = 0;
247 msg.msg_control = NULL;
251 size = sock_sendmsg(sock, &msg, len);
260 _udp_recv(struct socket * sock,
261 struct sockaddr_in * addr,
262 unsigned char * buf, int len, int nonblocking) {
268 if (sock->sk == NULL) {
275 msg.msg_flags = MSG_NOSIGNAL | (nonblocking ? MSG_DONTWAIT : 0);
277 msg.msg_namelen = sizeof(struct sockaddr_in);
278 msg.msg_control = NULL;
279 msg.msg_controllen = 0;
282 msg.msg_control = NULL;
286 size = sock_recvmsg(sock, &msg, len, msg.msg_flags);
293 /* send packets to VNET core */
295 send_to_palacios(unsigned char * buf,
298 struct v3_vnet_pkt pkt;
300 pkt.src_type = LINK_EDGE;
301 pkt.src_id = link_id;
302 memcpy(pkt.header, buf, ETHERNET_HEADER_LEN);
306 DEBUG("VNET Lnx Bridge: send pkt to VNET core (size: %d, src_id: %d, src_type: %d)\n",
307 pkt.size, pkt.src_id, pkt.src_type);
309 print_hex_dump(NULL, "pkt_data: ", 0, 20, 20, pkt.data, pkt.size, 0);
313 vnet_brg_s.stats.pkt_to_vmm ++;
315 return v3_vnet_send_pkt(&pkt, NULL);
319 /* send packet to extern network */
321 bridge_send_pkt(struct v3_vm_info * vm,
322 struct v3_vnet_pkt * pkt,
323 void * private_data) {
324 struct vnet_link * link;
327 DEBUG("VNET Lnx Host Bridge: packet received from VNET Core ... pkt size: %d, link: %d\n",
331 print_hex_dump(NULL, "pkt_data: ", 0, 20, 20, pkt->data, pkt->size, 0);
335 vnet_brg_s.stats.pkt_from_vmm ++;
337 link = _link_by_idx(pkt->dst_id);
339 switch(link->sock_proto){
341 _udp_send(link->sock, &(link->sock_addr), pkt->data, pkt->size);
342 vnet_brg_s.stats.pkt_to_phy ++;
345 vnet_brg_s.stats.pkt_to_phy ++;
349 WARNING("VNET Server: Invalid Link Protocol\n");
350 vnet_brg_s.stats.pkt_drop_vmm ++;
352 link->stats.tx_bytes += pkt->size;
353 link->stats.tx_pkts ++;
355 INFO("VNET Bridge Linux Host: wrong dst link, idx: %d, discarding the packet\n", pkt->dst_id);
356 vnet_brg_s.stats.pkt_drop_vmm ++;
363 static int init_vnet_serv(void) {
367 switch(vnet_brg_s.serv_proto){
369 protocol = IPPROTO_UDP;
372 protocol = IPPROTO_TCP;
376 WARNING("Unsupported VNET Server Protocol\n");
380 if ((err = sock_create(AF_INET, SOCK_DGRAM, protocol, &vnet_brg_s.serv_sock)) < 0) {
381 WARNING("Could not create VNET server socket, error: %d\n", err);
385 memset(&vnet_brg_s.serv_addr, 0, sizeof(struct sockaddr));
387 vnet_brg_s.serv_addr.sin_family = AF_INET;
388 vnet_brg_s.serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
389 vnet_brg_s.serv_addr.sin_port = htons(VNET_SERVER_PORT);
391 if ((err = vnet_brg_s.serv_sock->ops->bind(vnet_brg_s.serv_sock, (struct sockaddr *)&(vnet_brg_s.serv_addr), sizeof(struct sockaddr))) < 0) {
392 WARNING("Could not bind VNET server socket to port %d, error: %d\n", VNET_SERVER_PORT, err);
396 INFO("VNET server bind to port: %d\n", VNET_SERVER_PORT);
398 if(vnet_brg_s.serv_proto == TCP){
399 if((err = vnet_brg_s.serv_sock->ops->listen(vnet_brg_s.serv_sock, 32)) < 0){
400 WARNING("VNET Server error listening on port %d, error %d\n", VNET_SERVER_PORT, err);
408 static int _udp_server(void * arg) {
410 struct sockaddr_in pkt_addr;
411 struct vnet_link * link = NULL;
414 INFO("Palacios VNET Bridge: UDP receiving server ..... \n");
416 pkt = kmalloc(MAX_PACKET_LEN, GFP_KERNEL);
419 while (!kthread_should_stop()) {
421 // This is a NONBLOCKING receive
422 // If we block here, we will never detect that this thread
423 // is being signaled to stop, plus we might go uninterrupted on this core
424 // blocking out access to other threads - leave this NONBLOCKING
425 // unless you know what you are doing
426 len = _udp_recv(vnet_brg_s.serv_sock, &pkt_addr, pkt, MAX_PACKET_LEN, 1);
429 // If it would have blocked, we have no packet, and so
430 // we will give other threads on this core a chance
431 if (len==-EAGAIN || len==-EWOULDBLOCK || len==-EINTR) {
432 palacios_yield_cpu_timed(VNET_YIELD_TIME_USEC);
437 WARNING("Receive error: Could not get packet, error %d\n", len);
441 link = _link_by_ip(pkt_addr.sin_addr.s_addr);
444 WARNING("VNET Server: No VNET Link matches the src IP\n");
445 vnet_brg_s.stats.pkt_drop_phy ++;
449 vnet_brg_s.stats.pkt_from_phy ++;
450 link->stats.rx_bytes += len;
451 link->stats.rx_pkts ++;
453 send_to_palacios(pkt, len, link->idx);
456 INFO("VNET Server: UDP thread exiting\n");
464 static int _rx_server(void * arg) {
466 if(vnet_brg_s.serv_proto == UDP){
468 }else if(vnet_brg_s.serv_proto == TCP) {
469 //accept new connection
470 //use select to receive pkt from physical network
471 //or create new kthread to handle each connection?
472 WARNING("VNET Server: TCP is not currently supported\n");
475 WARNING ("VNET Server: Unsupported Protocol\n");
482 static inline unsigned int hash_fn(addr_t hdr_ptr) {
483 return vnet_hash_buffer((uint8_t *)hdr_ptr, sizeof(uint32_t));
486 static inline int hash_eq(addr_t key1, addr_t key2) {
487 return (memcmp((uint8_t *)key1, (uint8_t *)key2, sizeof(uint32_t)) == 0);
491 int vnet_bridge_init(void) {
492 struct v3_vnet_bridge_ops bridge_ops;
494 if(vnet_brg_s.status != 0) {
497 vnet_brg_s.status = 1;
499 memset(&vnet_brg_s, 0, sizeof(struct vnet_brg_state));
501 INIT_LIST_HEAD(&(vnet_brg_s.link_list));
502 spin_lock_init(&(vnet_brg_s.lock));
504 vnet_brg_s.serv_proto = UDP;
506 vnet_brg_s.ip2link = vnet_create_htable(10, hash_fn, hash_eq);
507 if(vnet_brg_s.ip2link == NULL){
508 WARNING("Failure to initiate VNET link hashtable\n");
512 if(init_vnet_serv() < 0){
513 WARNING("Failure to initiate VNET server\n");
517 vnet_brg_s.serv_thread = kthread_run(_rx_server, NULL, "vnet_brgd");
519 bridge_ops.input = bridge_send_pkt;
520 bridge_ops.poll = NULL;
522 if( v3_vnet_add_bridge(NULL, &bridge_ops, HOST_LNX_BRIDGE, NULL) < 0){
523 WARNING("VNET LNX Bridge: Fails to register bridge to VNET core");
526 INFO("VNET Linux Bridge initiated\n");
532 void vnet_bridge_deinit(void){
534 INFO("VNET LNX Bridge Deinit Started\n");
536 v3_vnet_del_bridge(HOST_LNX_BRIDGE);
538 //DEBUG("Stopping bridge service thread\n");
540 kthread_stop(vnet_brg_s.serv_thread);
542 //DEBUG("Releasing bridee service socket\n");
544 vnet_brg_s.serv_sock->ops->release(vnet_brg_s.serv_sock);
546 //DEBUG("Deiniting bridge links\n");
550 //DEBUG("Freeing bridge hash tables\n");
552 vnet_free_htable(vnet_brg_s.ip2link, 0, 0);
554 vnet_brg_s.status = 0;
556 INFO("VNET LNX Bridge Deinit Finished\n");