Commit | Line | Data |
---|---|---|
ad43c356 JD |
1 | /* |
2 | * Copyright (C) 2007 Luca Bigliardi (shammash@artha.org). | |
3 | * Licensed under the GPL. | |
4 | */ | |
5 | ||
cd1ae0e4 | 6 | #include <stddef.h> |
ad43c356 | 7 | #include <errno.h> |
ad43c356 | 8 | #include <libvdeplug.h> |
ad43c356 | 9 | #include "kern_constants.h" |
cd1ae0e4 | 10 | #include "net_user.h" |
ad43c356 | 11 | #include "um_malloc.h" |
cd1ae0e4 | 12 | #include "user.h" |
ad43c356 JD |
13 | #include "vde.h" |
14 | ||
15 | #define MAX_PACKET (ETH_MAX_PACKET + ETH_HEADER_OTHER) | |
16 | ||
17 | static int vde_user_init(void *data, void *dev) | |
18 | { | |
19 | struct vde_data *pri = data; | |
20 | VDECONN *conn = NULL; | |
21 | int err = -EINVAL; | |
22 | ||
23 | pri->dev = dev; | |
24 | ||
25 | conn = vde_open(pri->vde_switch, pri->descr, pri->args); | |
26 | ||
27 | if (conn == NULL) { | |
28 | err = -errno; | |
29 | printk(UM_KERN_ERR "vde_user_init: vde_open failed, " | |
30 | "errno = %d\n", errno); | |
31 | return err; | |
32 | } | |
33 | ||
34 | printk(UM_KERN_INFO "vde backend - connection opened\n"); | |
35 | ||
36 | pri->conn = conn; | |
37 | ||
38 | return 0; | |
39 | } | |
40 | ||
41 | static int vde_user_open(void *data) | |
42 | { | |
43 | struct vde_data *pri = data; | |
44 | ||
45 | if (pri->conn != NULL) | |
46 | return vde_datafd(pri->conn); | |
47 | ||
48 | printk(UM_KERN_WARNING "vde_open - we have no VDECONN to open"); | |
49 | return -EINVAL; | |
50 | } | |
51 | ||
52 | static void vde_remove(void *data) | |
53 | { | |
54 | struct vde_data *pri = data; | |
55 | ||
56 | if (pri->conn != NULL) { | |
57 | printk(UM_KERN_INFO "vde backend - closing connection\n"); | |
58 | vde_close(pri->conn); | |
59 | pri->conn = NULL; | |
60 | kfree(pri->args); | |
61 | pri->args = NULL; | |
62 | return; | |
63 | } | |
64 | ||
65 | printk(UM_KERN_WARNING "vde_remove - we have no VDECONN to remove"); | |
66 | } | |
67 | ||
68 | static int vde_set_mtu(int mtu, void *data) | |
69 | { | |
70 | return mtu; | |
71 | } | |
72 | ||
73 | const struct net_user_info vde_user_info = { | |
74 | .init = vde_user_init, | |
75 | .open = vde_user_open, | |
76 | .close = NULL, | |
77 | .remove = vde_remove, | |
78 | .set_mtu = vde_set_mtu, | |
79 | .add_address = NULL, | |
80 | .delete_address = NULL, | |
81 | .max_packet = MAX_PACKET - ETH_HEADER_OTHER | |
82 | }; | |
83 | ||
84 | void vde_init_libstuff(struct vde_data *vpri, struct vde_init *init) | |
85 | { | |
86 | struct vde_open_args *args; | |
87 | ||
88 | vpri->args = kmalloc(sizeof(struct vde_open_args), UM_GFP_KERNEL); | |
89 | if (vpri->args == NULL) { | |
90 | printk(UM_KERN_ERR "vde_init_libstuff - vde_open_args" | |
91 | "allocation failed"); | |
92 | return; | |
93 | } | |
94 | ||
95 | args = vpri->args; | |
96 | ||
97 | args->port = init->port; | |
98 | args->group = init->group; | |
99 | args->mode = init->mode ? init->mode : 0700; | |
100 | ||
101 | args->port ? printk(UM_KERN_INFO "port %d", args->port) : | |
102 | printk(UM_KERN_INFO "undefined port"); | |
103 | } | |
104 | ||
105 | int vde_user_read(void *conn, void *buf, int len) | |
106 | { | |
107 | VDECONN *vconn = conn; | |
108 | int rv; | |
109 | ||
110 | if (vconn == NULL) | |
111 | return 0; | |
112 | ||
113 | rv = vde_recv(vconn, buf, len, 0); | |
114 | if (rv < 0) { | |
115 | if (errno == EAGAIN) | |
116 | return 0; | |
117 | return -errno; | |
118 | } | |
119 | else if (rv == 0) | |
120 | return -ENOTCONN; | |
121 | ||
122 | return rv; | |
123 | } | |
124 | ||
125 | int vde_user_write(void *conn, void *buf, int len) | |
126 | { | |
127 | VDECONN *vconn = conn; | |
128 | ||
129 | if (vconn == NULL) | |
130 | return 0; | |
131 | ||
132 | return vde_send(vconn, buf, len, 0); | |
133 | } | |
134 |