summaryrefslogtreecommitdiffstats
path: root/sys/ofed/drivers/net/mlx4/en_frag.c
blob: c8429e4b12d38600da9d14ce89ef718d073bcf57 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - 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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#include "opt_inet.h"
#include "mlx4_en.h"

#ifdef INET

#include <net/ethernet.h>
#include <netinet/ip.h>
#include <machine/in_cksum.h>

static struct mlx4_en_ipfrag *find_session(struct mlx4_en_rx_ring *ring,
					   struct ip *iph)
{
	struct mlx4_en_ipfrag *session;
	int i;

	for (i = 0; i < MLX4_EN_NUM_IPFRAG_SESSIONS; i++) {
		session = &ring->ipfrag[i];
		if (session->fragments == NULL)
			continue;
		if (session->daddr == iph->ip_dst.s_addr &&
		    session->saddr == iph->ip_src.s_addr &&
		    session->id == iph->ip_id &&
		    session->protocol == iph->ip_p) {
			return session;
		}
	}
	return NULL;
}

static struct mlx4_en_ipfrag *start_session(struct mlx4_en_rx_ring *ring,
					    struct ip *iph)
{
	struct mlx4_en_ipfrag *session;
	int index = -1;
	int i;

	for (i = 0; i < MLX4_EN_NUM_IPFRAG_SESSIONS; i++) {
		if (ring->ipfrag[i].fragments == NULL) {
			index = i;
			break;
		}
	}
	if (index < 0)
		return NULL;

	session = &ring->ipfrag[index];

	return session;
}


static void flush_session(struct mlx4_en_priv *priv,
			  struct mlx4_en_ipfrag *session,
			  u16 more)
{
	struct mbuf *mb = session->fragments;
	struct ip *iph = mb->m_pkthdr.PH_loc.ptr;
	struct net_device *dev = mb->m_pkthdr.rcvif;

	/* Update IP length and checksum */
	iph->ip_len = htons(session->total_len);
	iph->ip_off = htons(more | (session->offset >> 3));
	iph->ip_sum = 0;
	iph->ip_sum = in_cksum_skip(mb, iph->ip_hl * 4,
	    (char *)iph - mb->m_data);

	dev->if_input(dev, mb);
	session->fragments = NULL;
	session->last = NULL;
}


static inline void frag_append(struct mlx4_en_priv *priv,
			       struct mlx4_en_ipfrag *session,
			       struct mbuf *mb,
			       unsigned int data_len)
{
	struct mbuf *parent = session->fragments;

	/* Update mb bookkeeping */
	parent->m_pkthdr.len += data_len;
	session->total_len += data_len;

	m_adj(mb, mb->m_pkthdr.len - data_len);

	session->last->m_next = mb;
	for (; mb->m_next != NULL; mb = mb->m_next);
	session->last = mb;
}

int mlx4_en_rx_frags(struct mlx4_en_priv *priv, struct mlx4_en_rx_ring *ring,
		     struct mbuf *mb, struct mlx4_cqe *cqe)
{
	struct mlx4_en_ipfrag *session;
	struct ip *iph;
	u16 ip_len;
	u16 ip_hlen;
	int data_len;
	u16 offset;

	iph = (struct ip *)(mtod(mb, char *) + ETHER_HDR_LEN);
	mb->m_pkthdr.PH_loc.ptr = iph;
	ip_len = ntohs(iph->ip_len);
	ip_hlen = iph->ip_hl * 4;
	data_len = ip_len - ip_hlen;
	offset = ntohs(iph->ip_off);
	offset &= IP_OFFMASK;
	offset <<= 3;

	session = find_session(ring, iph);
	if (unlikely(in_cksum_skip(mb, ip_hlen, (char *)iph - mb->m_data))) {
		if (session)
			flush_session(priv, session, IP_MF);
		return -EINVAL;
	}
	if (session) {
		if (unlikely(session->offset + session->total_len !=
		    offset + ip_hlen ||
		    session->total_len + mb->m_pkthdr.len > 65536)) {
			flush_session(priv, session, IP_MF);
			goto new_session;
		}
		frag_append(priv, session, mb, data_len);
	} else {
new_session:
		session = start_session(ring, iph);
		if (unlikely(!session))
			return -ENOSPC;

		session->fragments = mb;
		session->daddr = iph->ip_dst.s_addr;
		session->saddr = iph->ip_src.s_addr;
		session->id = iph->ip_id;
		session->protocol = iph->ip_p;
		session->total_len = ip_len;
		session->offset = offset;
		for (; mb->m_next != NULL; mb = mb->m_next);
		session->last = mb;
	}
	if (!(ntohs(iph->ip_off) & IP_MF))
		flush_session(priv, session, 0);

	return 0;
}


void mlx4_en_flush_frags(struct mlx4_en_priv *priv,
			 struct mlx4_en_rx_ring *ring)
{
	struct mlx4_en_ipfrag *session;
	int i;

	for (i = 0; i < MLX4_EN_NUM_IPFRAG_SESSIONS; i++) {
		session = &ring->ipfrag[i];
		if (session->fragments)
			flush_session(priv, session, IP_MF);
	}
}
#endif
OpenPOWER on IntegriCloud