blob: 5a75be43950f6bebe244c5b30bc4f3e2cdf44d40 [file] [log] [blame]
Atul Gupta36bedb32018-03-31 21:42:00 +05301/*
2 * Copyright (c) 2018 Chelsio Communications, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Written by: Atul Gupta (atul.gupta@chelsio.com)
9 */
10
11#include <linux/module.h>
12#include <linux/list.h>
13#include <linux/workqueue.h>
14#include <linux/skbuff.h>
15#include <linux/timer.h>
16#include <linux/notifier.h>
17#include <linux/inetdevice.h>
18#include <linux/ip.h>
19#include <linux/tcp.h>
20#include <linux/sched/signal.h>
21#include <net/tcp.h>
22#include <net/busy_poll.h>
23#include <crypto/aes.h>
24
25#include "chtls.h"
26#include "chtls_cm.h"
27
28static bool is_tls_tx(struct chtls_sock *csk)
29{
30 return csk->tlshws.txkey >= 0;
31}
32
Atul Guptab6479932018-03-31 21:42:01 +053033static bool is_tls_rx(struct chtls_sock *csk)
34{
35 return csk->tlshws.rxkey >= 0;
36}
37
Atul Gupta36bedb32018-03-31 21:42:00 +053038static int data_sgl_len(const struct sk_buff *skb)
39{
40 unsigned int cnt;
41
42 cnt = skb_shinfo(skb)->nr_frags;
43 return sgl_len(cnt) * 8;
44}
45
46static int nos_ivs(struct sock *sk, unsigned int size)
47{
48 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
49
50 return DIV_ROUND_UP(size, csk->tlshws.mfs);
51}
52
53static int set_ivs_imm(struct sock *sk, const struct sk_buff *skb)
54{
55 int ivs_size = nos_ivs(sk, skb->len) * CIPHER_BLOCK_SIZE;
56 int hlen = TLS_WR_CPL_LEN + data_sgl_len(skb);
57
58 if ((hlen + KEY_ON_MEM_SZ + ivs_size) <
59 MAX_IMM_OFLD_TX_DATA_WR_LEN) {
60 ULP_SKB_CB(skb)->ulp.tls.iv = 1;
61 return 1;
62 }
63 ULP_SKB_CB(skb)->ulp.tls.iv = 0;
64 return 0;
65}
66
67static int max_ivs_size(struct sock *sk, int size)
68{
69 return nos_ivs(sk, size) * CIPHER_BLOCK_SIZE;
70}
71
72static int ivs_size(struct sock *sk, const struct sk_buff *skb)
73{
74 return set_ivs_imm(sk, skb) ? (nos_ivs(sk, skb->len) *
75 CIPHER_BLOCK_SIZE) : 0;
76}
77
78static int flowc_wr_credits(int nparams, int *flowclenp)
79{
80 int flowclen16, flowclen;
81
82 flowclen = offsetof(struct fw_flowc_wr, mnemval[nparams]);
83 flowclen16 = DIV_ROUND_UP(flowclen, 16);
84 flowclen = flowclen16 * 16;
85
86 if (flowclenp)
87 *flowclenp = flowclen;
88
89 return flowclen16;
90}
91
92static struct sk_buff *create_flowc_wr_skb(struct sock *sk,
93 struct fw_flowc_wr *flowc,
94 int flowclen)
95{
96 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
97 struct sk_buff *skb;
98
99 skb = alloc_skb(flowclen, GFP_ATOMIC);
100 if (!skb)
101 return NULL;
102
103 memcpy(__skb_put(skb, flowclen), flowc, flowclen);
104 skb_set_queue_mapping(skb, (csk->txq_idx << 1) | CPL_PRIORITY_DATA);
105
106 return skb;
107}
108
109static int send_flowc_wr(struct sock *sk, struct fw_flowc_wr *flowc,
110 int flowclen)
111{
112 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
113 struct tcp_sock *tp = tcp_sk(sk);
Atul Gupta36bedb32018-03-31 21:42:00 +0530114 struct sk_buff *skb;
Atul Guptab6479932018-03-31 21:42:01 +0530115 int flowclen16;
Atul Gupta36bedb32018-03-31 21:42:00 +0530116 int ret;
117
Atul Guptab6479932018-03-31 21:42:01 +0530118 flowclen16 = flowclen / 16;
119
Atul Gupta36bedb32018-03-31 21:42:00 +0530120 if (csk_flag(sk, CSK_TX_DATA_SENT)) {
121 skb = create_flowc_wr_skb(sk, flowc, flowclen);
122 if (!skb)
123 return -ENOMEM;
124
125 skb_entail(sk, skb,
126 ULPCB_FLAG_NO_HDR | ULPCB_FLAG_NO_APPEND);
127 return 0;
128 }
129
130 ret = cxgb4_immdata_send(csk->egress_dev,
131 csk->txq_idx,
132 flowc, flowclen);
133 if (!ret)
134 return flowclen16;
135 skb = create_flowc_wr_skb(sk, flowc, flowclen);
136 if (!skb)
137 return -ENOMEM;
138 send_or_defer(sk, tp, skb, 0);
139 return flowclen16;
140}
141
142static u8 tcp_state_to_flowc_state(u8 state)
143{
144 switch (state) {
145 case TCP_ESTABLISHED:
146 return FW_FLOWC_MNEM_TCPSTATE_ESTABLISHED;
147 case TCP_CLOSE_WAIT:
148 return FW_FLOWC_MNEM_TCPSTATE_CLOSEWAIT;
149 case TCP_FIN_WAIT1:
150 return FW_FLOWC_MNEM_TCPSTATE_FINWAIT1;
151 case TCP_CLOSING:
152 return FW_FLOWC_MNEM_TCPSTATE_CLOSING;
153 case TCP_LAST_ACK:
154 return FW_FLOWC_MNEM_TCPSTATE_LASTACK;
155 case TCP_FIN_WAIT2:
156 return FW_FLOWC_MNEM_TCPSTATE_FINWAIT2;
157 }
158
159 return FW_FLOWC_MNEM_TCPSTATE_ESTABLISHED;
160}
161
162int send_tx_flowc_wr(struct sock *sk, int compl,
163 u32 snd_nxt, u32 rcv_nxt)
164{
165 struct flowc_packed {
166 struct fw_flowc_wr fc;
167 struct fw_flowc_mnemval mnemval[FW_FLOWC_MNEM_MAX];
168 } __packed sflowc;
169 int nparams, paramidx, flowclen16, flowclen;
170 struct fw_flowc_wr *flowc;
171 struct chtls_sock *csk;
172 struct tcp_sock *tp;
173
174 csk = rcu_dereference_sk_user_data(sk);
175 tp = tcp_sk(sk);
176 memset(&sflowc, 0, sizeof(sflowc));
177 flowc = &sflowc.fc;
178
179#define FLOWC_PARAM(__m, __v) \
180 do { \
181 flowc->mnemval[paramidx].mnemonic = FW_FLOWC_MNEM_##__m; \
182 flowc->mnemval[paramidx].val = cpu_to_be32(__v); \
183 paramidx++; \
184 } while (0)
185
186 paramidx = 0;
187
188 FLOWC_PARAM(PFNVFN, FW_PFVF_CMD_PFN_V(csk->cdev->lldi->pf));
189 FLOWC_PARAM(CH, csk->tx_chan);
190 FLOWC_PARAM(PORT, csk->tx_chan);
191 FLOWC_PARAM(IQID, csk->rss_qid);
192 FLOWC_PARAM(SNDNXT, tp->snd_nxt);
193 FLOWC_PARAM(RCVNXT, tp->rcv_nxt);
194 FLOWC_PARAM(SNDBUF, csk->sndbuf);
195 FLOWC_PARAM(MSS, tp->mss_cache);
196 FLOWC_PARAM(TCPSTATE, tcp_state_to_flowc_state(sk->sk_state));
197
198 if (SND_WSCALE(tp))
199 FLOWC_PARAM(RCV_SCALE, SND_WSCALE(tp));
200
201 if (csk->ulp_mode == ULP_MODE_TLS)
202 FLOWC_PARAM(ULD_MODE, ULP_MODE_TLS);
203
204 if (csk->tlshws.fcplenmax)
205 FLOWC_PARAM(TXDATAPLEN_MAX, csk->tlshws.fcplenmax);
206
207 nparams = paramidx;
208#undef FLOWC_PARAM
209
210 flowclen16 = flowc_wr_credits(nparams, &flowclen);
211 flowc->op_to_nparams =
212 cpu_to_be32(FW_WR_OP_V(FW_FLOWC_WR) |
213 FW_WR_COMPL_V(compl) |
214 FW_FLOWC_WR_NPARAMS_V(nparams));
215 flowc->flowid_len16 = cpu_to_be32(FW_WR_LEN16_V(flowclen16) |
216 FW_WR_FLOWID_V(csk->tid));
217
218 return send_flowc_wr(sk, flowc, flowclen);
219}
220
221/* Copy IVs to WR */
222static int tls_copy_ivs(struct sock *sk, struct sk_buff *skb)
223
224{
225 struct chtls_sock *csk;
226 unsigned char *iv_loc;
227 struct chtls_hws *hws;
228 unsigned char *ivs;
229 u16 number_of_ivs;
230 struct page *page;
231 int err = 0;
232
233 csk = rcu_dereference_sk_user_data(sk);
234 hws = &csk->tlshws;
235 number_of_ivs = nos_ivs(sk, skb->len);
236
237 if (number_of_ivs > MAX_IVS_PAGE) {
238 pr_warn("MAX IVs in PAGE exceeded %d\n", number_of_ivs);
239 return -ENOMEM;
240 }
241
242 /* generate the IVs */
243 ivs = kmalloc(number_of_ivs * CIPHER_BLOCK_SIZE, GFP_ATOMIC);
244 if (!ivs)
245 return -ENOMEM;
246 get_random_bytes(ivs, number_of_ivs * CIPHER_BLOCK_SIZE);
247
248 if (skb_ulp_tls_iv_imm(skb)) {
249 /* send the IVs as immediate data in the WR */
250 iv_loc = (unsigned char *)__skb_push(skb, number_of_ivs *
251 CIPHER_BLOCK_SIZE);
252 if (iv_loc)
253 memcpy(iv_loc, ivs, number_of_ivs * CIPHER_BLOCK_SIZE);
254
255 hws->ivsize = number_of_ivs * CIPHER_BLOCK_SIZE;
256 } else {
257 /* Send the IVs as sgls */
258 /* Already accounted IV DSGL for credits */
259 skb_shinfo(skb)->nr_frags--;
260 page = alloc_pages(sk->sk_allocation | __GFP_COMP, 0);
261 if (!page) {
262 pr_info("%s : Page allocation for IVs failed\n",
263 __func__);
264 err = -ENOMEM;
265 goto out;
266 }
267 memcpy(page_address(page), ivs, number_of_ivs *
268 CIPHER_BLOCK_SIZE);
269 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0,
270 number_of_ivs * CIPHER_BLOCK_SIZE);
271 hws->ivsize = 0;
272 }
273out:
274 kfree(ivs);
275 return err;
276}
277
278/* Copy Key to WR */
279static void tls_copy_tx_key(struct sock *sk, struct sk_buff *skb)
280{
281 struct ulptx_sc_memrd *sc_memrd;
282 struct chtls_sock *csk;
283 struct chtls_dev *cdev;
284 struct ulptx_idata *sc;
285 struct chtls_hws *hws;
286 u32 immdlen;
287 int kaddr;
288
289 csk = rcu_dereference_sk_user_data(sk);
290 hws = &csk->tlshws;
291 cdev = csk->cdev;
292
293 immdlen = sizeof(*sc) + sizeof(*sc_memrd);
294 kaddr = keyid_to_addr(cdev->kmap.start, hws->txkey);
295 sc = (struct ulptx_idata *)__skb_push(skb, immdlen);
296 if (sc) {
297 sc->cmd_more = htonl(ULPTX_CMD_V(ULP_TX_SC_NOOP));
298 sc->len = htonl(0);
299 sc_memrd = (struct ulptx_sc_memrd *)(sc + 1);
300 sc_memrd->cmd_to_len =
301 htonl(ULPTX_CMD_V(ULP_TX_SC_MEMRD) |
302 ULP_TX_SC_MORE_V(1) |
303 ULPTX_LEN16_V(hws->keylen >> 4));
304 sc_memrd->addr = htonl(kaddr);
305 }
306}
307
308static u64 tlstx_incr_seqnum(struct chtls_hws *hws)
309{
310 return hws->tx_seq_no++;
311}
312
313static bool is_sg_request(const struct sk_buff *skb)
314{
315 return skb->peeked ||
316 (skb->len > MAX_IMM_ULPTX_WR_LEN);
317}
318
319/*
320 * Returns true if an sk_buff carries urgent data.
321 */
322static bool skb_urgent(struct sk_buff *skb)
323{
324 return ULP_SKB_CB(skb)->flags & ULPCB_FLAG_URG;
325}
326
327/* TLS content type for CPL SFO */
328static unsigned char tls_content_type(unsigned char content_type)
329{
330 switch (content_type) {
331 case TLS_HDR_TYPE_CCS:
332 return CPL_TX_TLS_SFO_TYPE_CCS;
333 case TLS_HDR_TYPE_ALERT:
334 return CPL_TX_TLS_SFO_TYPE_ALERT;
335 case TLS_HDR_TYPE_HANDSHAKE:
336 return CPL_TX_TLS_SFO_TYPE_HANDSHAKE;
337 case TLS_HDR_TYPE_HEARTBEAT:
338 return CPL_TX_TLS_SFO_TYPE_HEARTBEAT;
339 }
340 return CPL_TX_TLS_SFO_TYPE_DATA;
341}
342
343static void tls_tx_data_wr(struct sock *sk, struct sk_buff *skb,
344 int dlen, int tls_immd, u32 credits,
345 int expn, int pdus)
346{
347 struct fw_tlstx_data_wr *req_wr;
348 struct cpl_tx_tls_sfo *req_cpl;
349 unsigned int wr_ulp_mode_force;
350 struct tls_scmd *updated_scmd;
351 unsigned char data_type;
352 struct chtls_sock *csk;
353 struct net_device *dev;
354 struct chtls_hws *hws;
355 struct tls_scmd *scmd;
356 struct adapter *adap;
357 unsigned char *req;
358 int immd_len;
359 int iv_imm;
360 int len;
361
362 csk = rcu_dereference_sk_user_data(sk);
363 iv_imm = skb_ulp_tls_iv_imm(skb);
364 dev = csk->egress_dev;
365 adap = netdev2adap(dev);
366 hws = &csk->tlshws;
367 scmd = &hws->scmd;
368 len = dlen + expn;
369
370 dlen = (dlen < hws->mfs) ? dlen : hws->mfs;
371 atomic_inc(&adap->chcr_stats.tls_pdu_tx);
372
373 updated_scmd = scmd;
374 updated_scmd->seqno_numivs &= 0xffffff80;
375 updated_scmd->seqno_numivs |= SCMD_NUM_IVS_V(pdus);
376 hws->scmd = *updated_scmd;
377
378 req = (unsigned char *)__skb_push(skb, sizeof(struct cpl_tx_tls_sfo));
379 req_cpl = (struct cpl_tx_tls_sfo *)req;
380 req = (unsigned char *)__skb_push(skb, (sizeof(struct
381 fw_tlstx_data_wr)));
382
383 req_wr = (struct fw_tlstx_data_wr *)req;
384 immd_len = (tls_immd ? dlen : 0);
385 req_wr->op_to_immdlen =
386 htonl(FW_WR_OP_V(FW_TLSTX_DATA_WR) |
387 FW_TLSTX_DATA_WR_COMPL_V(1) |
388 FW_TLSTX_DATA_WR_IMMDLEN_V(immd_len));
389 req_wr->flowid_len16 = htonl(FW_TLSTX_DATA_WR_FLOWID_V(csk->tid) |
390 FW_TLSTX_DATA_WR_LEN16_V(credits));
391 wr_ulp_mode_force = TX_ULP_MODE_V(ULP_MODE_TLS);
392
393 if (is_sg_request(skb))
394 wr_ulp_mode_force |= FW_OFLD_TX_DATA_WR_ALIGNPLD_F |
395 ((tcp_sk(sk)->nonagle & TCP_NAGLE_OFF) ? 0 :
396 FW_OFLD_TX_DATA_WR_SHOVE_F);
397
398 req_wr->lsodisable_to_flags =
399 htonl(TX_ULP_MODE_V(ULP_MODE_TLS) |
400 FW_OFLD_TX_DATA_WR_URGENT_V(skb_urgent(skb)) |
401 T6_TX_FORCE_F | wr_ulp_mode_force |
402 TX_SHOVE_V((!csk_flag(sk, CSK_TX_MORE_DATA)) &&
403 skb_queue_empty(&csk->txq)));
404
405 req_wr->ctxloc_to_exp =
406 htonl(FW_TLSTX_DATA_WR_NUMIVS_V(pdus) |
407 FW_TLSTX_DATA_WR_EXP_V(expn) |
408 FW_TLSTX_DATA_WR_CTXLOC_V(CHTLS_KEY_CONTEXT_DDR) |
409 FW_TLSTX_DATA_WR_IVDSGL_V(!iv_imm) |
410 FW_TLSTX_DATA_WR_KEYSIZE_V(hws->keylen >> 4));
411
412 /* Fill in the length */
413 req_wr->plen = htonl(len);
414 req_wr->mfs = htons(hws->mfs);
415 req_wr->adjustedplen_pkd =
416 htons(FW_TLSTX_DATA_WR_ADJUSTEDPLEN_V(hws->adjustlen));
417 req_wr->expinplenmax_pkd =
418 htons(FW_TLSTX_DATA_WR_EXPINPLENMAX_V(hws->expansion));
419 req_wr->pdusinplenmax_pkd =
420 FW_TLSTX_DATA_WR_PDUSINPLENMAX_V(hws->pdus);
421 req_wr->r10 = 0;
422
423 data_type = tls_content_type(ULP_SKB_CB(skb)->ulp.tls.type);
424 req_cpl->op_to_seg_len = htonl(CPL_TX_TLS_SFO_OPCODE_V(CPL_TX_TLS_SFO) |
425 CPL_TX_TLS_SFO_DATA_TYPE_V(data_type) |
426 CPL_TX_TLS_SFO_CPL_LEN_V(2) |
427 CPL_TX_TLS_SFO_SEG_LEN_V(dlen));
428 req_cpl->pld_len = htonl(len - expn);
429
430 req_cpl->type_protover = htonl(CPL_TX_TLS_SFO_TYPE_V
431 ((data_type == CPL_TX_TLS_SFO_TYPE_HEARTBEAT) ?
432 TLS_HDR_TYPE_HEARTBEAT : 0) |
433 CPL_TX_TLS_SFO_PROTOVER_V(0));
434
435 /* create the s-command */
436 req_cpl->r1_lo = 0;
437 req_cpl->seqno_numivs = cpu_to_be32(hws->scmd.seqno_numivs);
438 req_cpl->ivgen_hdrlen = cpu_to_be32(hws->scmd.ivgen_hdrlen);
439 req_cpl->scmd1 = cpu_to_be64(tlstx_incr_seqnum(hws));
440}
441
442/*
443 * Calculate the TLS data expansion size
444 */
445static int chtls_expansion_size(struct sock *sk, int data_len,
446 int fullpdu,
447 unsigned short *pducnt)
448{
449 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
450 struct chtls_hws *hws = &csk->tlshws;
451 struct tls_scmd *scmd = &hws->scmd;
452 int fragsize = hws->mfs;
453 int expnsize = 0;
454 int fragleft;
455 int fragcnt;
456 int expppdu;
457
458 if (SCMD_CIPH_MODE_G(scmd->seqno_numivs) ==
459 SCMD_CIPH_MODE_AES_GCM) {
460 expppdu = GCM_TAG_SIZE + AEAD_EXPLICIT_DATA_SIZE +
461 TLS_HEADER_LENGTH;
462
463 if (fullpdu) {
464 *pducnt = data_len / (expppdu + fragsize);
465 if (*pducnt > 32)
466 *pducnt = 32;
467 else if (!*pducnt)
468 *pducnt = 1;
469 expnsize = (*pducnt) * expppdu;
470 return expnsize;
471 }
472 fragcnt = (data_len / fragsize);
473 expnsize = fragcnt * expppdu;
474 fragleft = data_len % fragsize;
475 if (fragleft > 0)
476 expnsize += expppdu;
477 }
478 return expnsize;
479}
480
481/* WR with IV, KEY and CPL SFO added */
482static void make_tlstx_data_wr(struct sock *sk, struct sk_buff *skb,
483 int tls_tx_imm, int tls_len, u32 credits)
484{
485 unsigned short pdus_per_ulp = 0;
486 struct chtls_sock *csk;
487 struct chtls_hws *hws;
488 int expn_sz;
489 int pdus;
490
491 csk = rcu_dereference_sk_user_data(sk);
492 hws = &csk->tlshws;
493 pdus = DIV_ROUND_UP(tls_len, hws->mfs);
494 expn_sz = chtls_expansion_size(sk, tls_len, 0, NULL);
495 if (!hws->compute) {
496 hws->expansion = chtls_expansion_size(sk,
497 hws->fcplenmax,
498 1, &pdus_per_ulp);
499 hws->pdus = pdus_per_ulp;
500 hws->adjustlen = hws->pdus *
501 ((hws->expansion / hws->pdus) + hws->mfs);
502 hws->compute = 1;
503 }
504 if (tls_copy_ivs(sk, skb))
505 return;
506 tls_copy_tx_key(sk, skb);
507 tls_tx_data_wr(sk, skb, tls_len, tls_tx_imm, credits, expn_sz, pdus);
508 hws->tx_seq_no += (pdus - 1);
509}
510
511static void make_tx_data_wr(struct sock *sk, struct sk_buff *skb,
512 unsigned int immdlen, int len,
513 u32 credits, u32 compl)
514{
515 struct fw_ofld_tx_data_wr *req;
516 unsigned int wr_ulp_mode_force;
517 struct chtls_sock *csk;
518 unsigned int opcode;
519
520 csk = rcu_dereference_sk_user_data(sk);
521 opcode = FW_OFLD_TX_DATA_WR;
522
523 req = (struct fw_ofld_tx_data_wr *)__skb_push(skb, sizeof(*req));
524 req->op_to_immdlen = htonl(WR_OP_V(opcode) |
525 FW_WR_COMPL_V(compl) |
526 FW_WR_IMMDLEN_V(immdlen));
527 req->flowid_len16 = htonl(FW_WR_FLOWID_V(csk->tid) |
528 FW_WR_LEN16_V(credits));
529
530 wr_ulp_mode_force = TX_ULP_MODE_V(csk->ulp_mode);
531 if (is_sg_request(skb))
532 wr_ulp_mode_force |= FW_OFLD_TX_DATA_WR_ALIGNPLD_F |
533 ((tcp_sk(sk)->nonagle & TCP_NAGLE_OFF) ? 0 :
534 FW_OFLD_TX_DATA_WR_SHOVE_F);
535
536 req->tunnel_to_proxy = htonl(wr_ulp_mode_force |
537 FW_OFLD_TX_DATA_WR_URGENT_V(skb_urgent(skb)) |
538 FW_OFLD_TX_DATA_WR_SHOVE_V((!csk_flag
539 (sk, CSK_TX_MORE_DATA)) &&
540 skb_queue_empty(&csk->txq)));
541 req->plen = htonl(len);
542}
543
544static int chtls_wr_size(struct chtls_sock *csk, const struct sk_buff *skb,
545 bool size)
546{
547 int wr_size;
548
549 wr_size = TLS_WR_CPL_LEN;
550 wr_size += KEY_ON_MEM_SZ;
551 wr_size += ivs_size(csk->sk, skb);
552
553 if (size)
554 return wr_size;
555
556 /* frags counted for IV dsgl */
557 if (!skb_ulp_tls_iv_imm(skb))
558 skb_shinfo(skb)->nr_frags++;
559
560 return wr_size;
561}
562
563static bool is_ofld_imm(struct chtls_sock *csk, const struct sk_buff *skb)
564{
565 int length = skb->len;
566
567 if (skb->peeked || skb->len > MAX_IMM_ULPTX_WR_LEN)
568 return false;
569
570 if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR)) {
571 /* Check TLS header len for Immediate */
572 if (csk->ulp_mode == ULP_MODE_TLS &&
573 skb_ulp_tls_inline(skb))
574 length += chtls_wr_size(csk, skb, true);
575 else
576 length += sizeof(struct fw_ofld_tx_data_wr);
577
578 return length <= MAX_IMM_OFLD_TX_DATA_WR_LEN;
579 }
580 return true;
581}
582
583static unsigned int calc_tx_flits(const struct sk_buff *skb,
584 unsigned int immdlen)
585{
586 unsigned int flits, cnt;
587
588 flits = immdlen / 8; /* headers */
589 cnt = skb_shinfo(skb)->nr_frags;
590 if (skb_tail_pointer(skb) != skb_transport_header(skb))
591 cnt++;
592 return flits + sgl_len(cnt);
593}
594
595static void arp_failure_discard(void *handle, struct sk_buff *skb)
596{
597 kfree_skb(skb);
598}
599
600int chtls_push_frames(struct chtls_sock *csk, int comp)
601{
602 struct chtls_hws *hws = &csk->tlshws;
603 struct tcp_sock *tp;
604 struct sk_buff *skb;
605 int total_size = 0;
606 struct sock *sk;
607 int wr_size;
608
609 wr_size = sizeof(struct fw_ofld_tx_data_wr);
610 sk = csk->sk;
611 tp = tcp_sk(sk);
612
613 if (unlikely(sk_in_state(sk, TCPF_SYN_SENT | TCPF_CLOSE)))
614 return 0;
615
616 if (unlikely(csk_flag(sk, CSK_ABORT_SHUTDOWN)))
617 return 0;
618
619 while (csk->wr_credits && (skb = skb_peek(&csk->txq)) &&
620 (!(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_HOLD) ||
621 skb_queue_len(&csk->txq) > 1)) {
622 unsigned int credit_len = skb->len;
623 unsigned int credits_needed;
624 unsigned int completion = 0;
625 int tls_len = skb->len;/* TLS data len before IV/key */
626 unsigned int immdlen;
627 int len = skb->len; /* length [ulp bytes] inserted by hw */
628 int flowclen16 = 0;
629 int tls_tx_imm = 0;
630
631 immdlen = skb->len;
632 if (!is_ofld_imm(csk, skb)) {
633 immdlen = skb_transport_offset(skb);
634 if (skb_ulp_tls_inline(skb))
635 wr_size = chtls_wr_size(csk, skb, false);
636 credit_len = 8 * calc_tx_flits(skb, immdlen);
637 } else {
638 if (skb_ulp_tls_inline(skb)) {
639 wr_size = chtls_wr_size(csk, skb, false);
640 tls_tx_imm = 1;
641 }
642 }
643 if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR))
644 credit_len += wr_size;
645 credits_needed = DIV_ROUND_UP(credit_len, 16);
646 if (!csk_flag_nochk(csk, CSK_TX_DATA_SENT)) {
647 flowclen16 = send_tx_flowc_wr(sk, 1, tp->snd_nxt,
648 tp->rcv_nxt);
649 if (flowclen16 <= 0)
650 break;
651 csk->wr_credits -= flowclen16;
652 csk->wr_unacked += flowclen16;
653 csk->wr_nondata += flowclen16;
654 csk_set_flag(csk, CSK_TX_DATA_SENT);
655 }
656
657 if (csk->wr_credits < credits_needed) {
658 if (skb_ulp_tls_inline(skb) &&
659 !skb_ulp_tls_iv_imm(skb))
660 skb_shinfo(skb)->nr_frags--;
661 break;
662 }
663
664 __skb_unlink(skb, &csk->txq);
665 skb_set_queue_mapping(skb, (csk->txq_idx << 1) |
666 CPL_PRIORITY_DATA);
667 if (hws->ofld)
668 hws->txqid = (skb->queue_mapping >> 1);
669 skb->csum = (__force __wsum)(credits_needed + csk->wr_nondata);
670 csk->wr_credits -= credits_needed;
671 csk->wr_unacked += credits_needed;
672 csk->wr_nondata = 0;
673 enqueue_wr(csk, skb);
674
675 if (likely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NEED_HDR)) {
676 if ((comp && csk->wr_unacked == credits_needed) ||
677 (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_COMPL) ||
678 csk->wr_unacked >= csk->wr_max_credits / 2) {
679 completion = 1;
680 csk->wr_unacked = 0;
681 }
682 if (skb_ulp_tls_inline(skb))
683 make_tlstx_data_wr(sk, skb, tls_tx_imm,
684 tls_len, credits_needed);
685 else
686 make_tx_data_wr(sk, skb, immdlen, len,
687 credits_needed, completion);
688 tp->snd_nxt += len;
689 tp->lsndtime = tcp_time_stamp(tp);
690 if (completion)
691 ULP_SKB_CB(skb)->flags &= ~ULPCB_FLAG_NEED_HDR;
692 } else {
693 struct cpl_close_con_req *req = cplhdr(skb);
694 unsigned int cmd = CPL_OPCODE_G(ntohl
695 (OPCODE_TID(req)));
696
697 if (cmd == CPL_CLOSE_CON_REQ)
698 csk_set_flag(csk,
699 CSK_CLOSE_CON_REQUESTED);
700
701 if ((ULP_SKB_CB(skb)->flags & ULPCB_FLAG_COMPL) &&
702 (csk->wr_unacked >= csk->wr_max_credits / 2)) {
703 req->wr.wr_hi |= htonl(FW_WR_COMPL_F);
704 csk->wr_unacked = 0;
705 }
706 }
707 total_size += skb->truesize;
708 if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_BARRIER)
709 csk_set_flag(csk, CSK_TX_WAIT_IDLE);
710 t4_set_arp_err_handler(skb, NULL, arp_failure_discard);
711 cxgb4_l2t_send(csk->egress_dev, skb, csk->l2t_entry);
712 }
713 sk->sk_wmem_queued -= total_size;
714 return total_size;
715}
716
717static void mark_urg(struct tcp_sock *tp, int flags,
718 struct sk_buff *skb)
719{
720 if (unlikely(flags & MSG_OOB)) {
721 tp->snd_up = tp->write_seq;
722 ULP_SKB_CB(skb)->flags = ULPCB_FLAG_URG |
723 ULPCB_FLAG_BARRIER |
724 ULPCB_FLAG_NO_APPEND |
725 ULPCB_FLAG_NEED_HDR;
726 }
727}
728
729/*
730 * Returns true if a connection should send more data to TCP engine
731 */
732static bool should_push(struct sock *sk)
733{
734 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
735 struct chtls_dev *cdev = csk->cdev;
736 struct tcp_sock *tp = tcp_sk(sk);
737
738 /*
739 * If we've released our offload resources there's nothing to do ...
740 */
741 if (!cdev)
742 return false;
743
744 /*
745 * If there aren't any work requests in flight, or there isn't enough
746 * data in flight, or Nagle is off then send the current TX_DATA
747 * otherwise hold it and wait to accumulate more data.
748 */
749 return csk->wr_credits == csk->wr_max_credits ||
750 (tp->nonagle & TCP_NAGLE_OFF);
751}
752
753/*
754 * Returns true if a TCP socket is corked.
755 */
756static bool corked(const struct tcp_sock *tp, int flags)
757{
758 return (flags & MSG_MORE) || (tp->nonagle & TCP_NAGLE_CORK);
759}
760
761/*
762 * Returns true if a send should try to push new data.
763 */
764static bool send_should_push(struct sock *sk, int flags)
765{
766 return should_push(sk) && !corked(tcp_sk(sk), flags);
767}
768
769void chtls_tcp_push(struct sock *sk, int flags)
770{
771 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
772 int qlen = skb_queue_len(&csk->txq);
773
774 if (likely(qlen)) {
775 struct sk_buff *skb = skb_peek_tail(&csk->txq);
776 struct tcp_sock *tp = tcp_sk(sk);
777
778 mark_urg(tp, flags, skb);
779
780 if (!(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) &&
781 corked(tp, flags)) {
782 ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_HOLD;
783 return;
784 }
785
786 ULP_SKB_CB(skb)->flags &= ~ULPCB_FLAG_HOLD;
787 if (qlen == 1 &&
788 ((ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
789 should_push(sk)))
790 chtls_push_frames(csk, 1);
791 }
792}
793
794/*
795 * Calculate the size for a new send sk_buff. It's maximum size so we can
796 * pack lots of data into it, unless we plan to send it immediately, in which
797 * case we size it more tightly.
798 *
799 * Note: we don't bother compensating for MSS < PAGE_SIZE because it doesn't
800 * arise in normal cases and when it does we are just wasting memory.
801 */
802static int select_size(struct sock *sk, int io_len, int flags, int len)
803{
804 const int pgbreak = SKB_MAX_HEAD(len);
805
806 /*
807 * If the data wouldn't fit in the main body anyway, put only the
808 * header in the main body so it can use immediate data and place all
809 * the payload in page fragments.
810 */
811 if (io_len > pgbreak)
812 return 0;
813
814 /*
815 * If we will be accumulating payload get a large main body.
816 */
817 if (!send_should_push(sk, flags))
818 return pgbreak;
819
820 return io_len;
821}
822
823void skb_entail(struct sock *sk, struct sk_buff *skb, int flags)
824{
825 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
826 struct tcp_sock *tp = tcp_sk(sk);
827
828 ULP_SKB_CB(skb)->seq = tp->write_seq;
829 ULP_SKB_CB(skb)->flags = flags;
830 __skb_queue_tail(&csk->txq, skb);
831 sk->sk_wmem_queued += skb->truesize;
832
833 if (TCP_PAGE(sk) && TCP_OFF(sk)) {
834 put_page(TCP_PAGE(sk));
835 TCP_PAGE(sk) = NULL;
836 TCP_OFF(sk) = 0;
837 }
838}
839
840static struct sk_buff *get_tx_skb(struct sock *sk, int size)
841{
842 struct sk_buff *skb;
843
844 skb = alloc_skb(size + TX_HEADER_LEN, sk->sk_allocation);
845 if (likely(skb)) {
846 skb_reserve(skb, TX_HEADER_LEN);
847 skb_entail(sk, skb, ULPCB_FLAG_NEED_HDR);
848 skb_reset_transport_header(skb);
849 }
850 return skb;
851}
852
853static struct sk_buff *get_record_skb(struct sock *sk, int size, bool zcopy)
854{
855 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
856 struct sk_buff *skb;
857
858 skb = alloc_skb(((zcopy ? 0 : size) + TX_TLSHDR_LEN +
859 KEY_ON_MEM_SZ + max_ivs_size(sk, size)),
860 sk->sk_allocation);
861 if (likely(skb)) {
862 skb_reserve(skb, (TX_TLSHDR_LEN +
863 KEY_ON_MEM_SZ + max_ivs_size(sk, size)));
864 skb_entail(sk, skb, ULPCB_FLAG_NEED_HDR);
865 skb_reset_transport_header(skb);
866 ULP_SKB_CB(skb)->ulp.tls.ofld = 1;
867 ULP_SKB_CB(skb)->ulp.tls.type = csk->tlshws.type;
868 }
869 return skb;
870}
871
872static void tx_skb_finalize(struct sk_buff *skb)
873{
874 struct ulp_skb_cb *cb = ULP_SKB_CB(skb);
875
876 if (!(cb->flags & ULPCB_FLAG_NO_HDR))
877 cb->flags = ULPCB_FLAG_NEED_HDR;
878 cb->flags |= ULPCB_FLAG_NO_APPEND;
879}
880
881static void push_frames_if_head(struct sock *sk)
882{
883 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
884
885 if (skb_queue_len(&csk->txq) == 1)
886 chtls_push_frames(csk, 1);
887}
888
889static int chtls_skb_copy_to_page_nocache(struct sock *sk,
890 struct iov_iter *from,
891 struct sk_buff *skb,
892 struct page *page,
893 int off, int copy)
894{
895 int err;
896
897 err = skb_do_copy_data_nocache(sk, skb, from, page_address(page) +
898 off, copy, skb->len);
899 if (err)
900 return err;
901
902 skb->len += copy;
903 skb->data_len += copy;
904 skb->truesize += copy;
905 sk->sk_wmem_queued += copy;
906 return 0;
907}
908
909/* Read TLS header to find content type and data length */
910static u16 tls_header_read(struct tls_hdr *thdr, struct iov_iter *from)
911{
912 if (copy_from_iter(thdr, sizeof(*thdr), from) != sizeof(*thdr))
913 return -EFAULT;
914 return (__force u16)cpu_to_be16(thdr->length);
915}
916
917int chtls_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
918{
919 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
920 struct chtls_dev *cdev = csk->cdev;
921 struct tcp_sock *tp = tcp_sk(sk);
922 struct sk_buff *skb;
923 int mss, flags, err;
924 int recordsz = 0;
925 int copied = 0;
926 int hdrlen = 0;
927 long timeo;
928
929 lock_sock(sk);
930 flags = msg->msg_flags;
931 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
932
933 if (!sk_in_state(sk, TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) {
934 err = sk_stream_wait_connect(sk, &timeo);
935 if (err)
936 goto out_err;
937 }
938
939 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
940 err = -EPIPE;
941 if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
942 goto out_err;
943
944 mss = csk->mss;
945 csk_set_flag(csk, CSK_TX_MORE_DATA);
946
947 while (msg_data_left(msg)) {
948 int copy = 0;
949
950 skb = skb_peek_tail(&csk->txq);
951 if (skb) {
952 copy = mss - skb->len;
953 skb->ip_summed = CHECKSUM_UNNECESSARY;
954 }
955
956 if (is_tls_tx(csk) && !csk->tlshws.txleft) {
957 struct tls_hdr hdr;
958
959 recordsz = tls_header_read(&hdr, &msg->msg_iter);
960 size -= TLS_HEADER_LENGTH;
961 hdrlen += TLS_HEADER_LENGTH;
962 csk->tlshws.txleft = recordsz;
963 csk->tlshws.type = hdr.type;
964 if (skb)
965 ULP_SKB_CB(skb)->ulp.tls.type = hdr.type;
966 }
967
968 if (!skb || (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
969 copy <= 0) {
970new_buf:
971 if (skb) {
972 tx_skb_finalize(skb);
973 push_frames_if_head(sk);
974 }
975
976 if (is_tls_tx(csk)) {
977 skb = get_record_skb(sk,
978 select_size(sk,
979 recordsz,
980 flags,
981 TX_TLSHDR_LEN),
982 false);
983 } else {
984 skb = get_tx_skb(sk,
985 select_size(sk, size, flags,
986 TX_HEADER_LEN));
987 }
988 if (unlikely(!skb))
989 goto wait_for_memory;
990
991 skb->ip_summed = CHECKSUM_UNNECESSARY;
992 copy = mss;
993 }
994 if (copy > size)
995 copy = size;
996
997 if (skb_tailroom(skb) > 0) {
998 copy = min(copy, skb_tailroom(skb));
999 if (is_tls_tx(csk))
1000 copy = min_t(int, copy, csk->tlshws.txleft);
1001 err = skb_add_data_nocache(sk, skb,
1002 &msg->msg_iter, copy);
1003 if (err)
1004 goto do_fault;
1005 } else {
1006 int i = skb_shinfo(skb)->nr_frags;
1007 struct page *page = TCP_PAGE(sk);
1008 int pg_size = PAGE_SIZE;
1009 int off = TCP_OFF(sk);
1010 bool merge;
1011
1012 if (page)
1013 pg_size <<= compound_order(page);
1014
1015 if (off < pg_size &&
1016 skb_can_coalesce(skb, i, page, off)) {
1017 merge = 1;
1018 goto copy;
1019 }
1020 merge = 0;
1021 if (i == (is_tls_tx(csk) ? (MAX_SKB_FRAGS - 1) :
1022 MAX_SKB_FRAGS))
1023 goto new_buf;
1024
1025 if (page && off == pg_size) {
1026 put_page(page);
1027 TCP_PAGE(sk) = page = NULL;
1028 pg_size = PAGE_SIZE;
1029 }
1030
1031 if (!page) {
1032 gfp_t gfp = sk->sk_allocation;
1033 int order = cdev->send_page_order;
1034
1035 if (order) {
1036 page = alloc_pages(gfp | __GFP_COMP |
1037 __GFP_NOWARN |
1038 __GFP_NORETRY,
1039 order);
1040 if (page)
1041 pg_size <<=
1042 compound_order(page);
1043 }
1044 if (!page) {
1045 page = alloc_page(gfp);
1046 pg_size = PAGE_SIZE;
1047 }
1048 if (!page)
1049 goto wait_for_memory;
1050 off = 0;
1051 }
1052copy:
1053 if (copy > pg_size - off)
1054 copy = pg_size - off;
1055 if (is_tls_tx(csk))
1056 copy = min_t(int, copy, csk->tlshws.txleft);
1057
1058 err = chtls_skb_copy_to_page_nocache(sk, &msg->msg_iter,
1059 skb, page,
1060 off, copy);
1061 if (unlikely(err)) {
1062 if (!TCP_PAGE(sk)) {
1063 TCP_PAGE(sk) = page;
1064 TCP_OFF(sk) = 0;
1065 }
1066 goto do_fault;
1067 }
1068 /* Update the skb. */
1069 if (merge) {
1070 skb_shinfo(skb)->frags[i - 1].size += copy;
1071 } else {
1072 skb_fill_page_desc(skb, i, page, off, copy);
1073 if (off + copy < pg_size) {
1074 /* space left keep page */
1075 get_page(page);
1076 TCP_PAGE(sk) = page;
1077 } else {
1078 TCP_PAGE(sk) = NULL;
1079 }
1080 }
1081 TCP_OFF(sk) = off + copy;
1082 }
1083 if (unlikely(skb->len == mss))
1084 tx_skb_finalize(skb);
1085 tp->write_seq += copy;
1086 copied += copy;
1087 size -= copy;
1088
1089 if (is_tls_tx(csk))
1090 csk->tlshws.txleft -= copy;
1091
1092 if (corked(tp, flags) &&
1093 (sk_stream_wspace(sk) < sk_stream_min_wspace(sk)))
1094 ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_NO_APPEND;
1095
1096 if (size == 0)
1097 goto out;
1098
1099 if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND)
1100 push_frames_if_head(sk);
1101 continue;
1102wait_for_memory:
1103 err = sk_stream_wait_memory(sk, &timeo);
1104 if (err)
1105 goto do_error;
1106 }
1107out:
1108 csk_reset_flag(csk, CSK_TX_MORE_DATA);
1109 if (copied)
1110 chtls_tcp_push(sk, flags);
1111done:
1112 release_sock(sk);
1113 return copied + hdrlen;
1114do_fault:
1115 if (!skb->len) {
1116 __skb_unlink(skb, &csk->txq);
1117 sk->sk_wmem_queued -= skb->truesize;
1118 __kfree_skb(skb);
1119 }
1120do_error:
1121 if (copied)
1122 goto out;
1123out_err:
1124 if (csk_conn_inline(csk))
1125 csk_reset_flag(csk, CSK_TX_MORE_DATA);
1126 copied = sk_stream_error(sk, flags, err);
1127 goto done;
1128}
1129
1130int chtls_sendpage(struct sock *sk, struct page *page,
1131 int offset, size_t size, int flags)
1132{
1133 struct chtls_sock *csk;
1134 int mss, err, copied;
1135 struct tcp_sock *tp;
1136 long timeo;
1137
1138 tp = tcp_sk(sk);
1139 copied = 0;
1140 csk = rcu_dereference_sk_user_data(sk);
1141 timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1142
1143 err = sk_stream_wait_connect(sk, &timeo);
1144 if (!sk_in_state(sk, TCPF_ESTABLISHED | TCPF_CLOSE_WAIT) &&
1145 err != 0)
1146 goto out_err;
1147
1148 mss = csk->mss;
1149 csk_set_flag(csk, CSK_TX_MORE_DATA);
1150
1151 while (size > 0) {
1152 struct sk_buff *skb = skb_peek_tail(&csk->txq);
1153 int copy, i;
1154
1155 copy = mss - skb->len;
1156 if (!skb || (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
1157 copy <= 0) {
1158new_buf:
1159
1160 if (is_tls_tx(csk)) {
1161 skb = get_record_skb(sk,
1162 select_size(sk, size,
1163 flags,
1164 TX_TLSHDR_LEN),
1165 true);
1166 } else {
1167 skb = get_tx_skb(sk, 0);
1168 }
1169 if (!skb)
1170 goto do_error;
1171 copy = mss;
1172 }
1173 if (copy > size)
1174 copy = size;
1175
1176 i = skb_shinfo(skb)->nr_frags;
1177 if (skb_can_coalesce(skb, i, page, offset)) {
1178 skb_shinfo(skb)->frags[i - 1].size += copy;
1179 } else if (i < MAX_SKB_FRAGS) {
1180 get_page(page);
1181 skb_fill_page_desc(skb, i, page, offset, copy);
1182 } else {
1183 tx_skb_finalize(skb);
1184 push_frames_if_head(sk);
1185 goto new_buf;
1186 }
1187
1188 skb->len += copy;
1189 if (skb->len == mss)
1190 tx_skb_finalize(skb);
1191 skb->data_len += copy;
1192 skb->truesize += copy;
1193 sk->sk_wmem_queued += copy;
1194 tp->write_seq += copy;
1195 copied += copy;
1196 offset += copy;
1197 size -= copy;
1198
1199 if (corked(tp, flags) &&
1200 (sk_stream_wspace(sk) < sk_stream_min_wspace(sk)))
1201 ULP_SKB_CB(skb)->flags |= ULPCB_FLAG_NO_APPEND;
1202
1203 if (!size)
1204 break;
1205
1206 if (unlikely(ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND))
1207 push_frames_if_head(sk);
1208 continue;
1209
1210 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1211 }
1212out:
1213 csk_reset_flag(csk, CSK_TX_MORE_DATA);
1214 if (copied)
1215 chtls_tcp_push(sk, flags);
1216done:
1217 release_sock(sk);
1218 return copied;
1219
1220do_error:
1221 if (copied)
1222 goto out;
1223
1224out_err:
1225 if (csk_conn_inline(csk))
1226 csk_reset_flag(csk, CSK_TX_MORE_DATA);
1227 copied = sk_stream_error(sk, flags, err);
1228 goto done;
1229}
Atul Guptab6479932018-03-31 21:42:01 +05301230
1231static void chtls_select_window(struct sock *sk)
1232{
1233 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1234 struct tcp_sock *tp = tcp_sk(sk);
1235 unsigned int wnd = tp->rcv_wnd;
1236
1237 wnd = max_t(unsigned int, wnd, tcp_full_space(sk));
1238 wnd = max_t(unsigned int, MIN_RCV_WND, wnd);
1239
1240 if (wnd > MAX_RCV_WND)
1241 wnd = MAX_RCV_WND;
1242
1243/*
1244 * Check if we need to grow the receive window in response to an increase in
1245 * the socket's receive buffer size. Some applications increase the buffer
1246 * size dynamically and rely on the window to grow accordingly.
1247 */
1248
1249 if (wnd > tp->rcv_wnd) {
1250 tp->rcv_wup -= wnd - tp->rcv_wnd;
1251 tp->rcv_wnd = wnd;
1252 /* Mark the receive window as updated */
1253 csk_reset_flag(csk, CSK_UPDATE_RCV_WND);
1254 }
1255}
1256
1257/*
1258 * Send RX credits through an RX_DATA_ACK CPL message. We are permitted
1259 * to return without sending the message in case we cannot allocate
1260 * an sk_buff. Returns the number of credits sent.
1261 */
1262static u32 send_rx_credits(struct chtls_sock *csk, u32 credits)
1263{
1264 struct cpl_rx_data_ack *req;
1265 struct sk_buff *skb;
1266
1267 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
1268 if (!skb)
1269 return 0;
1270 __skb_put(skb, sizeof(*req));
1271 req = (struct cpl_rx_data_ack *)skb->head;
1272
1273 set_wr_txq(skb, CPL_PRIORITY_ACK, csk->port_id);
1274 INIT_TP_WR(req, csk->tid);
1275 OPCODE_TID(req) = cpu_to_be32(MK_OPCODE_TID(CPL_RX_DATA_ACK,
1276 csk->tid));
1277 req->credit_dack = cpu_to_be32(RX_CREDITS_V(credits) |
1278 RX_FORCE_ACK_F);
1279 cxgb4_ofld_send(csk->cdev->ports[csk->port_id], skb);
1280 return credits;
1281}
1282
1283#define CREDIT_RETURN_STATE (TCPF_ESTABLISHED | \
1284 TCPF_FIN_WAIT1 | \
1285 TCPF_FIN_WAIT2)
1286
1287/*
1288 * Called after some received data has been read. It returns RX credits
1289 * to the HW for the amount of data processed.
1290 */
1291static void chtls_cleanup_rbuf(struct sock *sk, int copied)
1292{
1293 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1294 struct tcp_sock *tp;
1295 int must_send;
1296 u32 credits;
1297 u32 thres;
1298
1299 thres = 15 * 1024;
1300
1301 if (!sk_in_state(sk, CREDIT_RETURN_STATE))
1302 return;
1303
1304 chtls_select_window(sk);
1305 tp = tcp_sk(sk);
1306 credits = tp->copied_seq - tp->rcv_wup;
1307 if (unlikely(!credits))
1308 return;
1309
1310/*
1311 * For coalescing to work effectively ensure the receive window has
1312 * at least 16KB left.
1313 */
1314 must_send = credits + 16384 >= tp->rcv_wnd;
1315
1316 if (must_send || credits >= thres)
1317 tp->rcv_wup += send_rx_credits(csk, credits);
1318}
1319
1320static int chtls_pt_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1321 int nonblock, int flags, int *addr_len)
1322{
1323 struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
1324 struct net_device *dev = csk->egress_dev;
1325 struct chtls_hws *hws = &csk->tlshws;
1326 struct tcp_sock *tp = tcp_sk(sk);
1327 struct adapter *adap;
1328 unsigned long avail;
1329 int buffers_freed;
1330 int copied = 0;
1331 int request;
1332 int target;
1333 long timeo;
1334
1335 adap = netdev2adap(dev);
1336 buffers_freed = 0;
1337
1338 timeo = sock_rcvtimeo(sk, nonblock);
1339 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1340 request = len;
1341
1342 if (unlikely(csk_flag(sk, CSK_UPDATE_RCV_WND)))
1343 chtls_cleanup_rbuf(sk, copied);
1344
1345 do {
1346 struct sk_buff *skb;
1347 u32 offset = 0;
1348
1349 if (unlikely(tp->urg_data &&
1350 tp->urg_seq == tp->copied_seq)) {
1351 if (copied)
1352 break;
1353 if (signal_pending(current)) {
1354 copied = timeo ? sock_intr_errno(timeo) :
1355 -EAGAIN;
1356 break;
1357 }
1358 }
1359 skb = skb_peek(&sk->sk_receive_queue);
1360 if (skb)
1361 goto found_ok_skb;
1362 if (csk->wr_credits &&
1363 skb_queue_len(&csk->txq) &&
1364 chtls_push_frames(csk, csk->wr_credits ==
1365 csk->wr_max_credits))
1366 sk->sk_write_space(sk);
1367
1368 if (copied >= target && !sk->sk_backlog.tail)
1369 break;
1370
1371 if (copied) {
1372 if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
1373 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1374 signal_pending(current))
1375 break;
1376
1377 if (!timeo)
1378 break;
1379 } else {
1380 if (sock_flag(sk, SOCK_DONE))
1381 break;
1382 if (sk->sk_err) {
1383 copied = sock_error(sk);
1384 break;
1385 }
1386 if (sk->sk_shutdown & RCV_SHUTDOWN)
1387 break;
1388 if (sk->sk_state == TCP_CLOSE) {
1389 copied = -ENOTCONN;
1390 break;
1391 }
1392 if (!timeo) {
1393 copied = -EAGAIN;
1394 break;
1395 }
1396 if (signal_pending(current)) {
1397 copied = sock_intr_errno(timeo);
1398 break;
1399 }
1400 }
1401 if (sk->sk_backlog.tail) {
1402 release_sock(sk);
1403 lock_sock(sk);
1404 chtls_cleanup_rbuf(sk, copied);
1405 continue;
1406 }
1407
1408 if (copied >= target)
1409 break;
1410 chtls_cleanup_rbuf(sk, copied);
1411 sk_wait_data(sk, &timeo, NULL);
1412 continue;
1413found_ok_skb:
1414 if (!skb->len) {
1415 skb_dst_set(skb, NULL);
1416 __skb_unlink(skb, &sk->sk_receive_queue);
1417 kfree_skb(skb);
1418
1419 if (!copied && !timeo) {
1420 copied = -EAGAIN;
1421 break;
1422 }
1423
1424 if (copied < target) {
1425 release_sock(sk);
1426 lock_sock(sk);
1427 continue;
1428 }
1429 break;
1430 }
1431 offset = hws->copied_seq;
1432 avail = skb->len - offset;
1433 if (len < avail)
1434 avail = len;
1435
1436 if (unlikely(tp->urg_data)) {
1437 u32 urg_offset = tp->urg_seq - tp->copied_seq;
1438
1439 if (urg_offset < avail) {
1440 if (urg_offset) {
1441 avail = urg_offset;
1442 } else if (!sock_flag(sk, SOCK_URGINLINE)) {
1443 /* First byte is urgent, skip */
1444 tp->copied_seq++;
1445 offset++;
1446 avail--;
1447 if (!avail)
1448 goto skip_copy;
1449 }
1450 }
1451 }
1452 if (hws->rstate == TLS_RCV_ST_READ_BODY) {
1453 if (skb_copy_datagram_msg(skb, offset,
1454 msg, avail)) {
1455 if (!copied) {
1456 copied = -EFAULT;
1457 break;
1458 }
1459 }
1460 } else {
1461 struct tlsrx_cmp_hdr *tls_hdr_pkt =
1462 (struct tlsrx_cmp_hdr *)skb->data;
1463
1464 if ((tls_hdr_pkt->res_to_mac_error &
1465 TLSRX_HDR_PKT_ERROR_M))
1466 tls_hdr_pkt->type = 0x7F;
1467
1468 /* CMP pld len is for recv seq */
1469 hws->rcvpld = skb->hdr_len;
1470 if (skb_copy_datagram_msg(skb, offset, msg, avail)) {
1471 if (!copied) {
1472 copied = -EFAULT;
1473 break;
1474 }
1475 }
1476 }
1477 copied += avail;
1478 len -= avail;
1479 hws->copied_seq += avail;
1480skip_copy:
1481 if (tp->urg_data && after(tp->copied_seq, tp->urg_seq))
1482 tp->urg_data = 0;
1483
1484 if (hws->rstate == TLS_RCV_ST_READ_BODY &&
1485 (avail + offset) >= skb->len) {
1486 if (likely(skb))
1487 chtls_free_skb(sk, skb);
1488 buffers_freed++;
1489 hws->rstate = TLS_RCV_ST_READ_HEADER;
1490 atomic_inc(&adap->chcr_stats.tls_pdu_rx);
1491 tp->copied_seq += hws->rcvpld;
1492 hws->copied_seq = 0;
1493 if (copied >= target &&
1494 !skb_peek(&sk->sk_receive_queue))
1495 break;
1496 } else {
1497 if (likely(skb)) {
1498 if (ULP_SKB_CB(skb)->flags &
1499 ULPCB_FLAG_TLS_ND)
1500 hws->rstate =
1501 TLS_RCV_ST_READ_HEADER;
1502 else
1503 hws->rstate =
1504 TLS_RCV_ST_READ_BODY;
1505 chtls_free_skb(sk, skb);
1506 }
1507 buffers_freed++;
1508 tp->copied_seq += avail;
1509 hws->copied_seq = 0;
1510 }
1511 } while (len > 0);
1512
1513 if (buffers_freed)
1514 chtls_cleanup_rbuf(sk, copied);
1515 release_sock(sk);
1516 return copied;
1517}
1518
1519/*
1520 * Peek at data in a socket's receive buffer.
1521 */
1522static int peekmsg(struct sock *sk, struct msghdr *msg,
1523 size_t len, int nonblock, int flags)
1524{
1525 struct tcp_sock *tp = tcp_sk(sk);
1526 u32 peek_seq, offset;
1527 struct sk_buff *skb;
1528 int copied = 0;
1529 size_t avail; /* amount of available data in current skb */
1530 long timeo;
1531
1532 lock_sock(sk);
1533 timeo = sock_rcvtimeo(sk, nonblock);
1534 peek_seq = tp->copied_seq;
1535
1536 do {
1537 if (unlikely(tp->urg_data && tp->urg_seq == peek_seq)) {
1538 if (copied)
1539 break;
1540 if (signal_pending(current)) {
1541 copied = timeo ? sock_intr_errno(timeo) :
1542 -EAGAIN;
1543 break;
1544 }
1545 }
1546
1547 skb_queue_walk(&sk->sk_receive_queue, skb) {
1548 offset = peek_seq - ULP_SKB_CB(skb)->seq;
1549 if (offset < skb->len)
1550 goto found_ok_skb;
1551 }
1552
1553 /* empty receive queue */
1554 if (copied)
1555 break;
1556 if (sock_flag(sk, SOCK_DONE))
1557 break;
1558 if (sk->sk_err) {
1559 copied = sock_error(sk);
1560 break;
1561 }
1562 if (sk->sk_shutdown & RCV_SHUTDOWN)
1563 break;
1564 if (sk->sk_state == TCP_CLOSE) {
1565 copied = -ENOTCONN;
1566 break;
1567 }
1568 if (!timeo) {
1569 copied = -EAGAIN;
1570 break;
1571 }
1572 if (signal_pending(current)) {
1573 copied = sock_intr_errno(timeo);
1574 break;
1575 }
1576
1577 if (sk->sk_backlog.tail) {
1578 /* Do not sleep, just process backlog. */
1579 release_sock(sk);
1580 lock_sock(sk);
1581 } else {
1582 sk_wait_data(sk, &timeo, NULL);
1583 }
1584
1585 if (unlikely(peek_seq != tp->copied_seq)) {
1586 if (net_ratelimit())
1587 pr_info("TCP(%s:%d), race in MSG_PEEK.\n",
1588 current->comm, current->pid);
1589 peek_seq = tp->copied_seq;
1590 }
1591 continue;
1592
1593found_ok_skb:
1594 avail = skb->len - offset;
1595 if (len < avail)
1596 avail = len;
1597 /*
1598 * Do we have urgent data here? We need to skip over the
1599 * urgent byte.
1600 */
1601 if (unlikely(tp->urg_data)) {
1602 u32 urg_offset = tp->urg_seq - peek_seq;
1603
1604 if (urg_offset < avail) {
1605 /*
1606 * The amount of data we are preparing to copy
1607 * contains urgent data.
1608 */
1609 if (!urg_offset) { /* First byte is urgent */
1610 if (!sock_flag(sk, SOCK_URGINLINE)) {
1611 peek_seq++;
1612 offset++;
1613 avail--;
1614 }
1615 if (!avail)
1616 continue;
1617 } else {
1618 /* stop short of the urgent data */
1619 avail = urg_offset;
1620 }
1621 }
1622 }
1623
1624 /*
1625 * If MSG_TRUNC is specified the data is discarded.
1626 */
1627 if (likely(!(flags & MSG_TRUNC)))
1628 if (skb_copy_datagram_msg(skb, offset, msg, len)) {
1629 if (!copied) {
1630 copied = -EFAULT;
1631 break;
1632 }
1633 }
1634 peek_seq += avail;
1635 copied += avail;
1636 len -= avail;
1637 } while (len > 0);
1638
1639 release_sock(sk);
1640 return copied;
1641}
1642
1643int chtls_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1644 int nonblock, int flags, int *addr_len)
1645{
1646 struct tcp_sock *tp = tcp_sk(sk);
1647 struct chtls_sock *csk;
1648 struct chtls_hws *hws;
1649 unsigned long avail; /* amount of available data in current skb */
1650 int buffers_freed;
1651 int copied = 0;
1652 int request;
1653 long timeo;
1654 int target; /* Read at least this many bytes */
1655
1656 buffers_freed = 0;
1657
1658 if (unlikely(flags & MSG_OOB))
1659 return tcp_prot.recvmsg(sk, msg, len, nonblock, flags,
1660 addr_len);
1661
1662 if (unlikely(flags & MSG_PEEK))
1663 return peekmsg(sk, msg, len, nonblock, flags);
1664
1665 if (sk_can_busy_loop(sk) &&
1666 skb_queue_empty(&sk->sk_receive_queue) &&
1667 sk->sk_state == TCP_ESTABLISHED)
1668 sk_busy_loop(sk, nonblock);
1669
1670 lock_sock(sk);
1671 csk = rcu_dereference_sk_user_data(sk);
1672 hws = &csk->tlshws;
1673
1674 if (is_tls_rx(csk))
1675 return chtls_pt_recvmsg(sk, msg, len, nonblock,
1676 flags, addr_len);
1677
1678 timeo = sock_rcvtimeo(sk, nonblock);
1679 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1680 request = len;
1681
1682 if (unlikely(csk_flag(sk, CSK_UPDATE_RCV_WND)))
1683 chtls_cleanup_rbuf(sk, copied);
1684
1685 do {
1686 struct sk_buff *skb;
1687 u32 offset;
1688
1689 if (unlikely(tp->urg_data && tp->urg_seq == tp->copied_seq)) {
1690 if (copied)
1691 break;
1692 if (signal_pending(current)) {
1693 copied = timeo ? sock_intr_errno(timeo) :
1694 -EAGAIN;
1695 break;
1696 }
1697 }
1698
1699 skb = skb_peek(&sk->sk_receive_queue);
1700 if (skb)
1701 goto found_ok_skb;
1702
1703 if (csk->wr_credits &&
1704 skb_queue_len(&csk->txq) &&
1705 chtls_push_frames(csk, csk->wr_credits ==
1706 csk->wr_max_credits))
1707 sk->sk_write_space(sk);
1708
1709 if (copied >= target && !sk->sk_backlog.tail)
1710 break;
1711
1712 if (copied) {
1713 if (sk->sk_err || sk->sk_state == TCP_CLOSE ||
1714 (sk->sk_shutdown & RCV_SHUTDOWN) ||
1715 signal_pending(current))
1716 break;
1717 } else {
1718 if (sock_flag(sk, SOCK_DONE))
1719 break;
1720 if (sk->sk_err) {
1721 copied = sock_error(sk);
1722 break;
1723 }
1724 if (sk->sk_shutdown & RCV_SHUTDOWN)
1725 break;
1726 if (sk->sk_state == TCP_CLOSE) {
1727 copied = -ENOTCONN;
1728 break;
1729 }
1730 if (!timeo) {
1731 copied = -EAGAIN;
1732 break;
1733 }
1734 if (signal_pending(current)) {
1735 copied = sock_intr_errno(timeo);
1736 break;
1737 }
1738 }
1739
1740 if (sk->sk_backlog.tail) {
1741 release_sock(sk);
1742 lock_sock(sk);
1743 chtls_cleanup_rbuf(sk, copied);
1744 continue;
1745 }
1746
1747 if (copied >= target)
1748 break;
1749 chtls_cleanup_rbuf(sk, copied);
1750 sk_wait_data(sk, &timeo, NULL);
1751 continue;
1752
1753found_ok_skb:
1754 if (!skb->len) {
1755 chtls_kfree_skb(sk, skb);
1756 if (!copied && !timeo) {
1757 copied = -EAGAIN;
1758 break;
1759 }
1760
1761 if (copied < target)
1762 continue;
1763
1764 break;
1765 }
1766
1767 offset = tp->copied_seq - ULP_SKB_CB(skb)->seq;
1768 avail = skb->len - offset;
1769 if (len < avail)
1770 avail = len;
1771
1772 if (unlikely(tp->urg_data)) {
1773 u32 urg_offset = tp->urg_seq - tp->copied_seq;
1774
1775 if (urg_offset < avail) {
1776 if (urg_offset) {
1777 avail = urg_offset;
1778 } else if (!sock_flag(sk, SOCK_URGINLINE)) {
1779 tp->copied_seq++;
1780 offset++;
1781 avail--;
1782 if (!avail)
1783 goto skip_copy;
1784 }
1785 }
1786 }
1787
1788 if (likely(!(flags & MSG_TRUNC))) {
1789 if (skb_copy_datagram_msg(skb, offset,
1790 msg, avail)) {
1791 if (!copied) {
1792 copied = -EFAULT;
1793 break;
1794 }
1795 }
1796 }
1797
1798 tp->copied_seq += avail;
1799 copied += avail;
1800 len -= avail;
1801
1802skip_copy:
1803 if (tp->urg_data && after(tp->copied_seq, tp->urg_seq))
1804 tp->urg_data = 0;
1805
1806 if (avail + offset >= skb->len) {
1807 if (likely(skb))
1808 chtls_free_skb(sk, skb);
1809 buffers_freed++;
1810
1811 if (copied >= target &&
1812 !skb_peek(&sk->sk_receive_queue))
1813 break;
1814 }
1815 } while (len > 0);
1816
1817 if (buffers_freed)
1818 chtls_cleanup_rbuf(sk, copied);
1819
1820 release_sock(sk);
1821 return copied;
1822}