blob: 998e01c71476c9345505c5116efb656adff17d4c [file] [log] [blame]
Andreas Eversberg050ace22013-03-16 16:22:02 +01001/* Measurements
2 *
3 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
Pau Espin Pedrol488aa292019-09-25 17:48:35 +020019
Stefan Sperling78ab6242018-05-31 12:28:55 +020020#include <osmocom/core/timer_compat.h>
21
Andreas Eversberg050ace22013-03-16 16:22:02 +010022#include <gprs_rlcmac.h>
23#include <gprs_debug.h>
24#include <pcu_l1_if.h>
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +020025#include <tbf.h>
Pau Espin Pedrol9d1cdb12019-09-25 17:47:02 +020026#include <tbf_dl.h>
Andreas Eversberg050ace22013-03-16 16:22:02 +010027
28#include <string.h>
29#include <errno.h>
30
31/*
32 * downlink measurement
33 */
Max5a6bcfb2017-09-01 14:36:44 +020034/* TODO: trigger the measurement report from the pollcontroller and use it for flow control */
Andreas Eversberg050ace22013-03-16 16:22:02 +010035
36/* received Measurement Report */
37int gprs_rlcmac_meas_rep(Packet_Measurement_Report_t *pmr)
38{
39 NC_Measurement_Report_t *ncr;
40 NC_Measurements_t *nc;
41 int i;
42
43 LOGP(DRLCMACMEAS, LOGL_INFO, "Measuement Report of TLLI=0x%08x:",
44 pmr->TLLI);
45
46 switch (pmr->UnionType) {
47 case 0:
48 ncr = &pmr->u.NC_Measurement_Report;
49 LOGPC(DRLCMACMEAS, LOGL_INFO, " NC%u Serv %d dbm",
50 ncr->NC_MODE + 1,
51 ncr->Serving_Cell_Data.RXLEV_SERVING_CELL - 110);
52 for (i = 0; i < ncr->NUMBER_OF_NC_MEASUREMENTS; i++) {
53 nc = &ncr->NC_Measurements[i];
54 LOGPC(DRLCMACMEAS, LOGL_DEBUG, ", Neigh %u %d dbm",
55 nc->FREQUENCY_N, nc->RXLEV_N - 110);
56 }
57 LOGPC(DRLCMACMEAS, LOGL_INFO, "\n");
58
59 break;
60 case 1:
61 LOGPC(DRLCMACMEAS, LOGL_INFO,
62 " <EXT Reporting not supported!>\n");
63 break;
64 }
65
66 return 0;
67}
68
69
70/*
71 * uplink measurement
72 */
73
74/* RSSI values received from MS */
75int gprs_rlcmac_rssi(struct gprs_rlcmac_tbf *tbf, int8_t rssi)
76{
Stefan Sperlingf0f7df12018-05-25 15:12:30 +020077 struct timespec now_tv, *rssi_tv = &tbf->meas.rssi_tv;
Stefan Sperling78ab6242018-05-31 12:28:55 +020078 struct timespec elapsed;
Andreas Eversberg050ace22013-03-16 16:22:02 +010079
80 tbf->meas.rssi_sum += rssi;
81 tbf->meas.rssi_num++;
82
Stefan Sperlingf0f7df12018-05-25 15:12:30 +020083 osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
Stefan Sperling78ab6242018-05-31 12:28:55 +020084
85 timespecsub(&now_tv, rssi_tv, &elapsed);
86 if (elapsed.tv_sec < 1)
Andreas Eversberg050ace22013-03-16 16:22:02 +010087 return 0;
88
89 gprs_rlcmac_rssi_rep(tbf);
90
91 /* reset rssi values and timestamp */
Stefan Sperlingf0f7df12018-05-25 15:12:30 +020092 memcpy(rssi_tv, &now_tv, sizeof(*rssi_tv));
Andreas Eversberg050ace22013-03-16 16:22:02 +010093 tbf->meas.rssi_sum = 0;
94 tbf->meas.rssi_num = 0;
95
96 return 0;
97}
98
99/* Give RSSI report */
100int gprs_rlcmac_rssi_rep(struct gprs_rlcmac_tbf *tbf)
101{
102 /* No measurement values */
103 if (!tbf->meas.rssi_num)
104 return -EINVAL;
105
106 LOGP(DRLCMACMEAS, LOGL_INFO, "UL RSSI of TLLI=0x%08x: %d dBm\n",
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100107 tbf->tlli(), tbf->meas.rssi_sum / tbf->meas.rssi_num);
Andreas Eversberg050ace22013-03-16 16:22:02 +0100108
109 return 0;
110}
111
112
113/*
114 * lost frames
115 */
116
117/* Lost frames reported from RLCMAC layer */
Daniel Willmann418a4232014-08-08 11:21:04 +0200118int gprs_rlcmac_received_lost(struct gprs_rlcmac_dl_tbf *tbf, uint16_t received,
Andreas Eversberg050ace22013-03-16 16:22:02 +0100119 uint16_t lost)
120{
Stefan Sperlingf0f7df12018-05-25 15:12:30 +0200121 struct timespec now_tv, *loss_tv = &tbf->m_bw.dl_loss_tv;
Stefan Sperling78ab6242018-05-31 12:28:55 +0200122 struct timespec elapsed;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100123 uint16_t sum = received + lost;
124
125 /* No measurement values */
126 if (!sum)
127 return -EINVAL;
128
129 LOGP(DRLCMACMEAS, LOGL_DEBUG, "DL Loss of TLLI 0x%08x: Received: %4d "
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100130 "Lost: %4d Sum: %4d\n", tbf->tlli(), received, lost, sum);
Andreas Eversberg050ace22013-03-16 16:22:02 +0100131
Daniel Willmann418a4232014-08-08 11:21:04 +0200132 tbf->m_bw.dl_loss_received += received;
133 tbf->m_bw.dl_loss_lost += lost;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100134
Harald Welte099f4f22018-10-21 11:50:10 +0200135 osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
Stefan Sperling78ab6242018-05-31 12:28:55 +0200136 timespecsub(&now_tv, loss_tv, &elapsed);
137 if (elapsed.tv_sec < 1)
Andreas Eversberg050ace22013-03-16 16:22:02 +0100138 return 0;
139
140 gprs_rlcmac_lost_rep(tbf);
141
142 /* reset lost values and timestamp */
Stefan Sperlingf0f7df12018-05-25 15:12:30 +0200143 memcpy(loss_tv, &now_tv, sizeof(*loss_tv));
Daniel Willmann418a4232014-08-08 11:21:04 +0200144 tbf->m_bw.dl_loss_received = 0;
145 tbf->m_bw.dl_loss_lost = 0;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100146
147 return 0;
148}
149
150/* Give Lost report */
Daniel Willmann418a4232014-08-08 11:21:04 +0200151int gprs_rlcmac_lost_rep(struct gprs_rlcmac_dl_tbf *tbf)
Andreas Eversberg050ace22013-03-16 16:22:02 +0100152{
Daniel Willmann418a4232014-08-08 11:21:04 +0200153 uint16_t sum = tbf->m_bw.dl_loss_lost + tbf->m_bw.dl_loss_received;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100154
155 /* No measurement values */
156 if (!sum)
157 return -EINVAL;
158
Stefan Sperling082443d2018-06-05 11:37:00 +0200159 LOGP(DRLCMACMEAS, LOGL_DEBUG, "DL packet loss of IMSI=%s / TLLI=0x%08x: "
Holger Hans Peter Freyther5464c9b2013-10-27 20:57:35 +0100160 "%d%%\n", tbf->imsi(), tbf->tlli(),
Daniel Willmann418a4232014-08-08 11:21:04 +0200161 tbf->m_bw.dl_loss_lost * 100 / sum);
Andreas Eversberg050ace22013-03-16 16:22:02 +0100162
163 return 0;
164}
165
166
167/*
168 * downlink bandwidth
169 */
170
Daniel Willmann418a4232014-08-08 11:21:04 +0200171int gprs_rlcmac_dl_bw(struct gprs_rlcmac_dl_tbf *tbf, uint16_t octets)
Andreas Eversberg050ace22013-03-16 16:22:02 +0100172{
Stefan Sperlingf0f7df12018-05-25 15:12:30 +0200173 struct timespec now_tv, *bw_tv = &tbf->m_bw.dl_bw_tv;
Stefan Sperling78ab6242018-05-31 12:28:55 +0200174 struct timespec elapsed;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100175
Daniel Willmann418a4232014-08-08 11:21:04 +0200176 tbf->m_bw.dl_bw_octets += octets;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100177
Stefan Sperlingf0f7df12018-05-25 15:12:30 +0200178 osmo_clock_gettime(CLOCK_MONOTONIC, &now_tv);
Stefan Sperling78ab6242018-05-31 12:28:55 +0200179 timespecsub(&now_tv, bw_tv, &elapsed);
180 if (elapsed.tv_sec < 1)
Andreas Eversberg050ace22013-03-16 16:22:02 +0100181 return 0;
182
Stefan Sperling78ab6242018-05-31 12:28:55 +0200183 tbf->m_bw.dl_throughput = (tbf->m_bw.dl_bw_octets << 10) / ((elapsed.tv_sec << 10) + (elapsed.tv_nsec >> 20));
Andreas Eversberg050ace22013-03-16 16:22:02 +0100184 LOGP(DRLCMACMEAS, LOGL_INFO, "DL Bandwitdh of IMSI=%s / TLLI=0x%08x: "
Stefan Sperling78ab6242018-05-31 12:28:55 +0200185 "%d KBits/s\n", tbf->imsi(), tbf->tlli(), tbf->m_bw.dl_throughput);
Andreas Eversberg050ace22013-03-16 16:22:02 +0100186
187 /* reset bandwidth values timestamp */
Stefan Sperlingf0f7df12018-05-25 15:12:30 +0200188 memcpy(bw_tv, &now_tv, sizeof(*bw_tv));
Daniel Willmann418a4232014-08-08 11:21:04 +0200189 tbf->m_bw.dl_bw_octets = 0;
Andreas Eversberg050ace22013-03-16 16:22:02 +0100190
191 return 0;
192}