]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/switchdev/switchdev.c
switchdev: introduce get/set attrs ops
[karo-tx-linux.git] / net / switchdev / switchdev.c
1 /*
2  * net/switchdev/switchdev.c - Switch device API
3  * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
4  * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/mutex.h>
16 #include <linux/notifier.h>
17 #include <linux/netdevice.h>
18 #include <net/ip_fib.h>
19 #include <net/switchdev.h>
20
21 /**
22  *      switchdev_parent_id_get - Get ID of a switch
23  *      @dev: port device
24  *      @psid: switch ID
25  *
26  *      Get ID of a switch this port is part of.
27  */
28 int switchdev_parent_id_get(struct net_device *dev,
29                             struct netdev_phys_item_id *psid)
30 {
31         const struct switchdev_ops *ops = dev->switchdev_ops;
32
33         if (!ops || !ops->switchdev_parent_id_get)
34                 return -EOPNOTSUPP;
35         return ops->switchdev_parent_id_get(dev, psid);
36 }
37 EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
38
39 /**
40  *      switchdev_port_attr_get - Get port attribute
41  *
42  *      @dev: port device
43  *      @attr: attribute to get
44  */
45 int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
46 {
47         const struct switchdev_ops *ops = dev->switchdev_ops;
48         struct net_device *lower_dev;
49         struct list_head *iter;
50         struct switchdev_attr first = {
51                 .id = SWITCHDEV_ATTR_UNDEFINED
52         };
53         int err = -EOPNOTSUPP;
54
55         if (ops && ops->switchdev_port_attr_get)
56                 return ops->switchdev_port_attr_get(dev, attr);
57
58         if (attr->flags & SWITCHDEV_F_NO_RECURSE)
59                 return err;
60
61         /* Switch device port(s) may be stacked under
62          * bond/team/vlan dev, so recurse down to get attr on
63          * each port.  Return -ENODATA if attr values don't
64          * compare across ports.
65          */
66
67         netdev_for_each_lower_dev(dev, lower_dev, iter) {
68                 err = switchdev_port_attr_get(lower_dev, attr);
69                 if (err)
70                         break;
71                 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
72                         first = *attr;
73                 else if (memcmp(&first, attr, sizeof(*attr)))
74                         return -ENODATA;
75         }
76
77         return err;
78 }
79 EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
80
81 static int __switchdev_port_attr_set(struct net_device *dev,
82                                      struct switchdev_attr *attr)
83 {
84         const struct switchdev_ops *ops = dev->switchdev_ops;
85         struct net_device *lower_dev;
86         struct list_head *iter;
87         int err = -EOPNOTSUPP;
88
89         if (ops && ops->switchdev_port_attr_set)
90                 return ops->switchdev_port_attr_set(dev, attr);
91
92         if (attr->flags & SWITCHDEV_F_NO_RECURSE)
93                 return err;
94
95         /* Switch device port(s) may be stacked under
96          * bond/team/vlan dev, so recurse down to set attr on
97          * each port.
98          */
99
100         netdev_for_each_lower_dev(dev, lower_dev, iter) {
101                 err = __switchdev_port_attr_set(lower_dev, attr);
102                 if (err)
103                         break;
104         }
105
106         return err;
107 }
108
109 struct switchdev_attr_set_work {
110         struct work_struct work;
111         struct net_device *dev;
112         struct switchdev_attr attr;
113 };
114
115 static void switchdev_port_attr_set_work(struct work_struct *work)
116 {
117         struct switchdev_attr_set_work *asw =
118                 container_of(work, struct switchdev_attr_set_work, work);
119         int err;
120
121         rtnl_lock();
122         err = switchdev_port_attr_set(asw->dev, &asw->attr);
123         BUG_ON(err);
124         rtnl_unlock();
125
126         dev_put(asw->dev);
127         kfree(work);
128 }
129
130 static int switchdev_port_attr_set_defer(struct net_device *dev,
131                                          struct switchdev_attr *attr)
132 {
133         struct switchdev_attr_set_work *asw;
134
135         asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
136         if (!asw)
137                 return -ENOMEM;
138
139         INIT_WORK(&asw->work, switchdev_port_attr_set_work);
140
141         dev_hold(dev);
142         asw->dev = dev;
143         memcpy(&asw->attr, attr, sizeof(asw->attr));
144
145         schedule_work(&asw->work);
146
147         return 0;
148 }
149
150 /**
151  *      switchdev_port_attr_set - Set port attribute
152  *
153  *      @dev: port device
154  *      @attr: attribute to set
155  *
156  *      Use a 2-phase prepare-commit transaction model to ensure
157  *      system is not left in a partially updated state due to
158  *      failure from driver/device.
159  */
160 int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
161 {
162         int err;
163
164         if (!rtnl_is_locked()) {
165                 /* Running prepare-commit transaction across stacked
166                  * devices requires nothing moves, so if rtnl_lock is
167                  * not held, schedule a worker thread to hold rtnl_lock
168                  * while setting attr.
169                  */
170
171                 return switchdev_port_attr_set_defer(dev, attr);
172         }
173
174         /* Phase I: prepare for attr set. Driver/device should fail
175          * here if there are going to be issues in the commit phase,
176          * such as lack of resources or support.  The driver/device
177          * should reserve resources needed for the commit phase here,
178          * but should not commit the attr.
179          */
180
181         attr->trans = SWITCHDEV_TRANS_PREPARE;
182         err = __switchdev_port_attr_set(dev, attr);
183         if (err) {
184                 /* Prepare phase failed: abort the transaction.  Any
185                  * resources reserved in the prepare phase are
186                  * released.
187                  */
188
189                 attr->trans = SWITCHDEV_TRANS_ABORT;
190                 __switchdev_port_attr_set(dev, attr);
191
192                 return err;
193         }
194
195         /* Phase II: commit attr set.  This cannot fail as a fault
196          * of driver/device.  If it does, it's a bug in the driver/device
197          * because the driver said everythings was OK in phase I.
198          */
199
200         attr->trans = SWITCHDEV_TRANS_COMMIT;
201         err = __switchdev_port_attr_set(dev, attr);
202         BUG_ON(err);
203
204         return err;
205 }
206 EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
207
208 /**
209  *      switchdev_port_stp_update - Notify switch device port of STP
210  *                                      state change
211  *      @dev: port device
212  *      @state: port STP state
213  *
214  *      Notify switch device port of bridge port STP state change.
215  */
216 int switchdev_port_stp_update(struct net_device *dev, u8 state)
217 {
218         const struct switchdev_ops *ops = dev->switchdev_ops;
219         struct net_device *lower_dev;
220         struct list_head *iter;
221         int err = -EOPNOTSUPP;
222
223         if (ops && ops->switchdev_port_stp_update)
224                 return ops->switchdev_port_stp_update(dev, state);
225
226         netdev_for_each_lower_dev(dev, lower_dev, iter) {
227                 err = switchdev_port_stp_update(lower_dev, state);
228                 if (err && err != -EOPNOTSUPP)
229                         return err;
230         }
231
232         return err;
233 }
234 EXPORT_SYMBOL_GPL(switchdev_port_stp_update);
235
236 static DEFINE_MUTEX(switchdev_mutex);
237 static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
238
239 /**
240  *      register_switchdev_notifier - Register notifier
241  *      @nb: notifier_block
242  *
243  *      Register switch device notifier. This should be used by code
244  *      which needs to monitor events happening in particular device.
245  *      Return values are same as for atomic_notifier_chain_register().
246  */
247 int register_switchdev_notifier(struct notifier_block *nb)
248 {
249         int err;
250
251         mutex_lock(&switchdev_mutex);
252         err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
253         mutex_unlock(&switchdev_mutex);
254         return err;
255 }
256 EXPORT_SYMBOL_GPL(register_switchdev_notifier);
257
258 /**
259  *      unregister_switchdev_notifier - Unregister notifier
260  *      @nb: notifier_block
261  *
262  *      Unregister switch device notifier.
263  *      Return values are same as for atomic_notifier_chain_unregister().
264  */
265 int unregister_switchdev_notifier(struct notifier_block *nb)
266 {
267         int err;
268
269         mutex_lock(&switchdev_mutex);
270         err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
271         mutex_unlock(&switchdev_mutex);
272         return err;
273 }
274 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
275
276 /**
277  *      call_switchdev_notifiers - Call notifiers
278  *      @val: value passed unmodified to notifier function
279  *      @dev: port device
280  *      @info: notifier information data
281  *
282  *      Call all network notifier blocks. This should be called by driver
283  *      when it needs to propagate hardware event.
284  *      Return values are same as for atomic_notifier_call_chain().
285  */
286 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
287                              struct switchdev_notifier_info *info)
288 {
289         int err;
290
291         info->dev = dev;
292         mutex_lock(&switchdev_mutex);
293         err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
294         mutex_unlock(&switchdev_mutex);
295         return err;
296 }
297 EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
298
299 /**
300  *      switchdev_port_bridge_setlink - Notify switch device port of bridge
301  *      port attributes
302  *
303  *      @dev: port device
304  *      @nlh: netlink msg with bridge port attributes
305  *      @flags: bridge setlink flags
306  *
307  *      Notify switch device port of bridge port attributes
308  */
309 int switchdev_port_bridge_setlink(struct net_device *dev,
310                                   struct nlmsghdr *nlh, u16 flags)
311 {
312         const struct net_device_ops *ops = dev->netdev_ops;
313
314         if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
315                 return 0;
316
317         if (!ops->ndo_bridge_setlink)
318                 return -EOPNOTSUPP;
319
320         return ops->ndo_bridge_setlink(dev, nlh, flags);
321 }
322 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
323
324 /**
325  *      switchdev_port_bridge_dellink - Notify switch device port of bridge
326  *      port attribute delete
327  *
328  *      @dev: port device
329  *      @nlh: netlink msg with bridge port attributes
330  *      @flags: bridge setlink flags
331  *
332  *      Notify switch device port of bridge port attribute delete
333  */
334 int switchdev_port_bridge_dellink(struct net_device *dev,
335                                   struct nlmsghdr *nlh, u16 flags)
336 {
337         const struct net_device_ops *ops = dev->netdev_ops;
338
339         if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
340                 return 0;
341
342         if (!ops->ndo_bridge_dellink)
343                 return -EOPNOTSUPP;
344
345         return ops->ndo_bridge_dellink(dev, nlh, flags);
346 }
347 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
348
349 /**
350  *      ndo_dflt_switchdev_port_bridge_setlink - default ndo bridge setlink
351  *                                               op for master devices
352  *
353  *      @dev: port device
354  *      @nlh: netlink msg with bridge port attributes
355  *      @flags: bridge setlink flags
356  *
357  *      Notify master device slaves of bridge port attributes
358  */
359 int ndo_dflt_switchdev_port_bridge_setlink(struct net_device *dev,
360                                            struct nlmsghdr *nlh, u16 flags)
361 {
362         struct net_device *lower_dev;
363         struct list_head *iter;
364         int ret = 0, err = 0;
365
366         if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
367                 return ret;
368
369         netdev_for_each_lower_dev(dev, lower_dev, iter) {
370                 err = switchdev_port_bridge_setlink(lower_dev, nlh, flags);
371                 if (err && err != -EOPNOTSUPP)
372                         ret = err;
373         }
374
375         return ret;
376 }
377 EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_setlink);
378
379 /**
380  *      ndo_dflt_switchdev_port_bridge_dellink - default ndo bridge dellink
381  *                                               op for master devices
382  *
383  *      @dev: port device
384  *      @nlh: netlink msg with bridge port attributes
385  *      @flags: bridge dellink flags
386  *
387  *      Notify master device slaves of bridge port attribute deletes
388  */
389 int ndo_dflt_switchdev_port_bridge_dellink(struct net_device *dev,
390                                            struct nlmsghdr *nlh, u16 flags)
391 {
392         struct net_device *lower_dev;
393         struct list_head *iter;
394         int ret = 0, err = 0;
395
396         if (!(dev->features & NETIF_F_HW_SWITCH_OFFLOAD))
397                 return ret;
398
399         netdev_for_each_lower_dev(dev, lower_dev, iter) {
400                 err = switchdev_port_bridge_dellink(lower_dev, nlh, flags);
401                 if (err && err != -EOPNOTSUPP)
402                         ret = err;
403         }
404
405         return ret;
406 }
407 EXPORT_SYMBOL_GPL(ndo_dflt_switchdev_port_bridge_dellink);
408
409 static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
410 {
411         const struct switchdev_ops *ops = dev->switchdev_ops;
412         struct net_device *lower_dev;
413         struct net_device *port_dev;
414         struct list_head *iter;
415
416         /* Recusively search down until we find a sw port dev.
417          * (A sw port dev supports switchdev_parent_id_get).
418          */
419
420         if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
421             ops && ops->switchdev_parent_id_get)
422                 return dev;
423
424         netdev_for_each_lower_dev(dev, lower_dev, iter) {
425                 port_dev = switchdev_get_lowest_dev(lower_dev);
426                 if (port_dev)
427                         return port_dev;
428         }
429
430         return NULL;
431 }
432
433 static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
434 {
435         struct netdev_phys_item_id psid;
436         struct netdev_phys_item_id prev_psid;
437         struct net_device *dev = NULL;
438         int nhsel;
439
440         /* For this route, all nexthop devs must be on the same switch. */
441
442         for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
443                 const struct fib_nh *nh = &fi->fib_nh[nhsel];
444
445                 if (!nh->nh_dev)
446                         return NULL;
447
448                 dev = switchdev_get_lowest_dev(nh->nh_dev);
449                 if (!dev)
450                         return NULL;
451
452                 if (switchdev_parent_id_get(dev, &psid))
453                         return NULL;
454
455                 if (nhsel > 0) {
456                         if (prev_psid.id_len != psid.id_len)
457                                 return NULL;
458                         if (memcmp(prev_psid.id, psid.id, psid.id_len))
459                                 return NULL;
460                 }
461
462                 prev_psid = psid;
463         }
464
465         return dev;
466 }
467
468 /**
469  *      switchdev_fib_ipv4_add - Add IPv4 route entry to switch
470  *
471  *      @dst: route's IPv4 destination address
472  *      @dst_len: destination address length (prefix length)
473  *      @fi: route FIB info structure
474  *      @tos: route TOS
475  *      @type: route type
476  *      @nlflags: netlink flags passed in (NLM_F_*)
477  *      @tb_id: route table ID
478  *
479  *      Add IPv4 route entry to switch device.
480  */
481 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
482                            u8 tos, u8 type, u32 nlflags, u32 tb_id)
483 {
484         struct net_device *dev;
485         const struct switchdev_ops *ops;
486         int err = 0;
487
488         /* Don't offload route if using custom ip rules or if
489          * IPv4 FIB offloading has been disabled completely.
490          */
491
492 #ifdef CONFIG_IP_MULTIPLE_TABLES
493         if (fi->fib_net->ipv4.fib_has_custom_rules)
494                 return 0;
495 #endif
496
497         if (fi->fib_net->ipv4.fib_offload_disabled)
498                 return 0;
499
500         dev = switchdev_get_dev_by_nhs(fi);
501         if (!dev)
502                 return 0;
503         ops = dev->switchdev_ops;
504
505         if (ops->switchdev_fib_ipv4_add) {
506                 err = ops->switchdev_fib_ipv4_add(dev, htonl(dst), dst_len,
507                                                   fi, tos, type, nlflags,
508                                                   tb_id);
509                 if (!err)
510                         fi->fib_flags |= RTNH_F_EXTERNAL;
511         }
512
513         return err;
514 }
515 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
516
517 /**
518  *      switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
519  *
520  *      @dst: route's IPv4 destination address
521  *      @dst_len: destination address length (prefix length)
522  *      @fi: route FIB info structure
523  *      @tos: route TOS
524  *      @type: route type
525  *      @tb_id: route table ID
526  *
527  *      Delete IPv4 route entry from switch device.
528  */
529 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
530                            u8 tos, u8 type, u32 tb_id)
531 {
532         struct net_device *dev;
533         const struct switchdev_ops *ops;
534         int err = 0;
535
536         if (!(fi->fib_flags & RTNH_F_EXTERNAL))
537                 return 0;
538
539         dev = switchdev_get_dev_by_nhs(fi);
540         if (!dev)
541                 return 0;
542         ops = dev->switchdev_ops;
543
544         if (ops->switchdev_fib_ipv4_del) {
545                 err = ops->switchdev_fib_ipv4_del(dev, htonl(dst), dst_len,
546                                                   fi, tos, type, tb_id);
547                 if (!err)
548                         fi->fib_flags &= ~RTNH_F_EXTERNAL;
549         }
550
551         return err;
552 }
553 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
554
555 /**
556  *      switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
557  *
558  *      @fi: route FIB info structure
559  */
560 void switchdev_fib_ipv4_abort(struct fib_info *fi)
561 {
562         /* There was a problem installing this route to the offload
563          * device.  For now, until we come up with more refined
564          * policy handling, abruptly end IPv4 fib offloading for
565          * for entire net by flushing offload device(s) of all
566          * IPv4 routes, and mark IPv4 fib offloading broken from
567          * this point forward.
568          */
569
570         fib_flush_external(fi->fib_net);
571         fi->fib_net->ipv4.fib_offload_disabled = true;
572 }
573 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);