2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/mutex.h>
15 #include <linux/notifier.h>
16 #include <linux/netdevice.h>
17 #include <net/ip_fib.h>
18 #include <net/switchdev.h>
21 * netdev_switch_parent_id_get - Get ID of a switch
25 * Get ID of a switch this port is part of.
27 int netdev_switch_parent_id_get(struct net_device
*dev
,
28 struct netdev_phys_item_id
*psid
)
30 const struct net_device_ops
*ops
= dev
->netdev_ops
;
32 if (!ops
->ndo_switch_parent_id_get
)
34 return ops
->ndo_switch_parent_id_get(dev
, psid
);
36 EXPORT_SYMBOL(netdev_switch_parent_id_get
);
39 * netdev_switch_port_stp_update - Notify switch device port of STP
42 * @state: port STP state
44 * Notify switch device port of bridge port STP state change.
46 int netdev_switch_port_stp_update(struct net_device
*dev
, u8 state
)
48 const struct net_device_ops
*ops
= dev
->netdev_ops
;
50 if (!ops
->ndo_switch_port_stp_update
)
52 WARN_ON(!ops
->ndo_switch_parent_id_get
);
53 return ops
->ndo_switch_port_stp_update(dev
, state
);
55 EXPORT_SYMBOL(netdev_switch_port_stp_update
);
57 static DEFINE_MUTEX(netdev_switch_mutex
);
58 static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain
);
61 * register_netdev_switch_notifier - Register nofifier
64 * Register switch device notifier. This should be used by code
65 * which needs to monitor events happening in particular device.
66 * Return values are same as for atomic_notifier_chain_register().
68 int register_netdev_switch_notifier(struct notifier_block
*nb
)
72 mutex_lock(&netdev_switch_mutex
);
73 err
= raw_notifier_chain_register(&netdev_switch_notif_chain
, nb
);
74 mutex_unlock(&netdev_switch_mutex
);
77 EXPORT_SYMBOL(register_netdev_switch_notifier
);
80 * unregister_netdev_switch_notifier - Unregister nofifier
83 * Unregister switch device notifier.
84 * Return values are same as for atomic_notifier_chain_unregister().
86 int unregister_netdev_switch_notifier(struct notifier_block
*nb
)
90 mutex_lock(&netdev_switch_mutex
);
91 err
= raw_notifier_chain_unregister(&netdev_switch_notif_chain
, nb
);
92 mutex_unlock(&netdev_switch_mutex
);
95 EXPORT_SYMBOL(unregister_netdev_switch_notifier
);
98 * call_netdev_switch_notifiers - Call nofifiers
99 * @val: value passed unmodified to notifier function
101 * @info: notifier information data
103 * Call all network notifier blocks. This should be called by driver
104 * when it needs to propagate hardware event.
105 * Return values are same as for atomic_notifier_call_chain().
107 int call_netdev_switch_notifiers(unsigned long val
, struct net_device
*dev
,
108 struct netdev_switch_notifier_info
*info
)
113 mutex_lock(&netdev_switch_mutex
);
114 err
= raw_notifier_call_chain(&netdev_switch_notif_chain
, val
, info
);
115 mutex_unlock(&netdev_switch_mutex
);
118 EXPORT_SYMBOL(call_netdev_switch_notifiers
);
121 * netdev_switch_port_bridge_setlink - Notify switch device port of bridge
125 * @nlh: netlink msg with bridge port attributes
126 * @flags: bridge setlink flags
128 * Notify switch device port of bridge port attributes
130 int netdev_switch_port_bridge_setlink(struct net_device
*dev
,
131 struct nlmsghdr
*nlh
, u16 flags
)
133 const struct net_device_ops
*ops
= dev
->netdev_ops
;
135 if (!(dev
->features
& NETIF_F_HW_SWITCH_OFFLOAD
))
138 if (!ops
->ndo_bridge_setlink
)
141 return ops
->ndo_bridge_setlink(dev
, nlh
, flags
);
143 EXPORT_SYMBOL(netdev_switch_port_bridge_setlink
);
146 * netdev_switch_port_bridge_dellink - Notify switch device port of bridge
147 * port attribute delete
150 * @nlh: netlink msg with bridge port attributes
151 * @flags: bridge setlink flags
153 * Notify switch device port of bridge port attribute delete
155 int netdev_switch_port_bridge_dellink(struct net_device
*dev
,
156 struct nlmsghdr
*nlh
, u16 flags
)
158 const struct net_device_ops
*ops
= dev
->netdev_ops
;
160 if (!(dev
->features
& NETIF_F_HW_SWITCH_OFFLOAD
))
163 if (!ops
->ndo_bridge_dellink
)
166 return ops
->ndo_bridge_dellink(dev
, nlh
, flags
);
168 EXPORT_SYMBOL(netdev_switch_port_bridge_dellink
);
171 * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink
172 * op for master devices
175 * @nlh: netlink msg with bridge port attributes
176 * @flags: bridge setlink flags
178 * Notify master device slaves of bridge port attributes
180 int ndo_dflt_netdev_switch_port_bridge_setlink(struct net_device
*dev
,
181 struct nlmsghdr
*nlh
, u16 flags
)
183 struct net_device
*lower_dev
;
184 struct list_head
*iter
;
185 int ret
= 0, err
= 0;
187 if (!(dev
->features
& NETIF_F_HW_SWITCH_OFFLOAD
))
190 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
191 err
= netdev_switch_port_bridge_setlink(lower_dev
, nlh
, flags
);
192 if (err
&& err
!= -EOPNOTSUPP
)
198 EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_setlink
);
201 * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink
202 * op for master devices
205 * @nlh: netlink msg with bridge port attributes
206 * @flags: bridge dellink flags
208 * Notify master device slaves of bridge port attribute deletes
210 int ndo_dflt_netdev_switch_port_bridge_dellink(struct net_device
*dev
,
211 struct nlmsghdr
*nlh
, u16 flags
)
213 struct net_device
*lower_dev
;
214 struct list_head
*iter
;
215 int ret
= 0, err
= 0;
217 if (!(dev
->features
& NETIF_F_HW_SWITCH_OFFLOAD
))
220 netdev_for_each_lower_dev(dev
, lower_dev
, iter
) {
221 err
= netdev_switch_port_bridge_dellink(lower_dev
, nlh
, flags
);
222 if (err
&& err
!= -EOPNOTSUPP
)
228 EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_dellink
);
231 * netdev_switch_fib_ipv4_add - Add IPv4 route entry to switch
233 * @dst: route's IPv4 destination address
234 * @dst_len: destination address length (prefix length)
235 * @fi: route FIB info structure
238 * @tb_id: route table ID
240 * Add IPv4 route entry to switch device.
242 int netdev_switch_fib_ipv4_add(u32 dst
, int dst_len
, struct fib_info
*fi
,
243 u8 tos
, u8 type
, u32 tb_id
)
247 EXPORT_SYMBOL(netdev_switch_fib_ipv4_add
);
250 * netdev_switch_fib_ipv4_del - Delete IPv4 route entry from switch
252 * @dst: route's IPv4 destination address
253 * @dst_len: destination address length (prefix length)
254 * @fi: route FIB info structure
257 * @tb_id: route table ID
259 * Delete IPv4 route entry from switch device.
261 int netdev_switch_fib_ipv4_del(u32 dst
, int dst_len
, struct fib_info
*fi
,
262 u8 tos
, u8 type
, u32 tb_id
)
266 EXPORT_SYMBOL(netdev_switch_fib_ipv4_del
);