netdev: Clean up and refactor packet receive interface.
[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                                const struct shash *, 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                     const struct shash *args,
72                     struct netdev_dev **netdev_devp)
73 {
74     static unsigned int n = 0xaa550000;
75     struct netdev_dev_dummy *netdev_dev;
76
77     netdev_dev = xzalloc(sizeof *netdev_dev);
78     netdev_dev_init(&netdev_dev->netdev_dev, name, args, class);
79     netdev_dev->hwaddr[0] = 0xaa;
80     netdev_dev->hwaddr[1] = 0x55;
81     netdev_dev->hwaddr[2] = n >> 24;
82     netdev_dev->hwaddr[3] = n >> 16;
83     netdev_dev->hwaddr[4] = n >> 8;
84     netdev_dev->hwaddr[5] = n;
85     netdev_dev->mtu = 1500;
86     netdev_dev->flags = 0;
87     netdev_dev->change_seq = 1;
88
89     n++;
90
91     *netdev_devp = &netdev_dev->netdev_dev;
92
93     return 0;
94 }
95
96 static void
97 netdev_dummy_destroy(struct netdev_dev *netdev_dev_)
98 {
99     struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
100
101     free(netdev_dev);
102 }
103
104 static int
105 netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
106 {
107     struct netdev_dummy *netdev;
108
109     netdev = xmalloc(sizeof *netdev);
110     netdev_init(&netdev->netdev, netdev_dev_);
111
112     *netdevp = &netdev->netdev;
113     return 0;
114 }
115
116 static void
117 netdev_dummy_close(struct netdev *netdev_)
118 {
119     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
120     free(netdev);
121 }
122
123 static int
124 netdev_dummy_listen(struct netdev *netdev_ OVS_UNUSED)
125 {
126     /* It's OK to listen on a dummy device.  It just never receives any
127      * packets. */
128     return 0;
129 }
130
131 static int
132 netdev_dummy_recv(struct netdev *netdev_ OVS_UNUSED,
133                   void *buffer OVS_UNUSED, size_t size OVS_UNUSED)
134 {
135     /* A dummy device never receives any packets. */
136     return -EAGAIN;
137 }
138
139 static int
140 netdev_dummy_set_etheraddr(struct netdev *netdev,
141                            const uint8_t mac[ETH_ADDR_LEN])
142 {
143     struct netdev_dev_dummy *dev =
144         netdev_dev_dummy_cast(netdev_get_dev(netdev));
145
146     if (!eth_addr_equals(dev->hwaddr, mac)) {
147         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
148         netdev_dummy_poll_notify(netdev);
149     }
150
151     return 0;
152 }
153
154 static int
155 netdev_dummy_get_etheraddr(const struct netdev *netdev,
156                            uint8_t mac[ETH_ADDR_LEN])
157 {
158     const struct netdev_dev_dummy *dev =
159         netdev_dev_dummy_cast(netdev_get_dev(netdev));
160
161     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
162     return 0;
163 }
164
165 static int
166 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
167 {
168     const struct netdev_dev_dummy *dev =
169         netdev_dev_dummy_cast(netdev_get_dev(netdev));
170
171     *mtup = dev->mtu;
172     return 0;
173 }
174
175 static int
176 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
177 {
178     const struct netdev_dev_dummy *dev =
179         netdev_dev_dummy_cast(netdev_get_dev(netdev));
180
181     *stats = dev->stats;
182     return 0;
183 }
184
185 static int
186 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
187 {
188     struct netdev_dev_dummy *dev =
189         netdev_dev_dummy_cast(netdev_get_dev(netdev));
190
191     dev->stats = *stats;
192     return 0;
193 }
194
195 static int
196 netdev_dummy_update_flags(struct netdev *netdev,
197                           enum netdev_flags off, enum netdev_flags on,
198                           enum netdev_flags *old_flagsp)
199 {
200     struct netdev_dev_dummy *dev =
201         netdev_dev_dummy_cast(netdev_get_dev(netdev));
202
203     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
204         return EINVAL;
205     }
206
207     *old_flagsp = dev->flags;
208     dev->flags |= on;
209     dev->flags &= ~off;
210     if (*old_flagsp != dev->flags) {
211         netdev_dummy_poll_notify(netdev);
212     }
213     return 0;
214 }
215
216 static unsigned int
217 netdev_dummy_change_seq(const struct netdev *netdev)
218 {
219     return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
220 }
221 \f
222 /* Helper functions. */
223
224 static void
225 netdev_dummy_poll_notify(const struct netdev *netdev)
226 {
227     struct netdev_dev_dummy *dev =
228         netdev_dev_dummy_cast(netdev_get_dev(netdev));
229
230     dev->change_seq++;
231     if (!dev->change_seq) {
232         dev->change_seq++;
233     }
234 }
235
236 static const struct netdev_class dummy_class = {
237     "dummy",
238     NULL,                       /* init */
239     NULL,                       /* run */
240     NULL,                       /* wait */
241
242     netdev_dummy_create,
243     netdev_dummy_destroy,
244     NULL,                       /* set_config */
245     NULL,                       /* config_equal */
246
247     netdev_dummy_open,
248     netdev_dummy_close,
249
250     NULL,                       /* enumerate */
251
252     netdev_dummy_listen,        /* listen */
253     netdev_dummy_recv,          /* recv */
254     NULL,                       /* recv_wait */
255     NULL,                       /* drain */
256
257     NULL,                       /* send */
258     NULL,                       /* send_wait */
259
260     netdev_dummy_set_etheraddr,
261     netdev_dummy_get_etheraddr,
262     netdev_dummy_get_mtu,
263     NULL,                       /* get_ifindex */
264     NULL,                       /* get_carrier */
265     NULL,                       /* get_miimon */
266     netdev_dummy_get_stats,
267     netdev_dummy_set_stats,
268
269     NULL,                       /* get_features */
270     NULL,                       /* set_advertisements */
271     NULL,                       /* get_vlan_vid */
272
273     NULL,                       /* set_policing */
274     NULL,                       /* get_qos_types */
275     NULL,                       /* get_qos_capabilities */
276     NULL,                       /* get_qos */
277     NULL,                       /* set_qos */
278     NULL,                       /* get_queue */
279     NULL,                       /* set_queue */
280     NULL,                       /* delete_queue */
281     NULL,                       /* get_queue_stats */
282     NULL,                       /* dump_queues */
283     NULL,                       /* dump_queue_stats */
284
285     NULL,                       /* get_in4 */
286     NULL,                       /* set_in4 */
287     NULL,                       /* get_in6 */
288     NULL,                       /* add_router */
289     NULL,                       /* get_next_hop */
290     NULL,                       /* get_status */
291     NULL,                       /* arp_lookup */
292
293     netdev_dummy_update_flags,
294
295     netdev_dummy_change_seq
296 };
297
298 void
299 netdev_dummy_register(void)
300 {
301     netdev_register_provider(&dummy_class);
302 }