blob: 5423b33cf6bc6d82d1c291b875b350bf3322080a [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>
25
26#include <string.h>
27
28static struct msgb *mtp_create_sltm(struct mtp_link *link)
29{
30 const uint8_t test_ptrn[14] = { 'G', 'S', 'M', 'M', 'M', 'S', };
31 struct mtp_level_3_hdr *hdr;
32 struct mtp_level_3_mng *mng;
33 struct msgb *msg = mtp_msg_alloc(link->set);
34 uint8_t *data;
35 if (!msg)
36 return NULL;
37
38 hdr = (struct mtp_level_3_hdr *) msg->l2h;
39 hdr->ser_ind = MTP_SI_MNT_REG_MSG;
40
41 mng = (struct mtp_level_3_mng *) msgb_put(msg, sizeof(*mng));
42 mng->cmn.h0 = MTP_TST_MSG_GRP;
43 mng->cmn.h1 = MTP_TST_MSG_SLTM;
44 mng->length = ARRAY_SIZE(test_ptrn);
45
46 data = msgb_put(msg, ARRAY_SIZE(test_ptrn));
47 memcpy(data, test_ptrn, ARRAY_SIZE(test_ptrn));
48
49 /* remember the last tst ptrn... once we have some */
50 memcpy(link->test_ptrn, test_ptrn, ARRAY_SIZE(test_ptrn));
51
52 return msg;
53}
54
55static void mtp_send_sltm(struct mtp_link *link)
56{
57 struct msgb *msg;
58
59 link->sltm_pending = 1;
60 msg = mtp_create_sltm(link);
61 if (!msg) {
62 LOGP(DINP, LOGL_ERROR, "Failed to allocate SLTM.\n");
63 return;
64 }
65
66 mtp_link_set_trasmit(link, 0, msg);
67}
68
69static void mtp_sltm_t1_timeout(void *_link)
70{
71 struct mtp_link *link = (struct mtp_link *) _link;
72
73 if (link->slta_misses == 0) {
74 LOGP(DINP, LOGL_ERROR, "No SLTM response. Retrying. Link: %p\n", link);
75 ++link->slta_misses;
76 mtp_send_sltm(link);
77 bsc_schedule_timer(&link->t1_timer, MTP_T1);
78 } else {
79 LOGP(DINP, LOGL_ERROR, "Two missing SLTAs. Restart link: %p\n", link);
80 bsc_del_timer(&link->t2_timer);
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +010081 mtp_link_failure(link);
Holger Hans Peter Freythera8ce0612011-01-20 16:30:24 +010082 }
83}
84
85static void mtp_sltm_t2_timeout(void *_link)
86{
87 struct mtp_link *link = (struct mtp_link *) _link;
88
89 if (!link->set->running) {
90 LOGP(DINP, LOGL_INFO, "The linkset is not active. Stopping SLTM now. %p\n", link);
91 return;
92 }
93
94 link->slta_misses = 0;
95 mtp_send_sltm(link);
96
97 bsc_schedule_timer(&link->t1_timer, MTP_T1);
98
99 if (link->set->sltm_once && link->was_up)
100 LOGP(DINP, LOGL_INFO, "Not sending SLTM again as configured.\n");
101 else
102 bsc_schedule_timer(&link->t2_timer, MTP_T2);
103}
104
105void mtp_link_init(struct mtp_link *link)
106{
107 link->t1_timer.data = link;
108 link->t1_timer.cb = mtp_sltm_t1_timeout;
109 link->t2_timer.data = link;
110 link->t2_timer.cb = mtp_sltm_t2_timeout;
111}
112
113void mtp_link_stop_link_test(struct mtp_link *link)
114{
115 bsc_del_timer(&link->t1_timer);
116 bsc_del_timer(&link->t2_timer);
117
118 link->sltm_pending = 0;
119}
120
121void mtp_link_start_link_test(struct mtp_link *link)
122{
123 mtp_sltm_t2_timeout(link);
124}
125
126int mtp_link_slta(struct mtp_link *link, uint16_t l3_len,
127 struct mtp_level_3_mng *mng)
128{
129 if (mng->length != 14) {
130 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
131 return -1;
132 }
133
134 if (l3_len != 16) {
135 LOGP(DINP, LOGL_ERROR, "Wrongly sized SLTA: %u\n", mng->length);
136 return -1;
137 }
138
139 if (memcmp(mng->data, link->test_ptrn, sizeof(link->test_ptrn)) != 0) {
140 LOGP(DINP, LOGL_ERROR, "Wrong test pattern SLTA\n");
141 return -1;
142 }
143
144 /* we had a matching slta */
145 bsc_del_timer(&link->t1_timer);
146 link->sltm_pending = 0;
147 link->was_up = 1;
148
149 return 0;
150}
Holger Hans Peter Freytherfa8cf2d2011-01-20 16:51:34 +0100151
152void mtp_link_failure(struct mtp_link *link)
153{
154 LOGP(DINP, LOGL_ERROR, "Link has failed. Resetting it: 0x%p\n", link);
155 link->reset(link);
156}