timestamping.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * PTP 1588 clock support - support for timestamping in PHY devices
  3. *
  4. * Copyright (C) 2010 OMICRON electronics GmbH
  5. *
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/errqueue.h>
  21. #include <linux/phy.h>
  22. #include <linux/ptp_classify.h>
  23. #include <linux/skbuff.h>
  24. static struct sock_filter ptp_filter[] = {
  25. PTP_FILTER
  26. };
  27. static unsigned int classify(const struct sk_buff *skb)
  28. {
  29. if (likely(skb->dev &&
  30. skb->dev->phydev &&
  31. skb->dev->phydev->drv))
  32. return sk_run_filter(skb, ptp_filter);
  33. else
  34. return PTP_CLASS_NONE;
  35. }
  36. void skb_clone_tx_timestamp(struct sk_buff *skb)
  37. {
  38. struct phy_device *phydev;
  39. struct sk_buff *clone;
  40. struct sock *sk = skb->sk;
  41. unsigned int type;
  42. if (!sk)
  43. return;
  44. type = classify(skb);
  45. switch (type) {
  46. case PTP_CLASS_V1_IPV4:
  47. case PTP_CLASS_V1_IPV6:
  48. case PTP_CLASS_V2_IPV4:
  49. case PTP_CLASS_V2_IPV6:
  50. case PTP_CLASS_V2_L2:
  51. case PTP_CLASS_V2_VLAN:
  52. phydev = skb->dev->phydev;
  53. if (likely(phydev->drv->txtstamp)) {
  54. if (!atomic_inc_not_zero(&sk->sk_refcnt))
  55. return;
  56. clone = skb_clone(skb, GFP_ATOMIC);
  57. if (!clone) {
  58. sock_put(sk);
  59. return;
  60. }
  61. clone->sk = sk;
  62. phydev->drv->txtstamp(phydev, clone, type);
  63. }
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. EXPORT_SYMBOL_GPL(skb_clone_tx_timestamp);
  70. void skb_complete_tx_timestamp(struct sk_buff *skb,
  71. struct skb_shared_hwtstamps *hwtstamps)
  72. {
  73. struct sock *sk = skb->sk;
  74. struct sock_exterr_skb *serr;
  75. int err;
  76. if (!hwtstamps) {
  77. sock_put(sk);
  78. kfree_skb(skb);
  79. return;
  80. }
  81. *skb_hwtstamps(skb) = *hwtstamps;
  82. serr = SKB_EXT_ERR(skb);
  83. memset(serr, 0, sizeof(*serr));
  84. serr->ee.ee_errno = ENOMSG;
  85. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  86. skb->sk = NULL;
  87. err = sock_queue_err_skb(sk, skb);
  88. sock_put(sk);
  89. if (err)
  90. kfree_skb(skb);
  91. }
  92. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  93. bool skb_defer_rx_timestamp(struct sk_buff *skb)
  94. {
  95. struct phy_device *phydev;
  96. unsigned int type;
  97. if (skb_headroom(skb) < ETH_HLEN)
  98. return false;
  99. __skb_push(skb, ETH_HLEN);
  100. type = classify(skb);
  101. __skb_pull(skb, ETH_HLEN);
  102. switch (type) {
  103. case PTP_CLASS_V1_IPV4:
  104. case PTP_CLASS_V1_IPV6:
  105. case PTP_CLASS_V2_IPV4:
  106. case PTP_CLASS_V2_IPV6:
  107. case PTP_CLASS_V2_L2:
  108. case PTP_CLASS_V2_VLAN:
  109. phydev = skb->dev->phydev;
  110. if (likely(phydev->drv->rxtstamp))
  111. return phydev->drv->rxtstamp(phydev, skb, type);
  112. break;
  113. default:
  114. break;
  115. }
  116. return false;
  117. }
  118. EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);
  119. void __init skb_timestamping_init(void)
  120. {
  121. BUG_ON(sk_chk_filter(ptp_filter, ARRAY_SIZE(ptp_filter)));
  122. }