blob: 0402b3633100c678e1a37d8d9441b6d93b2567f6 [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,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200273 enum switchdev_obj_id id,
274 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200275 struct switchdev_trans *trans)
Scott Feldman491d0f12015-05-10 09:47:52 -0700276{
277 const struct switchdev_ops *ops = dev->switchdev_ops;
278 struct net_device *lower_dev;
279 struct list_head *iter;
280 int err = -EOPNOTSUPP;
281
282 if (ops && ops->switchdev_port_obj_add)
Vivien Didelotab069002015-09-29 12:07:17 -0400283 return ops->switchdev_port_obj_add(dev, id, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700284
285 /* Switch device port(s) may be stacked under
286 * bond/team/vlan dev, so recurse down to add object on
287 * each port.
288 */
289
290 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelotab069002015-09-29 12:07:17 -0400291 err = __switchdev_port_obj_add(lower_dev, id, obj, trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700292 if (err)
293 break;
294 }
295
296 return err;
297}
298
299/**
300 * switchdev_port_obj_add - Add port object
301 *
302 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400303 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700304 * @obj: object to add
305 *
306 * Use a 2-phase prepare-commit transaction model to ensure
307 * system is not left in a partially updated state due to
308 * failure from driver/device.
309 *
310 * rtnl_lock must be held.
311 */
Vivien Didelotab069002015-09-29 12:07:17 -0400312int switchdev_port_obj_add(struct net_device *dev, enum switchdev_obj_id id,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200313 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700314{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200315 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700316 int err;
317
318 ASSERT_RTNL();
319
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200320 switchdev_trans_init(&trans);
321
Scott Feldman491d0f12015-05-10 09:47:52 -0700322 /* Phase I: prepare for obj add. Driver/device should fail
323 * here if there are going to be issues in the commit phase,
324 * such as lack of resources or support. The driver/device
325 * should reserve resources needed for the commit phase here,
326 * but should not commit the obj.
327 */
328
Jiri Pirkof623ab72015-09-24 10:02:49 +0200329 trans.ph_prepare = true;
Vivien Didelotab069002015-09-29 12:07:17 -0400330 err = __switchdev_port_obj_add(dev, id, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700331 if (err) {
332 /* Prepare phase failed: abort the transaction. Any
333 * resources reserved in the prepare phase are
334 * released.
335 */
336
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200337 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200338 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700339
340 return err;
341 }
342
343 /* Phase II: commit obj add. This cannot fail as a fault
344 * of driver/device. If it does, it's a bug in the driver/device
345 * because the driver said everythings was OK in phase I.
346 */
347
Jiri Pirkof623ab72015-09-24 10:02:49 +0200348 trans.ph_prepare = false;
Vivien Didelotab069002015-09-29 12:07:17 -0400349 err = __switchdev_port_obj_add(dev, id, obj, &trans);
350 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200351 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700352
353 return err;
354}
355EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
356
357/**
358 * switchdev_port_obj_del - Delete port object
359 *
360 * @dev: port device
Vivien Didelotab069002015-09-29 12:07:17 -0400361 * @id: object ID
Scott Feldman491d0f12015-05-10 09:47:52 -0700362 * @obj: object to delete
363 */
Vivien Didelotab069002015-09-29 12:07:17 -0400364int switchdev_port_obj_del(struct net_device *dev, enum switchdev_obj_id id,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200365 const struct switchdev_obj *obj)
Scott Feldman491d0f12015-05-10 09:47:52 -0700366{
367 const struct switchdev_ops *ops = dev->switchdev_ops;
368 struct net_device *lower_dev;
369 struct list_head *iter;
370 int err = -EOPNOTSUPP;
371
372 if (ops && ops->switchdev_port_obj_del)
Vivien Didelotab069002015-09-29 12:07:17 -0400373 return ops->switchdev_port_obj_del(dev, id, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700374
375 /* Switch device port(s) may be stacked under
376 * bond/team/vlan dev, so recurse down to delete object on
377 * each port.
378 */
379
380 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelotab069002015-09-29 12:07:17 -0400381 err = switchdev_port_obj_del(lower_dev, id, obj);
Scott Feldman491d0f12015-05-10 09:47:52 -0700382 if (err)
383 break;
384 }
385
386 return err;
387}
388EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
389
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700390/**
391 * switchdev_port_obj_dump - Dump port objects
392 *
393 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400394 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700395 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400396 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700397 */
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400398int switchdev_port_obj_dump(struct net_device *dev, enum switchdev_obj_id id,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200399 struct switchdev_obj *obj,
400 switchdev_obj_dump_cb_t *cb)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700401{
402 const struct switchdev_ops *ops = dev->switchdev_ops;
403 struct net_device *lower_dev;
404 struct list_head *iter;
405 int err = -EOPNOTSUPP;
406
407 if (ops && ops->switchdev_port_obj_dump)
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400408 return ops->switchdev_port_obj_dump(dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700409
410 /* Switch device port(s) may be stacked under
411 * bond/team/vlan dev, so recurse down to dump objects on
412 * first port at bottom of stack.
413 */
414
415 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400416 err = switchdev_port_obj_dump(lower_dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700417 break;
418 }
419
420 return err;
421}
422EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
423
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700424static DEFINE_MUTEX(switchdev_mutex);
425static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100426
427/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700428 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100429 * @nb: notifier_block
430 *
431 * Register switch device notifier. This should be used by code
432 * which needs to monitor events happening in particular device.
433 * Return values are same as for atomic_notifier_chain_register().
434 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700435int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100436{
437 int err;
438
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700439 mutex_lock(&switchdev_mutex);
440 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
441 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100442 return err;
443}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700444EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100445
446/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700447 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100448 * @nb: notifier_block
449 *
450 * Unregister switch device notifier.
451 * Return values are same as for atomic_notifier_chain_unregister().
452 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700453int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100454{
455 int err;
456
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700457 mutex_lock(&switchdev_mutex);
458 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
459 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100460 return err;
461}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700462EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100463
464/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700465 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100466 * @val: value passed unmodified to notifier function
467 * @dev: port device
468 * @info: notifier information data
469 *
470 * Call all network notifier blocks. This should be called by driver
471 * when it needs to propagate hardware event.
472 * Return values are same as for atomic_notifier_call_chain().
473 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700474int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
475 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100476{
477 int err;
478
479 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700480 mutex_lock(&switchdev_mutex);
481 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
482 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100483 return err;
484}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700485EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800486
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700487struct switchdev_vlan_dump {
Jiri Pirko8f24f302015-10-01 11:03:43 +0200488 struct switchdev_obj_port_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700489 struct sk_buff *skb;
490 u32 filter_mask;
491 u16 flags;
492 u16 begin;
493 u16 end;
494};
495
Vivien Didelote23b0022015-09-29 12:07:13 -0400496static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700497{
498 struct bridge_vlan_info vinfo;
499
500 vinfo.flags = dump->flags;
501
502 if (dump->begin == 0 && dump->end == 0) {
503 return 0;
504 } else if (dump->begin == dump->end) {
505 vinfo.vid = dump->begin;
506 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
507 sizeof(vinfo), &vinfo))
508 return -EMSGSIZE;
509 } else {
510 vinfo.vid = dump->begin;
511 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
512 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
513 sizeof(vinfo), &vinfo))
514 return -EMSGSIZE;
515 vinfo.vid = dump->end;
516 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
517 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
518 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
519 sizeof(vinfo), &vinfo))
520 return -EMSGSIZE;
521 }
522
523 return 0;
524}
525
Jiri Pirko648b4a92015-10-01 11:03:45 +0200526static int switchdev_port_vlan_dump_cb(struct switchdev_obj *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700527{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200528 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700529 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400530 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700531 int err = 0;
532
533 if (vlan->vid_begin > vlan->vid_end)
534 return -EINVAL;
535
536 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
537 dump->flags = vlan->flags;
538 for (dump->begin = dump->end = vlan->vid_begin;
539 dump->begin <= vlan->vid_end;
540 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400541 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700542 if (err)
543 return err;
544 }
545 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
546 if (dump->begin > vlan->vid_begin &&
547 dump->begin >= vlan->vid_end) {
548 if ((dump->begin - 1) == vlan->vid_end &&
549 dump->flags == vlan->flags) {
550 /* prepend */
551 dump->begin = vlan->vid_begin;
552 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400553 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700554 dump->flags = vlan->flags;
555 dump->begin = vlan->vid_begin;
556 dump->end = vlan->vid_end;
557 }
558 } else if (dump->end <= vlan->vid_begin &&
559 dump->end < vlan->vid_end) {
560 if ((dump->end + 1) == vlan->vid_begin &&
561 dump->flags == vlan->flags) {
562 /* append */
563 dump->end = vlan->vid_end;
564 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400565 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700566 dump->flags = vlan->flags;
567 dump->begin = vlan->vid_begin;
568 dump->end = vlan->vid_end;
569 }
570 } else {
571 err = -EINVAL;
572 }
573 }
574
575 return err;
576}
577
578static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
579 u32 filter_mask)
580{
581 struct switchdev_vlan_dump dump = {
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700582 .skb = skb,
583 .filter_mask = filter_mask,
584 };
585 int err = 0;
586
587 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
588 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Jiri Pirko57d80832015-10-01 11:03:41 +0200589 err = switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_ID_PORT_VLAN,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200590 &dump.vlan.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400591 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700592 if (err)
593 goto err_out;
594 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
595 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400596 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700597 }
598
599err_out:
600 return err == -EOPNOTSUPP ? 0 : err;
601}
602
Scott Feldman8793d0a2015-05-10 09:48:04 -0700603/**
604 * switchdev_port_bridge_getlink - Get bridge port attributes
605 *
606 * @dev: port device
607 *
608 * Called for SELF on rtnl_bridge_getlink to get bridge port
609 * attributes.
610 */
611int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
612 struct net_device *dev, u32 filter_mask,
613 int nlflags)
614{
615 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200616 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman8793d0a2015-05-10 09:48:04 -0700617 };
618 u16 mode = BRIDGE_MODE_UNDEF;
619 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
620 int err;
621
622 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400623 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700624 return err;
625
626 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700627 attr.u.brport_flags, mask, nlflags,
628 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700629}
630EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
631
Scott Feldman47f83282015-05-10 09:47:56 -0700632static int switchdev_port_br_setflag(struct net_device *dev,
633 struct nlattr *nlattr,
634 unsigned long brport_flag)
635{
636 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200637 .id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
Scott Feldman47f83282015-05-10 09:47:56 -0700638 };
639 u8 flag = nla_get_u8(nlattr);
640 int err;
641
642 err = switchdev_port_attr_get(dev, &attr);
643 if (err)
644 return err;
645
646 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700647 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700648 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700649 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700650
651 return switchdev_port_attr_set(dev, &attr);
652}
653
654static const struct nla_policy
655switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
656 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
657 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
658 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
659 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
660 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
661 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
662 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
663 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
664 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
665 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
666};
667
668static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
669 struct nlattr *protinfo)
670{
671 struct nlattr *attr;
672 int rem;
673 int err;
674
675 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
676 switchdev_port_bridge_policy);
677 if (err)
678 return err;
679
680 nla_for_each_nested(attr, protinfo, rem) {
681 switch (nla_type(attr)) {
682 case IFLA_BRPORT_LEARNING:
683 err = switchdev_port_br_setflag(dev, attr,
684 BR_LEARNING);
685 break;
686 case IFLA_BRPORT_LEARNING_SYNC:
687 err = switchdev_port_br_setflag(dev, attr,
688 BR_LEARNING_SYNC);
689 break;
690 default:
691 err = -EOPNOTSUPP;
692 break;
693 }
694 if (err)
695 return err;
696 }
697
698 return 0;
699}
700
701static int switchdev_port_br_afspec(struct net_device *dev,
702 struct nlattr *afspec,
703 int (*f)(struct net_device *dev,
Vivien Didelotab069002015-09-29 12:07:17 -0400704 enum switchdev_obj_id id,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200705 const struct switchdev_obj *obj))
Scott Feldman47f83282015-05-10 09:47:56 -0700706{
707 struct nlattr *attr;
708 struct bridge_vlan_info *vinfo;
Jiri Pirko648b4a92015-10-01 11:03:45 +0200709 struct switchdev_obj_port_vlan vlan = { {}, 0 };
Scott Feldman47f83282015-05-10 09:47:56 -0700710 int rem;
711 int err;
712
713 nla_for_each_nested(attr, afspec, rem) {
714 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
715 continue;
716 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
717 return -EINVAL;
718 vinfo = nla_data(attr);
Vivien Didelotab069002015-09-29 12:07:17 -0400719 vlan.flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700720 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Vivien Didelotab069002015-09-29 12:07:17 -0400721 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700722 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400723 vlan.vid_begin = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700724 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Vivien Didelotab069002015-09-29 12:07:17 -0400725 if (!vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700726 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400727 vlan.vid_end = vinfo->vid;
728 if (vlan.vid_end <= vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700729 return -EINVAL;
Jiri Pirko648b4a92015-10-01 11:03:45 +0200730 err = f(dev, SWITCHDEV_OBJ_ID_PORT_VLAN, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700731 if (err)
732 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400733 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700734 } else {
Vivien Didelotab069002015-09-29 12:07:17 -0400735 if (vlan.vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700736 return -EINVAL;
Vivien Didelotab069002015-09-29 12:07:17 -0400737 vlan.vid_begin = vinfo->vid;
738 vlan.vid_end = vinfo->vid;
Jiri Pirko648b4a92015-10-01 11:03:45 +0200739 err = f(dev, SWITCHDEV_OBJ_ID_PORT_VLAN, &vlan.obj);
Scott Feldman47f83282015-05-10 09:47:56 -0700740 if (err)
741 return err;
Vivien Didelotab069002015-09-29 12:07:17 -0400742 memset(&vlan, 0, sizeof(vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700743 }
744 }
745
746 return 0;
747}
748
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800749/**
Scott Feldman47f83282015-05-10 09:47:56 -0700750 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800751 *
752 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700753 * @nlh: netlink header
754 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800755 *
Scott Feldman47f83282015-05-10 09:47:56 -0700756 * Called for SELF on rtnl_bridge_setlink to set bridge port
757 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800758 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700759int switchdev_port_bridge_setlink(struct net_device *dev,
760 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800761{
Scott Feldman47f83282015-05-10 09:47:56 -0700762 struct nlattr *protinfo;
763 struct nlattr *afspec;
764 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800765
Scott Feldman47f83282015-05-10 09:47:56 -0700766 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
767 IFLA_PROTINFO);
768 if (protinfo) {
769 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
770 if (err)
771 return err;
772 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800773
Scott Feldman47f83282015-05-10 09:47:56 -0700774 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
775 IFLA_AF_SPEC);
776 if (afspec)
777 err = switchdev_port_br_afspec(dev, afspec,
778 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800779
Scott Feldman47f83282015-05-10 09:47:56 -0700780 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800781}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700782EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800783
784/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700785 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800786 *
787 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700788 * @nlh: netlink header
789 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800790 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700791 * Called for SELF on rtnl_bridge_dellink to set bridge port
792 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800793 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700794int switchdev_port_bridge_dellink(struct net_device *dev,
795 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800796{
Scott Feldman5c34e022015-05-10 09:48:00 -0700797 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800798
Scott Feldman5c34e022015-05-10 09:48:00 -0700799 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
800 IFLA_AF_SPEC);
801 if (afspec)
802 return switchdev_port_br_afspec(dev, afspec,
803 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800804
Scott Feldman5c34e022015-05-10 09:48:00 -0700805 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800806}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700807EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800808
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700809/**
810 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
811 *
812 * @ndmsg: netlink hdr
813 * @nlattr: netlink attributes
814 * @dev: port device
815 * @addr: MAC address to add
816 * @vid: VLAN to add
817 *
818 * Add FDB entry to switch device.
819 */
820int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
821 struct net_device *dev, const unsigned char *addr,
822 u16 vid, u16 nlm_flags)
823{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200824 struct switchdev_obj_port_fdb fdb = {
Vivien Didelotab069002015-09-29 12:07:17 -0400825 .addr = addr,
826 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700827 };
828
Jiri Pirko648b4a92015-10-01 11:03:45 +0200829 return switchdev_port_obj_add(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700830}
831EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
832
833/**
834 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
835 *
836 * @ndmsg: netlink hdr
837 * @nlattr: netlink attributes
838 * @dev: port device
839 * @addr: MAC address to delete
840 * @vid: VLAN to delete
841 *
842 * Delete FDB entry from switch device.
843 */
844int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
845 struct net_device *dev, const unsigned char *addr,
846 u16 vid)
847{
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200848 struct switchdev_obj_port_fdb fdb = {
Vivien Didelotab069002015-09-29 12:07:17 -0400849 .addr = addr,
850 .vid = vid,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700851 };
852
Jiri Pirko648b4a92015-10-01 11:03:45 +0200853 return switchdev_port_obj_del(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &fdb.obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700854}
855EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
856
857struct switchdev_fdb_dump {
Jiri Pirko52ba57c2015-10-01 11:03:44 +0200858 struct switchdev_obj_port_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400859 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700860 struct sk_buff *skb;
861 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700862 int idx;
863};
864
Jiri Pirko648b4a92015-10-01 11:03:45 +0200865static int switchdev_port_fdb_dump_cb(struct switchdev_obj *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700866{
Jiri Pirko648b4a92015-10-01 11:03:45 +0200867 struct switchdev_obj_port_fdb *fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700868 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400869 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700870 u32 portid = NETLINK_CB(dump->cb->skb).portid;
871 u32 seq = dump->cb->nlh->nlmsg_seq;
872 struct nlmsghdr *nlh;
873 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700874
875 if (dump->idx < dump->cb->args[0])
876 goto skip;
877
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700878 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
879 sizeof(*ndm), NLM_F_MULTI);
880 if (!nlh)
881 return -EMSGSIZE;
882
883 ndm = nlmsg_data(nlh);
884 ndm->ndm_family = AF_BRIDGE;
885 ndm->ndm_pad1 = 0;
886 ndm->ndm_pad2 = 0;
887 ndm->ndm_flags = NTF_SELF;
888 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400889 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400890 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700891
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400892 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700893 goto nla_put_failure;
894
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400895 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700896 goto nla_put_failure;
897
898 nlmsg_end(dump->skb, nlh);
899
900skip:
901 dump->idx++;
902 return 0;
903
904nla_put_failure:
905 nlmsg_cancel(dump->skb, nlh);
906 return -EMSGSIZE;
907}
908
909/**
910 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
911 *
912 * @skb: netlink skb
913 * @cb: netlink callback
914 * @dev: port device
915 * @filter_dev: filter device
916 * @idx:
917 *
918 * Delete FDB entry from switch device.
919 */
920int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
921 struct net_device *dev,
922 struct net_device *filter_dev, int idx)
923{
924 struct switchdev_fdb_dump dump = {
Vivien Didelote02a06b22015-09-29 12:07:14 -0400925 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700926 .skb = skb,
927 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700928 .idx = idx,
929 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700930
Jiri Pirko648b4a92015-10-01 11:03:45 +0200931 switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_ID_PORT_FDB, &dump.fdb.obj,
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400932 switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700933 return dump.idx;
934}
935EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
936
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700937static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800938{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700939 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800940 struct net_device *lower_dev;
941 struct net_device *port_dev;
942 struct list_head *iter;
943
944 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700945 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800946 */
947
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700948 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800949 return dev;
950
951 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700952 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800953 if (port_dev)
954 return port_dev;
955 }
956
957 return NULL;
958}
959
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700960static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800961{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700962 struct switchdev_attr attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +0200963 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700964 };
965 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800966 struct net_device *dev = NULL;
967 int nhsel;
968
969 /* For this route, all nexthop devs must be on the same switch. */
970
971 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
972 const struct fib_nh *nh = &fi->fib_nh[nhsel];
973
974 if (!nh->nh_dev)
975 return NULL;
976
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700977 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800978 if (!dev)
979 return NULL;
980
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700981 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800982 return NULL;
983
Scott Feldmand754f982015-07-18 18:24:49 -0700984 if (nhsel > 0 &&
985 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800986 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800987
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700988 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800989 }
990
991 return dev;
992}
993
Scott Feldman5e8d9042015-03-05 21:21:15 -0800994/**
Scott Feldman7616dcb2015-06-03 20:43:43 -0700995 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -0800996 *
997 * @dst: route's IPv4 destination address
998 * @dst_len: destination address length (prefix length)
999 * @fi: route FIB info structure
1000 * @tos: route TOS
1001 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001002 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001003 * @tb_id: route table ID
1004 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001005 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001006 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001007int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1008 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001009{
Vivien Didelotab069002015-09-29 12:07:17 -04001010 struct switchdev_obj_ipv4_fib ipv4_fib = {
1011 .dst = dst,
1012 .dst_len = dst_len,
1013 .fi = fi,
1014 .tos = tos,
1015 .type = type,
1016 .nlflags = nlflags,
1017 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001018 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001019 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001020 int err = 0;
1021
Scott Feldman8e05fd72015-03-05 21:21:19 -08001022 /* Don't offload route if using custom ip rules or if
1023 * IPv4 FIB offloading has been disabled completely.
1024 */
1025
Scott Feldmane1315db2015-03-06 01:14:36 -08001026#ifdef CONFIG_IP_MULTIPLE_TABLES
1027 if (fi->fib_net->ipv4.fib_has_custom_rules)
1028 return 0;
1029#endif
1030
1031 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001032 return 0;
1033
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001034 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001035 if (!dev)
1036 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001037
Jiri Pirko648b4a92015-10-01 11:03:45 +02001038 err = switchdev_port_obj_add(dev, SWITCHDEV_OBJ_ID_IPV4_FIB,
1039 &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001040 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001041 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001042
Scott Feldmanaf201f72015-06-10 17:04:49 -07001043 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001044}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001045EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001046
1047/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001048 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001049 *
1050 * @dst: route's IPv4 destination address
1051 * @dst_len: destination address length (prefix length)
1052 * @fi: route FIB info structure
1053 * @tos: route TOS
1054 * @type: route type
1055 * @tb_id: route table ID
1056 *
1057 * Delete IPv4 route entry from switch device.
1058 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001059int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1060 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001061{
Vivien Didelotab069002015-09-29 12:07:17 -04001062 struct switchdev_obj_ipv4_fib ipv4_fib = {
1063 .dst = dst,
1064 .dst_len = dst_len,
1065 .fi = fi,
1066 .tos = tos,
1067 .type = type,
1068 .nlflags = 0,
1069 .tb_id = tb_id,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001070 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001071 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001072 int err = 0;
1073
Roopa Prabhueea39942015-05-13 21:17:41 -07001074 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001075 return 0;
1076
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001077 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001078 if (!dev)
1079 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001080
Jiri Pirko648b4a92015-10-01 11:03:45 +02001081 err = switchdev_port_obj_del(dev, SWITCHDEV_OBJ_ID_IPV4_FIB,
1082 &ipv4_fib.obj);
Scott Feldman58c2cb12015-05-10 09:48:06 -07001083 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001084 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001085
Scott Feldmanaf201f72015-06-10 17:04:49 -07001086 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001087}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001088EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001089
1090/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001091 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001092 *
1093 * @fi: route FIB info structure
1094 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001095void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001096{
1097 /* There was a problem installing this route to the offload
1098 * device. For now, until we come up with more refined
1099 * policy handling, abruptly end IPv4 fib offloading for
1100 * for entire net by flushing offload device(s) of all
1101 * IPv4 routes, and mark IPv4 fib offloading broken from
1102 * this point forward.
1103 */
1104
1105 fib_flush_external(fi->fib_net);
1106 fi->fib_net->ipv4.fib_offload_disabled = true;
1107}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001108EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001109
1110static bool switchdev_port_same_parent_id(struct net_device *a,
1111 struct net_device *b)
1112{
1113 struct switchdev_attr a_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 struct switchdev_attr b_attr = {
Jiri Pirko1f868392015-10-01 11:03:42 +02001118 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001119 .flags = SWITCHDEV_F_NO_RECURSE,
1120 };
1121
1122 if (switchdev_port_attr_get(a, &a_attr) ||
1123 switchdev_port_attr_get(b, &b_attr))
1124 return false;
1125
1126 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1127}
1128
1129static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1130 struct net_device *group_dev)
1131{
1132 struct net_device *lower_dev;
1133 struct list_head *iter;
1134
1135 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1136 if (lower_dev == dev)
1137 continue;
1138 if (switchdev_port_same_parent_id(dev, lower_dev))
1139 return lower_dev->offload_fwd_mark;
1140 return switchdev_port_fwd_mark_get(dev, lower_dev);
1141 }
1142
1143 return dev->ifindex;
1144}
1145
1146static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1147 u32 old_mark, u32 *reset_mark)
1148{
1149 struct net_device *lower_dev;
1150 struct list_head *iter;
1151
1152 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1153 if (lower_dev->offload_fwd_mark == old_mark) {
1154 if (!*reset_mark)
1155 *reset_mark = lower_dev->ifindex;
1156 lower_dev->offload_fwd_mark = *reset_mark;
1157 }
1158 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1159 }
1160}
1161
1162/**
1163 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1164 *
1165 * @dev: port device
1166 * @group_dev: containing device
1167 * @joining: true if dev is joining group; false if leaving group
1168 *
1169 * An ungrouped port's offload mark is just its ifindex. A grouped
1170 * port's (member of a bridge, for example) offload mark is the ifindex
1171 * of one of the ports in the group with the same parent (switch) ID.
1172 * Ports on the same device in the same group will have the same mark.
1173 *
1174 * Example:
1175 *
1176 * br0 ifindex=9
1177 * sw1p1 ifindex=2 mark=2
1178 * sw1p2 ifindex=3 mark=2
1179 * sw2p1 ifindex=4 mark=5
1180 * sw2p2 ifindex=5 mark=5
1181 *
1182 * If sw2p2 leaves the bridge, we'll have:
1183 *
1184 * br0 ifindex=9
1185 * sw1p1 ifindex=2 mark=2
1186 * sw1p2 ifindex=3 mark=2
1187 * sw2p1 ifindex=4 mark=4
1188 * sw2p2 ifindex=5 mark=5
1189 */
1190void switchdev_port_fwd_mark_set(struct net_device *dev,
1191 struct net_device *group_dev,
1192 bool joining)
1193{
1194 u32 mark = dev->ifindex;
1195 u32 reset_mark = 0;
1196
1197 if (group_dev && joining) {
1198 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1199 } else if (group_dev && !joining) {
1200 if (dev->offload_fwd_mark == mark)
1201 /* Ohoh, this port was the mark reference port,
1202 * but it's leaving the group, so reset the
1203 * mark for the remaining ports in the group.
1204 */
1205 switchdev_port_fwd_mark_reset(group_dev, mark,
1206 &reset_mark);
1207 }
1208
1209 dev->offload_fwd_mark = mark;
1210}
1211EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);