aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/net/switchdev.h43
-rw-r--r--net/switchdev/switchdev.c169
2 files changed, 212 insertions, 0 deletions
diff --git a/include/net/switchdev.h b/include/net/switchdev.h
index 97b556daeef7..282043844bcf 100644
--- a/include/net/switchdev.h
+++ b/include/net/switchdev.h
@@ -14,6 +14,25 @@
14#include <linux/netdevice.h> 14#include <linux/netdevice.h>
15#include <linux/notifier.h> 15#include <linux/notifier.h>
16 16
17#define SWITCHDEV_F_NO_RECURSE BIT(0)
18
19enum switchdev_trans {
20 SWITCHDEV_TRANS_NONE,
21 SWITCHDEV_TRANS_PREPARE,
22 SWITCHDEV_TRANS_ABORT,
23 SWITCHDEV_TRANS_COMMIT,
24};
25
26enum switchdev_attr_id {
27 SWITCHDEV_ATTR_UNDEFINED,
28};
29
30struct switchdev_attr {
31 enum switchdev_attr_id id;
32 enum switchdev_trans trans;
33 u32 flags;
34};
35
17struct fib_info; 36struct fib_info;
18 37
19/** 38/**
@@ -23,6 +42,10 @@ struct fib_info;
23 * is part of. If driver implements this, it indicates that it 42 * is part of. If driver implements this, it indicates that it
24 * represents a port of a switch chip. 43 * represents a port of a switch chip.
25 * 44 *
45 * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
46 *
47 * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
48 *
26 * @switchdev_port_stp_update: Called to notify switch device port of bridge 49 * @switchdev_port_stp_update: Called to notify switch device port of bridge
27 * port STP state change. 50 * port STP state change.
28 * 51 *
@@ -33,6 +56,10 @@ struct fib_info;
33struct switchdev_ops { 56struct switchdev_ops {
34 int (*switchdev_parent_id_get)(struct net_device *dev, 57 int (*switchdev_parent_id_get)(struct net_device *dev,
35 struct netdev_phys_item_id *psid); 58 struct netdev_phys_item_id *psid);
59 int (*switchdev_port_attr_get)(struct net_device *dev,
60 struct switchdev_attr *attr);
61 int (*switchdev_port_attr_set)(struct net_device *dev,
62 struct switchdev_attr *attr);
36 int (*switchdev_port_stp_update)(struct net_device *dev, u8 state); 63 int (*switchdev_port_stp_update)(struct net_device *dev, u8 state);
37 int (*switchdev_fib_ipv4_add)(struct net_device *dev, __be32 dst, 64 int (*switchdev_fib_ipv4_add)(struct net_device *dev, __be32 dst,
38 int dst_len, struct fib_info *fi, 65 int dst_len, struct fib_info *fi,
@@ -68,6 +95,10 @@ switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
68 95
69int switchdev_parent_id_get(struct net_device *dev, 96int switchdev_parent_id_get(struct net_device *dev,
70 struct netdev_phys_item_id *psid); 97 struct netdev_phys_item_id *psid);
98int switchdev_port_attr_get(struct net_device *dev,
99 struct switchdev_attr *attr);
100int switchdev_port_attr_set(struct net_device *dev,
101 struct switchdev_attr *attr);
71int switchdev_port_stp_update(struct net_device *dev, u8 state); 102int switchdev_port_stp_update(struct net_device *dev, u8 state);
72int register_switchdev_notifier(struct notifier_block *nb); 103int register_switchdev_notifier(struct notifier_block *nb);
73int unregister_switchdev_notifier(struct notifier_block *nb); 104int unregister_switchdev_notifier(struct notifier_block *nb);
@@ -95,6 +126,18 @@ static inline int switchdev_parent_id_get(struct net_device *dev,
95 return -EOPNOTSUPP; 126 return -EOPNOTSUPP;
96} 127}
97 128
129static inline int switchdev_port_attr_get(struct net_device *dev,
130 struct switchdev_attr *attr)
131{
132 return -EOPNOTSUPP;
133}
134
135static inline int switchdev_port_attr_set(struct net_device *dev,
136 struct switchdev_attr *attr)
137{
138 return -EOPNOTSUPP;
139}
140
98static inline int switchdev_port_stp_update(struct net_device *dev, 141static inline int switchdev_port_stp_update(struct net_device *dev,
99 u8 state) 142 u8 state)
100{ 143{
diff --git a/net/switchdev/switchdev.c b/net/switchdev/switchdev.c
index b7f44a23def5..8f47187dc185 100644
--- a/net/switchdev/switchdev.c
+++ b/net/switchdev/switchdev.c
@@ -37,6 +37,175 @@ int switchdev_parent_id_get(struct net_device *dev,
37EXPORT_SYMBOL_GPL(switchdev_parent_id_get); 37EXPORT_SYMBOL_GPL(switchdev_parent_id_get);
38 38
39/** 39/**
40 * switchdev_port_attr_get - Get port attribute
41 *
42 * @dev: port device
43 * @attr: attribute to get
44 */
45int 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}
79EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
80
81static 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
109struct switchdev_attr_set_work {
110 struct work_struct work;
111 struct net_device *dev;
112 struct switchdev_attr attr;
113};
114
115static 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
130static 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 */
160int 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}
206EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
207
208/**
40 * switchdev_port_stp_update - Notify switch device port of STP 209 * switchdev_port_stp_update - Notify switch device port of STP
41 * state change 210 * state change
42 * @dev: port device 211 * @dev: port device