-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpico_dev_vde.c
113 lines (93 loc) · 3.02 KB
/
pico_dev_vde.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*********************************************************************
PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
See LICENSE and COPYING for usage.
Authors: Daniele Lacamera
*********************************************************************/
#ifndef UNIT_TEST
#include <libvdeplug.h>
#endif
#include "pico_device.h"
#include "pico_dev_vde.h"
#include "pico_stack.h"
#include <sys/poll.h>
struct pico_device_vde {
struct pico_device dev;
char *sock;
VDECONN *conn;
uint32_t counter_in;
uint32_t counter_out;
uint32_t lost_in;
uint32_t lost_out;
};
#define VDE_MTU 65536
static int pico_vde_send(struct pico_device *dev, void *buf, int len)
{
struct pico_device_vde *vde = (struct pico_device_vde *) dev;
//printf("[%s] send %d bytes.\n", dev->name, len);
if ((vde->lost_out == 0) || ((pico_rand() % 100) > vde->lost_out))
return (int)vde_send(vde->conn, buf, (uint32_t)len, 0);
else
return len; /* Silently discarded "on the wire" */
}
static int pico_vde_poll(struct pico_device *dev, int loop_score)
{
struct pico_device_vde *vde = (struct pico_device_vde *) dev;
struct pollfd pfd;
unsigned char buf[VDE_MTU];
int len;
pfd.fd = vde_datafd(vde->conn);
pfd.events = POLLIN;
do {
if (poll(&pfd, 1, 0) <= 0)
return loop_score;
len = vde_recv(vde->conn, buf, VDE_MTU, 0);
if (len > 0) {
//printf("Received pkt.\n");
if ((vde->lost_in == 0) || ((pico_rand() % 100) > vde->lost_in)) {
loop_score--;
pico_stack_recv(dev, buf, (uint32_t)len);
}
}
} while(loop_score > 0);
return 0;
}
/* Public interface: create/destroy. */
void pico_vde_destroy(struct pico_device *dev)
{
struct pico_device_vde *vde = (struct pico_device_vde *) dev;
vde_close(vde->conn);
}
void pico_vde_set_packetloss(struct pico_device *dev, uint32_t in_pct, uint32_t out_pct)
{
struct pico_device_vde *vde = (struct pico_device_vde *) dev;
vde->lost_in = in_pct;
vde->lost_out = out_pct;
}
struct pico_device *pico_vde_create(char *sock, char *name, uint8_t *mac)
{
struct pico_device_vde *vde = PICO_ZALLOC(sizeof(struct pico_device_vde));
struct vde_open_args open_args = {
.mode = 0700
};
char vdename[] = "picotcp";
if (!vde)
return NULL;
if( 0 != pico_device_init((struct pico_device *)vde, name, mac)) {
dbg ("Vde init failed.\n");
pico_vde_destroy((struct pico_device *)vde);
return NULL;
}
vde->dev.overhead = 0;
vde->sock = PICO_ZALLOC(strlen(sock) + 1);
memcpy(vde->sock, sock, strlen(sock));
vde->conn = vde_open(sock, vdename, &open_args);
if (!vde->conn) {
pico_vde_destroy((struct pico_device *)vde);
return NULL;
}
vde->dev.send = pico_vde_send;
vde->dev.poll = pico_vde_poll;
vde->dev.destroy = pico_vde_destroy;
//printf("Device %s created.\n", vde->dev.name);
return (struct pico_device *)vde;
}