2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
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.
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 <linux/if_bridge.h>
19 #include <linux/list.h>
20 #include <linux/workqueue.h>
21 #include <net/ip_fib.h>
22 #include <net/switchdev.h>
25 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
28 * @data: pointer to data being queued
29 * @destructor: data destructor
30 * @tritem: transaction item being queued
32 * Enqeueue data item to transaction queue. tritem is typically placed in
33 * cointainter pointed at by data pointer. Destructor is called on
34 * transaction abort and after successful commit phase in case
35 * the caller did not dequeue the item before.
37 void switchdev_trans_item_enqueue(struct switchdev_trans
*trans
,
38 void *data
, void (*destructor
)(void const *),
39 struct switchdev_trans_item
*tritem
)
42 tritem
->destructor
= destructor
;
43 list_add_tail(&tritem
->list
, &trans
->item_list
);
45 EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue
);
47 static struct switchdev_trans_item
*
48 __switchdev_trans_item_dequeue(struct switchdev_trans
*trans
)
50 struct switchdev_trans_item
*tritem
;
52 if (list_empty(&trans
->item_list
))
54 tritem
= list_first_entry(&trans
->item_list
,
55 struct switchdev_trans_item
, list
);
56 list_del(&tritem
->list
);
61 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
65 void *switchdev_trans_item_dequeue(struct switchdev_trans
*trans
)
67 struct switchdev_trans_item
*tritem
;
69 tritem
= __switchdev_trans_item_dequeue(trans
);
73 EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue
);
75 static void switchdev_trans_init(struct switchdev_trans
*trans
)
77 INIT_LIST_HEAD(&trans
->item_list
);
80 static void switchdev_trans_items_destroy(struct switchdev_trans
*trans
)
82 struct switchdev_trans_item
*tritem
;
84 while ((tritem
= __switchdev_trans_item_dequeue(trans
)))
85 tritem
->destructor(tritem
->data
);
88 static void switchdev_trans_items_warn_destroy(struct net_device
*dev
,
89 struct switchdev_trans
*trans
)
91 WARN(!list_empty(&trans
->item_list
), "%s: transaction item queue is not empty.\n",
93 switchdev_trans_items_destroy(trans
);
96 static LIST_HEAD(deferred
);
97 static DEFINE_SPINLOCK(deferred_lock
);
99 typedef void switchdev_deferred_func_t(struct net_device
*dev
,
102 struct switchdev_deferred_item
{
103 struct list_head list
;
104 struct net_device
*dev
;
105 switchdev_deferred_func_t
*func
;
106 unsigned long data
[0];
109 static struct switchdev_deferred_item
*switchdev_deferred_dequeue(void)
111 struct switchdev_deferred_item
*dfitem
;
113 spin_lock_bh(&deferred_lock
);
114 if (list_empty(&deferred
)) {
118 dfitem
= list_first_entry(&deferred
,
119 struct switchdev_deferred_item
, list
);
120 list_del(&dfitem
->list
);
122 spin_unlock_bh(&deferred_lock
);
127 * switchdev_deferred_process - Process ops in deferred queue
129 * Called to flush the ops currently queued in deferred ops queue.
130 * rtnl_lock must be held.
132 void switchdev_deferred_process(void)
134 struct switchdev_deferred_item
*dfitem
;
138 while ((dfitem
= switchdev_deferred_dequeue())) {
139 dfitem
->func(dfitem
->dev
, dfitem
->data
);
140 dev_put(dfitem
->dev
);
144 EXPORT_SYMBOL_GPL(switchdev_deferred_process
);
146 static void switchdev_deferred_process_work(struct work_struct
*work
)
149 switchdev_deferred_process();
153 static DECLARE_WORK(deferred_process_work
, switchdev_deferred_process_work
);
155 static int switchdev_deferred_enqueue(struct net_device
*dev
,
156 const void *data
, size_t data_len
,
157 switchdev_deferred_func_t
*func
)
159 struct switchdev_deferred_item
*dfitem
;
161 dfitem
= kmalloc(sizeof(*dfitem
) + data_len
, GFP_ATOMIC
);
166 memcpy(dfitem
->data
, data
, data_len
);
168 spin_lock_bh(&deferred_lock
);
169 list_add_tail(&dfitem
->list
, &deferred
);
170 spin_unlock_bh(&deferred_lock
);
171 schedule_work(&deferred_process_work
);
176 * switchdev_port_attr_get - Get port attribute
179 * @attr: attribute to get
181 int switchdev_port_attr_get(struct net_device
*dev
, struct switchdev_attr
*attr
)
183 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
184 struct net_device
*lower_dev
;
185 struct list_head
*iter
;
186 struct switchdev_attr first
= {
187 .id
= SWITCHDEV_ATTR_ID_UNDEFINED
189 int err
= -EOPNOTSUPP
;
191 if (ops
&& ops
->switchdev_port_attr_get
)
192 return ops
->switchdev_port_attr_get(dev
, attr
);
194 if (attr
->flags
& SWITCHDEV_F_NO_RECURSE
)
197 /* Switch device port(s) may be stacked under
198 * bond/team/vlan dev, so recurse down to get attr on
199 * each port. Return -ENODATA if attr values don't
200 * compare across ports.
203 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
204 err
= switchdev_port_attr_get(lower_dev
, attr
);
207 if (first
.id
== SWITCHDEV_ATTR_ID_UNDEFINED
)
209 else if (memcmp(&first
, attr
, sizeof(*attr
)))
215 EXPORT_SYMBOL_GPL(switchdev_port_attr_get
);
217 static int __switchdev_port_attr_set(struct net_device
*dev
,
218 const struct switchdev_attr
*attr
,
219 struct switchdev_trans
*trans
)
221 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
222 struct net_device
*lower_dev
;
223 struct list_head
*iter
;
224 int err
= -EOPNOTSUPP
;
226 if (ops
&& ops
->switchdev_port_attr_set
)
227 return ops
->switchdev_port_attr_set(dev
, attr
, trans
);
229 if (attr
->flags
& SWITCHDEV_F_NO_RECURSE
)
232 /* Switch device port(s) may be stacked under
233 * bond/team/vlan dev, so recurse down to set attr on
237 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
238 err
= __switchdev_port_attr_set(lower_dev
, attr
, trans
);
239 if (err
== -EOPNOTSUPP
&&
240 attr
->flags
& SWITCHDEV_F_SKIP_EOPNOTSUPP
)
247 if (err
== -EOPNOTSUPP
&& attr
->flags
& SWITCHDEV_F_SKIP_EOPNOTSUPP
)
253 static int switchdev_port_attr_set_now(struct net_device
*dev
,
254 const struct switchdev_attr
*attr
)
256 struct switchdev_trans trans
;
259 switchdev_trans_init(&trans
);
261 /* Phase I: prepare for attr set. Driver/device should fail
262 * here if there are going to be issues in the commit phase,
263 * such as lack of resources or support. The driver/device
264 * should reserve resources needed for the commit phase here,
265 * but should not commit the attr.
268 trans
.ph_prepare
= true;
269 err
= __switchdev_port_attr_set(dev
, attr
, &trans
);
271 /* Prepare phase failed: abort the transaction. Any
272 * resources reserved in the prepare phase are
276 if (err
!= -EOPNOTSUPP
)
277 switchdev_trans_items_destroy(&trans
);
282 /* Phase II: commit attr set. This cannot fail as a fault
283 * of driver/device. If it does, it's a bug in the driver/device
284 * because the driver said everythings was OK in phase I.
287 trans
.ph_prepare
= false;
288 err
= __switchdev_port_attr_set(dev
, attr
, &trans
);
289 WARN(err
, "%s: Commit of attribute (id=%d) failed.\n",
290 dev
->name
, attr
->id
);
291 switchdev_trans_items_warn_destroy(dev
, &trans
);
296 static void switchdev_port_attr_set_deferred(struct net_device
*dev
,
299 const struct switchdev_attr
*attr
= data
;
302 err
= switchdev_port_attr_set_now(dev
, attr
);
303 if (err
&& err
!= -EOPNOTSUPP
)
304 netdev_err(dev
, "failed (err=%d) to set attribute (id=%d)\n",
308 static int switchdev_port_attr_set_defer(struct net_device
*dev
,
309 const struct switchdev_attr
*attr
)
311 return switchdev_deferred_enqueue(dev
, attr
, sizeof(*attr
),
312 switchdev_port_attr_set_deferred
);
316 * switchdev_port_attr_set - Set port attribute
319 * @attr: attribute to set
321 * Use a 2-phase prepare-commit transaction model to ensure
322 * system is not left in a partially updated state due to
323 * failure from driver/device.
325 * rtnl_lock must be held and must not be in atomic section,
326 * in case SWITCHDEV_F_DEFER flag is not set.
328 int switchdev_port_attr_set(struct net_device
*dev
,
329 const struct switchdev_attr
*attr
)
331 if (attr
->flags
& SWITCHDEV_F_DEFER
)
332 return switchdev_port_attr_set_defer(dev
, attr
);
334 return switchdev_port_attr_set_now(dev
, attr
);
336 EXPORT_SYMBOL_GPL(switchdev_port_attr_set
);
338 static int __switchdev_port_obj_add(struct net_device
*dev
,
339 const struct switchdev_obj
*obj
,
340 struct switchdev_trans
*trans
)
342 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
343 struct net_device
*lower_dev
;
344 struct list_head
*iter
;
345 int err
= -EOPNOTSUPP
;
347 if (ops
&& ops
->switchdev_port_obj_add
)
348 return ops
->switchdev_port_obj_add(dev
, obj
, trans
);
350 /* Switch device port(s) may be stacked under
351 * bond/team/vlan dev, so recurse down to add object on
355 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
356 err
= __switchdev_port_obj_add(lower_dev
, obj
, trans
);
365 * switchdev_port_obj_add - Add port object
369 * @obj: object to add
371 * Use a 2-phase prepare-commit transaction model to ensure
372 * system is not left in a partially updated state due to
373 * failure from driver/device.
375 * rtnl_lock must be held.
377 int switchdev_port_obj_add(struct net_device
*dev
,
378 const struct switchdev_obj
*obj
)
380 struct switchdev_trans trans
;
385 switchdev_trans_init(&trans
);
387 /* Phase I: prepare for obj add. Driver/device should fail
388 * here if there are going to be issues in the commit phase,
389 * such as lack of resources or support. The driver/device
390 * should reserve resources needed for the commit phase here,
391 * but should not commit the obj.
394 trans
.ph_prepare
= true;
395 err
= __switchdev_port_obj_add(dev
, obj
, &trans
);
397 /* Prepare phase failed: abort the transaction. Any
398 * resources reserved in the prepare phase are
402 if (err
!= -EOPNOTSUPP
)
403 switchdev_trans_items_destroy(&trans
);
408 /* Phase II: commit obj add. This cannot fail as a fault
409 * of driver/device. If it does, it's a bug in the driver/device
410 * because the driver said everythings was OK in phase I.
413 trans
.ph_prepare
= false;
414 err
= __switchdev_port_obj_add(dev
, obj
, &trans
);
415 WARN(err
, "%s: Commit of object (id=%d) failed.\n", dev
->name
, obj
->id
);
416 switchdev_trans_items_warn_destroy(dev
, &trans
);
420 EXPORT_SYMBOL_GPL(switchdev_port_obj_add
);
423 * switchdev_port_obj_del - Delete port object
427 * @obj: object to delete
429 int switchdev_port_obj_del(struct net_device
*dev
,
430 const struct switchdev_obj
*obj
)
432 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
433 struct net_device
*lower_dev
;
434 struct list_head
*iter
;
435 int err
= -EOPNOTSUPP
;
437 if (ops
&& ops
->switchdev_port_obj_del
)
438 return ops
->switchdev_port_obj_del(dev
, obj
);
440 /* Switch device port(s) may be stacked under
441 * bond/team/vlan dev, so recurse down to delete object on
445 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
446 err
= switchdev_port_obj_del(lower_dev
, obj
);
453 EXPORT_SYMBOL_GPL(switchdev_port_obj_del
);
456 * switchdev_port_obj_dump - Dump port objects
460 * @obj: object to dump
461 * @cb: function to call with a filled object
463 int switchdev_port_obj_dump(struct net_device
*dev
, struct switchdev_obj
*obj
,
464 switchdev_obj_dump_cb_t
*cb
)
466 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
467 struct net_device
*lower_dev
;
468 struct list_head
*iter
;
469 int err
= -EOPNOTSUPP
;
471 if (ops
&& ops
->switchdev_port_obj_dump
)
472 return ops
->switchdev_port_obj_dump(dev
, obj
, cb
);
474 /* Switch device port(s) may be stacked under
475 * bond/team/vlan dev, so recurse down to dump objects on
476 * first port at bottom of stack.
479 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
480 err
= switchdev_port_obj_dump(lower_dev
, obj
, cb
);
486 EXPORT_SYMBOL_GPL(switchdev_port_obj_dump
);
488 static DEFINE_MUTEX(switchdev_mutex
);
489 static RAW_NOTIFIER_HEAD(switchdev_notif_chain
);
492 * register_switchdev_notifier - Register notifier
493 * @nb: notifier_block
495 * Register switch device notifier. This should be used by code
496 * which needs to monitor events happening in particular device.
497 * Return values are same as for atomic_notifier_chain_register().
499 int register_switchdev_notifier(struct notifier_block
*nb
)
503 mutex_lock(&switchdev_mutex
);
504 err
= raw_notifier_chain_register(&switchdev_notif_chain
, nb
);
505 mutex_unlock(&switchdev_mutex
);
508 EXPORT_SYMBOL_GPL(register_switchdev_notifier
);
511 * unregister_switchdev_notifier - Unregister notifier
512 * @nb: notifier_block
514 * Unregister switch device notifier.
515 * Return values are same as for atomic_notifier_chain_unregister().
517 int unregister_switchdev_notifier(struct notifier_block
*nb
)
521 mutex_lock(&switchdev_mutex
);
522 err
= raw_notifier_chain_unregister(&switchdev_notif_chain
, nb
);
523 mutex_unlock(&switchdev_mutex
);
526 EXPORT_SYMBOL_GPL(unregister_switchdev_notifier
);
529 * call_switchdev_notifiers - Call notifiers
530 * @val: value passed unmodified to notifier function
532 * @info: notifier information data
534 * Call all network notifier blocks. This should be called by driver
535 * when it needs to propagate hardware event.
536 * Return values are same as for atomic_notifier_call_chain().
538 int call_switchdev_notifiers(unsigned long val
, struct net_device
*dev
,
539 struct switchdev_notifier_info
*info
)
544 mutex_lock(&switchdev_mutex
);
545 err
= raw_notifier_call_chain(&switchdev_notif_chain
, val
, info
);
546 mutex_unlock(&switchdev_mutex
);
549 EXPORT_SYMBOL_GPL(call_switchdev_notifiers
);
551 struct switchdev_vlan_dump
{
552 struct switchdev_obj_port_vlan vlan
;
560 static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump
*dump
)
562 struct bridge_vlan_info vinfo
;
564 vinfo
.flags
= dump
->flags
;
566 if (dump
->begin
== 0 && dump
->end
== 0) {
568 } else if (dump
->begin
== dump
->end
) {
569 vinfo
.vid
= dump
->begin
;
570 if (nla_put(dump
->skb
, IFLA_BRIDGE_VLAN_INFO
,
571 sizeof(vinfo
), &vinfo
))
574 vinfo
.vid
= dump
->begin
;
575 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
576 if (nla_put(dump
->skb
, IFLA_BRIDGE_VLAN_INFO
,
577 sizeof(vinfo
), &vinfo
))
579 vinfo
.vid
= dump
->end
;
580 vinfo
.flags
&= ~BRIDGE_VLAN_INFO_RANGE_BEGIN
;
581 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_END
;
582 if (nla_put(dump
->skb
, IFLA_BRIDGE_VLAN_INFO
,
583 sizeof(vinfo
), &vinfo
))
590 static int switchdev_port_vlan_dump_cb(struct switchdev_obj
*obj
)
592 struct switchdev_obj_port_vlan
*vlan
= SWITCHDEV_OBJ_PORT_VLAN(obj
);
593 struct switchdev_vlan_dump
*dump
=
594 container_of(vlan
, struct switchdev_vlan_dump
, vlan
);
597 if (vlan
->vid_begin
> vlan
->vid_end
)
600 if (dump
->filter_mask
& RTEXT_FILTER_BRVLAN
) {
601 dump
->flags
= vlan
->flags
;
602 for (dump
->begin
= dump
->end
= vlan
->vid_begin
;
603 dump
->begin
<= vlan
->vid_end
;
604 dump
->begin
++, dump
->end
++) {
605 err
= switchdev_port_vlan_dump_put(dump
);
609 } else if (dump
->filter_mask
& RTEXT_FILTER_BRVLAN_COMPRESSED
) {
610 if (dump
->begin
> vlan
->vid_begin
&&
611 dump
->begin
>= vlan
->vid_end
) {
612 if ((dump
->begin
- 1) == vlan
->vid_end
&&
613 dump
->flags
== vlan
->flags
) {
615 dump
->begin
= vlan
->vid_begin
;
617 err
= switchdev_port_vlan_dump_put(dump
);
618 dump
->flags
= vlan
->flags
;
619 dump
->begin
= vlan
->vid_begin
;
620 dump
->end
= vlan
->vid_end
;
622 } else if (dump
->end
<= vlan
->vid_begin
&&
623 dump
->end
< vlan
->vid_end
) {
624 if ((dump
->end
+ 1) == vlan
->vid_begin
&&
625 dump
->flags
== vlan
->flags
) {
627 dump
->end
= vlan
->vid_end
;
629 err
= switchdev_port_vlan_dump_put(dump
);
630 dump
->flags
= vlan
->flags
;
631 dump
->begin
= vlan
->vid_begin
;
632 dump
->end
= vlan
->vid_end
;
642 static int switchdev_port_vlan_fill(struct sk_buff
*skb
, struct net_device
*dev
,
645 struct switchdev_vlan_dump dump
= {
646 .vlan
.obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
648 .filter_mask
= filter_mask
,
652 if ((filter_mask
& RTEXT_FILTER_BRVLAN
) ||
653 (filter_mask
& RTEXT_FILTER_BRVLAN_COMPRESSED
)) {
654 err
= switchdev_port_obj_dump(dev
, &dump
.vlan
.obj
,
655 switchdev_port_vlan_dump_cb
);
658 if (filter_mask
& RTEXT_FILTER_BRVLAN_COMPRESSED
)
660 err
= switchdev_port_vlan_dump_put(&dump
);
664 return err
== -EOPNOTSUPP
? 0 : err
;
668 * switchdev_port_bridge_getlink - Get bridge port attributes
672 * Called for SELF on rtnl_bridge_getlink to get bridge port
675 int switchdev_port_bridge_getlink(struct sk_buff
*skb
, u32 pid
, u32 seq
,
676 struct net_device
*dev
, u32 filter_mask
,
679 struct switchdev_attr attr
= {
680 .id
= SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
,
682 u16 mode
= BRIDGE_MODE_UNDEF
;
683 u32 mask
= BR_LEARNING
| BR_LEARNING_SYNC
;
686 err
= switchdev_port_attr_get(dev
, &attr
);
687 if (err
&& err
!= -EOPNOTSUPP
)
690 return ndo_dflt_bridge_getlink(skb
, pid
, seq
, dev
, mode
,
691 attr
.u
.brport_flags
, mask
, nlflags
,
692 filter_mask
, switchdev_port_vlan_fill
);
694 EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink
);
696 static int switchdev_port_br_setflag(struct net_device
*dev
,
697 struct nlattr
*nlattr
,
698 unsigned long brport_flag
)
700 struct switchdev_attr attr
= {
701 .id
= SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS
,
703 u8 flag
= nla_get_u8(nlattr
);
706 err
= switchdev_port_attr_get(dev
, &attr
);
711 attr
.u
.brport_flags
|= brport_flag
;
713 attr
.u
.brport_flags
&= ~brport_flag
;
715 return switchdev_port_attr_set(dev
, &attr
);
718 static const struct nla_policy
719 switchdev_port_bridge_policy
[IFLA_BRPORT_MAX
+ 1] = {
720 [IFLA_BRPORT_STATE
] = { .type
= NLA_U8
},
721 [IFLA_BRPORT_COST
] = { .type
= NLA_U32
},
722 [IFLA_BRPORT_PRIORITY
] = { .type
= NLA_U16
},
723 [IFLA_BRPORT_MODE
] = { .type
= NLA_U8
},
724 [IFLA_BRPORT_GUARD
] = { .type
= NLA_U8
},
725 [IFLA_BRPORT_PROTECT
] = { .type
= NLA_U8
},
726 [IFLA_BRPORT_FAST_LEAVE
] = { .type
= NLA_U8
},
727 [IFLA_BRPORT_LEARNING
] = { .type
= NLA_U8
},
728 [IFLA_BRPORT_LEARNING_SYNC
] = { .type
= NLA_U8
},
729 [IFLA_BRPORT_UNICAST_FLOOD
] = { .type
= NLA_U8
},
732 static int switchdev_port_br_setlink_protinfo(struct net_device
*dev
,
733 struct nlattr
*protinfo
)
739 err
= nla_validate_nested(protinfo
, IFLA_BRPORT_MAX
,
740 switchdev_port_bridge_policy
);
744 nla_for_each_nested(attr
, protinfo
, rem
) {
745 switch (nla_type(attr
)) {
746 case IFLA_BRPORT_LEARNING
:
747 err
= switchdev_port_br_setflag(dev
, attr
,
750 case IFLA_BRPORT_LEARNING_SYNC
:
751 err
= switchdev_port_br_setflag(dev
, attr
,
765 static int switchdev_port_br_afspec(struct net_device
*dev
,
766 struct nlattr
*afspec
,
767 int (*f
)(struct net_device
*dev
,
768 const struct switchdev_obj
*obj
))
771 struct bridge_vlan_info
*vinfo
;
772 struct switchdev_obj_port_vlan vlan
= {
773 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
778 nla_for_each_nested(attr
, afspec
, rem
) {
779 if (nla_type(attr
) != IFLA_BRIDGE_VLAN_INFO
)
781 if (nla_len(attr
) != sizeof(struct bridge_vlan_info
))
783 vinfo
= nla_data(attr
);
784 vlan
.flags
= vinfo
->flags
;
785 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
788 vlan
.vid_begin
= vinfo
->vid
;
789 /* don't allow range of pvids */
790 if (vlan
.flags
& BRIDGE_VLAN_INFO_PVID
)
792 } else if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
) {
795 vlan
.vid_end
= vinfo
->vid
;
796 if (vlan
.vid_end
<= vlan
.vid_begin
)
798 err
= f(dev
, &vlan
.obj
);
801 memset(&vlan
, 0, sizeof(vlan
));
805 vlan
.vid_begin
= vinfo
->vid
;
806 vlan
.vid_end
= vinfo
->vid
;
807 err
= f(dev
, &vlan
.obj
);
810 memset(&vlan
, 0, sizeof(vlan
));
818 * switchdev_port_bridge_setlink - Set bridge port attributes
821 * @nlh: netlink header
822 * @flags: netlink flags
824 * Called for SELF on rtnl_bridge_setlink to set bridge port
827 int switchdev_port_bridge_setlink(struct net_device
*dev
,
828 struct nlmsghdr
*nlh
, u16 flags
)
830 struct nlattr
*protinfo
;
831 struct nlattr
*afspec
;
834 protinfo
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
),
837 err
= switchdev_port_br_setlink_protinfo(dev
, protinfo
);
842 afspec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
),
845 err
= switchdev_port_br_afspec(dev
, afspec
,
846 switchdev_port_obj_add
);
850 EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink
);
853 * switchdev_port_bridge_dellink - Set bridge port attributes
856 * @nlh: netlink header
857 * @flags: netlink flags
859 * Called for SELF on rtnl_bridge_dellink to set bridge port
862 int switchdev_port_bridge_dellink(struct net_device
*dev
,
863 struct nlmsghdr
*nlh
, u16 flags
)
865 struct nlattr
*afspec
;
867 afspec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
),
870 return switchdev_port_br_afspec(dev
, afspec
,
871 switchdev_port_obj_del
);
875 EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink
);
878 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
880 * @ndmsg: netlink hdr
881 * @nlattr: netlink attributes
883 * @addr: MAC address to add
886 * Add FDB entry to switch device.
888 int switchdev_port_fdb_add(struct ndmsg
*ndm
, struct nlattr
*tb
[],
889 struct net_device
*dev
, const unsigned char *addr
,
890 u16 vid
, u16 nlm_flags
)
892 struct switchdev_obj_port_fdb fdb
= {
893 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_FDB
,
898 return switchdev_port_obj_add(dev
, &fdb
.obj
);
900 EXPORT_SYMBOL_GPL(switchdev_port_fdb_add
);
903 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
905 * @ndmsg: netlink hdr
906 * @nlattr: netlink attributes
908 * @addr: MAC address to delete
909 * @vid: VLAN to delete
911 * Delete FDB entry from switch device.
913 int switchdev_port_fdb_del(struct ndmsg
*ndm
, struct nlattr
*tb
[],
914 struct net_device
*dev
, const unsigned char *addr
,
917 struct switchdev_obj_port_fdb fdb
= {
918 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_FDB
,
923 return switchdev_port_obj_del(dev
, &fdb
.obj
);
925 EXPORT_SYMBOL_GPL(switchdev_port_fdb_del
);
927 struct switchdev_fdb_dump
{
928 struct switchdev_obj_port_fdb fdb
;
929 struct net_device
*dev
;
931 struct netlink_callback
*cb
;
935 static int switchdev_port_fdb_dump_cb(struct switchdev_obj
*obj
)
937 struct switchdev_obj_port_fdb
*fdb
= SWITCHDEV_OBJ_PORT_FDB(obj
);
938 struct switchdev_fdb_dump
*dump
=
939 container_of(fdb
, struct switchdev_fdb_dump
, fdb
);
940 u32 portid
= NETLINK_CB(dump
->cb
->skb
).portid
;
941 u32 seq
= dump
->cb
->nlh
->nlmsg_seq
;
942 struct nlmsghdr
*nlh
;
945 if (dump
->idx
< dump
->cb
->args
[0])
948 nlh
= nlmsg_put(dump
->skb
, portid
, seq
, RTM_NEWNEIGH
,
949 sizeof(*ndm
), NLM_F_MULTI
);
953 ndm
= nlmsg_data(nlh
);
954 ndm
->ndm_family
= AF_BRIDGE
;
957 ndm
->ndm_flags
= NTF_SELF
;
959 ndm
->ndm_ifindex
= dump
->dev
->ifindex
;
960 ndm
->ndm_state
= fdb
->ndm_state
;
962 if (nla_put(dump
->skb
, NDA_LLADDR
, ETH_ALEN
, fdb
->addr
))
963 goto nla_put_failure
;
965 if (fdb
->vid
&& nla_put_u16(dump
->skb
, NDA_VLAN
, fdb
->vid
))
966 goto nla_put_failure
;
968 nlmsg_end(dump
->skb
, nlh
);
975 nlmsg_cancel(dump
->skb
, nlh
);
980 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
983 * @cb: netlink callback
985 * @filter_dev: filter device
988 * Delete FDB entry from switch device.
990 int switchdev_port_fdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
991 struct net_device
*dev
,
992 struct net_device
*filter_dev
, int idx
)
994 struct switchdev_fdb_dump dump
= {
995 .fdb
.obj
.id
= SWITCHDEV_OBJ_ID_PORT_FDB
,
1002 switchdev_port_obj_dump(dev
, &dump
.fdb
.obj
, switchdev_port_fdb_dump_cb
);
1005 EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump
);
1007 static struct net_device
*switchdev_get_lowest_dev(struct net_device
*dev
)
1009 const struct switchdev_ops
*ops
= dev
->switchdev_ops
;
1010 struct net_device
*lower_dev
;
1011 struct net_device
*port_dev
;
1012 struct list_head
*iter
;
1014 /* Recusively search down until we find a sw port dev.
1015 * (A sw port dev supports switchdev_port_attr_get).
1018 if (ops
&& ops
->switchdev_port_attr_get
)
1021 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
1022 port_dev
= switchdev_get_lowest_dev(lower_dev
);
1030 static struct net_device
*switchdev_get_dev_by_nhs(struct fib_info
*fi
)
1032 struct switchdev_attr attr
= {
1033 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1035 struct switchdev_attr prev_attr
;
1036 struct net_device
*dev
= NULL
;
1039 /* For this route, all nexthop devs must be on the same switch. */
1041 for (nhsel
= 0; nhsel
< fi
->fib_nhs
; nhsel
++) {
1042 const struct fib_nh
*nh
= &fi
->fib_nh
[nhsel
];
1047 dev
= switchdev_get_lowest_dev(nh
->nh_dev
);
1051 if (switchdev_port_attr_get(dev
, &attr
))
1055 !netdev_phys_item_id_same(&prev_attr
.u
.ppid
, &attr
.u
.ppid
))
1065 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
1067 * @dst: route's IPv4 destination address
1068 * @dst_len: destination address length (prefix length)
1069 * @fi: route FIB info structure
1072 * @nlflags: netlink flags passed in (NLM_F_*)
1073 * @tb_id: route table ID
1075 * Add/modify switch IPv4 route entry.
1077 int switchdev_fib_ipv4_add(u32 dst
, int dst_len
, struct fib_info
*fi
,
1078 u8 tos
, u8 type
, u32 nlflags
, u32 tb_id
)
1080 struct switchdev_obj_ipv4_fib ipv4_fib
= {
1081 .obj
.id
= SWITCHDEV_OBJ_ID_IPV4_FIB
,
1090 struct net_device
*dev
;
1093 /* Don't offload route if using custom ip rules or if
1094 * IPv4 FIB offloading has been disabled completely.
1097 #ifdef CONFIG_IP_MULTIPLE_TABLES
1098 if (fi
->fib_net
->ipv4
.fib_has_custom_rules
)
1102 if (fi
->fib_net
->ipv4
.fib_offload_disabled
)
1105 dev
= switchdev_get_dev_by_nhs(fi
);
1109 err
= switchdev_port_obj_add(dev
, &ipv4_fib
.obj
);
1111 fi
->fib_flags
|= RTNH_F_OFFLOAD
;
1113 return err
== -EOPNOTSUPP
? 0 : err
;
1115 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add
);
1118 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
1120 * @dst: route's IPv4 destination address
1121 * @dst_len: destination address length (prefix length)
1122 * @fi: route FIB info structure
1125 * @tb_id: route table ID
1127 * Delete IPv4 route entry from switch device.
1129 int switchdev_fib_ipv4_del(u32 dst
, int dst_len
, struct fib_info
*fi
,
1130 u8 tos
, u8 type
, u32 tb_id
)
1132 struct switchdev_obj_ipv4_fib ipv4_fib
= {
1133 .obj
.id
= SWITCHDEV_OBJ_ID_IPV4_FIB
,
1142 struct net_device
*dev
;
1145 if (!(fi
->fib_flags
& RTNH_F_OFFLOAD
))
1148 dev
= switchdev_get_dev_by_nhs(fi
);
1152 err
= switchdev_port_obj_del(dev
, &ipv4_fib
.obj
);
1154 fi
->fib_flags
&= ~RTNH_F_OFFLOAD
;
1156 return err
== -EOPNOTSUPP
? 0 : err
;
1158 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del
);
1161 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
1163 * @fi: route FIB info structure
1165 void switchdev_fib_ipv4_abort(struct fib_info
*fi
)
1167 /* There was a problem installing this route to the offload
1168 * device. For now, until we come up with more refined
1169 * policy handling, abruptly end IPv4 fib offloading for
1170 * for entire net by flushing offload device(s) of all
1171 * IPv4 routes, and mark IPv4 fib offloading broken from
1172 * this point forward.
1175 fib_flush_external(fi
->fib_net
);
1176 fi
->fib_net
->ipv4
.fib_offload_disabled
= true;
1178 EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort
);
1180 static bool switchdev_port_same_parent_id(struct net_device
*a
,
1181 struct net_device
*b
)
1183 struct switchdev_attr a_attr
= {
1184 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1185 .flags
= SWITCHDEV_F_NO_RECURSE
,
1187 struct switchdev_attr b_attr
= {
1188 .id
= SWITCHDEV_ATTR_ID_PORT_PARENT_ID
,
1189 .flags
= SWITCHDEV_F_NO_RECURSE
,
1192 if (switchdev_port_attr_get(a
, &a_attr
) ||
1193 switchdev_port_attr_get(b
, &b_attr
))
1196 return netdev_phys_item_id_same(&a_attr
.u
.ppid
, &b_attr
.u
.ppid
);
1199 static u32
switchdev_port_fwd_mark_get(struct net_device
*dev
,
1200 struct net_device
*group_dev
)
1202 struct net_device
*lower_dev
;
1203 struct list_head
*iter
;
1205 netdev_for_each_lower_dev(group_dev
, lower_dev
, iter
) {
1206 if (lower_dev
== dev
)
1208 if (switchdev_port_same_parent_id(dev
, lower_dev
))
1209 return lower_dev
->offload_fwd_mark
;
1210 return switchdev_port_fwd_mark_get(dev
, lower_dev
);
1213 return dev
->ifindex
;
1216 static void switchdev_port_fwd_mark_reset(struct net_device
*group_dev
,
1217 u32 old_mark
, u32
*reset_mark
)
1219 struct net_device
*lower_dev
;
1220 struct list_head
*iter
;
1222 netdev_for_each_lower_dev(group_dev
, lower_dev
, iter
) {
1223 if (lower_dev
->offload_fwd_mark
== old_mark
) {
1225 *reset_mark
= lower_dev
->ifindex
;
1226 lower_dev
->offload_fwd_mark
= *reset_mark
;
1228 switchdev_port_fwd_mark_reset(lower_dev
, old_mark
, reset_mark
);
1233 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1236 * @group_dev: containing device
1237 * @joining: true if dev is joining group; false if leaving group
1239 * An ungrouped port's offload mark is just its ifindex. A grouped
1240 * port's (member of a bridge, for example) offload mark is the ifindex
1241 * of one of the ports in the group with the same parent (switch) ID.
1242 * Ports on the same device in the same group will have the same mark.
1247 * sw1p1 ifindex=2 mark=2
1248 * sw1p2 ifindex=3 mark=2
1249 * sw2p1 ifindex=4 mark=5
1250 * sw2p2 ifindex=5 mark=5
1252 * If sw2p2 leaves the bridge, we'll have:
1255 * sw1p1 ifindex=2 mark=2
1256 * sw1p2 ifindex=3 mark=2
1257 * sw2p1 ifindex=4 mark=4
1258 * sw2p2 ifindex=5 mark=5
1260 void switchdev_port_fwd_mark_set(struct net_device
*dev
,
1261 struct net_device
*group_dev
,
1264 u32 mark
= dev
->ifindex
;
1267 if (group_dev
&& joining
) {
1268 mark
= switchdev_port_fwd_mark_get(dev
, group_dev
);
1269 } else if (group_dev
&& !joining
) {
1270 if (dev
->offload_fwd_mark
== mark
)
1271 /* Ohoh, this port was the mark reference port,
1272 * but it's leaving the group, so reset the
1273 * mark for the remaining ports in the group.
1275 switchdev_port_fwd_mark_reset(group_dev
, mark
,
1279 dev
->offload_fwd_mark
= mark
;
1281 EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set
);