1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/in.h>
36#include <net/net_namespace.h>
37#include <net/netns/generic.h>
38
39#include "rds_single_path.h"
40#include "rds.h"
41#include "loop.h"
42
43static DEFINE_SPINLOCK(loop_conns_lock);
44static LIST_HEAD(loop_conns);
45static atomic_t rds_loop_unloading = ATOMIC_INIT(0);
46
47static void rds_loop_set_unloading(void)
48{
49 atomic_set(&rds_loop_unloading, 1);
50}
51
52static bool rds_loop_is_unloading(struct rds_connection *conn)
53{
54 return atomic_read(&rds_loop_unloading) != 0;
55}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
75 unsigned int hdr_off, unsigned int sg,
76 unsigned int off)
77{
78 struct scatterlist *sgp = &rm->data.op_sg[sg];
79 int ret = sizeof(struct rds_header) +
80 be32_to_cpu(rm->m_inc.i_hdr.h_len);
81
82
83 if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
84 rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
85 ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
86 goto out;
87 }
88
89 BUG_ON(hdr_off || sg || off);
90
91 rds_inc_init(&rm->m_inc, conn, conn->c_laddr);
92
93 rds_message_addref(rm);
94
95 rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
96 GFP_KERNEL);
97
98 rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
99 NULL);
100
101 rds_inc_put(&rm->m_inc);
102out:
103 return ret;
104}
105
106
107
108
109
110static void rds_loop_inc_free(struct rds_incoming *inc)
111{
112 struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
113
114 rds_message_put(rm);
115}
116
117
118static int rds_loop_recv_path(struct rds_conn_path *cp)
119{
120 return 0;
121}
122
123struct rds_loop_connection {
124 struct list_head loop_node;
125 struct rds_connection *conn;
126};
127
128
129
130
131
132
133
134static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
135{
136 struct rds_loop_connection *lc;
137 unsigned long flags;
138
139 lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
140 if (!lc)
141 return -ENOMEM;
142
143 INIT_LIST_HEAD(&lc->loop_node);
144 lc->conn = conn;
145 conn->c_transport_data = lc;
146
147 spin_lock_irqsave(&loop_conns_lock, flags);
148 list_add_tail(&lc->loop_node, &loop_conns);
149 spin_unlock_irqrestore(&loop_conns_lock, flags);
150
151 return 0;
152}
153
154static void rds_loop_conn_free(void *arg)
155{
156 struct rds_loop_connection *lc = arg;
157 unsigned long flags;
158
159 rdsdebug("lc %p\n", lc);
160 spin_lock_irqsave(&loop_conns_lock, flags);
161 list_del(&lc->loop_node);
162 spin_unlock_irqrestore(&loop_conns_lock, flags);
163 kfree(lc);
164}
165
166static int rds_loop_conn_path_connect(struct rds_conn_path *cp)
167{
168 rds_connect_complete(cp->cp_conn);
169 return 0;
170}
171
172static void rds_loop_conn_path_shutdown(struct rds_conn_path *cp)
173{
174}
175
176void rds_loop_exit(void)
177{
178 struct rds_loop_connection *lc, *_lc;
179 LIST_HEAD(tmp_list);
180
181 rds_loop_set_unloading();
182 synchronize_rcu();
183
184 spin_lock_irq(&loop_conns_lock);
185 list_splice(&loop_conns, &tmp_list);
186 INIT_LIST_HEAD(&loop_conns);
187 spin_unlock_irq(&loop_conns_lock);
188
189 list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
190 WARN_ON(lc->conn->c_passive);
191 rds_conn_destroy(lc->conn);
192 }
193}
194
195static void rds_loop_kill_conns(struct net *net)
196{
197 struct rds_loop_connection *lc, *_lc;
198 LIST_HEAD(tmp_list);
199
200 spin_lock_irq(&loop_conns_lock);
201 list_for_each_entry_safe(lc, _lc, &loop_conns, loop_node) {
202 struct net *c_net = read_pnet(&lc->conn->c_net);
203
204 if (net != c_net)
205 continue;
206 list_move_tail(&lc->loop_node, &tmp_list);
207 }
208 spin_unlock_irq(&loop_conns_lock);
209
210 list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
211 WARN_ON(lc->conn->c_passive);
212 rds_conn_destroy(lc->conn);
213 }
214}
215
216static void __net_exit rds_loop_exit_net(struct net *net)
217{
218 rds_loop_kill_conns(net);
219}
220
221static struct pernet_operations rds_loop_net_ops = {
222 .exit = rds_loop_exit_net,
223};
224
225int rds_loop_net_init(void)
226{
227 return register_pernet_device(&rds_loop_net_ops);
228}
229
230void rds_loop_net_exit(void)
231{
232 unregister_pernet_device(&rds_loop_net_ops);
233}
234
235
236
237
238
239
240
241struct rds_transport rds_loop_transport = {
242 .xmit = rds_loop_xmit,
243 .recv_path = rds_loop_recv_path,
244 .conn_alloc = rds_loop_conn_alloc,
245 .conn_free = rds_loop_conn_free,
246 .conn_path_connect = rds_loop_conn_path_connect,
247 .conn_path_shutdown = rds_loop_conn_path_shutdown,
248 .inc_copy_to_user = rds_message_inc_copy_to_user,
249 .inc_free = rds_loop_inc_free,
250 .t_name = "loopback",
251 .t_type = RDS_TRANS_LOOP,
252 .t_unloading = rds_loop_is_unloading,
253};
254