mesh_sync.c 9.2 KB

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