netdev: Remove monitors and notifiers.
[sliver-openvswitch.git] / lib / netdev-dummy.c
1 /*
2  * Copyright (c) 2010 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_, int ethertype OVS_UNUSED,
106                   struct netdev **netdevp)
107 {
108     struct netdev_dummy *netdev;
109
110     netdev = xmalloc(sizeof *netdev);
111     netdev_init(&netdev->netdev, netdev_dev_);
112
113     *netdevp = &netdev->netdev;
114     return 0;
115 }
116
117 static void
118 netdev_dummy_close(struct netdev *netdev_)
119 {
120     struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
121     free(netdev);
122 }
123
124 static int
125 netdev_dummy_set_etheraddr(struct netdev *netdev,
126                            const uint8_t mac[ETH_ADDR_LEN])
127 {
128     struct netdev_dev_dummy *dev =
129         netdev_dev_dummy_cast(netdev_get_dev(netdev));
130
131     if (!eth_addr_equals(dev->hwaddr, mac)) {
132         memcpy(dev->hwaddr, mac, ETH_ADDR_LEN);
133         netdev_dummy_poll_notify(netdev);
134     }
135
136     return 0;
137 }
138
139 static int
140 netdev_dummy_get_etheraddr(const struct netdev *netdev,
141                            uint8_t mac[ETH_ADDR_LEN])
142 {
143     const struct netdev_dev_dummy *dev =
144         netdev_dev_dummy_cast(netdev_get_dev(netdev));
145
146     memcpy(mac, dev->hwaddr, ETH_ADDR_LEN);
147     return 0;
148 }
149
150 static int
151 netdev_dummy_get_mtu(const struct netdev *netdev, int *mtup)
152 {
153     const struct netdev_dev_dummy *dev =
154         netdev_dev_dummy_cast(netdev_get_dev(netdev));
155
156     *mtup = dev->mtu;
157     return 0;
158 }
159
160 static int
161 netdev_dummy_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
162 {
163     const struct netdev_dev_dummy *dev =
164         netdev_dev_dummy_cast(netdev_get_dev(netdev));
165
166     *stats = dev->stats;
167     return 0;
168 }
169
170 static int
171 netdev_dummy_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
172 {
173     struct netdev_dev_dummy *dev =
174         netdev_dev_dummy_cast(netdev_get_dev(netdev));
175
176     dev->stats = *stats;
177     return 0;
178 }
179
180 static int
181 netdev_dummy_update_flags(struct netdev *netdev,
182                           enum netdev_flags off, enum netdev_flags on,
183                           enum netdev_flags *old_flagsp)
184 {
185     struct netdev_dev_dummy *dev =
186         netdev_dev_dummy_cast(netdev_get_dev(netdev));
187
188     if ((off | on) & ~(NETDEV_UP | NETDEV_PROMISC)) {
189         return EINVAL;
190     }
191
192     *old_flagsp = dev->flags;
193     dev->flags |= on;
194     dev->flags &= ~off;
195     if (*old_flagsp != dev->flags) {
196         netdev_dummy_poll_notify(netdev);
197     }
198     return 0;
199 }
200
201 static unsigned int
202 netdev_dummy_change_seq(const struct netdev *netdev)
203 {
204     return netdev_dev_dummy_cast(netdev_get_dev(netdev))->change_seq;
205 }
206 \f
207 /* Helper functions. */
208
209 static void
210 netdev_dummy_poll_notify(const struct netdev *netdev)
211 {
212     struct netdev_dev_dummy *dev =
213         netdev_dev_dummy_cast(netdev_get_dev(netdev));
214
215     dev->change_seq++;
216     if (!dev->change_seq) {
217         dev->change_seq++;
218     }
219 }
220
221 static const struct netdev_class dummy_class = {
222     "dummy",
223     NULL,                       /* init */
224     NULL,                       /* run */
225     NULL,                       /* wait */
226
227     netdev_dummy_create,
228     netdev_dummy_destroy,
229     NULL,
230
231     netdev_dummy_open,
232     netdev_dummy_close,
233
234     NULL,                       /* enumerate */
235
236     NULL,                       /* recv */
237     NULL,                       /* recv_wait */
238     NULL,                       /* drain */
239
240     NULL,                       /* send */
241     NULL,                       /* send_wait */
242
243     netdev_dummy_set_etheraddr,
244     netdev_dummy_get_etheraddr,
245     netdev_dummy_get_mtu,
246     NULL,                       /* get_ifindex */
247     NULL,                       /* get_carrier */
248     NULL,                       /* get_miimon */
249     netdev_dummy_get_stats,
250     netdev_dummy_set_stats,
251
252     NULL,                       /* get_features */
253     NULL,                       /* set_advertisements */
254     NULL,                       /* get_vlan_vid */
255
256     NULL,                       /* set_policing */
257     NULL,                       /* get_qos_types */
258     NULL,                       /* get_qos_capabilities */
259     NULL,                       /* get_qos */
260     NULL,                       /* set_qos */
261     NULL,                       /* get_queue */
262     NULL,                       /* set_queue */
263     NULL,                       /* delete_queue */
264     NULL,                       /* get_queue_stats */
265     NULL,                       /* dump_queues */
266     NULL,                       /* dump_queue_stats */
267
268     NULL,                       /* get_in4 */
269     NULL,                       /* set_in4 */
270     NULL,                       /* get_in6 */
271     NULL,                       /* add_router */
272     NULL,                       /* get_next_hop */
273     NULL,                       /* get_status */
274     NULL,                       /* arp_lookup */
275
276     netdev_dummy_update_flags,
277
278     netdev_dummy_change_seq
279 };
280
281 void
282 netdev_dummy_register(void)
283 {
284     netdev_register_provider(&dummy_class);
285 }