mesh_sync.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2011-2012, Pavel Zubarev <pavel.zubarev@gmail.com>
  4. * Copyright 2011-2012, Marco Porsch <marco.porsch@s2005.tu-chemnitz.de>
  5. * Copyright 2011-2012, cozybit Inc.
  6. * Copyright (C) 2021 Intel Corporation
  7. */
  8. #include "ieee80211_i.h"
  9. #include "mesh.h"
  10. #include "driver-ops.h"
  11. /* This is not in the standard. It represents a tolerable tsf drift below
  12. * which we do no TSF adjustment.
  13. */
  14. #define TOFFSET_MINIMUM_ADJUSTMENT 10
  15. /* This is not in the standard. It is a margin added to the
  16. * Toffset setpoint to mitigate TSF overcorrection
  17. * introduced by TSF adjustment latency.
  18. */
  19. #define TOFFSET_SET_MARGIN 20
  20. /* This is not in the standard. It represents the maximum Toffset jump above
  21. * which we'll invalidate the Toffset setpoint and choose a new setpoint. This
  22. * could be, for instance, in case a neighbor is restarted and its TSF counter
  23. * reset.
  24. */
  25. #define TOFFSET_MAXIMUM_ADJUSTMENT 800 /* 0.8 ms */
  26. struct sync_method {
  27. u8 method;
  28. struct ieee80211_mesh_sync_ops ops;
  29. };
  30. /**
  31. * mesh_peer_tbtt_adjusting - check if an mp is currently adjusting its TBTT
  32. *
  33. * @cfg: mesh config element from the mesh peer (or %NULL)
  34. */
  35. static bool mesh_peer_tbtt_adjusting(const struct ieee80211_meshconf_ie *cfg)
  36. {
  37. return cfg &&
  38. (cfg->meshconf_cap & IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING);
  39. }
  40. void mesh_sync_adjust_tsf(struct ieee80211_sub_if_data *sdata)
  41. {
  42. struct ieee80211_local *local = sdata->local;
  43. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  44. /* sdata->vif.bss_conf.beacon_int in 1024us units, 0.04% */
  45. u64 beacon_int_fraction = sdata->vif.bss_conf.beacon_int * 1024 / 2500;
  46. u64 tsf;
  47. u64 tsfdelta;
  48. spin_lock_bh(&ifmsh->sync_offset_lock);
  49. if (ifmsh->sync_offset_clockdrift_max < beacon_int_fraction) {
  50. msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting\n",
  51. (long long) ifmsh->sync_offset_clockdrift_max);
  52. tsfdelta = -ifmsh->sync_offset_clockdrift_max;
  53. ifmsh->sync_offset_clockdrift_max = 0;
  54. } else {
  55. msync_dbg(sdata, "TSF : max clockdrift=%lld; adjusting by %llu\n",
  56. (long long) ifmsh->sync_offset_clockdrift_max,
  57. (unsigned long long) beacon_int_fraction);
  58. tsfdelta = -beacon_int_fraction;
  59. ifmsh->sync_offset_clockdrift_max -= beacon_int_fraction;
  60. }
  61. spin_unlock_bh(&ifmsh->sync_offset_lock);
  62. if (local->ops->offset_tsf) {
  63. drv_offset_tsf(local, sdata, tsfdelta);
  64. } else {
  65. tsf = drv_get_tsf(local, sdata);
  66. if (tsf != -1ULL)
  67. drv_set_tsf(local, sdata, tsf + tsfdelta);
  68. }
  69. }
  70. static void
  71. mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata, u16 stype,
  72. struct ieee80211_mgmt *mgmt, unsigned int len,
  73. const struct ieee80211_meshconf_ie *mesh_cfg,
  74. struct ieee80211_rx_status *rx_status)
  75. {
  76. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  77. struct ieee80211_local *local = sdata->local;
  78. struct sta_info *sta;
  79. u64 t_t, t_r;
  80. WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  81. /* standard mentions only beacons */
  82. if (stype != IEEE80211_STYPE_BEACON)
  83. return;
  84. /*
  85. * Get time when timestamp field was received. If we don't
  86. * have rx timestamps, then use current tsf as an approximation.
  87. * drv_get_tsf() must be called before entering the rcu-read
  88. * section.
  89. */
  90. if (ieee80211_have_rx_timestamp(rx_status))
  91. t_r = ieee80211_calculate_rx_timestamp(local, rx_status,
  92. len + FCS_LEN, 24);
  93. else
  94. t_r = drv_get_tsf(local, sdata);
  95. rcu_read_lock();
  96. sta = sta_info_get(sdata, mgmt->sa);
  97. if (!sta)
  98. goto no_sync;
  99. /* check offset sync conditions (13.13.2.2.1)
  100. *
  101. * TODO also sync to
  102. * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
  103. */
  104. if (mesh_peer_tbtt_adjusting(mesh_cfg)) {
  105. msync_dbg(sdata, "STA %pM : is adjusting TBTT\n",
  106. sta->sta.addr);
  107. goto no_sync;
  108. }
  109. /* Timing offset calculation (see 13.13.2.2.2) */
  110. t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
  111. sta->mesh->t_offset = t_t - t_r;
  112. if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
  113. s64 t_clockdrift = sta->mesh->t_offset_setpoint - sta->mesh->t_offset;
  114. msync_dbg(sdata,
  115. "STA %pM : t_offset=%lld, t_offset_setpoint=%lld, t_clockdrift=%lld\n",
  116. sta->sta.addr, (long long) sta->mesh->t_offset,
  117. (long long) sta->mesh->t_offset_setpoint,
  118. (long long) t_clockdrift);
  119. if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
  120. t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
  121. msync_dbg(sdata,
  122. "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
  123. sta->sta.addr,
  124. (long long) t_clockdrift);
  125. clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  126. goto no_sync;
  127. }
  128. spin_lock_bh(&ifmsh->sync_offset_lock);
  129. if (t_clockdrift > ifmsh->sync_offset_clockdrift_max)
  130. ifmsh->sync_offset_clockdrift_max = t_clockdrift;
  131. spin_unlock_bh(&ifmsh->sync_offset_lock);
  132. } else {
  133. sta->mesh->t_offset_setpoint = sta->mesh->t_offset - TOFFSET_SET_MARGIN;
  134. set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  135. msync_dbg(sdata,
  136. "STA %pM : offset was invalid, t_offset=%lld\n",
  137. sta->sta.addr,
  138. (long long) sta->mesh->t_offset);
  139. }
  140. no_sync:
  141. rcu_read_unlock();
  142. }
  143. static void mesh_sync_offset_adjust_tsf(struct ieee80211_sub_if_data *sdata,
  144. struct beacon_data *beacon)
  145. {
  146. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  147. WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  148. WARN_ON(!rcu_read_lock_held());
  149. spin_lock_bh(&ifmsh->sync_offset_lock);
  150. if (ifmsh->sync_offset_clockdrift_max > TOFFSET_MINIMUM_ADJUSTMENT) {
  151. /* Since ajusting the tsf here would
  152. * require a possibly blocking call
  153. * to the driver tsf setter, we punt
  154. * the tsf adjustment to the mesh tasklet
  155. */
  156. msync_dbg(sdata,
  157. "TSF : kicking off TSF adjustment with clockdrift_max=%lld\n",
  158. ifmsh->sync_offset_clockdrift_max);
  159. set_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags);
  160. } else {
  161. msync_dbg(sdata,
  162. "TSF : max clockdrift=%lld; too small to adjust\n",
  163. (long long)ifmsh->sync_offset_clockdrift_max);
  164. ifmsh->sync_offset_clockdrift_max = 0;
  165. }
  166. spin_unlock_bh(&ifmsh->sync_offset_lock);
  167. }
  168. static const struct sync_method sync_methods[] = {
  169. {
  170. .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  171. .ops = {
  172. .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
  173. .adjust_tsf = &mesh_sync_offset_adjust_tsf,
  174. }
  175. },
  176. };
  177. const struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
  178. {
  179. int i;
  180. for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
  181. if (sync_methods[i].method == method)
  182. return &sync_methods[i].ops;
  183. }
  184. return NULL;
  185. }