netdev: Decouple creating and configuring network devices.
[sliver-openvswitch.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dummy.h"
20
21 #include <errno.h>
22
23 #include "list.h"
24 #include "netdev-provider.h"
25 #include "packets.h"
26 #include "shash.h"
27 #include "vlog.h"
28
29 VLOG_DEFINE_THIS_MODULE(netdev_dummy);
30
31 struct netdev_dev_dummy {
32     struct netdev_dev netdev_dev;
33     uint8_t hwaddr[ETH_ADDR_LEN];
34     int mtu;
35     struct netdev_stats stats;
36     enum netdev_flags flags;
37     unsigned int change_seq;
38 };
39
40 struct netdev_dummy {
41     struct netdev netdev;
42 };
43
44 static int netdev_dummy_create(const struct netdev_class *, const char *,
45                                struct netdev_dev **);
46 static void netdev_dummy_poll_notify(const struct netdev *);
47
48 static bool
49 is_dummy_class(const struct netdev_class *class)
50 {
51     return class->create == netdev_dummy_create;
52 }
53
54 static struct netdev_dev_dummy *
55 netdev_dev_dummy_cast(const struct netdev_dev *netdev_dev)
56 {
57     assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
58     return CONTAINER_OF(netdev_dev, struct netdev_dev_dummy, netdev_dev);
59 }
60
61 static struct netdev_dummy *
62 netdev_dummy_cast(const struct netdev *netdev)
63 {
64     struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
65     assert(is_dummy_class(netdev_dev_get_class(netdev_dev)));
66     return CONTAINER_OF(netdev, struct netdev_dummy, netdev);
67 }
68
69 static int
70 netdev_dummy_create(const struct netdev_class *class, const char *name,
71                     struct netdev_dev **netdev_devp)
72 {
73     static unsigned int n = 0xaa550000;
74     struct netdev_dev_dummy *netdev_dev;
75
76     netdev_dev = xzalloc(sizeof *netdev_dev);
77     netdev_dev_init(&netdev_dev->netdev_dev, name, class);
78     netdev_dev->hwaddr[0] = 0xaa;
79     netdev_dev->hwaddr[1] = 0x55;
80     netdev_dev->hwaddr[2] = n >> 24;
81     netdev_dev->hwaddr[3] = n >> 16;
82     netdev_dev->hwaddr[4] = n >> 8;
83     netdev_dev->hwaddr[5] = n;
84     netdev_dev->mtu = 1500;
85     netdev_dev->flags = 0;
86     netdev_dev->change_seq = 1;
87
88     n++;
89
90     *netdev_devp = &netdev_dev->netdev_dev;
91
92     return 0;
93 }
94
95 static void
96 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
97 {
98     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
99
100     free(netdev_dev);
101 }
102
103 static int
104 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
105 {
106     struct netdev_dummy *netdev;
107
108     netdev = xmalloc(sizeof *netdev);
109     netdev_init(&netdev->netdev, netdev_dev_);
110
111     *netdevp = &netdev->netdev;
112     return 0;
113 }
114
115 static void
116 netdev_dummy_close(struct netdev *netdev_)
117 {
118     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
119     free(netdev);
120 }
121
122 static int
123 netdev_dummy_listen(struct netdev *netdev_ OVS_UNUSED)
124 {
125     /* It's OK to listen on a dummy device.  It just never receives any
126      * packets. */
127     return 0;
128 }
129
130 static int
131 netdev_dummy_recv(struct netdev *netdev_ OVS_UNUSED,
132                   void *buffer OVS_UNUSED, size_t size OVS_UNUSED)
133 {
134     /* A dummy device never receives any packets. */
135     return -EAGAIN;
136 }
137
138 static int
139 netdev_dummy_set_etheraddr(struct netdev *netdev,
140                            const uint8_t mac[ETH_ADDR_LEN])
141 {
142     struct netdev_dev_dummy *dev =
143         netdev_dev_dummy_cast(netdev_get_dev(netdev));
144
145     if (!eth_addr_equals(dev->hwaddr, mac)) {
146         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
147         netdev_dummy_poll_notify(netdev);
148     }
149
150     return 0;
151 }
152
153 static int
154 netdev_dummy_get_etheraddr(const struct netdev *netdev,
155                            uint8_t mac[ETH_ADDR_LEN])
156 {
157     const struct netdev_dev_dummy *dev =
158         netdev_dev_dummy_cast(netdev_get_dev(netdev));
159
160     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
161     return 0;
162 }
163
164 static int
165 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
166 {
167     const struct netdev_dev_dummy *dev =
168         netdev_dev_dummy_cast(netdev_get_dev(netdev));
169
170     *mtup = dev->mtu;
171     return 0;
172 }
173
174 static int
175 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
176 {
177     const struct netdev_dev_dummy *dev =
178         netdev_dev_dummy_cast(netdev_get_dev(netdev));
179
180     *stats = dev->stats;
181     return 0;
182 }
183
184 static int
185 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
186 {
187     struct netdev_dev_dummy *dev =
188         netdev_dev_dummy_cast(netdev_get_dev(netdev));
189
190     dev->stats = *stats;
191     return 0;
192 }
193
194 static int
195 netdev_dummy_update_flags(struct netdev *netdev,
196                           enum netdev_flags off, enum netdev_flags on,
197                           enum netdev_flags *old_flagsp)
198 {
199     struct netdev_dev_dummy *dev =
200         netdev_dev_dummy_cast(netdev_get_dev(netdev));
201
202     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
203         return EINVAL;
204     }
205
206     *old_flagsp = dev->flags;
207     dev->flags |= on;
208     dev->flags &= ~off;
209     if (*old_flagsp != dev->flags) {
210         netdev_dummy_poll_notify(netdev);
211     }
212     return 0;
213 }
214
215 static unsigned int
216 netdev_dummy_change_seq(const struct netdev *netdev)
217 {
218     return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
219 }
220 \f
221 /* Helper functions. */
222
223 static void
224 netdev_dummy_poll_notify(const struct netdev *netdev)
225 {
226     struct netdev_dev_dummy *dev =
227         netdev_dev_dummy_cast(netdev_get_dev(netdev));
228
229     dev->change_seq++;
230     if (!dev->change_seq) {
231         dev->change_seq++;
232     }
233 }
234
235 static const struct netdev_class dummy_class = {
236     "dummy",
237     NULL,                       /* init */
238     NULL,                       /* run */
239     NULL,                       /* wait */
240
241     netdev_dummy_create,
242     netdev_dummy_destroy,
243     NULL,                       /* get_config */
244     NULL,                       /* set_config */
245
246     netdev_dummy_open,
247     netdev_dummy_close,
248
249     NULL,                       /* enumerate */
250
251     netdev_dummy_listen,        /* listen */
252     netdev_dummy_recv,          /* recv */
253     NULL,                       /* recv_wait */
254     NULL,                       /* drain */
255
256     NULL,                       /* send */
257     NULL,                       /* send_wait */
258
259     netdev_dummy_set_etheraddr,
260     netdev_dummy_get_etheraddr,
261     netdev_dummy_get_mtu,
262     NULL,                       /* get_ifindex */
263     NULL,                       /* get_carrier */
264     NULL,                       /* get_miimon */
265     netdev_dummy_get_stats,
266     netdev_dummy_set_stats,
267
268     NULL,                       /* get_features */
269     NULL,                       /* set_advertisements */
270     NULL,                       /* get_vlan_vid */
271
272     NULL,                       /* set_policing */
273     NULL,                       /* get_qos_types */
274     NULL,                       /* get_qos_capabilities */
275     NULL,                       /* get_qos */
276     NULL,                       /* set_qos */
277     NULL,                       /* get_queue */
278     NULL,                       /* set_queue */
279     NULL,                       /* delete_queue */
280     NULL,                       /* get_queue_stats */
281     NULL,                       /* dump_queues */
282     NULL,                       /* dump_queue_stats */
283
284     NULL,                       /* get_in4 */
285     NULL,                       /* set_in4 */
286     NULL,                       /* get_in6 */
287     NULL,                       /* add_router */
288     NULL,                       /* get_next_hop */
289     NULL,                       /* get_status */
290     NULL,                       /* arp_lookup */
291
292     netdev_dummy_update_flags,
293
294     netdev_dummy_change_seq
295 };
296
297 void
298 netdev_dummy_register(void)
299 {
300     netdev_register_provider(&dummy_class);
301 }