subscr.c 5.4 KB
Newer Older
P
Per Liden 已提交
1
/*
2
 * net/tipc/subscr.c: TIPC network topology service
3
 *
4
 * Copyright (c) 2000-2017, Ericsson AB
5
 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems
6
 * Copyright (c) 2020, Red Hat Inc
P
Per Liden 已提交
7 8
 * All rights reserved.
 *
P
Per Liden 已提交
9
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
10 11
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
12 13 14 15 16 17 18 19
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
P
Per Liden 已提交
20
 *
P
Per Liden 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
P
Per Liden 已提交
35 36 37 38 39
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "name_table.h"
40
#include "subscr.h"
P
Per Liden 已提交
41

J
Jon Maloy 已提交
42 43 44
static void tipc_sub_send_event(struct tipc_subscription *sub,
				u32 found_lower, u32 found_upper,
				u32 event, u32 port, u32 node)
P
Per Liden 已提交
45
{
46
	struct tipc_event *evt = &sub->evt;
P
Per Liden 已提交
47

48 49
	if (sub->inactive)
		return;
50 51 52 53 54
	tipc_evt_write(evt, event, event);
	tipc_evt_write(evt, found_lower, found_lower);
	tipc_evt_write(evt, found_upper, found_upper);
	tipc_evt_write(evt, port.ref, port);
	tipc_evt_write(evt, port.node, node);
55
	tipc_topsrv_queue_evt(sub->net, sub->conid, event, evt);
P
Per Liden 已提交
56 57 58
}

/**
J
Jon Maloy 已提交
59
 * tipc_sub_check_overlap - test for subscription overlap with the
60
 * given values
P
Per Liden 已提交
61 62 63
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
64
int tipc_sub_check_overlap(struct tipc_service_range *seq, u32 found_lower,
J
Jon Maloy 已提交
65
			   u32 found_upper)
P
Per Liden 已提交
66
{
67 68 69 70
	if (found_lower < seq->lower)
		found_lower = seq->lower;
	if (found_upper > seq->upper)
		found_upper = seq->upper;
P
Per Liden 已提交
71 72 73 74 75
	if (found_lower > found_upper)
		return 0;
	return 1;
}

J
Jon Maloy 已提交
76 77 78 79
void tipc_sub_report_overlap(struct tipc_subscription *sub,
			     u32 found_lower, u32 found_upper,
			     u32 event, u32 port, u32 node,
			     u32 scope, int must)
80
{
81 82
	struct tipc_subscr *s = &sub->evt.s;
	u32 filter = tipc_sub_read(s, filter);
83
	struct tipc_service_range seq;
84 85 86 87

	seq.type = tipc_sub_read(s, seq.type);
	seq.lower = tipc_sub_read(s, seq.lower);
	seq.upper = tipc_sub_read(s, seq.upper);
88

J
Jon Maloy 已提交
89
	if (!tipc_sub_check_overlap(&seq, found_lower, found_upper))
P
Per Liden 已提交
90
		return;
91

92 93 94 95 96
	if (!must && !(filter & TIPC_SUB_PORTS))
		return;
	if (filter & TIPC_SUB_CLUSTER_SCOPE && scope == TIPC_NODE_SCOPE)
		return;
	if (filter & TIPC_SUB_NODE_SCOPE && scope != TIPC_NODE_SCOPE)
P
Per Liden 已提交
97
		return;
98
	spin_lock(&sub->lock);
J
Jon Maloy 已提交
99 100
	tipc_sub_send_event(sub, found_lower, found_upper,
			    event, port, node);
101
	spin_unlock(&sub->lock);
P
Per Liden 已提交
102 103
}

J
Jon Maloy 已提交
104
static void tipc_sub_timeout(struct timer_list *t)
P
Per Liden 已提交
105
{
106
	struct tipc_subscription *sub = from_timer(sub, t, timer);
107
	struct tipc_subscr *s = &sub->evt.s;
108

109
	spin_lock(&sub->lock);
J
Jon Maloy 已提交
110 111
	tipc_sub_send_event(sub, s->seq.lower, s->seq.upper,
			    TIPC_SUBSCR_TIMEOUT, 0, 0);
112 113
	sub->inactive = true;
	spin_unlock(&sub->lock);
114 115
}

J
Jon Maloy 已提交
116
static void tipc_sub_kref_release(struct kref *kref)
117
{
118
	kfree(container_of(kref, struct tipc_subscription, kref));
119 120
}

J
Jon Maloy 已提交
121
void tipc_sub_put(struct tipc_subscription *subscription)
122
{
J
Jon Maloy 已提交
123
	kref_put(&subscription->kref, tipc_sub_kref_release);
124 125
}

J
Jon Maloy 已提交
126
void tipc_sub_get(struct tipc_subscription *subscription)
127 128 129 130
{
	kref_get(&subscription->kref);
}

131
struct tipc_subscription *tipc_sub_subscribe(struct net *net,
132 133
					     struct tipc_subscr *s,
					     int conid)
134
{
135
	u32 filter = tipc_sub_read(s, filter);
J
Jon Maloy 已提交
136
	struct tipc_subscription *sub;
137
	u32 timeout;
138

139 140 141
	if ((filter & TIPC_SUB_PORTS && filter & TIPC_SUB_SERVICE) ||
	    (tipc_sub_read(s, seq.lower) > tipc_sub_read(s, seq.upper))) {
		pr_warn("Subscription rejected, illegal request\n");
142
		return NULL;
P
Per Liden 已提交
143
	}
144
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
145
	if (!sub) {
146
		pr_warn("Subscription rejected, no memory\n");
147
		return NULL;
P
Per Liden 已提交
148
	}
149 150
	INIT_LIST_HEAD(&sub->service_list);
	INIT_LIST_HEAD(&sub->sub_list);
151
	sub->net = net;
152 153
	sub->conid = conid;
	sub->inactive = false;
154
	memcpy(&sub->evt.s, s, sizeof(*s));
155
	spin_lock_init(&sub->lock);
156
	kref_init(&sub->kref);
157 158 159 160
	if (!tipc_nametbl_subscribe(sub)) {
		kfree(sub);
		return NULL;
	}
J
Jon Maloy 已提交
161
	timer_setup(&sub->timer, tipc_sub_timeout, 0);
162
	timeout = tipc_sub_read(&sub->evt.s, timeout);
163 164
	if (timeout != TIPC_WAIT_FOREVER)
		mod_timer(&sub->timer, jiffies + msecs_to_jiffies(timeout));
165
	return sub;
P
Per Liden 已提交
166 167
}

168
void tipc_sub_unsubscribe(struct tipc_subscription *sub)
P
Per Liden 已提交
169
{
170 171 172
	tipc_nametbl_unsubscribe(sub);
	if (sub->evt.s.timeout != TIPC_WAIT_FOREVER)
		del_timer_sync(&sub->timer);
J
Jon Maloy 已提交
173 174
	list_del(&sub->sub_list);
	tipc_sub_put(sub);
P
Per Liden 已提交
175
}