blob: 02ee926ebde6397293a42fc95f1ec344d09fd829 [file] [log] [blame]
Jiri Pirko007f7902014-11-28 14:34:17 +01001/*
2 * net/switchdev/switchdev.c - Switch device API
Jiri Pirko7ea6eb32015-09-24 10:02:41 +02003 * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
Scott Feldmanf8f21472015-03-09 13:59:09 -07004 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
Jiri Pirko007f7902014-11-28 14:34:17 +01005 *
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>
Jiri Pirko03bf0c22015-01-15 23:49:36 +010015#include <linux/mutex.h>
16#include <linux/notifier.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010017#include <linux/netdevice.h>
Scott Feldman47f83282015-05-10 09:47:56 -070018#include <linux/if_bridge.h>
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020019#include <linux/list.h>
Scott Feldman5e8d9042015-03-05 21:21:15 -080020#include <net/ip_fib.h>
Jiri Pirko007f7902014-11-28 14:34:17 +010021#include <net/switchdev.h>
22
23/**
Jiri Pirko7ea6eb32015-09-24 10:02:41 +020024 * switchdev_trans_item_enqueue - Enqueue data item to transaction queue
25 *
26 * @trans: transaction
27 * @data: pointer to data being queued
28 * @destructor: data destructor
29 * @tritem: transaction item being queued
30 *
31 * Enqeueue data item to transaction queue. tritem is typically placed in
32 * cointainter pointed at by data pointer. Destructor is called on
33 * transaction abort and after successful commit phase in case
34 * the caller did not dequeue the item before.
35 */
36void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
37 void *data, void (*destructor)(void const *),
38 struct switchdev_trans_item *tritem)
39{
40 tritem->data = data;
41 tritem->destructor = destructor;
42 list_add_tail(&tritem->list, &trans->item_list);
43}
44EXPORT_SYMBOL_GPL(switchdev_trans_item_enqueue);
45
46static struct switchdev_trans_item *
47__switchdev_trans_item_dequeue(struct switchdev_trans *trans)
48{
49 struct switchdev_trans_item *tritem;
50
51 if (list_empty(&trans->item_list))
52 return NULL;
53 tritem = list_first_entry(&trans->item_list,
54 struct switchdev_trans_item, list);
55 list_del(&tritem->list);
56 return tritem;
57}
58
59/**
60 * switchdev_trans_item_dequeue - Dequeue data item from transaction queue
61 *
62 * @trans: transaction
63 */
64void *switchdev_trans_item_dequeue(struct switchdev_trans *trans)
65{
66 struct switchdev_trans_item *tritem;
67
68 tritem = __switchdev_trans_item_dequeue(trans);
69 BUG_ON(!tritem);
70 return tritem->data;
71}
72EXPORT_SYMBOL_GPL(switchdev_trans_item_dequeue);
73
74static void switchdev_trans_init(struct switchdev_trans *trans)
75{
76 INIT_LIST_HEAD(&trans->item_list);
77}
78
79static void switchdev_trans_items_destroy(struct switchdev_trans *trans)
80{
81 struct switchdev_trans_item *tritem;
82
83 while ((tritem = __switchdev_trans_item_dequeue(trans)))
84 tritem->destructor(tritem->data);
85}
86
87static void switchdev_trans_items_warn_destroy(struct net_device *dev,
88 struct switchdev_trans *trans)
89{
90 WARN(!list_empty(&trans->item_list), "%s: transaction item queue is not empty.\n",
91 dev->name);
92 switchdev_trans_items_destroy(trans);
93}
94
95/**
Scott Feldman30943332015-05-10 09:47:48 -070096 * switchdev_port_attr_get - Get port attribute
97 *
98 * @dev: port device
99 * @attr: attribute to get
100 */
101int switchdev_port_attr_get(struct net_device *dev, struct switchdev_attr *attr)
102{
103 const struct switchdev_ops *ops = dev->switchdev_ops;
104 struct net_device *lower_dev;
105 struct list_head *iter;
106 struct switchdev_attr first = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200107 .id = SWITCHDEV_ATTR_ID_UNDEFINED
Scott Feldman30943332015-05-10 09:47:48 -0700108 };
109 int err = -EOPNOTSUPP;
110
111 if (ops && ops->switchdev_port_attr_get)
112 return ops->switchdev_port_attr_get(dev, attr);
113
114 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
115 return err;
116
117 /* Switch device port(s) may be stacked under
118 * bond/team/vlan dev, so recurse down to get attr on
119 * each port. Return -ENODATA if attr values don't
120 * compare across ports.
121 */
122
123 netdev_for_each_lower_dev(dev, lower_dev, iter) {
124 err = switchdev_port_attr_get(lower_dev, attr);
125 if (err)
126 break;
Jiri Pirko1f868392015-10-01 11:03:42 +0200127 if (first.id == SWITCHDEV_ATTR_ID_UNDEFINED)
Scott Feldman30943332015-05-10 09:47:48 -0700128 first = *attr;
129 else if (memcmp(&first, attr, sizeof(*attr)))
130 return -ENODATA;
131 }
132
133 return err;
134}
135EXPORT_SYMBOL_GPL(switchdev_port_attr_get);
136
137static int __switchdev_port_attr_set(struct net_device *dev,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200138 struct switchdev_attr *attr,
139 struct switchdev_trans *trans)
Scott Feldman30943332015-05-10 09:47:48 -0700140{
141 const struct switchdev_ops *ops = dev->switchdev_ops;
142 struct net_device *lower_dev;
143 struct list_head *iter;
144 int err = -EOPNOTSUPP;
145
146 if (ops && ops->switchdev_port_attr_set)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200147 return ops->switchdev_port_attr_set(dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700148
149 if (attr->flags & SWITCHDEV_F_NO_RECURSE)
150 return err;
151
152 /* Switch device port(s) may be stacked under
153 * bond/team/vlan dev, so recurse down to set attr on
154 * each port.
155 */
156
157 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200158 err = __switchdev_port_attr_set(lower_dev, attr, trans);
Scott Feldman30943332015-05-10 09:47:48 -0700159 if (err)
160 break;
161 }
162
163 return err;
164}
165
166struct switchdev_attr_set_work {
167 struct work_struct work;
168 struct net_device *dev;
169 struct switchdev_attr attr;
170};
171
172static void switchdev_port_attr_set_work(struct work_struct *work)
173{
174 struct switchdev_attr_set_work *asw =
175 container_of(work, struct switchdev_attr_set_work, work);
176 int err;
177
178 rtnl_lock();
179 err = switchdev_port_attr_set(asw->dev, &asw->attr);
Scott Feldman57225e72015-06-11 08:19:01 -0700180 if (err && err != -EOPNOTSUPP)
181 netdev_err(asw->dev, "failed (err=%d) to set attribute (id=%d)\n",
182 err, asw->attr.id);
Scott Feldman30943332015-05-10 09:47:48 -0700183 rtnl_unlock();
184
185 dev_put(asw->dev);
186 kfree(work);
187}
188
189static int switchdev_port_attr_set_defer(struct net_device *dev,
190 struct switchdev_attr *attr)
191{
192 struct switchdev_attr_set_work *asw;
193
194 asw = kmalloc(sizeof(*asw), GFP_ATOMIC);
195 if (!asw)
196 return -ENOMEM;
197
198 INIT_WORK(&asw->work, switchdev_port_attr_set_work);
199
200 dev_hold(dev);
201 asw->dev = dev;
202 memcpy(&asw->attr, attr, sizeof(asw->attr));
203
204 schedule_work(&asw->work);
205
206 return 0;
207}
208
209/**
210 * switchdev_port_attr_set - Set port attribute
211 *
212 * @dev: port device
213 * @attr: attribute to set
214 *
215 * Use a 2-phase prepare-commit transaction model to ensure
216 * system is not left in a partially updated state due to
217 * failure from driver/device.
218 */
219int switchdev_port_attr_set(struct net_device *dev, struct switchdev_attr *attr)
220{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200221 struct switchdev_trans trans;
Scott Feldman30943332015-05-10 09:47:48 -0700222 int err;
223
224 if (!rtnl_is_locked()) {
225 /* Running prepare-commit transaction across stacked
226 * devices requires nothing moves, so if rtnl_lock is
227 * not held, schedule a worker thread to hold rtnl_lock
228 * while setting attr.
229 */
230
231 return switchdev_port_attr_set_defer(dev, attr);
232 }
233
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200234 switchdev_trans_init(&trans);
235
Scott Feldman30943332015-05-10 09:47:48 -0700236 /* Phase I: prepare for attr set. Driver/device should fail
237 * here if there are going to be issues in the commit phase,
238 * such as lack of resources or support. The driver/device
239 * should reserve resources needed for the commit phase here,
240 * but should not commit the attr.
241 */
242
Jiri Pirkof623ab72015-09-24 10:02:49 +0200243 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200244 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700245 if (err) {
246 /* Prepare phase failed: abort the transaction. Any
247 * resources reserved in the prepare phase are
248 * released.
249 */
250
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200251 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200252 switchdev_trans_items_destroy(&trans);
Scott Feldman30943332015-05-10 09:47:48 -0700253
254 return err;
255 }
256
257 /* Phase II: commit attr set. This cannot fail as a fault
258 * of driver/device. If it does, it's a bug in the driver/device
259 * because the driver said everythings was OK in phase I.
260 */
261
Jiri Pirkof623ab72015-09-24 10:02:49 +0200262 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200263 err = __switchdev_port_attr_set(dev, attr, &trans);
Scott Feldmane9fdaec2015-06-11 11:20:42 -0700264 WARN(err, "%s: Commit of attribute (id=%d) failed.\n",
265 dev->name, attr->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200266 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman30943332015-05-10 09:47:48 -0700267
268 return err;
269}
270EXPORT_SYMBOL_GPL(switchdev_port_attr_set);
271
Scott Feldman22c1f672015-05-12 23:03:51 -0700272static int __switchdev_port_obj_add(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400273 enum switchdev_obj_id id, const void *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200274 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700275{
276 const struct switchdev_ops *ops = dev->switchdev_ops;
277 struct net_device *lower_dev;
278 struct list_head *iter;
279 int err = -EOPNOTSUPP;
280
281 if (ops && ops->switchdev_port_obj_add)
Vivien Didelotab069002015-09-29 12:07:17 -0400282 return ops->switchdev_port_obj_add(dev, id, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700283
284 /* Switch device port(s) may be stacked under
285 * bond/team/vlan dev, so recurse down to add object on
286 * each port.
287 */
288
289 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelotab069002015-09-29 12:07:17 -0400290 err = __switchdev_port_obj_add(lower_dev, id, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700291 if (err)
292 break;
293 }
294
295 return err;
296}
297
298/**
299 * switchdev_port_obj_add - Add port object
300 *
301 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400302 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700303 * @obj: object to add
304 *
305 * Use a 2-phase prepare-commit transaction model to ensure
306 * system is not left in a partially updated state due to
307 * failure from driver/device.
308 *
309 * rtnl_lock must be held.
310 */
Vivien Didelotab069002015-09-29 12:07:17 -0400311int switchdev_port_obj_add(struct net_device *dev, enum switchdev_obj_id id,
312 const void *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700313{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200314 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700315 int err;
316
317 ASSERT_RTNL();
318
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200319 switchdev_trans_init(&trans);
320
Scott Feldman491d0f12015-05-10 09:47:52 -0700321 /* Phase I: prepare for obj add. Driver/device should fail
322 * here if there are going to be issues in the commit phase,
323 * such as lack of resources or support. The driver/device
324 * should reserve resources needed for the commit phase here,
325 * but should not commit the obj.
326 */
327
Jiri Pirkof623ab72015-09-24 10:02:49 +0200328 trans.ph_prepare = true;
Vivien Didelotab069002015-09-29 12:07:17 -0400329 err = __switchdev_port_obj_add(dev, id, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700330 if (err) {
331 /* Prepare phase failed: abort the transaction. Any
332 * resources reserved in the prepare phase are
333 * released.
334 */
335
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200336 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200337 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700338
339 return err;
340 }
341
342 /* Phase II: commit obj add. This cannot fail as a fault
343 * of driver/device. If it does, it's a bug in the driver/device
344 * because the driver said everythings was OK in phase I.
345 */
346
Jiri Pirkof623ab72015-09-24 10:02:49 +0200347 trans.ph_prepare = false;
Vivien Didelotab069002015-09-29 12:07:17 -0400348 err = __switchdev_port_obj_add(dev, id, obj, &trans);
349 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200350 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700351
352 return err;
353}
354EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
355
356/**
357 * switchdev_port_obj_del - Delete port object
358 *
359 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400360 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700361 * @obj: object to delete
362 */
Vivien Didelotab069002015-09-29 12:07:17 -0400363int switchdev_port_obj_del(struct net_device *dev, enum switchdev_obj_id id,
364 const void *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700365{
366 const struct switchdev_ops *ops = dev->switchdev_ops;
367 struct net_device *lower_dev;
368 struct list_head *iter;
369 int err = -EOPNOTSUPP;
370
371 if (ops && ops->switchdev_port_obj_del)
Vivien Didelotab069002015-09-29 12:07:17 -0400372 return ops->switchdev_port_obj_del(dev, id, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700373
374 /* Switch device port(s) may be stacked under
375 * bond/team/vlan dev, so recurse down to delete object on
376 * each port.
377 */
378
379 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelotab069002015-09-29 12:07:17 -0400380 err = switchdev_port_obj_del(lower_dev, id, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700381 if (err)
382 break;
383 }
384
385 return err;
386}
387EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
388
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700389/**
390 * switchdev_port_obj_dump - Dump port objects
391 *
392 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400393 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700394 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400395 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700396 */
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400397int switchdev_port_obj_dump(struct net_device *dev, enum switchdev_obj_id id,
398 void *obj, int (*cb)(void *obj))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700399{
400 const struct switchdev_ops *ops = dev->switchdev_ops;
401 struct net_device *lower_dev;
402 struct list_head *iter;
403 int err = -EOPNOTSUPP;
404
405 if (ops && ops->switchdev_port_obj_dump)
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400406 return ops->switchdev_port_obj_dump(dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700407
408 /* Switch device port(s) may be stacked under
409 * bond/team/vlan dev, so recurse down to dump objects on
410 * first port at bottom of stack.
411 */
412
413 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400414 err = switchdev_port_obj_dump(lower_dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700415 break;
416 }
417
418 return err;
419}
420EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
421
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700422static DEFINE_MUTEX(switchdev_mutex);
423static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100424
425/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700426 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100427 * @nb: notifier_block
428 *
429 * Register switch device notifier. This should be used by code
430 * which needs to monitor events happening in particular device.
431 * Return values are same as for atomic_notifier_chain_register().
432 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700433int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100434{
435 int err;
436
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700437 mutex_lock(&switchdev_mutex);
438 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
439 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100440 return err;
441}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700442EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100443
444/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700445 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100446 * @nb: notifier_block
447 *
448 * Unregister switch device notifier.
449 * Return values are same as for atomic_notifier_chain_unregister().
450 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700451int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100452{
453 int err;
454
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700455 mutex_lock(&switchdev_mutex);
456 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
457 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100458 return err;
459}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700460EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100461
462/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700463 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100464 * @val: value passed unmodified to notifier function
465 * @dev: port device
466 * @info: notifier information data
467 *
468 * Call all network notifier blocks. This should be called by driver
469 * when it needs to propagate hardware event.
470 * Return values are same as for atomic_notifier_call_chain().
471 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700472int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
473 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100474{
475 int err;
476
477 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700478 mutex_lock(&switchdev_mutex);
479 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
480 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100481 return err;
482}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700483EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800484
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700485struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200486 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700487 struct sk_buff *skb;
488 u32 filter_mask;
489 u16 flags;
490 u16 begin;
491 u16 end;
492};
493
Vivien Didelote23b0022015-09-29 12:07:13 -0400494static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700495{
496 struct bridge_vlan_info vinfo;
497
498 vinfo.flags = dump->flags;
499
500 if (dump->begin == 0 && dump->end == 0) {
501 return 0;
502 } else if (dump->begin == dump->end) {
503 vinfo.vid = dump->begin;
504 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
505 sizeof(vinfo), &vinfo))
506 return -EMSGSIZE;
507 } else {
508 vinfo.vid = dump->begin;
509 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
510 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
511 sizeof(vinfo), &vinfo))
512 return -EMSGSIZE;
513 vinfo.vid = dump->end;
514 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
515 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
516 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
517 sizeof(vinfo), &vinfo))
518 return -EMSGSIZE;
519 }
520
521 return 0;
522}
523
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400524static int switchdev_port_vlan_dump_cb(void *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700525{
Jiri Pirko8f24f302015-10-01 11:03:43 +0200526 struct switchdev_obj_port_vlan *vlan = obj;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700527 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400528 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700529 int err = 0;
530
531 if (vlan->vid_begin > vlan->vid_end)
532 return -EINVAL;
533
534 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
535 dump->flags = vlan->flags;
536 for (dump->begin = dump->end = vlan->vid_begin;
537 dump->begin <= vlan->vid_end;
538 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400539 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700540 if (err)
541 return err;
542 }
543 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
544 if (dump->begin > vlan->vid_begin &&
545 dump->begin >= vlan->vid_end) {
546 if ((dump->begin - 1) == vlan->vid_end &&
547 dump->flags == vlan->flags) {
548 /* prepend */
549 dump->begin = vlan->vid_begin;
550 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400551 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700552 dump->flags = vlan->flags;
553 dump->begin = vlan->vid_begin;
554 dump->end = vlan->vid_end;
555 }
556 } else if (dump->end <= vlan->vid_begin &&
557 dump->end < vlan->vid_end) {
558 if ((dump->end + 1) == vlan->vid_begin &&
559 dump->flags == vlan->flags) {
560 /* append */
561 dump->end = vlan->vid_end;
562 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400563 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700564 dump->flags = vlan->flags;
565 dump->begin = vlan->vid_begin;
566 dump->end = vlan->vid_end;
567 }
568 } else {
569 err = -EINVAL;
570 }
571 }
572
573 return err;
574}
575
576static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
577 u32 filter_mask)
578{
579 struct switchdev_vlan_dump dump = {
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700580 .skb = skb,
581 .filter_mask = filter_mask,
582 };
583 int err = 0;
584
585 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
586 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200587 err = switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_ID_PORT_VLAN,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400588 &dump.vlan,
589 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700590 if (err)
591 goto err_out;
592 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
593 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400594 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700595 }
596
597err_out:
598 return err == -EOPNOTSUPP ? 0 : err;
599}
600
Scott Feldman8793d0a2015-05-10 09:48:04 -0700601/**
602 * switchdev_port_bridge_getlink - Get bridge port attributes
603 *
604 * @dev: port device
605 *
606 * Called for SELF on rtnl_bridge_getlink to get bridge port
607 * attributes.
608 */
609int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
610 struct net_device *dev, u32 filter_mask,
611 int nlflags)
612{
613 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200614 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700615 };
616 u16 mode = BRIDGE_MODE_UNDEF;
617 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
618 int err;
619
620 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400621 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700622 return err;
623
624 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700625 attr.u.brport_flags, mask, nlflags,
626 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700627}
628EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
629
Scott Feldman47f83282015-05-10 09:47:56 -0700630static int switchdev_port_br_setflag(struct net_device *dev,
631 struct nlattr *nlattr,
632 unsigned long brport_flag)
633{
634 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200635 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700636 };
637 u8 flag = nla_get_u8(nlattr);
638 int err;
639
640 err = switchdev_port_attr_get(dev, &attr);
641 if (err)
642 return err;
643
644 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700645 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700646 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700647 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700648
649 return switchdev_port_attr_set(dev, &attr);
650}
651
652static const struct nla_policy
653switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
654 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
655 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
656 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
657 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
658 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
659 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
660 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
661 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
662 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
663 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
664};
665
666static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
667 struct nlattr *protinfo)
668{
669 struct nlattr *attr;
670 int rem;
671 int err;
672
673 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
674 switchdev_port_bridge_policy);
675 if (err)
676 return err;
677
678 nla_for_each_nested(attr, protinfo, rem) {
679 switch (nla_type(attr)) {
680 case IFLA_BRPORT_LEARNING:
681 err = switchdev_port_br_setflag(dev, attr,
682 BR_LEARNING);
683 break;
684 case IFLA_BRPORT_LEARNING_SYNC:
685 err = switchdev_port_br_setflag(dev, attr,
686 BR_LEARNING_SYNC);
687 break;
688 default:
689 err = -EOPNOTSUPP;
690 break;
691 }
692 if (err)
693 return err;
694 }
695
696 return 0;
697}
698
699static int switchdev_port_br_afspec(struct net_device *dev,
700 struct nlattr *afspec,
701 int (*f)(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400702 enum switchdev_obj_id id,
703 const void *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700704{
705 struct nlattr *attr;
706 struct bridge_vlan_info *vinfo;
Jiri Pirko8f24f302015-10-01 11:03:43 +0200707 struct switchdev_obj_port_vlan vlan = { 0 };
Scott Feldman47f83282015-05-10 09:47:56 -0700708 int rem;
709 int err;
710
711 nla_for_each_nested(attr, afspec, rem) {
712 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
713 continue;
714 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
715 return -EINVAL;
716 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400717 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700718 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400719 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700720 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400721 vlan.vid_begin = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700722 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400723 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700724 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400725 vlan.vid_end = vinfo->vid;
726 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700727 return -EINVAL;
Jiri Pirko57d80832015-10-01 11:03:41 +0200728 err = f(dev, SWITCHDEV_OBJ_ID_PORT_VLAN, &vlan);
Scott Feldman47f83282015-05-10 09:47:56 -0700729 if (err)
730 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400731 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700732 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400733 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700734 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400735 vlan.vid_begin = vinfo->vid;
736 vlan.vid_end = vinfo->vid;
Jiri Pirko57d80832015-10-01 11:03:41 +0200737 err = f(dev, SWITCHDEV_OBJ_ID_PORT_VLAN, &vlan);
Scott Feldman47f83282015-05-10 09:47:56 -0700738 if (err)
739 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400740 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700741 }
742 }
743
744 return 0;
745}
746
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800747/**
Scott Feldman47f83282015-05-10 09:47:56 -0700748 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800749 *
750 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700751 * @nlh: netlink header
752 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800753 *
Scott Feldman47f83282015-05-10 09:47:56 -0700754 * Called for SELF on rtnl_bridge_setlink to set bridge port
755 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800756 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700757int switchdev_port_bridge_setlink(struct net_device *dev,
758 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800759{
Scott Feldman47f83282015-05-10 09:47:56 -0700760 struct nlattr *protinfo;
761 struct nlattr *afspec;
762 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800763
Scott Feldman47f83282015-05-10 09:47:56 -0700764 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
765 IFLA_PROTINFO);
766 if (protinfo) {
767 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
768 if (err)
769 return err;
770 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800771
Scott Feldman47f83282015-05-10 09:47:56 -0700772 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
773 IFLA_AF_SPEC);
774 if (afspec)
775 err = switchdev_port_br_afspec(dev, afspec,
776 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800777
Scott Feldman47f83282015-05-10 09:47:56 -0700778 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800779}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700780EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800781
782/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700783 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800784 *
785 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700786 * @nlh: netlink header
787 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800788 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700789 * Called for SELF on rtnl_bridge_dellink to set bridge port
790 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800791 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700792int switchdev_port_bridge_dellink(struct net_device *dev,
793 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800794{
Scott Feldman5c34e022015-05-10 09:48:00 -0700795 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800796
Scott Feldman5c34e022015-05-10 09:48:00 -0700797 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
798 IFLA_AF_SPEC);
799 if (afspec)
800 return switchdev_port_br_afspec(dev, afspec,
801 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800802
Scott Feldman5c34e022015-05-10 09:48:00 -0700803 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800804}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700805EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800806
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700807/**
808 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
809 *
810 * @ndmsg: netlink hdr
811 * @nlattr: netlink attributes
812 * @dev: port device
813 * @addr: MAC address to add
814 * @vid: VLAN to add
815 *
816 * Add FDB entry to switch device.
817 */
818int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
819 struct net_device *dev, const unsigned char *addr,
820 u16 vid, u16 nlm_flags)
821{
Vivien Didelotab069002015-09-29 12:07:17 -0400822 struct switchdev_obj_fdb fdb = {
823 .addr = addr,
824 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700825 };
826
Jiri Pirko57d80832015-10-01 11:03:41 +0200827 return switchdev_port_obj_add(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700828}
829EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
830
831/**
832 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
833 *
834 * @ndmsg: netlink hdr
835 * @nlattr: netlink attributes
836 * @dev: port device
837 * @addr: MAC address to delete
838 * @vid: VLAN to delete
839 *
840 * Delete FDB entry from switch device.
841 */
842int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
843 struct net_device *dev, const unsigned char *addr,
844 u16 vid)
845{
Vivien Didelotab069002015-09-29 12:07:17 -0400846 struct switchdev_obj_fdb fdb = {
847 .addr = addr,
848 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700849 };
850
Jiri Pirko57d80832015-10-01 11:03:41 +0200851 return switchdev_port_obj_del(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700852}
853EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
854
855struct switchdev_fdb_dump {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400856 struct switchdev_obj_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400857 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700858 struct sk_buff *skb;
859 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700860 int idx;
861};
862
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400863static int switchdev_port_fdb_dump_cb(void *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700864{
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400865 struct switchdev_obj_fdb *fdb = obj;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700866 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400867 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700868 u32 portid = NETLINK_CB(dump->cb->skb).portid;
869 u32 seq = dump->cb->nlh->nlmsg_seq;
870 struct nlmsghdr *nlh;
871 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700872
873 if (dump->idx < dump->cb->args[0])
874 goto skip;
875
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700876 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
877 sizeof(*ndm), NLM_F_MULTI);
878 if (!nlh)
879 return -EMSGSIZE;
880
881 ndm = nlmsg_data(nlh);
882 ndm->ndm_family = AF_BRIDGE;
883 ndm->ndm_pad1 = 0;
884 ndm->ndm_pad2 = 0;
885 ndm->ndm_flags = NTF_SELF;
886 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400887 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400888 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700889
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400890 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700891 goto nla_put_failure;
892
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400893 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700894 goto nla_put_failure;
895
896 nlmsg_end(dump->skb, nlh);
897
898skip:
899 dump->idx++;
900 return 0;
901
902nla_put_failure:
903 nlmsg_cancel(dump->skb, nlh);
904 return -EMSGSIZE;
905}
906
907/**
908 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
909 *
910 * @skb: netlink skb
911 * @cb: netlink callback
912 * @dev: port device
913 * @filter_dev: filter device
914 * @idx:
915 *
916 * Delete FDB entry from switch device.
917 */
918int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
919 struct net_device *dev,
920 struct net_device *filter_dev, int idx)
921{
922 struct switchdev_fdb_dump dump = {
Vivien Didelote02a06b22015-09-29 12:07:14 -0400923 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700924 .skb = skb,
925 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700926 .idx = idx,
927 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700928
Jiri Pirko57d80832015-10-01 11:03:41 +0200929 switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &dump.fdb,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400930 switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700931 return dump.idx;
932}
933EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
934
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700935static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800936{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700937 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800938 struct net_device *lower_dev;
939 struct net_device *port_dev;
940 struct list_head *iter;
941
942 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700943 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800944 */
945
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700946 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800947 return dev;
948
949 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700950 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800951 if (port_dev)
952 return port_dev;
953 }
954
955 return NULL;
956}
957
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700958static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800959{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700960 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200961 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700962 };
963 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800964 struct net_device *dev = NULL;
965 int nhsel;
966
967 /* For this route, all nexthop devs must be on the same switch. */
968
969 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
970 const struct fib_nh *nh = &fi->fib_nh[nhsel];
971
972 if (!nh->nh_dev)
973 return NULL;
974
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700975 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800976 if (!dev)
977 return NULL;
978
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700979 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800980 return NULL;
981
Scott Feldmand754f982015-07-18 18:24:49 -0700982 if (nhsel > 0 &&
983 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800984 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800985
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700986 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800987 }
988
989 return dev;
990}
991
Scott Feldman5e8d9042015-03-05 21:21:15 -0800992/**
Scott Feldman7616dcb2015-06-03 20:43:43 -0700993 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -0800994 *
995 * @dst: route's IPv4 destination address
996 * @dst_len: destination address length (prefix length)
997 * @fi: route FIB info structure
998 * @tos: route TOS
999 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001000 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001001 * @tb_id: route table ID
1002 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001003 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001004 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001005int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1006 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001007{
Vivien Didelotab069002015-09-29 12:07:17 -04001008 struct switchdev_obj_ipv4_fib ipv4_fib = {
1009 .dst = dst,
1010 .dst_len = dst_len,
1011 .fi = fi,
1012 .tos = tos,
1013 .type = type,
1014 .nlflags = nlflags,
1015 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001016 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001017 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001018 int err = 0;
1019
Scott Feldman8e05fd72015-03-05 21:21:19 -08001020 /* Don't offload route if using custom ip rules or if
1021 * IPv4 FIB offloading has been disabled completely.
1022 */
1023
Scott Feldmane1315db2015-03-06 01:14:36 -08001024#ifdef CONFIG_IP_MULTIPLE_TABLES
1025 if (fi->fib_net->ipv4.fib_has_custom_rules)
1026 return 0;
1027#endif
1028
1029 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001030 return 0;
1031
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001032 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001033 if (!dev)
1034 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001035
Jiri Pirko57d80832015-10-01 11:03:41 +02001036 err = switchdev_port_obj_add(dev, SWITCHDEV_OBJ_ID_IPV4_FIB, &ipv4_fib);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001037 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001038 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001039
Scott Feldmanaf201f72015-06-10 17:04:49 -07001040 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001041}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001042EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001043
1044/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001045 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001046 *
1047 * @dst: route's IPv4 destination address
1048 * @dst_len: destination address length (prefix length)
1049 * @fi: route FIB info structure
1050 * @tos: route TOS
1051 * @type: route type
1052 * @tb_id: route table ID
1053 *
1054 * Delete IPv4 route entry from switch device.
1055 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001056int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1057 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001058{
Vivien Didelotab069002015-09-29 12:07:17 -04001059 struct switchdev_obj_ipv4_fib ipv4_fib = {
1060 .dst = dst,
1061 .dst_len = dst_len,
1062 .fi = fi,
1063 .tos = tos,
1064 .type = type,
1065 .nlflags = 0,
1066 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001067 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001068 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001069 int err = 0;
1070
Roopa Prabhueea39942015-05-13 21:17:41 -07001071 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001072 return 0;
1073
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001074 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001075 if (!dev)
1076 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001077
Jiri Pirko57d80832015-10-01 11:03:41 +02001078 err = switchdev_port_obj_del(dev, SWITCHDEV_OBJ_ID_IPV4_FIB, &ipv4_fib);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001079 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001080 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001081
Scott Feldmanaf201f72015-06-10 17:04:49 -07001082 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001083}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001084EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001085
1086/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001087 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001088 *
1089 * @fi: route FIB info structure
1090 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001091void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001092{
1093 /* There was a problem installing this route to the offload
1094 * device. For now, until we come up with more refined
1095 * policy handling, abruptly end IPv4 fib offloading for
1096 * for entire net by flushing offload device(s) of all
1097 * IPv4 routes, and mark IPv4 fib offloading broken from
1098 * this point forward.
1099 */
1100
1101 fib_flush_external(fi->fib_net);
1102 fi->fib_net->ipv4.fib_offload_disabled = true;
1103}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001104EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001105
1106static bool switchdev_port_same_parent_id(struct net_device *a,
1107 struct net_device *b)
1108{
1109 struct switchdev_attr a_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001110 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001111 .flags = SWITCHDEV_F_NO_RECURSE,
1112 };
1113 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001114 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001115 .flags = SWITCHDEV_F_NO_RECURSE,
1116 };
1117
1118 if (switchdev_port_attr_get(a, &a_attr) ||
1119 switchdev_port_attr_get(b, &b_attr))
1120 return false;
1121
1122 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1123}
1124
1125static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1126 struct net_device *group_dev)
1127{
1128 struct net_device *lower_dev;
1129 struct list_head *iter;
1130
1131 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1132 if (lower_dev == dev)
1133 continue;
1134 if (switchdev_port_same_parent_id(dev, lower_dev))
1135 return lower_dev->offload_fwd_mark;
1136 return switchdev_port_fwd_mark_get(dev, lower_dev);
1137 }
1138
1139 return dev->ifindex;
1140}
1141
1142static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1143 u32 old_mark, u32 *reset_mark)
1144{
1145 struct net_device *lower_dev;
1146 struct list_head *iter;
1147
1148 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1149 if (lower_dev->offload_fwd_mark == old_mark) {
1150 if (!*reset_mark)
1151 *reset_mark = lower_dev->ifindex;
1152 lower_dev->offload_fwd_mark = *reset_mark;
1153 }
1154 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1155 }
1156}
1157
1158/**
1159 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1160 *
1161 * @dev: port device
1162 * @group_dev: containing device
1163 * @joining: true if dev is joining group; false if leaving group
1164 *
1165 * An ungrouped port's offload mark is just its ifindex. A grouped
1166 * port's (member of a bridge, for example) offload mark is the ifindex
1167 * of one of the ports in the group with the same parent (switch) ID.
1168 * Ports on the same device in the same group will have the same mark.
1169 *
1170 * Example:
1171 *
1172 * br0 ifindex=9
1173 * sw1p1 ifindex=2 mark=2
1174 * sw1p2 ifindex=3 mark=2
1175 * sw2p1 ifindex=4 mark=5
1176 * sw2p2 ifindex=5 mark=5
1177 *
1178 * If sw2p2 leaves the bridge, we'll have:
1179 *
1180 * br0 ifindex=9
1181 * sw1p1 ifindex=2 mark=2
1182 * sw1p2 ifindex=3 mark=2
1183 * sw2p1 ifindex=4 mark=4
1184 * sw2p2 ifindex=5 mark=5
1185 */
1186void switchdev_port_fwd_mark_set(struct net_device *dev,
1187 struct net_device *group_dev,
1188 bool joining)
1189{
1190 u32 mark = dev->ifindex;
1191 u32 reset_mark = 0;
1192
1193 if (group_dev && joining) {
1194 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1195 } else if (group_dev && !joining) {
1196 if (dev->offload_fwd_mark == mark)
1197 /* Ohoh, this port was the mark reference port,
1198 * but it's leaving the group, so reset the
1199 * mark for the remaining ports in the group.
1200 */
1201 switchdev_port_fwd_mark_reset(group_dev, mark,
1202 &reset_mark);
1203 }
1204
1205 dev->offload_fwd_mark = mark;
1206}
1207EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);