switchdev.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * net/switchdev/switchdev.c - Switch device API
  3. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  4. *
  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.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/init.h>
  13. #include <linux/mutex.h>
  14. #include <linux/notifier.h>
  15. #include <linux/netdevice.h>
  16. #include <net/switchdev.h>
  17. /**
  18. * netdev_switch_parent_id_get - Get ID of a switch
  19. * @dev: port device
  20. * @psid: switch ID
  21. *
  22. * Get ID of a switch this port is part of.
  23. */
  24. int netdev_switch_parent_id_get(struct net_device *dev,
  25. struct netdev_phys_item_id *psid)
  26. {
  27. const struct net_device_ops *ops = dev->netdev_ops;
  28. if (!ops->ndo_switch_parent_id_get)
  29. return -EOPNOTSUPP;
  30. return ops->ndo_switch_parent_id_get(dev, psid);
  31. }
  32. EXPORT_SYMBOL(netdev_switch_parent_id_get);
  33. /**
  34. * netdev_switch_port_stp_update - Notify switch device port of STP
  35. * state change
  36. * @dev: port device
  37. * @state: port STP state
  38. *
  39. * Notify switch device port of bridge port STP state change.
  40. */
  41. int netdev_switch_port_stp_update(struct net_device *dev, u8 state)
  42. {
  43. const struct net_device_ops *ops = dev->netdev_ops;
  44. if (!ops->ndo_switch_port_stp_update)
  45. return -EOPNOTSUPP;
  46. WARN_ON(!ops->ndo_switch_parent_id_get);
  47. return ops->ndo_switch_port_stp_update(dev, state);
  48. }
  49. EXPORT_SYMBOL(netdev_switch_port_stp_update);
  50. static DEFINE_MUTEX(netdev_switch_mutex);
  51. static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain);
  52. /**
  53. * register_netdev_switch_notifier - Register nofifier
  54. * @nb: notifier_block
  55. *
  56. * Register switch device notifier. This should be used by code
  57. * which needs to monitor events happening in particular device.
  58. * Return values are same as for atomic_notifier_chain_register().
  59. */
  60. int register_netdev_switch_notifier(struct notifier_block *nb)
  61. {
  62. int err;
  63. mutex_lock(&netdev_switch_mutex);
  64. err = raw_notifier_chain_register(&netdev_switch_notif_chain, nb);
  65. mutex_unlock(&netdev_switch_mutex);
  66. return err;
  67. }
  68. EXPORT_SYMBOL(register_netdev_switch_notifier);
  69. /**
  70. * unregister_netdev_switch_notifier - Unregister nofifier
  71. * @nb: notifier_block
  72. *
  73. * Unregister switch device notifier.
  74. * Return values are same as for atomic_notifier_chain_unregister().
  75. */
  76. int unregister_netdev_switch_notifier(struct notifier_block *nb)
  77. {
  78. int err;
  79. mutex_lock(&netdev_switch_mutex);
  80. err = raw_notifier_chain_unregister(&netdev_switch_notif_chain, nb);
  81. mutex_unlock(&netdev_switch_mutex);
  82. return err;
  83. }
  84. EXPORT_SYMBOL(unregister_netdev_switch_notifier);
  85. /**
  86. * call_netdev_switch_notifiers - Call nofifiers
  87. * @val: value passed unmodified to notifier function
  88. * @dev: port device
  89. * @info: notifier information data
  90. *
  91. * Call all network notifier blocks. This should be called by driver
  92. * when it needs to propagate hardware event.
  93. * Return values are same as for atomic_notifier_call_chain().
  94. */
  95. int call_netdev_switch_notifiers(unsigned long val, struct net_device *dev,
  96. struct netdev_switch_notifier_info *info)
  97. {
  98. int err;
  99. info->dev = dev;
  100. mutex_lock(&netdev_switch_mutex);
  101. err = raw_notifier_call_chain(&netdev_switch_notif_chain, val, info);
  102. mutex_unlock(&netdev_switch_mutex);
  103. return err;
  104. }
  105. EXPORT_SYMBOL(call_netdev_switch_notifiers);