blob: ffade7e7b18273938422f134f0fd906db813f15f [file] [log] [blame]
xf.li2f424182024-08-20 00:47:34 -07001/*******************************************************************************
2 * Include header files *
3 ******************************************************************************/
4#include <linux/module.h>
5#include <linux/etherdevice.h>
6#include <net/sock.h>
7#include <uapi/linux/sched/types.h>
8#include "zvnet_dev.h"
9#include "ram_config.h"
10#include <net/netfilter/nf_conntrack.h>
l.yangafee7ee2024-10-10 15:01:10 +080011#include <net/SI/fast_common.h>
xf.li6b423c72025-03-14 00:07:42 -070012#include <pub_debug_info.h>
13
xf.li2f424182024-08-20 00:47:34 -070014/*******************************************************************************
15 * Macro definitions *
16 ******************************************************************************/
17#define USE_ZVNET_PACKET
18
19#define WATCHDOG_TIMEO (5*HZ)
20#define XMIT_RETRANS_TIMES 3
21#define ZVNET_SKB_PAD 128
22#define ZVNET_TMP_BUFF_LEN 2048
23#define ZVNET_FREE_BUFF_NUM 256
24#define ZVNET_XMIT_BUFF_NUM 64
25#define ZVNET_XMIT_MAX_QUEUE_NUM 2048
26
27/*******************************************************************************
28 * Type definitions *
29 ******************************************************************************/
30//AP´«µÝ¸øCAPµÄÊý¾Ý°üÐÅÏ¢£¬¸ÃÄÚÈÝдÈë¹²ÏíDDR
31struct T_zvnet_rpmsg
32{
33 void *buff;//skb_headÖ¸Õ룬ÓÃÓÚÊÍ·Åʱ´«µÝ¸øºË¼ä£¬ÒÔ±ã¿ìËÙÊÍ·Å;
34 void *head;//ºË¼äÄÜʹÓõĵØÖ·±ß½ç£¬²»ÄÜÔ½½ç£¬·ñÔòÄÚ´æÒç³öÒì³£;ÎïÀíµØÖ·
35 unsigned short data_off;//ºË¼ä´«µÝÀ´µÄÊý¾Ý°üÊ×µØÖ·£¬Ö¸ÏòMACÖ¡Í·;ÎïÀíµØÖ·
36 unsigned short len;//Êý¾Ý°üÓÐЧ³¤¶È£¬Ò»°ãΪMACÖ¡³¤¶È
37 unsigned short end_off;//end offset
38 unsigned char dev;//cid 1->8
39 unsigned char flag;//0ÆÕͨ°ü£¬1¶þ´Îת·¢°ü£¬2¶þ´Îfastת·¢°ü
40};
41struct T_zvnet_pkt_stats
42{
43 unsigned int pkt;
44 unsigned int len;
45};
46//AP´«µÝ¸øCAPµÄCTÐÅÏ¢£¬¸ÃÄÚÈÝдÈë¹²ÏíDDR
47struct T_zvnet_rpmsg_ctstat
48{
49 void *cap_nfct;
50 unsigned char in;
51 unsigned char out;
52 unsigned short flag;
53 struct T_zvnet_pkt_stats pkt[2];
54};
xf.li6b423c72025-03-14 00:07:42 -070055struct zvnet_arphdr {
56 unsigned short ar_hrd; /* format of hardware address */
57 unsigned short ar_pro; /* format of protocol address */
58 unsigned char ar_hln; /* length of hardware address */
59 unsigned char ar_pln; /* length of protocol address */
60 unsigned short ar_op; /* ARP opcode (command) */
61 unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
62 unsigned char ar_sip[4]; /* sender IP address */
63 unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
64 unsigned char ar_tip[4]; /* target IP address */
65};
66
xf.li2f424182024-08-20 00:47:34 -070067/*******************************************************************************
68 * Local variable definitions *
69 ******************************************************************************/
70struct zvnet_device zvnet_dev[DDR_ZVNET_DEV_MAX];
71int *vir_addr_ap = NULL;
72struct sk_buff_head g_zvnet_skb_queue;
73struct zvnet_channel g_zvnet_chn_info;
74
75#ifdef USE_ZVNET_PACKET
76void *g_zvnet_free_buff[ZVNET_FREE_BUFF_NUM];
77int g_zvnet_free_num;
78spinlock_t g_zvnet_free_lock;
79struct semaphore g_zvnet_free_sem;
80struct semaphore g_zvnet_xmit_sem;
81struct sk_buff_head g_zvnet_skb_xmit_queue;
xf.li6b423c72025-03-14 00:07:42 -070082atomic_t g_zvnet_pm_flag;
xf.li2f424182024-08-20 00:47:34 -070083
84unsigned int g_wrap_packet_size = 1000;
85module_param(g_wrap_packet_size, int, 0644);
86unsigned int g_wrap_num = 10;
87module_param(g_wrap_num, int, 0644);
88unsigned int g_wrap_timeout = 10;
89module_param(g_wrap_timeout, int, 0644);
xf.li1867bfa2024-08-20 02:32:16 -070090unsigned int g_trace_limit = 0;
91module_param(g_trace_limit, int, 0644);
xf.li2f424182024-08-20 00:47:34 -070092#endif
93
94/*******************************************************************************
95 * Global variable definitions *
96 ******************************************************************************/
97extern int (*fast_from_driver)(struct sk_buff *skb, struct net_device* dev);
98extern void v7_dma_map_area(const void *, size_t, int);
99extern void *get_ct_for_ap(struct sk_buff *skb);
100extern void put_ct_for_ap(void *ct);
xf.li2f424182024-08-20 00:47:34 -0700101/*******************************************************************************
102 * Local function declarations *
103 ******************************************************************************/
104static int zvnet_open(struct net_device *net);
105static int zvnet_close(struct net_device *net);
106static netdev_tx_t zvnet_xmit(struct sk_buff *skb, struct net_device *net);
107static void zvnet_tx_timeout(struct net_device *net, unsigned int txqueue);
108static struct net_device_stats *zvnet_get_stats(struct net_device *net);
109static void v2xnet_init_netdev(struct net_device *net);
110static void zvnet_skb_return (struct zvnet *dev, struct sk_buff *skb);
111static void zvnet_bh (unsigned long param);
112static struct zvnet *v2xnet_dev_init(struct net_device *net, struct zvnet_device *zvnetdev);
113
114static int zvnet_channel_write(struct zvnet_channel *chninfo, void *buf, unsigned int len);
115static int zvnet_channel_read(struct zvnet_channel *chninfo, void *buf, unsigned int len);
116static int zvnet_channel_clear(struct zvnet_channel *chninfo);
117static int zvnet_read_header(struct zvnet_channel *chninfo, struct zvp_header *phzvp);
118static struct sk_buff *zvnet_direct_read_skb(struct zvnet_channel *chninfo);
119static struct sk_buff *zvnet_read_skb(struct zvnet_channel *chninfo, unsigned int tlen, struct zvnet *dev);
120static int zvnet_receive_thread(void *argv);
121static int rpmsgCreateChannel_v2xnet (T_RpMsg_CoreID dstCoreID, T_RpMsg_ChID chID, unsigned int size);
122static int zvnet_createIcpChannel(T_RpMsg_CoreID core_id, T_RpMsg_ChID channel_id, unsigned int channel_size);
123static int zvnet_channel_create(struct zvnet_device *zvnetdev);
124
125/*******************************************************************************
126 * Local function implementations *
127 ******************************************************************************/
128
129unsigned long virt_to_phys_ap(unsigned long virt)
130{
131 if(virt >= (unsigned long)vir_addr_ap && virt <= ((unsigned long)vir_addr_ap+DDR_BASE_LEN_AP))
132 return DDR_BASE_ADDR_AP + (virt - (unsigned long)vir_addr_ap);
133 return NULL;
134}
135
136unsigned long phys_to_virt_ap(unsigned long phys)
137{
138 if(phys >= DDR_BASE_ADDR_AP && phys <= (DDR_BASE_ADDR_AP + DDR_BASE_LEN_AP))
139 return (unsigned long)vir_addr_ap + (phys - DDR_BASE_ADDR_AP);
140 return NULL;
141}
142
l.yangafee7ee2024-10-10 15:01:10 +0800143/* Started by AICoder, pid:2fa080381bb2e3d14fbc0aa44091291a60d78e35 */
xf.li2f424182024-08-20 00:47:34 -0700144void check_skb_test(struct sk_buff *skb)
145{
l.yangafee7ee2024-10-10 15:01:10 +0800146 if (skb && vir_addr_ap) {
147 struct sk_buff *tmp_skb;
148 if ((skb->capHead && (virt_to_phys_ap(skb->head) == NULL))
149 || ((skb->capHead == NULL) && virt_to_phys_ap(skb->head))) {
150 dump_stack();
151 panic("capHead err");
152 }
153 skb_queue_walk(&g_zvnet_skb_queue, tmp_skb) {
154 if (tmp_skb == skb) {
155 dump_stack();
156 panic("dup free");
157 }
158 }
159 }
xf.li2f424182024-08-20 00:47:34 -0700160}
l.yangafee7ee2024-10-10 15:01:10 +0800161/* Ended by AICoder, pid:2fa080381bb2e3d14fbc0aa44091291a60d78e35 */
162
163/* Started by AICoder, pid:z5702yf8bad07ad1448a083e806dc31250b2418f */
xf.li1867bfa2024-08-20 02:32:16 -0700164void zvnet_dump_packet(unsigned char * data, int len, int limit_len)
165{
166 int i = 0;
l.yangafee7ee2024-10-10 15:01:10 +0800167 unsigned char *p = data;
168 for(i = 0; i < len && i < limit_len; i+=16)
xf.li1867bfa2024-08-20 02:32:16 -0700169 {
xf.li6b423c72025-03-14 00:07:42 -0700170 printk("0x%04x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",i,
l.yangafee7ee2024-10-10 15:01:10 +0800171 p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7],
172 p[8],p[9],p[10],p[11],p[12],p[13],p[14],p[15]);
173 p += 16;
xf.li1867bfa2024-08-20 02:32:16 -0700174 }
175}
l.yangafee7ee2024-10-10 15:01:10 +0800176/* Ended by AICoder, pid:z5702yf8bad07ad1448a083e806dc31250b2418f */
177
xf.li6b423c72025-03-14 00:07:42 -0700178void zvnet_set_pm_flag(unsigned int flag){
179 if(flag & 0x100000)
180 atomic_set(&g_zvnet_pm_flag, 1);
181}
182
xf.li2f424182024-08-20 00:47:34 -0700183int zvnet_get_index_by_netdev(struct net_device *net)
184{
185 int i;
186
187 for (i = 0; i < DDR_ZVNET_DEV_MAX; i++) {
188 if(zvnet_dev[i].net == net)
189 return i;
190 }
191 return -1;
192}
193#ifdef USE_ZVNET_PACKET
194void write_free_apbuf_packet(void)
195{
196 int ret,size;
197 unsigned long flags;
198 void *buf[ZVNET_FREE_BUFF_NUM];
199
200 spin_lock_irqsave(&g_zvnet_free_lock, flags);
201 if(g_zvnet_free_num == 0){
202 spin_unlock_irqrestore(&g_zvnet_free_lock, flags);
203 return;
204 }
205 size = (g_zvnet_free_num << 2);
206 memcpy(buf, g_zvnet_free_buff, size);
207 g_zvnet_free_num = 0;
208 spin_unlock_irqrestore(&g_zvnet_free_lock, flags);
209 {
210 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
211 .chID = 29,
212 .flag = RPMSG_WRITE_INT|RPMSG_WRITE_IRQLOCK};
213 if((size >> 2) > ZVNET_FREE_BUFF_NUM)
214 panic("free packet err");
215 msg.buf = &buf;
216 msg.len = size;
217 zv_info("tofree size=%d", size);
218 ret = zvnetWrite(&msg);
219 if(ret < 0)
220 panic("err, ret:%d!!!!!!", ret);
221 }
222}
223#endif
224//½«CAP²à´«µÝÀ´µÄcapbufÖ¸ÕëдÈëDDR£¬ÒÔICP·½Ê½Í¨ÖªCAP²à
225void write_free_apbuf(void *head)
226{
227#ifdef USE_ZVNET_PACKET
228 unsigned long flags;
229
230 zv_info("g_zvnet_free_num=%d skb=0x%x", g_zvnet_free_num, head);
231 spin_lock_irqsave(&g_zvnet_free_lock, flags);
232 g_zvnet_free_buff[g_zvnet_free_num] = head;
233 g_zvnet_free_num++;
234 if(g_zvnet_free_num == ZVNET_FREE_BUFF_NUM){
235 int size;
236 void *buf[ZVNET_FREE_BUFF_NUM];
237
238 size = (g_zvnet_free_num << 2);
239 memcpy(buf, g_zvnet_free_buff, size);
240 g_zvnet_free_num = 0;
241 spin_unlock_irqrestore(&g_zvnet_free_lock, flags);
242 if((size >> 2) > ZVNET_FREE_BUFF_NUM)
243 panic("free packet err");
244 {
245 int ret;
246 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
247 .chID = 29,
248 .flag = RPMSG_WRITE_INT|RPMSG_WRITE_IRQLOCK};
249 msg.buf = &buf;
250 msg.len = size;
251 if(printk_ratelimit())
252 zv_warn("tofree quick size=%d", size);
253 ret = zvnetWrite(&msg);
254 if(ret < 0)
255 panic("err, ret:%d!!!!!!", ret);
256 }
257 return;
258 }
259 if(g_zvnet_free_num > g_wrap_num)
260 up(&g_zvnet_free_sem);
261 if(g_zvnet_free_num > ZVNET_FREE_BUFF_NUM)
262 panic("free_buff err");
263 spin_unlock_irqrestore(&g_zvnet_free_lock, flags);
264#else
265 int ret = 0;
266 long buf = (long)head;
267
268 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
269 .chID = 29,
270 .flag = RPMSG_WRITE_INT,
271 .buf = NULL,
272 .len = 4 };
273 msg.buf = &buf;
274 zv_info("tofree 0x%x", head);
275 ret = zvnetWrite(&msg);
276 if(ret < 0)
277 panic("err, ret:%d!!!!!!", ret);
278#endif
279}
280
281struct sk_buff *skb_build_apbuf(struct T_zvnet_rpmsg *pbuf_temp)
282{
283 struct skb_shared_info *shinfo;
284 struct sk_buff *skb;
285 zv_info("build 0x%x 0x%x %d %d", pbuf_temp->buff, pbuf_temp->head, pbuf_temp->data_off, pbuf_temp->len);
286 if((unsigned long )pbuf_temp->head < DDR_BASE_ADDR_AP || (unsigned long )pbuf_temp->head > (DDR_BASE_ADDR_AP + DDR_BASE_LEN_AP)){
287 zv_err("err 0x%x 0x%x %d %d", pbuf_temp->buff, pbuf_temp->head, pbuf_temp->data_off, pbuf_temp->len);
288 panic("addr is not APBUF mem!!!");
289 return NULL;
290 }
291 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
292 if (!skb)
293 {
294 write_free_apbuf(pbuf_temp->buff);
295 zv_err("alloc fail");
296 return NULL;
297 }
298 memset(skb, 0, offsetof(struct sk_buff, tail));
299
300 //ÅжÏÊÇ·ñÊÇapbuff
301
302 if(pbuf_temp->dev < 0 || pbuf_temp->dev >= DDR_ZVNET_DEV_MAX)
303 panic("dev index error!!!");
304 skb->head = phys_to_virt_ap((unsigned long )pbuf_temp->head);
305 skb->data = skb->head + pbuf_temp->data_off;
306 skb->capHead = pbuf_temp->buff;
307 //ÐèÒª¿¼ÂÇcacheÐÐ¶ÔÆë
308 skb->truesize = SKB_TRUESIZE(skb->data - skb->head + pbuf_temp->len);//°´µÀÀíβ²¿»¹ÓпÉÓõÄÄÚ´æ¿Õ¼ä£¬ÔÝʱδ¿¼ÂÇ;SKB_DATA_ALIGN
309
310 refcount_set(&skb->users, 1);
311 skb_reset_tail_pointer(skb);
312 skb->tail += pbuf_temp->len;
313 skb->len = pbuf_temp->len;
314 skb->end = skb->head + pbuf_temp->end_off;
315 skb->mac_header = (typeof(skb->mac_header))~0U;
316 skb->transport_header = (typeof(skb->transport_header))~0U;
317 skb->dev = zvnet_dev[pbuf_temp->dev].net;
318 if(skb->len == 0 || skb->len > 2000)
319 panic("len ERR!!!!!!!!!!\n");
320 v7_dma_map_area(skb->head, sizeof(struct skb_shared_info) + pbuf_temp->end_off, DMA_FROM_DEVICE);
321 if(IFF_NOARP & skb->dev->flags)
322 memcpy(skb->data, skb->dev->dev_addr, 6);
323 atomic_set(&skb_shinfo(skb)->dataref, 1);
324 /* make sure we initialize shinfo sequentially */
325 skb_reset_network_header(skb);
326 skb_set_kcov_handle(skb, kcov_common_handle());
xf.li6b423c72025-03-14 00:07:42 -0700327 if(unlikely(g_trace_limit & 1)){
328 printk("-%s-dump_fromap-start-%d\n", skb->dev->name, skb->len);
l.yangafee7ee2024-10-10 15:01:10 +0800329 zvnet_dump_packet(skb->data, skb->len, g_trace_limit);
xf.li6b423c72025-03-14 00:07:42 -0700330 printk("-%s-dump_fromap-end-\n", skb->dev->name);
l.yangafee7ee2024-10-10 15:01:10 +0800331 }
xf.li6b423c72025-03-14 00:07:42 -0700332/* Started by AICoder, pid:j2d34uccf7y1f37146a108290182771184940711 */
333 if (atomic_read(&g_zvnet_pm_flag)) {
334 unsigned short l2_hdr_len = 0;
335 unsigned short h_proto = htons(*(unsigned short *)(skb->data + ETH_ALEN + ETH_ALEN));
336 again:
337 if (l2_hdr_len + ETH_HLEN < skb->len) {
338 switch (h_proto) {
339 case ETH_P_IP: {
340 struct iphdr *iph = (struct iphdr *)(skb->data + ETH_HLEN + l2_hdr_len);
341 if (iph->protocol == IPPROTO_TCP) {
342 struct tcphdr *tcph = (struct tcphdr *)(((unsigned char *)iph) + iph->ihl * 4);
343 char *flag;
344 if (tcph->ack) {
345 if (tcph->fin)
346 flag = "FA";
347 else if (tcph->syn)
348 flag = "SA";
349 else if (tcph->psh)
350 flag = "PA";
351 else
352 flag = "A";
353 } else {
354 if (tcph->fin)
355 flag = "F";
356 else if (tcph->syn)
357 flag = "S";
358 else if (tcph->rst)
359 flag = "R";
360 else
361 flag = "";
362 }
363 sc_debug_info_record("cap_net", "%u-%pI4-%pI4-%u%s %u:%u/%u\n",
364 pbuf_temp->dev, &iph->saddr, &iph->daddr,
365 iph->protocol, flag, ntohs(tcph->source), ntohs(tcph->dest), skb->len);
366 } else if (iph->protocol == IPPROTO_UDP) {
367 struct udphdr *udph = (struct udphdr *)(((unsigned char *)iph) + iph->ihl * 4);
368 sc_debug_info_record("cap_net", "%u-%pI4-%pI4-%u %u:%u/%u\n",
369 pbuf_temp->dev, &iph->saddr, &iph->daddr,
370 iph->protocol, ntohs(udph->source), ntohs(udph->dest), skb->len);
371 } else if (iph->protocol == IPPROTO_ICMP) {
372 struct icmphdr *icmph = (struct icmphdr *)(((unsigned char *)iph) + iph->ihl * 4);
373 sc_debug_info_record("cap_net", "%u-%pI4-%pI4-%u %u:%u/%u\n",
374 pbuf_temp->dev, &iph->saddr, &iph->daddr,
375 iph->protocol, icmph->type, icmph->code, skb->len);
376 } else {
377 sc_debug_info_record("cap_net", "%u-%pI4-%pI4-%u/%u\n",
378 pbuf_temp->dev, &iph->saddr, &iph->daddr,
379 iph->protocol, skb->len);
380 }
381 break;
382 }
383 case ETH_P_IPV6: {
384 struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + ETH_HLEN + l2_hdr_len);
385 if (iph->nexthdr == NEXTHDR_TCP) {
386 struct tcphdr *tcph = (struct tcphdr *)(((unsigned char *)iph) + sizeof(struct ipv6hdr));
387 char *flag;
388 if (tcph->ack) {
389 if (tcph->fin)
390 flag = "FA";
391 else if (tcph->syn)
392 flag = "SA";
393 else if (tcph->psh)
394 flag = "PA";
395 else
396 flag = "A";
397 } else {
398 if (tcph->fin)
399 flag = "F";
400 else if (tcph->syn)
401 flag = "S";
402 else if (tcph->rst)
403 flag = "R";
404 else
405 flag = "";
406 }
407 sc_debug_info_record("cap_net", "%u-%pI6-%pI6-%u%s %u:%u/%u\n",
408 pbuf_temp->dev, iph->saddr.s6_addr32, iph->daddr.s6_addr32,
409 iph->nexthdr, flag, ntohs(tcph->source), ntohs(tcph->dest), skb->len);
410 } else if (iph->nexthdr == NEXTHDR_UDP) {
411 struct udphdr *udph = (struct udphdr *)(((unsigned char *)iph) + sizeof(struct ipv6hdr));
412 sc_debug_info_record("cap_net", "%u-%pI6-%pI6-%u %u:%u/%u\n",
413 pbuf_temp->dev, iph->saddr.s6_addr32, iph->daddr.s6_addr32,
414 iph->nexthdr, ntohs(udph->source), ntohs(udph->dest), skb->len);
415 } else if (iph->nexthdr == NEXTHDR_ICMP) {
416 struct icmp6hdr *icmph = (struct icmp6hdr *)(((unsigned char *)iph) + sizeof(struct ipv6hdr));
417 sc_debug_info_record("cap_net", "%u-%pI6-%pI6-%u %u:%u/%u\n",
418 pbuf_temp->dev, iph->saddr.s6_addr32, iph->daddr.s6_addr32,
419 iph->nexthdr, icmph->icmp6_type, icmph->icmp6_code,skb->len);
420 } else {
421 sc_debug_info_record("cap_net", "%u-%pI6-%pI6-%u/%u\n",
422 pbuf_temp->dev, iph->saddr.s6_addr32, iph->daddr.s6_addr32,
423 iph->nexthdr, skb->len);
424 }
425 break;
426 }
427 case ETH_P_ARP: {
428 struct zvnet_arphdr *arph = (struct zvnet_arphdr *)(skb->data + ETH_HLEN + l2_hdr_len);
429 sc_debug_info_record("cap_net", "%u:%04x-%pI4-%pI4-%u/%u\n",
430 pbuf_temp->dev, h_proto, arph->ar_sip, arph->ar_tip, htons(arph->ar_op), skb->len);
431 break;
432 }
433 case ETH_P_8021Q: {
434 struct vlan_hdr *vlanh = (struct vlan_hdr *)(skb->data + ETH_HLEN + l2_hdr_len);
435 sc_debug_info_record("cap_net", "%u:%04x-%u\n",
436 pbuf_temp->dev, h_proto, htons(vlanh->h_vlan_TCI) & VLAN_VID_MASK);
437 l2_hdr_len += VLAN_HLEN;
438 h_proto = htons(vlanh->h_vlan_encapsulated_proto);
439 goto again;
440 }
441 default:
442 sc_debug_info_record("cap_net", "%u:%04x/%u\n", pbuf_temp->dev, h_proto, skb->len);
443 }
444 }
445 atomic_set(&g_zvnet_pm_flag, 0);
446 }
447/* Ended by AICoder, pid:j2d34uccf7y1f37146a108290182771184940711 */
xf.li2f424182024-08-20 00:47:34 -0700448 return skb;
449}
450
451int eth_change_mtu(struct net_device *dev, int new_mtu)
452{
453 netdev_warn(dev, "%s is deprecated!\n", __func__);
454 dev->mtu = new_mtu;
455 return 0;
456}
457
l.yangafee7ee2024-10-10 15:01:10 +0800458/* Started by AICoder, pid:b001dtf2551fd53146790a57201be3321cf0a682 */
xf.li2f424182024-08-20 00:47:34 -0700459static void skb_debug_test(struct sk_buff *skb)
460{
461 int i;
l.yangafee7ee2024-10-10 15:01:10 +0800462 int vcount = skb->len / 10;
463 int rcount = skb->len % 10;
xf.li2f424182024-08-20 00:47:34 -0700464 char tmp[64] = {0};
465 char strbuf[64] = {0};
l.yangafee7ee2024-10-10 15:01:10 +0800466 const unsigned char *data = skb->data;
xf.li2f424182024-08-20 00:47:34 -0700467
468 zv_info("\n");
l.yangafee7ee2024-10-10 15:01:10 +0800469 for (i = 0; i < vcount; i++) {
470 zv_info("%d---%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n", i,
471 data[0 + 10 * i], data[1 + 10 * i], data[2 + 10 * i], data[3 + 10 * i],
472 data[4 + 10 * i], data[5 + 10 * i], data[6 + 10 * i], data[7 + 10 * i],
473 data[8 + 10 * i], data[9 + 10 * i]);
xf.li2f424182024-08-20 00:47:34 -0700474 }
l.yangafee7ee2024-10-10 15:01:10 +0800475 if (vcount > 0) {
476 memset(tmp, 0, sizeof(tmp));
477 sprintf(strbuf, "%d---", vcount);
478 char *p = strbuf + strlen(strbuf);
xf.li2f424182024-08-20 00:47:34 -0700479
l.yangafee7ee2024-10-10 15:01:10 +0800480 for (i = 0; i < rcount; i++) {
481 sprintf(p, "%02x,", data[10 * vcount + i]);
482 p += strlen(p);
xf.li2f424182024-08-20 00:47:34 -0700483 }
l.yangafee7ee2024-10-10 15:01:10 +0800484 *(p - 1) = '\0'; // ÒÆ³ý×îºóÒ»¸ö¶ººÅ
485 zv_info("%s ", strbuf);
xf.li2f424182024-08-20 00:47:34 -0700486 }
487 zv_info("\n");
488}
l.yangafee7ee2024-10-10 15:01:10 +0800489/* Ended by AICoder, pid:b001dtf2551fd53146790a57201be3321cf0a682 */
xf.li2f424182024-08-20 00:47:34 -0700490
491static int zvnet_open(struct net_device *net)
492{
493 struct zvnet *dev = netdev_priv(net);
494
495 if(net->flags & IFF_UP) {
496 zv_dbg("%s has been opened!", dev->net->name);
497 return -EBUSY;
498 }
499 netif_start_queue (net);
500
501 return 0;
502}
503
504static int zvnet_close(struct net_device *net)
505{
506 struct zvnet *dev = netdev_priv(net);
507
508 zv_info("%s", dev->net->name);
509 netif_stop_queue(net);
510 tasklet_kill (&dev->bh);
511
512 return 0;
513}
514#ifdef USE_ZVNET_PACKET
515static void zvnet_xmit_packet(void)
516{
517 int i,j,k,ret,num;
518 unsigned long flags;
519 unsigned long flags1;
520 struct sk_buff *skb, *tmp;
521 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
522 .chID = 20,
523 .flag = RPMSG_WRITE_INT};
524 static struct T_zvnet_rpmsg buff[ZVNET_XMIT_MAX_QUEUE_NUM+1];
525
526 spin_lock_irqsave(&g_zvnet_skb_xmit_queue.lock, flags);
527 if (skb_queue_empty(&g_zvnet_skb_xmit_queue)) {
528 spin_unlock_irqrestore(&g_zvnet_skb_xmit_queue.lock, flags);
529 return;
530 }
531 i = 0;
532 skb_queue_walk_safe(&g_zvnet_skb_xmit_queue, skb, tmp) {
533 //buff[i].buff = skb;
534 buff[i].data_off = skb->data - skb->head;
535 //buff[i].head = virt_to_phys(skb->head);
536 buff[i].len = skb->len;
537 buff[i].end_off = skb->end - skb->head;
538 buff[i].dev = zvnet_get_index_by_netdev(skb->dev);
xf.li6b423c72025-03-14 00:07:42 -0700539 if(unlikely(g_trace_limit & 2)){
540 printk("-%s-dump_toap-start-%d\n", skb->dev->name, skb->len);
541 zvnet_dump_packet(skb->data, skb->len, g_trace_limit);
542 printk("-%s-dump_toap-end-\n", skb->dev->name);
543 }
xf.li2f424182024-08-20 00:47:34 -0700544 if(skb->capHead){
545 buff[i].buff = skb->capHead;
546#ifdef CONFIG_FASTNAT_MODULE
547 if(skb->isFastnat){
548 buff[i].head = get_ct_for_ap(skb);
549 buff[i].flag = 2;
550 }else
551#endif
552 {
553 buff[i].head = NULL;
554 buff[i].flag = 1;
555 }
556 __skb_unlink(skb, &g_zvnet_skb_xmit_queue);
557 kfree_skb(skb);
558 }else{
559 buff[i].buff = skb;
560 buff[i].head = virt_to_phys(skb->head);
561 buff[i].flag = 0;
562 }
563 i++;
564 zv_info("xmit skb=0x%x i=%d", skb, i);
565 if(i > ZVNET_XMIT_MAX_QUEUE_NUM){
566 panic("qlen:%d!", i);
567 break;
568 }
569 }
570 spin_lock_irqsave(&g_zvnet_skb_queue.lock, flags1);
571 skb_queue_splice_tail_init(&g_zvnet_skb_xmit_queue, &g_zvnet_skb_queue);
572 spin_unlock_irqrestore(&g_zvnet_skb_queue.lock, flags1);
573 spin_unlock_irqrestore(&g_zvnet_skb_xmit_queue.lock, flags);
574 zv_info("g_zvnet_skb_queue.qlen=%d i=%d", g_zvnet_skb_queue.qlen, i);
575 for(j = 0; j < i; j = j + ZVNET_XMIT_BUFF_NUM){
576 if(i <= (j + ZVNET_XMIT_BUFF_NUM)){
577 msg.buf = (void *)&buff[j];
578 msg.len = sizeof(struct T_zvnet_rpmsg)*(i-j);/*±¾´ÎÄÜÈ¡¹â*/
579 ret = zvnetWrite(&msg);
580 }else{
581 msg.buf = (void *)&buff[j];
582 msg.len = sizeof(struct T_zvnet_rpmsg)*ZVNET_XMIT_BUFF_NUM;
583 ret = zvnetWrite(&msg);
584 }
585 zv_info("xmit write ret=%d size=%d i=%d j=%d", ret, msg.len, i, j);
586 if(ret < 0) {
587 if(printk_ratelimit())
588 zv_warn("zvnet_channel_write ret=%d fail.",ret);
589 num = msg.len / sizeof(struct T_zvnet_rpmsg);
590 for(k = j; k < j+num; k++){
591 if(buff[k].flag == 0){
592 skb = (struct sk_buff *)buff[k].buff;
593 skb_unlink(skb, &g_zvnet_skb_queue);
594 skb->isToap = 0;
595 kfree_skb(skb);
596 }else{
597 if(buff[k].head)
598 put_ct_for_ap(buff[k].head);
599 write_free_apbuf(buff[k].buff);
600 }
601 }
602 }
603 }
604}
605#endif
606static netdev_tx_t zvnet_xmit(struct sk_buff *skb, struct net_device *net)
607{
608#ifdef USE_ZVNET_PACKET
609 struct sk_buff *data = NULL;
610
611 //zv_info("g_zvnet_skb_xmit_queue.qlen=%d", g_zvnet_skb_xmit_queue.qlen);
612 if(g_zvnet_skb_xmit_queue.qlen >= ZVNET_XMIT_MAX_QUEUE_NUM){
613 net->stats.tx_errors++;
614 net->stats.tx_dropped++;
615 zv_err("write err, qlen:%d!", g_zvnet_skb_xmit_queue.qlen);
616 kfree_skb(skb);
617 return NET_XMIT_SUCCESS;
618 }
619
l.yangafee7ee2024-10-10 15:01:10 +0800620 if(unlikely(skb->next//|| skb->capHead || skb_headroom(skb) < NET_SKB_PAD
621 || skb->fclone || skb->cloned || (skb_shinfo(skb)->nr_frags) || skb->sk || (skb->indev == NULL)
xf.li2f424182024-08-20 00:47:34 -0700622 || (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) || (skb_has_frag_list(skb)))){
623 int ret_len = skb->len;
624
625 data = dev_alloc_skb(ret_len + NET_IP_ALIGN);
626 if (unlikely(!data)) {
627 zv_err("dev_alloc_skb fail,len %d",ret_len);
628 net->stats.tx_errors++;
629 net->stats.tx_dropped++;
630 kfree_skb(skb);
631 return NET_XMIT_SUCCESS;
632 }
633 skb_put(data,ret_len);
634 skb_reserve(data, NET_IP_ALIGN);
635 memcpy(data->data, skb->data, ret_len);
636 zv_info("ap=0x%x next=0x%x clone=%d nr_frags=%d tx_flags=%d frag_list=0x%x", skb->capHead, skb->next, skb->cloned, (skb_shinfo(skb)->nr_frags), skb_shinfo(skb)->tx_flags, skb_shinfo(skb)->frag_list);
637 kfree_skb(skb);
638 }else{
639 data = skb;
640 }
641 data->dev = net;
642 data->isToap = 1;
643 v7_dma_map_area(data->head, data->end - data->head + sizeof(struct skb_shared_info), DMA_TO_DEVICE);
xf.li6b423c72025-03-14 00:07:42 -0700644 net->stats.tx_packets++;
645 net->stats.tx_bytes += data->len;
xf.li2f424182024-08-20 00:47:34 -0700646 skb_queue_tail(&g_zvnet_skb_xmit_queue, data);
647 if(data->len < g_wrap_packet_size || g_zvnet_skb_xmit_queue.qlen > g_wrap_num)
648 up(&g_zvnet_xmit_sem);
xf.li2f424182024-08-20 00:47:34 -0700649#else
650 struct zvnet *dev = netdev_priv(net);
651 struct zvnet_device *zvnetdev = (struct zvnet_device *)dev->dev_priv;
652 int ret = 0;
653 struct zvp_header hzvp;
654
655 if (!skb) {
656 zv_err("err: skb == 0!");
657 }
658#if 0
659 if (skb->len > ZVNET_TMP_BUFF_LEN) {
660 zv_err("err: skb->len(%d)>%d!", skb->len, ZVNET_TMP_BUFF_LEN);
661 }
662
663send_header:
664 ret = zvnet_channel_write(&(zvnetdev->chn_info), skb->data, skb->len);
665
666 if((ret < 0) && (zvnetdev->retran_times < XMIT_RETRANS_TIMES)) {
667 zvnetdev->retran_times ++;
668 zv_warn("The retran_times is %d.",zvnetdev->retran_times);
669 goto send_header;
670 }
671
672 if (ret >= 0) {
673 net->stats.tx_packets++;
674 net->stats.tx_bytes += skb->len;
675 } else {
676 net->stats.tx_errors++;
677 net->stats.tx_dropped++;
678 zv_err("write err, ret:%d!", ret);
679 }
680
681exit:
682 kfree_skb(skb);
683#else
684 struct T_zvnet_rpmsg buff = {0};
685 struct sk_buff *data = NULL;
686 if(unlikely(skb_headroom(skb) < NET_SKB_PAD || skb->capHead || skb->next
687 || skb->fclone || skb->cloned || (skb_shinfo(skb)->nr_frags)
688 || (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) || (skb_has_frag_list(skb)))){
689 int ret_len = skb->len;
690
691 data = dev_alloc_skb(ret_len + NET_IP_ALIGN);
692 if (unlikely(!data)) {
693 zv_err("dev_alloc_skb fail,len %d",ret_len);
694 net->stats.tx_errors++;
695 net->stats.tx_dropped++;
696 kfree_skb(skb);
697 return NET_XMIT_SUCCESS;
698 }
699 skb_put(data,ret_len);
700 skb_reserve(data, NET_IP_ALIGN);
701 memcpy(data->data, skb->data, ret_len);
702 data->isToap = 1;
703 buff.buff = data;
704 buff.data_off = data->data - data->head;
705 buff.head = virt_to_phys(data->head);
706 buff.len = ret_len;
707 buff.end_off = data->end - data->head;
708 buff.dev = zvnet_get_index_by_netdev(net);
709 zv_info("alloc 0x%x 0x%x %d %d", buff.buff, buff.head, buff.data_off, buff.len);
710 zv_info("ap=0x%x next=0x%x clone=%d nr_frags=%d tx_flags=%d frag_list=0x%x", skb->capHead, skb->next, skb->cloned, (skb_shinfo(skb)->nr_frags), skb_shinfo(skb)->tx_flags, skb_shinfo(skb)->frag_list);
711 v7_dma_map_area(data->head, buff.end_off + sizeof(struct skb_shared_info), DMA_TO_DEVICE);
712 }else{
713 skb->isToap = 1;
714 buff.buff = skb;
715 buff.data_off = skb->data - skb->head;
716 buff.head = virt_to_phys(skb->head);
717 buff.len = skb->len;
718 buff.end_off = skb->end - skb->head;
719 buff.dev = zvnet_get_index_by_netdev(net);
720 zv_info("transfer 0x%x %d 0x%x %d", buff.buff, buff.head, buff.data_off, buff.len);
721 v7_dma_map_area(skb->head, buff.end_off + sizeof(struct skb_shared_info), DMA_TO_DEVICE);
722 }
723send_header:
724 ret = zvnet_channel_write(&g_zvnet_chn_info, &buff, sizeof(struct T_zvnet_rpmsg));
725
726 if((ret < 0) && (zvnetdev->retran_times < XMIT_RETRANS_TIMES)) {
727 zvnetdev->retran_times ++;
728 zv_warn("The retran_times is %d.",zvnetdev->retran_times);
729 goto send_header;
730 }
731
732 if (ret >= 0) {
733 net->stats.tx_packets++;
734 net->stats.tx_bytes += skb->len;
735 if(data){
736 kfree_skb(skb);
737 skb_queue_tail(&g_zvnet_skb_queue, data);
738 }else
739 skb_queue_tail(&g_zvnet_skb_queue, skb);
740 zvnetdev->retran_times = 0;
741 } else {
742 net->stats.tx_errors++;
743 net->stats.tx_dropped++;
744 zv_err("write err, ret:%d!", ret);
745 if(data){
746 data->isToap = 0;
747 kfree_skb(data);
748 }
749 else
750 skb->isToap = 0;
751 kfree_skb(skb);
752 }
753#endif
754#endif
755 return NET_XMIT_SUCCESS;
756}
757
758/* Called by the kernel when transmit times out */
759static void zvnet_tx_timeout(struct net_device *net, unsigned int txqueue)
760{
761 zv_warn("sent timeout!");
762 net->stats.tx_errors++;
763 netif_wake_queue(net);
764}
765
766static struct net_device_stats *zvnet_get_stats(struct net_device *net)
767{
768 return &net->stats;
769}
770
771const struct net_device_ops zvnet_netdev_ops = {
772 .ndo_open = zvnet_open,
773 .ndo_stop = zvnet_close,
774 .ndo_start_xmit = zvnet_xmit,
775 .ndo_tx_timeout = zvnet_tx_timeout,
776 .ndo_get_stats = zvnet_get_stats,
777 .ndo_change_mtu = eth_change_mtu,
778 .ndo_validate_addr = eth_validate_addr,
l.yangafee7ee2024-10-10 15:01:10 +0800779 .ndo_set_mac_address = eth_mac_addr,
xf.li2f424182024-08-20 00:47:34 -0700780};
781
782static void v2xnet_init_netdev(struct net_device *net)
783{
784 u8 node_id [ETH_ALEN];
785
786 random_ether_addr(node_id);
787 memcpy (net->dev_addr, node_id, sizeof node_id);
788
789 net->netdev_ops = &zvnet_netdev_ops;
790 net->watchdog_timeo = WATCHDOG_TIMEO;
791 net->flags |= IFF_NOARP;
792}
793
794static void zvnet_skb_return (struct zvnet *dev, struct sk_buff *skb)
795{
796 int status;
797
798 //zv_info("enter...");
799
800 //skb->protocol = eth_type_trans(skb, dev->net);
801
802 status = netif_rx (skb);
803 if (status == NET_RX_SUCCESS) {
804 dev->net->stats.rx_packets++;
805 dev->net->stats.rx_bytes += skb->len;
806 } else {
807 dev->net->stats.rx_errors++;
808 zv_err("netif_rx status %d.", status);
809 }
810}
811
812static void zvnet_bh (unsigned long param)
813{
814 struct zvnet *dev = (struct zvnet *)param;
815 struct sk_buff *skb;
816
817 while((skb = skb_dequeue(&dev->rxq)) != NULL) {
818 if (skb->len)
819 zvnet_skb_return(dev, skb);
820 else {
821 dev->net->stats.rx_errors++;
822 dev_kfree_skb (skb);
823 zv_err("drop!!!ddrnet_bh skb len == 0.");
824 }
825 }
826}
827
828static struct zvnet *v2xnet_dev_init(struct net_device *net, struct zvnet_device *zvnetdev)
829{
830 struct zvnet *dev = NULL;
831
832 dev = netdev_priv(net);
833 if(!dev) {
834 zv_err("dev is null.\n");
835 return NULL;
836 }
837
838 dev->net = net;
839 dev->bh.func = zvnet_bh;
840 dev->bh.data = (unsigned long) dev;
841
842 skb_queue_head_init (&dev->rxq);
843
844 dev->dev_priv = zvnetdev;
845
846 return dev;
847}
848
849/*·µ»ØÖµ´óÓÚµÈÓÚ0£¬±íʾдͨµÀ³É¹¦£»Ð¡ÓÚ0±íʾдͨµÀʧ°Ü*/
850static int zvnet_channel_write(struct zvnet_channel *chninfo, void *buf, unsigned int len)
851{
852 T_RpMsg_Msg msg;
853
854 if(NULL == buf) {
855 return -EINVAL;
856 }
857 memset(&msg, 0, sizeof(msg));
858 msg.coreID = chninfo->core_id;
859 msg.chID = chninfo->channel_id;
860 msg.flag |= RPMSG_WRITE_INT; //| RPMSG_WRITE_IRQLOCK;
861 msg.buf = buf;
862 msg.len = len;
863
864 return zvnetWrite(&msg);
865}
866
867/*·µ»ØÖµ´óÓÚ0£¬±íʾ¶ÁȡͨµÀ³É¹¦£»Ð¡ÓÚµÈÓÚ0±íʾͨµÀÊý¾ÝΪ¿Õ»òʧ°Ü*/
868static int zvnet_channel_read(struct zvnet_channel *chninfo, void *buf, unsigned int len)
869{
870 T_RpMsg_Msg msg;
871 int ret = 0;
872
873 if(NULL == buf) {
874 return -EINVAL;
875 }
876
877 memset(&msg, 0, sizeof(msg));
878 msg.coreID = chninfo->core_id;
879 msg.chID = chninfo->channel_id;
880 msg.buf = buf;
881 msg.len = len;
882
883 ret = zvnetRead(&msg);
884 if (ret <= 0) {
885 zv_err("rpm read err=%d!",ret);
886 return ret;
887 }
888
889 return ret;
890}
891
892static int zvnet_channel_clear(struct zvnet_channel *chninfo)
893{
894 char *tbuf = NULL;
895 unsigned int tlen = chninfo->channel_size/2;
896 int ret = 0;
897
898 tbuf = (char *)kzalloc(tlen,GFP_ATOMIC);
899 if(IS_ERR(tbuf)) {
900 zv_err("kzalloc fail! %d byte.", tlen);
901 return -ENOMEM;
902 }
903 ret = zvnet_channel_read(chninfo, tbuf, tlen);
904 if(ret < 0) {
905 zv_err("zvnet_channel_read fail!");
906 ret = 0;
907 }
908 kfree(tbuf);
909 zv_err("Drop channel data. %d byte.",ret);
910
911 return ret;
912}
913
914static int zvnet_read_header(struct zvnet_channel *chninfo, struct zvp_header *phzvp)
915{
916 return zvnet_channel_read(chninfo, phzvp, sizeof(struct zvp_header));
917}
918
919static struct sk_buff *zvnet_read_skb(struct zvnet_channel *chninfo, unsigned int tlen, struct zvnet *dev)
920{
921 struct sk_buff *skb;
922
923 if(NULL == chninfo || 0 >= tlen || NULL == dev) {
924 return NULL;
925 }
926 skb = dev_alloc_skb(tlen);
927 if (unlikely(!skb)) {
928 zv_err("netdev_alloc_skb fail,len %d",tlen);
929 return NULL;
930 }
931 skb_put(skb,tlen);
932
933 if(zvnet_channel_read(chninfo, (void *)skb->data, tlen) != tlen) {
934 zv_err("zvnet_channel_read fail.\n");
935 kfree_skb(skb);
936 return NULL;
937 }
938
939 zv_info("%s dev receive packet %d byte.",dev->net->name, tlen);
940
941 skb->dev = dev->net;
942
943 return skb;
944}
945
946static struct sk_buff *zvnet_direct_read_skb(struct zvnet_channel *chninfo)
947{
948 struct sk_buff *skb;
949#if 0
l.yangafee7ee2024-10-10 15:01:10 +0800950/* Started by AICoder, pid:sd1cfsbc2eu87c41445f09652039f525fa147687 */
951int ret_len = 0;
952struct sk_buff *skb;
xf.li2f424182024-08-20 00:47:34 -0700953
l.yangafee7ee2024-10-10 15:01:10 +0800954ret_len = zvnet_channel_read(chninfo, NULL, 0); // »ñÈ¡ÐèÒª¶ÁÈ¡µÄÊý¾Ý³¤¶È
955if(ret_len <= 0) {
956 zv_err("zvnet_channel_read fail.\n");
957 return NULL;
958}
xf.li2f424182024-08-20 00:47:34 -0700959
l.yangafee7ee2024-10-10 15:01:10 +0800960skb = dev_alloc_skb(ret_len + ZVNET_SKB_PAD);
961if (unlikely(!skb)) {
962 zv_err("netdev_alloc_skb fail,len %d",ret_len);
963 return NULL;
964}
xf.li2f424182024-08-20 00:47:34 -0700965
l.yangafee7ee2024-10-10 15:01:10 +0800966ret_len = zvnet_channel_read(chninfo, skb->data, ret_len); // ¶ÁÈ¡Êý¾Ýµ½skb->data
967if(ret_len <= 0) {
968 kfree_skb(skb); // Èç¹û¶Áȡʧ°Ü£¬ÊÍ·ÅÒÑ·ÖÅäµÄskb
969 zv_err("zvnet_channel_read fail.\n");
970 return NULL;
971}
xf.li2f424182024-08-20 00:47:34 -0700972
l.yangafee7ee2024-10-10 15:01:10 +0800973skb_put(skb,ret_len);
974skb_reserve(skb, ZVNET_SKB_PAD);
975/* Ended by AICoder, pid:sd1cfsbc2eu87c41445f09652039f525fa147687 */
xf.li2f424182024-08-20 00:47:34 -0700976#else
977 struct T_zvnet_rpmsg buff = {0};
978 int ret_len = 0;
979 ret_len = zvnet_channel_read(chninfo, (void *)&buff, sizeof(struct T_zvnet_rpmsg));
980
981 if(ret_len <= 0) {
982 zv_err("rpm read err=%d", ret_len);
983 msleep(1000);
984 return NULL;
985 }
986 if(ret_len != sizeof(struct T_zvnet_rpmsg)) {
987 panic("err, ret:%d!!!!!!", ret_len);
988 }
989 skb = skb_build_apbuf(&buff);
990 if (unlikely(!skb)) {
991 zv_err("netdev_alloc_skb fail,len %d",ret_len);
992 return NULL;
993 }
994#endif
995 //skb->dev = dev->net;
996 return skb;
997}
998
999static int zvnet_receive_thread(void *argv)
1000{
1001 //struct zvnet_device *zvnetdev = (struct zvnet_device *)argv;
1002 //struct zvnet_channel *chninfo = NULL;
1003 struct zvnet *dev = NULL;
1004 int index,ret_len,i,num;
1005 unsigned long flags;
1006 struct sk_buff *skb = NULL;
1007 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
1008 .chID = 20,
1009 .flag = 0};
1010 struct T_zvnet_rpmsg buff[ZVNET_XMIT_BUFF_NUM];
1011 //struct zvp_header hzvp;
1012/*
1013 if(IS_ERR(zvnetdev)) {
1014 zv_err("The receive thread create fail!");
1015 return -EINVAL;
1016 }
1017 chninfo = &zvnetdev->chn_info;
1018 dev = zvnetdev->dev;
1019*/
1020 while(1) {
1021/*
1022 if(unlikely(!(zvnetdev->net->flags & IFF_UP))) {
1023 msleep(1000);
1024 continue;
1025 }
1026*/
1027 //memset(&hzvp, 0, sizeof(hzvp));
1028#ifdef USE_ZVNET_PACKET
1029 //ret_len = zvnet_channel_read(&g_zvnet_chn_info, (void *)buff, sizeof(struct T_zvnet_rpmsg)*ZVNET_XMIT_BUFF_NUM);
1030 msg.buf = (void *)(buff); // Êý¾Ý
1031 msg.len = sizeof(struct T_zvnet_rpmsg)*ZVNET_XMIT_BUFF_NUM;// ¶ÁÈ¡µÄ³¤¶È
1032 ret_len = zvnetRead(&msg); // ¶ÁÈ¡»·ÐζÓÁÐÖÐÒ»¸ö½Úµã£¬
1033 zv_info("zvnetRead ret=%d", ret_len);
1034 if(ret_len <= 0) {
1035 zv_err("rpm read err=%d", ret_len);
1036 msleep(1000);
1037 continue;
1038 }
1039 if((ret_len % sizeof(struct T_zvnet_rpmsg)) != 0) {
1040 panic("err, ret:%d!!!!!!", ret_len);
1041 }
1042 num = ret_len / sizeof(struct T_zvnet_rpmsg);
1043 for(i = 0; i < num; i++){
1044 skb = skb_build_apbuf(&buff[i]);
1045 if (unlikely(!skb)) {
1046 zv_err("skb_build_apbuf fail,len=%d i=%d",ret_len,i);
1047 continue;
1048 }
1049 if(unlikely(!(skb->dev->flags & IFF_UP))) {
1050 if(printk_ratelimit())
1051 zv_err("drop!!!%s is down.", skb->dev->name);
1052 dev_kfree_skb (skb);
1053 continue;
1054 }
1055 skb->protocol = eth_type_trans(skb, skb->dev);
1056 if (fast_from_driver && fast_from_driver(skb, skb->dev))
1057 {
1058 continue;
1059 }
1060 index = zvnet_get_index_by_netdev(skb->dev);
1061 if(index < 0)
1062 panic("");
1063 dev = zvnet_dev[index].dev;
1064 spin_lock_irqsave(&dev->rxq.lock, flags);
1065 __skb_queue_tail(&dev->rxq, skb);
1066 spin_unlock_irqrestore(&dev->rxq.lock, flags);
1067 tasklet_schedule(&dev->bh);
1068 }
1069#else
1070 if(0 != (skb = zvnet_direct_read_skb(&g_zvnet_chn_info))) {
1071 //skb_debug_test(skb);
1072 if(unlikely(!(skb->dev->flags & IFF_UP))) {
1073 zv_err("drop!!!%s is down.", skb->dev->name);
1074 dev_kfree_skb (skb);
1075 continue;
1076 }
1077 skb->protocol = eth_type_trans(skb, skb->dev);
1078#if 1
1079 if (fast_from_driver && fast_from_driver(skb, skb->dev))
1080 {
1081 continue;
1082 }
1083#endif
1084 index = zvnet_get_index_by_netdev(skb->dev);
1085 if(index < 0)
1086 panic("");
1087 dev = zvnet_dev[index].dev;
1088 spin_lock_irqsave(&dev->rxq.lock, flags);
1089 __skb_queue_tail(&dev->rxq, skb);
1090 spin_unlock_irqrestore(&dev->rxq.lock, flags);
1091 tasklet_schedule(&dev->bh);
1092 }
1093 else {
1094 zv_err("zvnet_read_header fail.");
1095 msleep(1000);
1096 }
1097#endif
1098 }
1099
1100 zv_err("The receive thread exit!");
1101 return 0;
1102}
1103
1104static int rpmsgCreateChannel_v2xnet (T_RpMsg_CoreID dstCoreID, T_RpMsg_ChID chID, unsigned int size)
1105{
1106 return zvnetCreateChannel (dstCoreID, chID, size);
1107}
1108
1109static int zvnet_createIcpChannel(T_RpMsg_CoreID core_id, T_RpMsg_ChID channel_id, unsigned int channel_size)
1110{
1111 int retval;
1112
1113 retval = rpmsgCreateChannel_v2xnet (core_id, channel_id, channel_size);
1114 if(retval != RPMSG_SUCCESS && retval != RPMSG_CHANNEL_ALREADY_EXIST)
1115 goto out;
1116
1117 return retval;
1118
1119out:
1120 zv_err("could not create channel.");
1121 return retval;
1122}
1123/*
1124static int zvnet_channel_create(struct zvnet_device *zvnetdev)
1125{
1126 struct task_struct *th = NULL;
1127 int retval = 0;
1128 struct zvnet_channel *chninfo = NULL;
1129
1130 if (IS_ERR(zvnetdev)) {
1131 return -EINVAL;
1132 }
1133 chninfo = &(zvnetdev->chn_info);
1134 retval = zvnet_createIcpChannel(chninfo->core_id, chninfo->channel_id, chninfo->channel_size);
1135 if(retval < 0) {
1136 zv_err("Create IcpChannel fail.");
1137 return retval;
1138 }
1139
1140 th = kthread_run(zvnet_receive_thread, (void *)zvnetdev, "zvnet-recv%d", chninfo->channel_id);
1141 if (IS_ERR(th)) {
1142 zv_err("Unable to start receive thread.");
1143 return PTR_ERR(th);
1144 }
1145 chninfo->rcv_thread = th;
1146
1147 return 0;
1148}
1149*/
1150static int zvnet_release_thread(void * nouse)
1151{
1152 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
1153 .chID = 29,
1154 .flag = 0};
1155 void *buff[ZVNET_FREE_BUFF_NUM];
1156 int i,num,retval;
1157 struct sk_buff *skb;
1158 struct sched_param param = { .sched_priority = 1 };
1159 param.sched_priority = 37;
1160 sched_setscheduler(current, SCHED_FIFO, &param);
1161
1162 while(1) {
1163 zv_info("g_zvnet_skb_queue.qlen=%d", g_zvnet_skb_queue.qlen);
1164#ifdef USE_ZVNET_PACKET
1165 msg.buf = (unsigned char *)(buff); // Êý¾Ý
1166 msg.len = 4*ZVNET_FREE_BUFF_NUM;// ¶ÁÈ¡µÄ³¤¶È
1167 retval = zvnetRead(&msg); // ¶ÁÈ¡»·ÐζÓÁÐÖÐÒ»¸ö½Úµã£¬
1168 zv_info("free read ret=%d", retval);
1169 if (retval <= 0) {
1170 zv_err("rpm read err=%d", retval);
1171 msleep(1000);
1172 continue;
1173 }
1174 if((retval%4) != 0) {
1175 panic("err, ret:%d!!!!!!", retval);
1176 }
1177 num = retval>>2;
1178 for(i = 0; i < num; i++){
1179 skb = (struct sk_buff *)buff[i];
1180 zv_info("free 0x%x", skb);
1181 if (skb == NULL || skb->next == NULL || skb->prev == NULL) {
1182 panic("rpm read=%d i=%d NULL", retval, i);
1183 continue;
1184 }
1185 skb_unlink(skb, &g_zvnet_skb_queue);
1186 if(skb->isToap != 1)
1187 panic("");
1188 skb->isToap = 0;
1189 kfree_skb(skb);
1190 }
1191#else
1192 void *buff;
1193 msg.coreID = CORE_PS0;
1194 msg.chID = 29;
1195 msg.buf = (unsigned char *)(&buff); // Êý¾Ý
1196 msg.len = 4;// ¶ÁÈ¡µÄ³¤¶È
1197 //msg.flag |= RPMSG_READ_POLL;
1198
1199 retval = zvnetRead(&msg); // ¶ÁÈ¡»·ÐζÓÁÐÖÐÒ»¸ö½Úµã£¬
1200 if (retval <= 0) {
1201 zv_err("no msg or threand exited");
1202 msleep(1000);
1203 continue;
1204 }
1205 if(retval != 4) {
1206 panic("err, ret:%d!!!!!!", retval);
1207 }
1208 zv_info("free 0x%x", buff);
1209 skb = (struct sk_buff *)buff;
1210 skb_unlink(skb, &g_zvnet_skb_queue);
1211 if(skb->isToap != 1)
1212 panic("");
1213 skb->isToap = 0;
1214 kfree_skb(skb);
1215#endif
1216 }
1217 zv_err("The realse thread exit!");
1218 return 0;
1219}
1220#ifdef USE_ZVNET_PACKET
1221static int zvnet_xmit_warp_thread(void * nouse)
1222{
1223 while(1) {
1224 down_timeout(&g_zvnet_xmit_sem, msecs_to_jiffies(g_wrap_timeout));
1225 zvnet_xmit_packet();
1226 }
1227 zv_err("The xmit warp thread exit!");
1228 return 0;
1229}
1230
1231static int zvnet_free_warp_thread(void * nouse)
1232{
1233 while(1) {
1234 down_timeout(&g_zvnet_free_sem, msecs_to_jiffies(g_wrap_timeout));
1235 write_free_apbuf_packet();
1236 }
1237 zv_err("The free warp thread exit!");
1238 return 0;
1239}
1240#endif
1241
1242static int zvnet_update_thread(void * nouse)
1243{
1244 T_RpMsg_Msg msg = { .coreID = CORE_PS0,
1245 .chID = 21,
1246 .flag = 0};
1247 int ret_len = 0;
1248 struct nf_conn *ct;
l.yangafee7ee2024-10-10 15:01:10 +08001249 fast_entry_t *entry;
1250 struct net_device *in;
1251 struct net_device *out;
xf.li2f424182024-08-20 00:47:34 -07001252
1253 while(1) {
1254 struct T_zvnet_rpmsg_ctstat buff = {0};
1255 msg.buf = (void *)(&buff); // Êý¾Ý
1256 msg.len = sizeof(struct T_zvnet_rpmsg_ctstat);// ¶ÁÈ¡µÄ³¤¶È
1257 ret_len = zvnetRead(&msg); // ¶ÁÈ¡»·ÐζÓÁÐÖÐÒ»¸ö½Úµã£¬
1258
1259 if(ret_len <= 0) {
1260 zv_err("rpm read err=%d", ret_len);
1261 msleep(1000);
1262 continue;
1263 }
1264 if(ret_len != sizeof(struct T_zvnet_rpmsg_ctstat)) {
1265 panic("err, ret:%d!!!!!!", ret_len);
1266 }
1267 ct = (struct nf_conn *)buff.cap_nfct;
xf.li2f424182024-08-20 00:47:34 -07001268 WARN_ON(atomic_read(&ct->ct_general.use) == 0);
1269 if(buff.flag){
l.yangafee7ee2024-10-10 15:01:10 +08001270 if(!(buff.pkt[0].pkt || buff.pkt[0].len || buff.pkt[1].pkt || buff.pkt[1].len))
1271 continue;
xf.li2f424182024-08-20 00:47:34 -07001272 BUG_ON(buff.in <= 0 || buff.out <= 0);
l.yangafee7ee2024-10-10 15:01:10 +08001273 in = zvnet_dev[buff.in-1].net;
1274 out = zvnet_dev[buff.out-1].net;
xf.li2f424182024-08-20 00:47:34 -07001275 if(buff.pkt[0].pkt && buff.pkt[0].len){
1276 zv_info("nf_update %x %s %s %d %d", buff.cap_nfct, ct->indev[0]->name, ct->outdev[0]->name, buff.in, buff.out);
l.yangafee7ee2024-10-10 15:01:10 +08001277 in->stats.rx_packets += buff.pkt[0].pkt;
1278 in->stats.rx_bytes += buff.pkt[0].len;
1279 out->stats.tx_packets += buff.pkt[0].pkt;
1280 out->stats.tx_bytes += buff.pkt[0].len;
xf.li2f424182024-08-20 00:47:34 -07001281 }
1282 if(buff.pkt[1].pkt && buff.pkt[1].len){
1283 zv_info("nf_update %x %s %s %d %d", buff.cap_nfct, ct->indev[1]->name, ct->outdev[1]->name, buff.out, buff.in);
l.yangafee7ee2024-10-10 15:01:10 +08001284 out->stats.rx_packets += buff.pkt[1].pkt;
1285 out->stats.rx_bytes += buff.pkt[1].len;
1286 in->stats.tx_packets += buff.pkt[1].pkt;
1287 in->stats.tx_bytes += buff.pkt[1].len;
xf.li2f424182024-08-20 00:47:34 -07001288 }
1289 spin_lock_bh(&fast_fw_spinlock);
l.yangafee7ee2024-10-10 15:01:10 +08001290 /*¸üÐÂÁ´½Ó³¬Ê±*/
1291 if (IPPROTO_TCP == nf_ct_protonum(ct))
1292 {
1293 ct->timeout = jiffies + tcp_timeouts[ct->proto.tcp.state];
1294 }else if (IPPROTO_UDP == nf_ct_protonum(ct)){
1295 /*udp*/
1296 if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)){
1297 ct->timeout = jiffies + fast_udp_timeout_stream;
1298 }else{
1299 ct->timeout = jiffies + fast_udp_timeout;
1300 }
1301 }
1302 entry = (fast_entry_t *)ct->fast_entry;
1303 if(entry){
1304 WARN_ON(entry->ct != ct);
1305 mod_timer(&entry->timeout, ct->timeout);
1306 }
xf.li2f424182024-08-20 00:47:34 -07001307 ct->packet_info[IP_CT_DIR_ORIGINAL].bytes += buff.pkt[IP_CT_DIR_ORIGINAL].len;
1308 ct->packet_info[IP_CT_DIR_ORIGINAL].packets += buff.pkt[IP_CT_DIR_ORIGINAL].pkt;
1309 ct->packet_info[IP_CT_DIR_REPLY].bytes += buff.pkt[IP_CT_DIR_REPLY].len;
1310 ct->packet_info[IP_CT_DIR_REPLY].packets += buff.pkt[IP_CT_DIR_REPLY].pkt;
l.yangafee7ee2024-10-10 15:01:10 +08001311 if(ct->indev[0] && is_vlan_dev(ct->indev[0])){
1312 struct net_device *tmp = vlan_dev_real_dev(ct->indev[0]);
1313 struct vlan_pcpu_stats *stats = this_cpu_ptr(vlan_dev_priv(ct->indev[0])->vlan_pcpu_stats);
1314
1315 if(tmp == in){
1316/* Started by AICoder, pid:tbef0151bf4135d1479d0a5d108c870bc756e858 */
1317u64_stats_update_begin(&stats->syncp);
1318stats->rx_packets += buff.pkt[0].pkt;
1319stats->rx_bytes += buff.pkt[0].len;
1320stats->tx_packets += buff.pkt[1].pkt;
1321stats->tx_bytes += buff.pkt[1].len;
1322u64_stats_update_end(&stats->syncp);
1323/* Ended by AICoder, pid:tbef0151bf4135d1479d0a5d108c870bc756e858 */
1324 }else if(tmp == out){
1325/* Started by AICoder, pid:y34f7id6bcs049f144f10bb8a05c9703b196635b */
1326u64_stats_update_begin(&stats->syncp);
1327stats->tx_packets += buff.pkt[0].pkt;
1328stats->tx_bytes += buff.pkt[0].len;
1329stats->rx_packets += buff.pkt[1].pkt;
1330stats->rx_bytes += buff.pkt[1].len;
1331u64_stats_update_end(&stats->syncp);
1332/* Ended by AICoder, pid:y34f7id6bcs049f144f10bb8a05c9703b196635b */
1333 }else
1334 zv_err("nf_update0 %s->%s!=%s-%s", in->name, out->name, tmp->name, ct->indev[0]->name);
1335 }
1336 if(ct->indev[1] && is_vlan_dev(ct->indev[1])){
1337 struct net_device *tmp = vlan_dev_real_dev(ct->indev[1]);
1338 struct vlan_pcpu_stats *stats = this_cpu_ptr(vlan_dev_priv(ct->indev[1])->vlan_pcpu_stats);
1339
1340 if(tmp == in){
1341/* Started by AICoder, pid:8bef0t51bfu135d1479d0a5d108c870bc756e858 */
1342u64_stats_update_begin(&stats->syncp);
1343stats->rx_packets += buff.pkt[0].pkt;
1344stats->rx_bytes += buff.pkt[0].len;
1345stats->tx_packets += buff.pkt[1].pkt;
1346stats->tx_bytes += buff.pkt[1].len;
1347u64_stats_update_end(&stats->syncp);
1348/* Ended by AICoder, pid:8bef0t51bfu135d1479d0a5d108c870bc756e858 */
1349 }else if(tmp == out){
1350/* Started by AICoder, pid:934f7zd6bcl049f144f10bb8a05c9703b196635b */
1351u64_stats_update_begin(&stats->syncp);
1352stats->tx_packets += buff.pkt[0].pkt;
1353stats->tx_bytes += buff.pkt[0].len;
1354stats->rx_packets += buff.pkt[1].pkt;
1355stats->rx_bytes += buff.pkt[1].len;
1356u64_stats_update_end(&stats->syncp);
1357/* Ended by AICoder, pid:934f7zd6bcl049f144f10bb8a05c9703b196635b */
1358 }else
1359 zv_err("nf_update1 %s->%s!=%s-%s", in->name, out->name, tmp->name, ct->indev[1]->name);
1360 }
xf.li2f424182024-08-20 00:47:34 -07001361 spin_unlock_bh(&fast_fw_spinlock);
1362 zv_info("nf_update %x %d %d %d %d", buff.cap_nfct, buff.pkt[0].pkt, buff.pkt[0].len, buff.pkt[1].pkt, buff.pkt[1].len);
1363 }else{
1364 zv_info("nf_put %x", buff.cap_nfct);
l.yangafee7ee2024-10-10 15:01:10 +08001365 WRITE_ONCE(ct->timeout, nfct_time_stamp);
xf.li2f424182024-08-20 00:47:34 -07001366 nf_conntrack_put(buff.cap_nfct);
1367 }
1368 }
1369 zv_err("The update thread exit!");
1370 return 0;
1371}
1372
1373/*******************************************************************************
1374 * Global function implementations *
1375 ******************************************************************************/
1376static int __init zvnet_init(void)
1377{
1378 int i;
1379 int err = -ENOMEM;
1380 struct zvnet *dev = NULL;
1381 struct net_device *net = NULL;
1382 struct zvnet_device *zvnetdev = NULL;
1383
xf.li6b423c72025-03-14 00:07:42 -07001384 atomic_set(&g_zvnet_pm_flag, 0);
xf.li2f424182024-08-20 00:47:34 -07001385#ifdef USE_ZVNET_PACKET
1386 skb_queue_head_init(&g_zvnet_skb_xmit_queue);
1387 spin_lock_init(&g_zvnet_free_lock);
1388 sema_init(&g_zvnet_free_sem, 0);
1389 sema_init(&g_zvnet_xmit_sem, 0);
1390#endif
1391 skb_queue_head_init(&g_zvnet_skb_queue);
1392 g_zvnet_chn_info.core_id = CORE_PS0;
1393 g_zvnet_chn_info.channel_id = ICP_CHN_ZVNET1;
1394 g_zvnet_chn_info.channel_size = ICP_CHANNEL_SIZE;
1395 for (i = 0; i < DDR_ZVNET_DEV_MAX; i++) {
1396 zvnetdev = &zvnet_dev[i];
1397 memset(zvnetdev, 0, sizeof(struct zvnet_device));
1398 net = alloc_etherdev(sizeof(struct zvnet));
1399 if (!net) {
1400 zv_err("could not allocate device.\n");
1401 return err;
1402 }
1403
l.yangafee7ee2024-10-10 15:01:10 +08001404 //net->needed_headroom += ZVNET_SKB_PAD;//NET_SKB_PAD;
xf.li2f424182024-08-20 00:47:34 -07001405 sprintf(net->name, "%s%d", ZVNET_IFNAME_PREFIX, i);
1406 dev = v2xnet_dev_init(net, zvnetdev);
1407 v2xnet_init_netdev(net);
l.yangafee7ee2024-10-10 15:01:10 +08001408 if(0 == i || i > 8){
xf.li2f424182024-08-20 00:47:34 -07001409 net->flags = (net->flags & (~IFF_NOARP));
1410 }
1411 err = register_netdev(net);
1412 if (err) {
1413 zv_err("register_netdev error:%d :%d\n",err,i);
1414 return err;
1415 }
1416 zvnetdev->dev = dev;
1417 zvnetdev->net = net;
1418/*
1419 zvnetdev->chn_info.core_id = CAP_ID;
1420 zvnetdev->chn_info.channel_id = ICP_CHN_ZVNET1 + i;//zvnet_collect[i];
1421 zvnetdev->chn_info.channel_size = ICP_CHANNEL_SIZE;
1422 err = zvnet_channel_create(zvnetdev);
1423 if(0 != err) {
1424 zv_err("zvnet_channel_create error:%d :%d\n",err,i);
1425 goto out_unregister_netdev;
1426 }
1427*/
1428 }
1429 {
1430 struct task_struct *th = NULL;
1431 int retval = 0;
1432 retval = zvnet_createIcpChannel(CORE_PS0, 21, 64);
1433 if(retval < 0) {
1434 zv_err("Create IcpChannel channel_21 fail.");
1435 return retval;
1436 }
1437
1438 th = kthread_run(zvnet_update_thread, 0, "zvnet-update%d", 21);
1439 if (IS_ERR(th)) {
1440 zv_err("Unable to start update thread.");
1441 return PTR_ERR(th);
1442 }
1443 retval = zvnet_createIcpChannel(CORE_PS0, 20, ICP_CHANNEL_SIZE);
1444 if(retval < 0) {
1445 zv_err("Create IcpChannel channel_20 fail.");
1446 return retval;
1447 }
1448
1449 th = kthread_run(zvnet_receive_thread, 0, "zvnet-recv%d", 20);
1450 if (IS_ERR(th)) {
1451 zv_err("Unable to start receive thread.");
1452 return PTR_ERR(th);
1453 }
1454 g_zvnet_chn_info.rcv_thread = th;
1455
1456 retval = zvnet_createIcpChannel(CORE_PS0, 29, ICP_CHANNEL_SIZE);
1457 if(retval < 0) {
1458 zv_err("Create IcpChannel channel_29 fail.");
1459 return retval;
1460 }
1461
1462 th = kthread_run(zvnet_release_thread, 0, "zvnet-free%d", 29);
1463 if (IS_ERR(th)) {
1464 zv_err("Unable to start release thread.");
1465 return PTR_ERR(th);
1466 }
1467#ifdef USE_ZVNET_PACKET
1468 th = kthread_run(zvnet_xmit_warp_thread, 0, "zvnet-xmit-wrap");
1469 if (IS_ERR(th)) {
1470 zv_err("Unable to start xmit_warp thread.");
1471 return PTR_ERR(th);
1472 }
1473
1474 th = kthread_run(zvnet_free_warp_thread, 0, "zvnet-free-wrap");
1475 if (IS_ERR(th)) {
1476 zv_err("Unable to start free_warp thread.");
1477 return PTR_ERR(th);
1478 }
1479#endif
1480 vir_addr_ap = ioremap_cache(DDR_BASE_ADDR_AP, DDR_BASE_LEN_AP);
1481 zv_warn("vir_addr_ap vir=0x%x phy=0x%x len=0x%x", vir_addr_ap, DDR_BASE_ADDR_AP, DDR_BASE_LEN_AP);
1482 if(vir_addr_ap == NULL)
1483 {
1484 zv_err("AP mmap failed.\n");
1485 return -1;
1486 }
1487
1488 }
1489 zv_dbg("success.\n");
1490 return 0;
1491
1492}
1493
1494static void __exit zvnet_exit(void)
1495{
1496 int i;
1497 struct net_device *net;
1498
1499 for (i = 0; i < DDR_ZVNET_DEV_MAX; i++) {
1500 net = zvnet_dev[i].net;
1501 unregister_netdev(net);
1502 free_netdev(net);
1503 zvnet_dev[i].net = NULL;
1504 }
1505 zv_warn("success.\n");
1506}
1507
1508late_initcall(zvnet_init);
1509module_exit(zvnet_exit);
1510
1511MODULE_AUTHOR("ZXIC");
1512MODULE_DESCRIPTION("ZXIC CAP LAN NET DEVICE");
1513MODULE_LICENSE("GPL");
1514