blob: 7133718fc0e3023f5925a2f3bfc534d29c8464cb [file] [log] [blame]
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +01001/* MTP level3 link */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <mtp_data.h>
23#include <mtp_level3.h>
24#include <cellmgr_debug.h>
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +010025#include <counter.h>
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010026
27#include <string.h>
28
29static struct msgb *mtp_create_sltm(struct mtp_link *link)
30{
31 const uint8_t test_ptrn[14] = { 'G', 'S', 'M', 'M', 'M', 'S', };
32 struct mtp_level_3_hdr *hdr;
33 struct mtp_level_3_mng *mng;
34 struct msgb *msg = mtp_msg_alloc(link->set);
35 uint8_t *data;
36 if (!msg)
37 return NULL;
38
39 hdr = (struct mtp_level_3_hdr *) msg->l2h;
40 hdr->ser_ind = MTP_SI_MNT_REG_MSG;
Holger Hans Peter Freyther050577a2011-01-20 19:28:15 +010041 hdr->addr = MTP_ADDR(link->link_no % 16, link->set->dpc, link->set->opc);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010042
43 mng = (struct mtp_level_3_mng *) msgb_put(msg, sizeof(*mng));
44 mng->cmn.h0 = MTP_TST_MSG_GRP;
45 mng->cmn.h1 = MTP_TST_MSG_SLTM;
46 mng->length = ARRAY_SIZE(test_ptrn);
47
48 data = msgb_put(msg, ARRAY_SIZE(test_ptrn));
49 memcpy(data, test_ptrn, ARRAY_SIZE(test_ptrn));
50
51 /* remember the last tst ptrn... once we have some */
52 memcpy(link->test_ptrn, test_ptrn, ARRAY_SIZE(test_ptrn));
53
54 return msg;
55}
56
57static void mtp_send_sltm(struct mtp_link *link)
58{
59 struct msgb *msg;
60
61 link->sltm_pending = 1;
62 msg = mtp_create_sltm(link);
63 if (!msg) {
64 LOGP(DINP, LOGL_ERROR, "Failed to allocate SLTM.\n");
65 return;
66 }
67
Holger Hans Peter Freyther9543f4a2011-01-24 20:49:58 +010068 mtp_link_submit(link, msg);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010069}
70
71static void mtp_sltm_t1_timeout(void *_link)
72{
73 struct mtp_link *link = (struct mtp_link *) _link;
74
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +010075 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_SLTM_TOUT]);
76
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010077 if (link->slta_misses == 0) {
78 LOGP(DINP, LOGL_ERROR, "No SLTM response. Retrying. Link: %p\n", link);
79 ++link->slta_misses;
80 mtp_send_sltm(link);
81 bsc_schedule_timer(&link->t1_timer, MTP_T1);
82 } else {
83 LOGP(DINP, LOGL_ERROR, "Two missing SLTAs. Restart link: %p\n", link);
84 bsc_del_timer(&link->t2_timer);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +010085 mtp_link_failure(link);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010086 }
87}
88
89static void mtp_sltm_t2_timeout(void *_link)
90{
91 struct mtp_link *link = (struct mtp_link *) _link;
92
93 if (!link->set->running) {
94 LOGP(DINP, LOGL_INFO, "The linkset is not active. Stopping SLTM now. %p\n", link);
95 return;
96 }
97
98 link->slta_misses = 0;
99 mtp_send_sltm(link);
100
101 bsc_schedule_timer(&link->t1_timer, MTP_T1);
102
103 if (link->set->sltm_once && link->was_up)
104 LOGP(DINP, LOGL_INFO, "Not sending SLTM again as configured.\n");
105 else
106 bsc_schedule_timer(&link->t2_timer, MTP_T2);
107}
108
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100109int mtp_link_init(struct mtp_link *link)
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100110{
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100111 link->ctrg = rate_ctr_group_alloc(link,
112 mtp_link_rate_ctr_desc(), link->link_no);
113 if (!link->ctrg) {
114 LOGP(DINP, LOGL_ERROR, "Failed to allocate rate_ctr.\n");
115 return -1;
116 }
117
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100118 link->t1_timer.data = link;
119 link->t1_timer.cb = mtp_sltm_t1_timeout;
120 link->t2_timer.data = link;
121 link->t2_timer.cb = mtp_sltm_t2_timeout;
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100122 return 0;
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +0100123}
124
125void mtp_link_stop_link_test(struct mtp_link *link)
126{
127 bsc_del_timer(&link->t1_timer);
128 bsc_del_timer(&link->t2_timer);
129
130 link->sltm_pending = 0;
131}
132
133void mtp_link_start_link_test(struct mtp_link *link)
134{
135 mtp_sltm_t2_timeout(link);
136}
137
138int mtp_link_slta(struct mtp_link *link, uint16_t l3_len,
139 struct mtp_level_3_mng *mng)
140{
141 if (mng->length != 14) {
142 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
143 return -1;
144 }
145
146 if (l3_len != 16) {
147 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
148 return -1;
149 }
150
151 if (memcmp(mng->data, link->test_ptrn, sizeof(link->test_ptrn)) != 0) {
152 LOGP(DINP, LOGL_ERROR, "Wrong test pattern SLTA\n");
153 return -1;
154 }
155
156 /* we had a matching slta */
157 bsc_del_timer(&link->t1_timer);
158 link->sltm_pending = 0;
159 link->was_up = 1;
160
161 return 0;
162}
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100163
164void mtp_link_failure(struct mtp_link *link)
165{
166 LOGP(DINP, LOGL_ERROR, "Link has failed. Resetting it: 0x%p\n", link);
Holger Hans Peter Freyther4c1eb0e2011-01-22 15:52:07 +0100167 rate_ctr_inc(&link->ctrg->ctr[MTP_LNK_ERROR]);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100168 link->reset(link);
169}
Holger Hans Peter Freytherea5ce232011-01-23 23:31:26 +0100170
171void mtp_link_block(struct mtp_link *link)
172{
173 link->blocked = 1;
174 link->shutdown(link);
175}
176
177void mtp_link_unblock(struct mtp_link *link)
178{
179 if (!link->blocked)
180 return;
181 link->blocked = 0;
182 link->reset(link);
183}