Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


518fa750d61f1dc9ae641adba50d6f56d5083374
[palacios.git] / palacios / src / devices / nic_bridge.c
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2010, Lei Xia <lxia@northwestern.edu>
11  * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Lei Xia <lxia@northwestern.edu>
15  *               
16  *
17  * This is free software.  You are permitted to use,
18  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
19  */
20 //backend device for Virtio NIC, Direct Network Bridge
21
22 #include <palacios/vmm.h>
23 #include <palacios/vmm_dev_mgr.h>
24 #include <palacios/vm_guest_mem.h>
25 #include <palacios/vmm_sprintf.h>
26 #include <interfaces/vmm_packet.h>
27
28 #ifndef V3_CONFIG_DEBUG_NIC_BRIDGE
29 #undef PrintDebug
30 #define PrintDebug(fmt, args...)
31 #endif
32
33 struct nic_bridge_state {
34     struct v3_vm_info * vm;
35     struct v3_dev_net_ops net_ops;
36 };
37
38 static int bridge_send(uint8_t * buf, uint32_t len, 
39                        int synchronize,
40                        void * private_data) {
41
42 #ifdef V3_CONFIG_DEBUG_NIC_BRIDGE
43     {
44         PrintDebug("NIC Bridge: send pkt size: %d\n", len);
45         v3_hexdump(buf, len, NULL, 0);
46     }
47 #endif
48
49     return V3_send_raw(buf, len);
50 }
51
52 static int packet_input(struct v3_vm_info * vm,
53                         struct v3_packet_event * evt, 
54                         void * private_data) {
55     struct nic_bridge_state * bridge = (struct nic_bridge_state *)private_data;
56  
57 #ifdef V3_CONFIG_DEBUG_NIC_BRIDGE
58     {
59         PrintDebug("NIC Bridge: recv pkt size: %d\n", evt->size);
60         v3_hexdump(evt->pkt, evt->size, NULL, 0);
61     }
62 #endif
63
64     return bridge->net_ops.recv(evt->pkt, 
65                                 evt->size, 
66                                 bridge->net_ops.frontend_data);
67 }
68
69
70 static int nic_bridge_free(struct nic_bridge_state * bridge) {
71
72     /*detach from front device */
73
74     V3_Free(bridge);
75         
76     return 0;
77 }
78
79 static struct v3_device_ops dev_ops = {
80     .free = (int (*)(void *))nic_bridge_free,
81
82 };
83
84 static int nic_bridge_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
85     struct nic_bridge_state * bridge = NULL;
86     char * dev_id = v3_cfg_val(cfg, "ID");
87
88     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
89         
90     bridge = (struct nic_bridge_state *)V3_Malloc(sizeof(struct nic_bridge_state));
91     memset(bridge, 0, sizeof(struct nic_bridge_state));
92
93     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, bridge);
94
95     if (dev == NULL) {
96         PrintError("Could not attach device %s\n", dev_id);
97         V3_Free(bridge);
98         return -1;
99     }
100
101     bridge->net_ops.send = bridge_send;
102     bridge->vm = vm;
103         
104     if (v3_dev_connect_net(vm, v3_cfg_val(frontend_cfg, "tag"), 
105                            &(bridge->net_ops), frontend_cfg, bridge) == -1) {
106         PrintError("Could not connect %s to frontend %s\n", 
107                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
108         v3_remove_device(dev);
109         return -1;
110     }
111
112     PrintDebug("NIC-Bridge: Connect %s to frontend %s\n", 
113               dev_id, v3_cfg_val(frontend_cfg, "tag"));
114
115
116     V3_packet_add_recver(bridge->net_ops.fnt_mac, vm);
117     v3_hook_host_event(vm, HOST_PACKET_EVT, V3_HOST_EVENT_HANDLER(packet_input), bridge);
118
119     return 0;
120 }
121
122 device_register("NIC_BRIDGE", nic_bridge_init)