mesh_sync.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 30000 /* 30 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. 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. tsf = drv_get_tsf(local, sdata);
  64. if (tsf != -1ULL)
  65. drv_set_tsf(local, sdata, tsf + tsfdelta);
  66. spin_unlock_bh(&ifmsh->sync_offset_lock);
  67. }
  68. static void mesh_sync_offset_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  69. u16 stype,
  70. struct ieee80211_mgmt *mgmt,
  71. struct ieee802_11_elems *elems,
  72. struct ieee80211_rx_status *rx_status)
  73. {
  74. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  75. struct ieee80211_local *local = sdata->local;
  76. struct sta_info *sta;
  77. u64 t_t, t_r;
  78. WARN_ON(ifmsh->mesh_sp_id != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  79. /* standard mentions only beacons */
  80. if (stype != IEEE80211_STYPE_BEACON)
  81. return;
  82. /* The current tsf is a first approximation for the timestamp
  83. * for the received beacon. Further down we try to get a
  84. * better value from the rx_status->mactime field if
  85. * available. Also we have to call drv_get_tsf() before
  86. * entering the rcu-read section.*/
  87. t_r = drv_get_tsf(local, sdata);
  88. rcu_read_lock();
  89. sta = sta_info_get(sdata, mgmt->sa);
  90. if (!sta)
  91. goto no_sync;
  92. /* check offset sync conditions (13.13.2.2.1)
  93. *
  94. * TODO also sync to
  95. * dot11MeshNbrOffsetMaxNeighbor non-peer non-MBSS neighbors
  96. */
  97. if (elems->mesh_config && mesh_peer_tbtt_adjusting(elems)) {
  98. clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  99. msync_dbg(sdata, "STA %pM : is adjusting TBTT\n", sta->sta.addr);
  100. goto no_sync;
  101. }
  102. if (rx_status->flag & RX_FLAG_MACTIME_MPDU && rx_status->mactime) {
  103. /*
  104. * The mactime is defined as the time the first data symbol
  105. * of the frame hits the PHY, and the timestamp of the beacon
  106. * is defined as "the time that the data symbol containing the
  107. * first bit of the timestamp is transmitted to the PHY plus
  108. * the transmitting STA's delays through its local PHY from the
  109. * MAC-PHY interface to its interface with the WM" (802.11
  110. * 11.1.2)
  111. *
  112. * T_r, in 13.13.2.2.2, is just defined as "the frame reception
  113. * time" but we unless we interpret that time to be the same
  114. * time of the beacon timestamp, the offset calculation will be
  115. * off. Below we adjust t_r to be "the time at which the first
  116. * symbol of the timestamp element in the beacon is received".
  117. * This correction depends on the rate.
  118. *
  119. * Based on similar code in ibss.c
  120. */
  121. int rate;
  122. if (rx_status->flag & RX_FLAG_HT) {
  123. /* TODO:
  124. * In principle there could be HT-beacons (Dual Beacon
  125. * HT Operation options), but for now ignore them and
  126. * just use the primary (i.e. non-HT) beacons for
  127. * synchronization.
  128. * */
  129. goto no_sync;
  130. } else
  131. rate = local->hw.wiphy->bands[rx_status->band]->
  132. bitrates[rx_status->rate_idx].bitrate;
  133. /* 24 bytes of header * 8 bits/byte *
  134. * 10*(100 Kbps)/Mbps / rate (100 Kbps)*/
  135. t_r = rx_status->mactime + (24 * 8 * 10 / rate);
  136. }
  137. /* Timing offset calculation (see 13.13.2.2.2) */
  138. t_t = le64_to_cpu(mgmt->u.beacon.timestamp);
  139. sta->t_offset = t_t - t_r;
  140. if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) {
  141. s64 t_clockdrift = sta->t_offset_setpoint
  142. - sta->t_offset;
  143. msync_dbg(sdata,
  144. "STA %pM : sta->t_offset=%lld, sta->t_offset_setpoint=%lld, t_clockdrift=%lld\n",
  145. sta->sta.addr,
  146. (long long) sta->t_offset,
  147. (long long)
  148. sta->t_offset_setpoint,
  149. (long long) t_clockdrift);
  150. if (t_clockdrift > TOFFSET_MAXIMUM_ADJUSTMENT ||
  151. t_clockdrift < -TOFFSET_MAXIMUM_ADJUSTMENT) {
  152. msync_dbg(sdata,
  153. "STA %pM : t_clockdrift=%lld too large, setpoint reset\n",
  154. sta->sta.addr,
  155. (long long) t_clockdrift);
  156. clear_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  157. goto no_sync;
  158. }
  159. rcu_read_unlock();
  160. spin_lock_bh(&ifmsh->sync_offset_lock);
  161. if (t_clockdrift >
  162. ifmsh->sync_offset_clockdrift_max)
  163. ifmsh->sync_offset_clockdrift_max
  164. = t_clockdrift;
  165. spin_unlock_bh(&ifmsh->sync_offset_lock);
  166. } else {
  167. sta->t_offset_setpoint = sta->t_offset - TOFFSET_SET_MARGIN;
  168. set_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN);
  169. msync_dbg(sdata,
  170. "STA %pM : offset was invalid, sta->t_offset=%lld\n",
  171. sta->sta.addr,
  172. (long long) sta->t_offset);
  173. rcu_read_unlock();
  174. }
  175. return;
  176. no_sync:
  177. rcu_read_unlock();
  178. }
  179. static void mesh_sync_offset_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
  180. {
  181. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  182. WARN_ON(ifmsh->mesh_sp_id
  183. != IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET);
  184. BUG_ON(!rcu_read_lock_held());
  185. spin_lock_bh(&ifmsh->sync_offset_lock);
  186. if (ifmsh->sync_offset_clockdrift_max >
  187. TOFFSET_MINIMUM_ADJUSTMENT) {
  188. /* Since ajusting the tsf here would
  189. * require a possibly blocking call
  190. * to the driver tsf setter, we punt
  191. * the tsf adjustment to the mesh tasklet
  192. */
  193. msync_dbg(sdata,
  194. "TBTT : kicking off TBTT adjustment with clockdrift_max=%lld\n",
  195. ifmsh->sync_offset_clockdrift_max);
  196. set_bit(MESH_WORK_DRIFT_ADJUST,
  197. &ifmsh->wrkq_flags);
  198. } else {
  199. msync_dbg(sdata,
  200. "TBTT : max clockdrift=%lld; too small to adjust\n",
  201. (long long)ifmsh->sync_offset_clockdrift_max);
  202. ifmsh->sync_offset_clockdrift_max = 0;
  203. }
  204. spin_unlock_bh(&ifmsh->sync_offset_lock);
  205. }
  206. static const u8 *mesh_get_vendor_oui(struct ieee80211_sub_if_data *sdata)
  207. {
  208. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  209. u8 offset;
  210. if (!ifmsh->ie || !ifmsh->ie_len)
  211. return NULL;
  212. offset = ieee80211_ie_split_vendor(ifmsh->ie,
  213. ifmsh->ie_len, 0);
  214. if (!offset)
  215. return NULL;
  216. return ifmsh->ie + offset + 2;
  217. }
  218. static void mesh_sync_vendor_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  219. u16 stype,
  220. struct ieee80211_mgmt *mgmt,
  221. struct ieee802_11_elems *elems,
  222. struct ieee80211_rx_status *rx_status)
  223. {
  224. const u8 *oui;
  225. WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
  226. msync_dbg(sdata, "called mesh_sync_vendor_rx_bcn_presp\n");
  227. oui = mesh_get_vendor_oui(sdata);
  228. /* here you would implement the vendor offset tracking for this oui */
  229. }
  230. static void mesh_sync_vendor_adjust_tbtt(struct ieee80211_sub_if_data *sdata)
  231. {
  232. const u8 *oui;
  233. WARN_ON(sdata->u.mesh.mesh_sp_id != IEEE80211_SYNC_METHOD_VENDOR);
  234. msync_dbg(sdata, "called mesh_sync_vendor_adjust_tbtt\n");
  235. oui = mesh_get_vendor_oui(sdata);
  236. /* here you would implement the vendor tsf adjustment for this oui */
  237. }
  238. /* global variable */
  239. static struct sync_method sync_methods[] = {
  240. {
  241. .method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  242. .ops = {
  243. .rx_bcn_presp = &mesh_sync_offset_rx_bcn_presp,
  244. .adjust_tbtt = &mesh_sync_offset_adjust_tbtt,
  245. }
  246. },
  247. {
  248. .method = IEEE80211_SYNC_METHOD_VENDOR,
  249. .ops = {
  250. .rx_bcn_presp = &mesh_sync_vendor_rx_bcn_presp,
  251. .adjust_tbtt = &mesh_sync_vendor_adjust_tbtt,
  252. }
  253. },
  254. };
  255. struct ieee80211_mesh_sync_ops *ieee80211_mesh_sync_ops_get(u8 method)
  256. {
  257. struct ieee80211_mesh_sync_ops *ops = NULL;
  258. u8 i;
  259. for (i = 0 ; i < ARRAY_SIZE(sync_methods); ++i) {
  260. if (sync_methods[i].method == method) {
  261. ops = &sync_methods[i].ops;
  262. break;
  263. }
  264. }
  265. return ops;
  266. }