mesh_sync.c 6.8 KB

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