blob: 2ef863c96359667e0ccdce2246ef589fb7290c61 [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 = {
107 .id = SWITCHDEV_ATTR_UNDEFINED
108 };
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;
127 if (first.id == SWITCHDEV_ATTR_UNDEFINED)
128 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 Pirko7ea6eb32015-09-24 10:02:41 +0200273 struct switchdev_obj *obj,
274 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)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200282 return ops->switchdev_port_obj_add(dev, 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) {
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200290 err = __switchdev_port_obj_add(lower_dev, 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
302 * @obj: object to add
303 *
304 * Use a 2-phase prepare-commit transaction model to ensure
305 * system is not left in a partially updated state due to
306 * failure from driver/device.
307 *
308 * rtnl_lock must be held.
309 */
310int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj)
311{
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200312 struct switchdev_trans trans;
Scott Feldman491d0f12015-05-10 09:47:52 -0700313 int err;
314
315 ASSERT_RTNL();
316
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200317 switchdev_trans_init(&trans);
318
Scott Feldman491d0f12015-05-10 09:47:52 -0700319 /* Phase I: prepare for obj add. Driver/device should fail
320 * here if there are going to be issues in the commit phase,
321 * such as lack of resources or support. The driver/device
322 * should reserve resources needed for the commit phase here,
323 * but should not commit the obj.
324 */
325
Jiri Pirkof623ab72015-09-24 10:02:49 +0200326 trans.ph_prepare = true;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200327 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700328 if (err) {
329 /* Prepare phase failed: abort the transaction. Any
330 * resources reserved in the prepare phase are
331 * released.
332 */
333
Jiri Pirko9f6467c2015-09-24 10:02:47 +0200334 if (err != -EOPNOTSUPP)
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200335 switchdev_trans_items_destroy(&trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700336
337 return err;
338 }
339
340 /* Phase II: commit obj add. This cannot fail as a fault
341 * of driver/device. If it does, it's a bug in the driver/device
342 * because the driver said everythings was OK in phase I.
343 */
344
Jiri Pirkof623ab72015-09-24 10:02:49 +0200345 trans.ph_prepare = false;
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200346 err = __switchdev_port_obj_add(dev, obj, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700347 WARN(err, "%s: Commit of object (id=%d) failed.\n", dev->name, obj->id);
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200348 switchdev_trans_items_warn_destroy(dev, &trans);
Scott Feldman491d0f12015-05-10 09:47:52 -0700349
350 return err;
351}
352EXPORT_SYMBOL_GPL(switchdev_port_obj_add);
353
354/**
355 * switchdev_port_obj_del - Delete port object
356 *
357 * @dev: port device
358 * @obj: object to delete
359 */
360int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj)
361{
362 const struct switchdev_ops *ops = dev->switchdev_ops;
363 struct net_device *lower_dev;
364 struct list_head *iter;
365 int err = -EOPNOTSUPP;
366
367 if (ops && ops->switchdev_port_obj_del)
368 return ops->switchdev_port_obj_del(dev, obj);
369
370 /* Switch device port(s) may be stacked under
371 * bond/team/vlan dev, so recurse down to delete object on
372 * each port.
373 */
374
375 netdev_for_each_lower_dev(dev, lower_dev, iter) {
376 err = switchdev_port_obj_del(lower_dev, obj);
377 if (err)
378 break;
379 }
380
381 return err;
382}
383EXPORT_SYMBOL_GPL(switchdev_port_obj_del);
384
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700385/**
386 * switchdev_port_obj_dump - Dump port objects
387 *
388 * @dev: port device
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400389 * @id: object ID
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700390 * @obj: object to dump
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400391 * @cb: function to call with a filled object
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700392 */
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400393int switchdev_port_obj_dump(struct net_device *dev, enum switchdev_obj_id id,
394 void *obj, int (*cb)(void *obj))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700395{
396 const struct switchdev_ops *ops = dev->switchdev_ops;
397 struct net_device *lower_dev;
398 struct list_head *iter;
399 int err = -EOPNOTSUPP;
400
401 if (ops && ops->switchdev_port_obj_dump)
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400402 return ops->switchdev_port_obj_dump(dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700403
404 /* Switch device port(s) may be stacked under
405 * bond/team/vlan dev, so recurse down to dump objects on
406 * first port at bottom of stack.
407 */
408
409 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400410 err = switchdev_port_obj_dump(lower_dev, id, obj, cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700411 break;
412 }
413
414 return err;
415}
416EXPORT_SYMBOL_GPL(switchdev_port_obj_dump);
417
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700418static DEFINE_MUTEX(switchdev_mutex);
419static RAW_NOTIFIER_HEAD(switchdev_notif_chain);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100420
421/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700422 * register_switchdev_notifier - Register notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100423 * @nb: notifier_block
424 *
425 * Register switch device notifier. This should be used by code
426 * which needs to monitor events happening in particular device.
427 * Return values are same as for atomic_notifier_chain_register().
428 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700429int register_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100430{
431 int err;
432
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700433 mutex_lock(&switchdev_mutex);
434 err = raw_notifier_chain_register(&switchdev_notif_chain, nb);
435 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100436 return err;
437}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700438EXPORT_SYMBOL_GPL(register_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100439
440/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700441 * unregister_switchdev_notifier - Unregister notifier
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100442 * @nb: notifier_block
443 *
444 * Unregister switch device notifier.
445 * Return values are same as for atomic_notifier_chain_unregister().
446 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700447int unregister_switchdev_notifier(struct notifier_block *nb)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100448{
449 int err;
450
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700451 mutex_lock(&switchdev_mutex);
452 err = raw_notifier_chain_unregister(&switchdev_notif_chain, nb);
453 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100454 return err;
455}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700456EXPORT_SYMBOL_GPL(unregister_switchdev_notifier);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100457
458/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700459 * call_switchdev_notifiers - Call notifiers
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100460 * @val: value passed unmodified to notifier function
461 * @dev: port device
462 * @info: notifier information data
463 *
464 * Call all network notifier blocks. This should be called by driver
465 * when it needs to propagate hardware event.
466 * Return values are same as for atomic_notifier_call_chain().
467 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700468int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
469 struct switchdev_notifier_info *info)
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100470{
471 int err;
472
473 info->dev = dev;
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700474 mutex_lock(&switchdev_mutex);
475 err = raw_notifier_call_chain(&switchdev_notif_chain, val, info);
476 mutex_unlock(&switchdev_mutex);
Jiri Pirko03bf0c22015-01-15 23:49:36 +0100477 return err;
478}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700479EXPORT_SYMBOL_GPL(call_switchdev_notifiers);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800480
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700481struct switchdev_vlan_dump {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400482 struct switchdev_obj_vlan vlan;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700483 struct sk_buff *skb;
484 u32 filter_mask;
485 u16 flags;
486 u16 begin;
487 u16 end;
488};
489
Vivien Didelote23b0022015-09-29 12:07:13 -0400490static int switchdev_port_vlan_dump_put(struct switchdev_vlan_dump *dump)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700491{
492 struct bridge_vlan_info vinfo;
493
494 vinfo.flags = dump->flags;
495
496 if (dump->begin == 0 && dump->end == 0) {
497 return 0;
498 } else if (dump->begin == dump->end) {
499 vinfo.vid = dump->begin;
500 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
501 sizeof(vinfo), &vinfo))
502 return -EMSGSIZE;
503 } else {
504 vinfo.vid = dump->begin;
505 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
506 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
507 sizeof(vinfo), &vinfo))
508 return -EMSGSIZE;
509 vinfo.vid = dump->end;
510 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
511 vinfo.flags |= BRIDGE_VLAN_INFO_RANGE_END;
512 if (nla_put(dump->skb, IFLA_BRIDGE_VLAN_INFO,
513 sizeof(vinfo), &vinfo))
514 return -EMSGSIZE;
515 }
516
517 return 0;
518}
519
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400520static int switchdev_port_vlan_dump_cb(void *obj)
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700521{
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400522 struct switchdev_obj_vlan *vlan = obj;
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700523 struct switchdev_vlan_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400524 container_of(vlan, struct switchdev_vlan_dump, vlan);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700525 int err = 0;
526
527 if (vlan->vid_begin > vlan->vid_end)
528 return -EINVAL;
529
530 if (dump->filter_mask & RTEXT_FILTER_BRVLAN) {
531 dump->flags = vlan->flags;
532 for (dump->begin = dump->end = vlan->vid_begin;
533 dump->begin <= vlan->vid_end;
534 dump->begin++, dump->end++) {
Vivien Didelote23b0022015-09-29 12:07:13 -0400535 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700536 if (err)
537 return err;
538 }
539 } else if (dump->filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) {
540 if (dump->begin > vlan->vid_begin &&
541 dump->begin >= vlan->vid_end) {
542 if ((dump->begin - 1) == vlan->vid_end &&
543 dump->flags == vlan->flags) {
544 /* prepend */
545 dump->begin = vlan->vid_begin;
546 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400547 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700548 dump->flags = vlan->flags;
549 dump->begin = vlan->vid_begin;
550 dump->end = vlan->vid_end;
551 }
552 } else if (dump->end <= vlan->vid_begin &&
553 dump->end < vlan->vid_end) {
554 if ((dump->end + 1) == vlan->vid_begin &&
555 dump->flags == vlan->flags) {
556 /* append */
557 dump->end = vlan->vid_end;
558 } else {
Vivien Didelote23b0022015-09-29 12:07:13 -0400559 err = switchdev_port_vlan_dump_put(dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700560 dump->flags = vlan->flags;
561 dump->begin = vlan->vid_begin;
562 dump->end = vlan->vid_end;
563 }
564 } else {
565 err = -EINVAL;
566 }
567 }
568
569 return err;
570}
571
572static int switchdev_port_vlan_fill(struct sk_buff *skb, struct net_device *dev,
573 u32 filter_mask)
574{
575 struct switchdev_vlan_dump dump = {
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700576 .skb = skb,
577 .filter_mask = filter_mask,
578 };
579 int err = 0;
580
581 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
582 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400583 err = switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_PORT_VLAN,
584 &dump.vlan,
585 switchdev_port_vlan_dump_cb);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700586 if (err)
587 goto err_out;
588 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
589 /* last one */
Vivien Didelote23b0022015-09-29 12:07:13 -0400590 err = switchdev_port_vlan_dump_put(&dump);
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700591 }
592
593err_out:
594 return err == -EOPNOTSUPP ? 0 : err;
595}
596
Scott Feldman8793d0a2015-05-10 09:48:04 -0700597/**
598 * switchdev_port_bridge_getlink - Get bridge port attributes
599 *
600 * @dev: port device
601 *
602 * Called for SELF on rtnl_bridge_getlink to get bridge port
603 * attributes.
604 */
605int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
606 struct net_device *dev, u32 filter_mask,
607 int nlflags)
608{
609 struct switchdev_attr attr = {
610 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
611 };
612 u16 mode = BRIDGE_MODE_UNDEF;
613 u32 mask = BR_LEARNING | BR_LEARNING_SYNC;
614 int err;
615
616 err = switchdev_port_attr_get(dev, &attr);
Vivien Didelot5c8079d2015-06-23 10:26:04 -0400617 if (err && err != -EOPNOTSUPP)
Scott Feldman8793d0a2015-05-10 09:48:04 -0700618 return err;
619
620 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode,
Scott Feldman7d4f8d82015-06-22 00:27:17 -0700621 attr.u.brport_flags, mask, nlflags,
622 filter_mask, switchdev_port_vlan_fill);
Scott Feldman8793d0a2015-05-10 09:48:04 -0700623}
624EXPORT_SYMBOL_GPL(switchdev_port_bridge_getlink);
625
Scott Feldman47f83282015-05-10 09:47:56 -0700626static int switchdev_port_br_setflag(struct net_device *dev,
627 struct nlattr *nlattr,
628 unsigned long brport_flag)
629{
630 struct switchdev_attr attr = {
631 .id = SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
632 };
633 u8 flag = nla_get_u8(nlattr);
634 int err;
635
636 err = switchdev_port_attr_get(dev, &attr);
637 if (err)
638 return err;
639
640 if (flag)
Scott Feldman42275bd2015-05-13 11:16:50 -0700641 attr.u.brport_flags |= brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700642 else
Scott Feldman42275bd2015-05-13 11:16:50 -0700643 attr.u.brport_flags &= ~brport_flag;
Scott Feldman47f83282015-05-10 09:47:56 -0700644
645 return switchdev_port_attr_set(dev, &attr);
646}
647
648static const struct nla_policy
649switchdev_port_bridge_policy[IFLA_BRPORT_MAX + 1] = {
650 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
651 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
652 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
653 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
654 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
655 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
656 [IFLA_BRPORT_FAST_LEAVE] = { .type = NLA_U8 },
657 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
658 [IFLA_BRPORT_LEARNING_SYNC] = { .type = NLA_U8 },
659 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
660};
661
662static int switchdev_port_br_setlink_protinfo(struct net_device *dev,
663 struct nlattr *protinfo)
664{
665 struct nlattr *attr;
666 int rem;
667 int err;
668
669 err = nla_validate_nested(protinfo, IFLA_BRPORT_MAX,
670 switchdev_port_bridge_policy);
671 if (err)
672 return err;
673
674 nla_for_each_nested(attr, protinfo, rem) {
675 switch (nla_type(attr)) {
676 case IFLA_BRPORT_LEARNING:
677 err = switchdev_port_br_setflag(dev, attr,
678 BR_LEARNING);
679 break;
680 case IFLA_BRPORT_LEARNING_SYNC:
681 err = switchdev_port_br_setflag(dev, attr,
682 BR_LEARNING_SYNC);
683 break;
684 default:
685 err = -EOPNOTSUPP;
686 break;
687 }
688 if (err)
689 return err;
690 }
691
692 return 0;
693}
694
695static int switchdev_port_br_afspec(struct net_device *dev,
696 struct nlattr *afspec,
697 int (*f)(struct net_device *dev,
698 struct switchdev_obj *obj))
699{
700 struct nlattr *attr;
701 struct bridge_vlan_info *vinfo;
702 struct switchdev_obj obj = {
703 .id = SWITCHDEV_OBJ_PORT_VLAN,
704 };
Scott Feldman42275bd2015-05-13 11:16:50 -0700705 struct switchdev_obj_vlan *vlan = &obj.u.vlan;
Scott Feldman47f83282015-05-10 09:47:56 -0700706 int rem;
707 int err;
708
709 nla_for_each_nested(attr, afspec, rem) {
710 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
711 continue;
712 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
713 return -EINVAL;
714 vinfo = nla_data(attr);
Scott Feldman42275bd2015-05-13 11:16:50 -0700715 vlan->flags = vinfo->flags;
Scott Feldman47f83282015-05-10 09:47:56 -0700716 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700717 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700718 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700719 vlan->vid_begin = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700720 } else if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700721 if (!vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700722 return -EINVAL;
Scott Feldman42275bd2015-05-13 11:16:50 -0700723 vlan->vid_end = vinfo->vid;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700724 if (vlan->vid_end <= vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700725 return -EINVAL;
726 err = f(dev, &obj);
727 if (err)
728 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700729 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700730 } else {
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700731 if (vlan->vid_begin)
Scott Feldman47f83282015-05-10 09:47:56 -0700732 return -EINVAL;
Scott Feldman3e3a78b2015-06-22 00:27:16 -0700733 vlan->vid_begin = vinfo->vid;
Scott Feldman42275bd2015-05-13 11:16:50 -0700734 vlan->vid_end = vinfo->vid;
Scott Feldman47f83282015-05-10 09:47:56 -0700735 err = f(dev, &obj);
736 if (err)
737 return err;
Scott Feldman42275bd2015-05-13 11:16:50 -0700738 memset(vlan, 0, sizeof(*vlan));
Scott Feldman47f83282015-05-10 09:47:56 -0700739 }
740 }
741
742 return 0;
743}
744
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800745/**
Scott Feldman47f83282015-05-10 09:47:56 -0700746 * switchdev_port_bridge_setlink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800747 *
748 * @dev: port device
Scott Feldman47f83282015-05-10 09:47:56 -0700749 * @nlh: netlink header
750 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800751 *
Scott Feldman47f83282015-05-10 09:47:56 -0700752 * Called for SELF on rtnl_bridge_setlink to set bridge port
753 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800754 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700755int switchdev_port_bridge_setlink(struct net_device *dev,
756 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800757{
Scott Feldman47f83282015-05-10 09:47:56 -0700758 struct nlattr *protinfo;
759 struct nlattr *afspec;
760 int err = 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800761
Scott Feldman47f83282015-05-10 09:47:56 -0700762 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
763 IFLA_PROTINFO);
764 if (protinfo) {
765 err = switchdev_port_br_setlink_protinfo(dev, protinfo);
766 if (err)
767 return err;
768 }
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800769
Scott Feldman47f83282015-05-10 09:47:56 -0700770 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
771 IFLA_AF_SPEC);
772 if (afspec)
773 err = switchdev_port_br_afspec(dev, afspec,
774 switchdev_port_obj_add);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800775
Scott Feldman47f83282015-05-10 09:47:56 -0700776 return err;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800777}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700778EXPORT_SYMBOL_GPL(switchdev_port_bridge_setlink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800779
780/**
Scott Feldman5c34e022015-05-10 09:48:00 -0700781 * switchdev_port_bridge_dellink - Set bridge port attributes
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800782 *
783 * @dev: port device
Scott Feldman5c34e022015-05-10 09:48:00 -0700784 * @nlh: netlink header
785 * @flags: netlink flags
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800786 *
Scott Feldman5c34e022015-05-10 09:48:00 -0700787 * Called for SELF on rtnl_bridge_dellink to set bridge port
788 * attributes.
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800789 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700790int switchdev_port_bridge_dellink(struct net_device *dev,
791 struct nlmsghdr *nlh, u16 flags)
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800792{
Scott Feldman5c34e022015-05-10 09:48:00 -0700793 struct nlattr *afspec;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800794
Scott Feldman5c34e022015-05-10 09:48:00 -0700795 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg),
796 IFLA_AF_SPEC);
797 if (afspec)
798 return switchdev_port_br_afspec(dev, afspec,
799 switchdev_port_obj_del);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800800
Scott Feldman5c34e022015-05-10 09:48:00 -0700801 return 0;
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800802}
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700803EXPORT_SYMBOL_GPL(switchdev_port_bridge_dellink);
Roopa Prabhu8a44dbb2015-01-29 22:40:13 -0800804
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700805/**
806 * switchdev_port_fdb_add - Add FDB (MAC/VLAN) entry to port
807 *
808 * @ndmsg: netlink hdr
809 * @nlattr: netlink attributes
810 * @dev: port device
811 * @addr: MAC address to add
812 * @vid: VLAN to add
813 *
814 * Add FDB entry to switch device.
815 */
816int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
817 struct net_device *dev, const unsigned char *addr,
818 u16 vid, u16 nlm_flags)
819{
820 struct switchdev_obj obj = {
821 .id = SWITCHDEV_OBJ_PORT_FDB,
822 .u.fdb = {
David S. Millercdf09692015-08-11 12:00:37 -0700823 .addr = addr,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700824 .vid = vid,
825 },
826 };
827
828 return switchdev_port_obj_add(dev, &obj);
829}
830EXPORT_SYMBOL_GPL(switchdev_port_fdb_add);
831
832/**
833 * switchdev_port_fdb_del - Delete FDB (MAC/VLAN) entry from port
834 *
835 * @ndmsg: netlink hdr
836 * @nlattr: netlink attributes
837 * @dev: port device
838 * @addr: MAC address to delete
839 * @vid: VLAN to delete
840 *
841 * Delete FDB entry from switch device.
842 */
843int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
844 struct net_device *dev, const unsigned char *addr,
845 u16 vid)
846{
847 struct switchdev_obj obj = {
848 .id = SWITCHDEV_OBJ_PORT_FDB,
849 .u.fdb = {
David S. Millercdf09692015-08-11 12:00:37 -0700850 .addr = addr,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700851 .vid = vid,
852 },
853 };
854
855 return switchdev_port_obj_del(dev, &obj);
856}
857EXPORT_SYMBOL_GPL(switchdev_port_fdb_del);
858
859struct switchdev_fdb_dump {
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400860 struct switchdev_obj_fdb fdb;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400861 struct net_device *dev;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700862 struct sk_buff *skb;
863 struct netlink_callback *cb;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700864 int idx;
865};
866
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400867static int switchdev_port_fdb_dump_cb(void *obj)
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700868{
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400869 struct switchdev_obj_fdb *fdb = obj;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700870 struct switchdev_fdb_dump *dump =
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400871 container_of(fdb, struct switchdev_fdb_dump, fdb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700872 u32 portid = NETLINK_CB(dump->cb->skb).portid;
873 u32 seq = dump->cb->nlh->nlmsg_seq;
874 struct nlmsghdr *nlh;
875 struct ndmsg *ndm;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700876
877 if (dump->idx < dump->cb->args[0])
878 goto skip;
879
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700880 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
881 sizeof(*ndm), NLM_F_MULTI);
882 if (!nlh)
883 return -EMSGSIZE;
884
885 ndm = nlmsg_data(nlh);
886 ndm->ndm_family = AF_BRIDGE;
887 ndm->ndm_pad1 = 0;
888 ndm->ndm_pad2 = 0;
889 ndm->ndm_flags = NTF_SELF;
890 ndm->ndm_type = 0;
Vivien Didelote02a06b22015-09-29 12:07:14 -0400891 ndm->ndm_ifindex = dump->dev->ifindex;
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400892 ndm->ndm_state = fdb->ndm_state;
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700893
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400894 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, fdb->addr))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700895 goto nla_put_failure;
896
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400897 if (fdb->vid && nla_put_u16(dump->skb, NDA_VLAN, fdb->vid))
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700898 goto nla_put_failure;
899
900 nlmsg_end(dump->skb, nlh);
901
902skip:
903 dump->idx++;
904 return 0;
905
906nla_put_failure:
907 nlmsg_cancel(dump->skb, nlh);
908 return -EMSGSIZE;
909}
910
911/**
912 * switchdev_port_fdb_dump - Dump port FDB (MAC/VLAN) entries
913 *
914 * @skb: netlink skb
915 * @cb: netlink callback
916 * @dev: port device
917 * @filter_dev: filter device
918 * @idx:
919 *
920 * Delete FDB entry from switch device.
921 */
922int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
923 struct net_device *dev,
924 struct net_device *filter_dev, int idx)
925{
926 struct switchdev_fdb_dump dump = {
Vivien Didelote02a06b22015-09-29 12:07:14 -0400927 .dev = dev,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700928 .skb = skb,
929 .cb = cb,
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700930 .idx = idx,
931 };
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700932
Vivien Didelot25f07ad2015-09-29 12:07:16 -0400933 switchdev_port_obj_dump(dev, SWITCHDEV_OBJ_PORT_FDB, &dump.fdb,
934 switchdev_port_fdb_dump_cb);
Samudrala, Sridhar45d41222015-05-13 21:55:43 -0700935 return dump.idx;
936}
937EXPORT_SYMBOL_GPL(switchdev_port_fdb_dump);
938
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700939static struct net_device *switchdev_get_lowest_dev(struct net_device *dev)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800940{
Jiri Pirko9d47c0a2015-05-10 09:47:47 -0700941 const struct switchdev_ops *ops = dev->switchdev_ops;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800942 struct net_device *lower_dev;
943 struct net_device *port_dev;
944 struct list_head *iter;
945
946 /* Recusively search down until we find a sw port dev.
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700947 * (A sw port dev supports switchdev_port_attr_get).
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800948 */
949
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700950 if (ops && ops->switchdev_port_attr_get)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800951 return dev;
952
953 netdev_for_each_lower_dev(dev, lower_dev, iter) {
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700954 port_dev = switchdev_get_lowest_dev(lower_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800955 if (port_dev)
956 return port_dev;
957 }
958
959 return NULL;
960}
961
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700962static struct net_device *switchdev_get_dev_by_nhs(struct fib_info *fi)
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800963{
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700964 struct switchdev_attr attr = {
965 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
966 };
967 struct switchdev_attr prev_attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800968 struct net_device *dev = NULL;
969 int nhsel;
970
971 /* For this route, all nexthop devs must be on the same switch. */
972
973 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
974 const struct fib_nh *nh = &fi->fib_nh[nhsel];
975
976 if (!nh->nh_dev)
977 return NULL;
978
Jiri Pirkoebb9a032015-05-10 09:47:46 -0700979 dev = switchdev_get_lowest_dev(nh->nh_dev);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800980 if (!dev)
981 return NULL;
982
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700983 if (switchdev_port_attr_get(dev, &attr))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800984 return NULL;
985
Scott Feldmand754f982015-07-18 18:24:49 -0700986 if (nhsel > 0 &&
987 !netdev_phys_item_id_same(&prev_attr.u.ppid, &attr.u.ppid))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800988 return NULL;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800989
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700990 prev_attr = attr;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -0800991 }
992
993 return dev;
994}
995
Scott Feldman5e8d9042015-03-05 21:21:15 -0800996/**
Scott Feldman7616dcb2015-06-03 20:43:43 -0700997 * switchdev_fib_ipv4_add - Add/modify switch IPv4 route entry
Scott Feldman5e8d9042015-03-05 21:21:15 -0800998 *
999 * @dst: route's IPv4 destination address
1000 * @dst_len: destination address length (prefix length)
1001 * @fi: route FIB info structure
1002 * @tos: route TOS
1003 * @type: route type
Scott Feldmanf8f21472015-03-09 13:59:09 -07001004 * @nlflags: netlink flags passed in (NLM_F_*)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001005 * @tb_id: route table ID
1006 *
Scott Feldman7616dcb2015-06-03 20:43:43 -07001007 * Add/modify switch IPv4 route entry.
Scott Feldman5e8d9042015-03-05 21:21:15 -08001008 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001009int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
1010 u8 tos, u8 type, u32 nlflags, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001011{
Scott Feldman58c2cb12015-05-10 09:48:06 -07001012 struct switchdev_obj fib_obj = {
1013 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -07001014 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -07001015 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001016 .dst_len = dst_len,
1017 .fi = fi,
1018 .tos = tos,
1019 .type = type,
1020 .nlflags = nlflags,
1021 .tb_id = tb_id,
1022 },
1023 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001024 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001025 int err = 0;
1026
Scott Feldman8e05fd72015-03-05 21:21:19 -08001027 /* Don't offload route if using custom ip rules or if
1028 * IPv4 FIB offloading has been disabled completely.
1029 */
1030
Scott Feldmane1315db2015-03-06 01:14:36 -08001031#ifdef CONFIG_IP_MULTIPLE_TABLES
1032 if (fi->fib_net->ipv4.fib_has_custom_rules)
1033 return 0;
1034#endif
1035
1036 if (fi->fib_net->ipv4.fib_offload_disabled)
Scott Feldman104616e2015-03-05 21:21:16 -08001037 return 0;
1038
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001039 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001040 if (!dev)
1041 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001042
Scott Feldman58c2cb12015-05-10 09:48:06 -07001043 err = switchdev_port_obj_add(dev, &fib_obj);
1044 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001045 fi->fib_flags |= RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001046
Scott Feldmanaf201f72015-06-10 17:04:49 -07001047 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001048}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001049EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_add);
Scott Feldman5e8d9042015-03-05 21:21:15 -08001050
1051/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001052 * switchdev_fib_ipv4_del - Delete IPv4 route entry from switch
Scott Feldman5e8d9042015-03-05 21:21:15 -08001053 *
1054 * @dst: route's IPv4 destination address
1055 * @dst_len: destination address length (prefix length)
1056 * @fi: route FIB info structure
1057 * @tos: route TOS
1058 * @type: route type
1059 * @tb_id: route table ID
1060 *
1061 * Delete IPv4 route entry from switch device.
1062 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001063int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
1064 u8 tos, u8 type, u32 tb_id)
Scott Feldman5e8d9042015-03-05 21:21:15 -08001065{
Scott Feldman58c2cb12015-05-10 09:48:06 -07001066 struct switchdev_obj fib_obj = {
1067 .id = SWITCHDEV_OBJ_IPV4_FIB,
Scott Feldman42275bd2015-05-13 11:16:50 -07001068 .u.ipv4_fib = {
Scott Feldman7a7ee532015-05-12 23:03:52 -07001069 .dst = dst,
Scott Feldman58c2cb12015-05-10 09:48:06 -07001070 .dst_len = dst_len,
1071 .fi = fi,
1072 .tos = tos,
1073 .type = type,
1074 .nlflags = 0,
1075 .tb_id = tb_id,
1076 },
1077 };
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001078 struct net_device *dev;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001079 int err = 0;
1080
Roopa Prabhueea39942015-05-13 21:17:41 -07001081 if (!(fi->fib_flags & RTNH_F_OFFLOAD))
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001082 return 0;
1083
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001084 dev = switchdev_get_dev_by_nhs(fi);
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001085 if (!dev)
1086 return 0;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001087
Scott Feldman58c2cb12015-05-10 09:48:06 -07001088 err = switchdev_port_obj_del(dev, &fib_obj);
1089 if (!err)
David S. Miller36583eb2015-05-23 01:22:35 -04001090 fi->fib_flags &= ~RTNH_F_OFFLOAD;
Scott Feldmanb5d6fbd2015-03-05 21:21:17 -08001091
Scott Feldmanaf201f72015-06-10 17:04:49 -07001092 return err == -EOPNOTSUPP ? 0 : err;
Scott Feldman5e8d9042015-03-05 21:21:15 -08001093}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001094EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_del);
Scott Feldman8e05fd72015-03-05 21:21:19 -08001095
1096/**
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001097 * switchdev_fib_ipv4_abort - Abort an IPv4 FIB operation
Scott Feldman8e05fd72015-03-05 21:21:19 -08001098 *
1099 * @fi: route FIB info structure
1100 */
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001101void switchdev_fib_ipv4_abort(struct fib_info *fi)
Scott Feldman8e05fd72015-03-05 21:21:19 -08001102{
1103 /* There was a problem installing this route to the offload
1104 * device. For now, until we come up with more refined
1105 * policy handling, abruptly end IPv4 fib offloading for
1106 * for entire net by flushing offload device(s) of all
1107 * IPv4 routes, and mark IPv4 fib offloading broken from
1108 * this point forward.
1109 */
1110
1111 fib_flush_external(fi->fib_net);
1112 fi->fib_net->ipv4.fib_offload_disabled = true;
1113}
Jiri Pirkoebb9a032015-05-10 09:47:46 -07001114EXPORT_SYMBOL_GPL(switchdev_fib_ipv4_abort);
Scott Feldman1a3b2ec2015-07-18 18:24:50 -07001115
1116static bool switchdev_port_same_parent_id(struct net_device *a,
1117 struct net_device *b)
1118{
1119 struct switchdev_attr a_attr = {
1120 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1121 .flags = SWITCHDEV_F_NO_RECURSE,
1122 };
1123 struct switchdev_attr b_attr = {
1124 .id = SWITCHDEV_ATTR_PORT_PARENT_ID,
1125 .flags = SWITCHDEV_F_NO_RECURSE,
1126 };
1127
1128 if (switchdev_port_attr_get(a, &a_attr) ||
1129 switchdev_port_attr_get(b, &b_attr))
1130 return false;
1131
1132 return netdev_phys_item_id_same(&a_attr.u.ppid, &b_attr.u.ppid);
1133}
1134
1135static u32 switchdev_port_fwd_mark_get(struct net_device *dev,
1136 struct net_device *group_dev)
1137{
1138 struct net_device *lower_dev;
1139 struct list_head *iter;
1140
1141 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1142 if (lower_dev == dev)
1143 continue;
1144 if (switchdev_port_same_parent_id(dev, lower_dev))
1145 return lower_dev->offload_fwd_mark;
1146 return switchdev_port_fwd_mark_get(dev, lower_dev);
1147 }
1148
1149 return dev->ifindex;
1150}
1151
1152static void switchdev_port_fwd_mark_reset(struct net_device *group_dev,
1153 u32 old_mark, u32 *reset_mark)
1154{
1155 struct net_device *lower_dev;
1156 struct list_head *iter;
1157
1158 netdev_for_each_lower_dev(group_dev, lower_dev, iter) {
1159 if (lower_dev->offload_fwd_mark == old_mark) {
1160 if (!*reset_mark)
1161 *reset_mark = lower_dev->ifindex;
1162 lower_dev->offload_fwd_mark = *reset_mark;
1163 }
1164 switchdev_port_fwd_mark_reset(lower_dev, old_mark, reset_mark);
1165 }
1166}
1167
1168/**
1169 * switchdev_port_fwd_mark_set - Set port offload forwarding mark
1170 *
1171 * @dev: port device
1172 * @group_dev: containing device
1173 * @joining: true if dev is joining group; false if leaving group
1174 *
1175 * An ungrouped port's offload mark is just its ifindex. A grouped
1176 * port's (member of a bridge, for example) offload mark is the ifindex
1177 * of one of the ports in the group with the same parent (switch) ID.
1178 * Ports on the same device in the same group will have the same mark.
1179 *
1180 * Example:
1181 *
1182 * br0 ifindex=9
1183 * sw1p1 ifindex=2 mark=2
1184 * sw1p2 ifindex=3 mark=2
1185 * sw2p1 ifindex=4 mark=5
1186 * sw2p2 ifindex=5 mark=5
1187 *
1188 * If sw2p2 leaves the bridge, we'll have:
1189 *
1190 * br0 ifindex=9
1191 * sw1p1 ifindex=2 mark=2
1192 * sw1p2 ifindex=3 mark=2
1193 * sw2p1 ifindex=4 mark=4
1194 * sw2p2 ifindex=5 mark=5
1195 */
1196void switchdev_port_fwd_mark_set(struct net_device *dev,
1197 struct net_device *group_dev,
1198 bool joining)
1199{
1200 u32 mark = dev->ifindex;
1201 u32 reset_mark = 0;
1202
1203 if (group_dev && joining) {
1204 mark = switchdev_port_fwd_mark_get(dev, group_dev);
1205 } else if (group_dev && !joining) {
1206 if (dev->offload_fwd_mark == mark)
1207 /* Ohoh, this port was the mark reference port,
1208 * but it's leaving the group, so reset the
1209 * mark for the remaining ports in the group.
1210 */
1211 switchdev_port_fwd_mark_reset(group_dev, mark,
1212 &reset_mark);
1213 }
1214
1215 dev->offload_fwd_mark = mark;
1216}
1217EXPORT_SYMBOL_GPL(switchdev_port_fwd_mark_set);