Tiny210 driver virtual network card driver

Publisher:闪耀的星空Latest update time:2018-10-12 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

virt_net.c driver:

// Reference drivers\net\cs89x0.c

 

#include "linux/module.h"

#include "linux/errno.h"

#include "linux/netdevice.h"

#include "linux/etherdevice.h"

#include "linux/kernel.h"

#include "linux/types.h"

#include "linux/fcntl.h"

#include "linux/interrupt.h"

#include "linux/ioport.h"

#include "linux/in.h"

#include "linux/skbuff.h"

#include "linux/slab.h"

#include "linux/spinlock.h"

#include "linux/string.h"

#include "linux/init.h"

#include "linux/bitops.h"

#include "linux/delay.h"

#include "linux/ip.h"

#include "asm/system.h"

#include "asm/io.h"

#include "asm/irq.h"

static struct net_device *vnet_dev;

static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)

{

    // Refer to LDD3 

    unsigned char *type;

    struct iphdr *ih;

    __be32 *saddr, *daddr, tmp;

    unsigned char    tmp_dev_addr[ETH_ALEN];

    struct ethhdr *ethhdr;

    

    struct sk_buff *rx_skb;

        

    // Read/save data from hardware

    // Swap the mac addresses of "source/destination" 

    ethhdr = (struct ethhdr *)skb->data;

    memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);

    memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);

    memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN);

    // Swap the "source/destination" IP addresses     

    ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));

    saddr = &ih->saddr;

    daddr = &ih->daddr;

    tmp = *saddr;

    *saddr = *daddr;

    *daddr = tmp;

    

    //((u8 *)saddr)[2] ^= 1; // change the third octet (class C) 

    //((u8 *)daddr)[2] ^= 1;

    type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);

    //printk("tx package type = x\n", *type);

    // Modify the type, originally 0x8 means ping

    *type = 0; // 0 means reply 

    

    ih->check = 0;           // and rebuild the checksum (ip needs it) 

    ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);

    

    //Construct a sk_buff

    rx_skb = dev_alloc_skb(skb->len + 2);

    skb_reserve(rx_skb, 2); // align IP on 16B boundary     

    memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);

    // Write metadata, and then pass to the receive level 

    rx_skb->dev = dev;

    rx_skb->protocol = eth_type_trans(rx_skb, dev);

    rx_skb->ip_summed = CHECKSUM_UNNECESSARY; // don't check it 

    dev->stats.rx_packets++;

    dev->stats.rx_bytes += skb->len;

    // Submit sk_buff

    netif_rx(rx_skb);

}

static int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)

{

    static int cnt = 0;

    printk("virt_net_send_packet cnt = %d\n", ++cnt);

    // For the real network card, send the data in skb through the network card 

    netif_stop_queue(dev); // Stop the queue of the network card 

    // ...... // Write skb data to the network card 

    //Construct a fake sk_buff and report it 

    emulator_rx_packet(skb, dev);

    dev_kfree_skb (skb); // release skb 

    netif_wake_queue(dev); //After all data is sent out, wake up the queue of the network card 

    // Update statistics 

    dev->stats.tx_packets++;

    dev->stats.tx_bytes += skb->len;

    

    return 0;

}

static const struct net_device_ops virt_netdev_ops = {

    .ndo_start_xmit = virt_net_send_packet,

};

static int virt_net_init(void)

{

    // 1. Allocate a net_device structure 

    vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);  // alloc_etherdev 

    // 2. Setup 

    vnet_dev->netdev_ops    = &virt_netdev_ops;

    // Set the MAC address 

    vnet_dev->dev_addr[0] = 0x08;

    vnet_dev->dev_addr[1] = 0x89;

    vnet_dev->dev_addr[2] = 0x89;

    vnet_dev->dev_addr[3] = 0x89;

    vnet_dev->dev_addr[4] = 0x89;

    vnet_dev->dev_addr[5] = 0x11;

    // Set the following two items to ping successfully 

    vnet_dev->flags           |= IFF_NOARP;

    vnet_dev->features        |= NETIF_F_NO_CSUM;    

    // 3. Registration 

    //register_netdevice(vnet_dev);

    register_netdev(vnet_dev);

    

    return 0;

}

static void virt_net_exit(void)

{

    unregister_netdev(vnet_dev);

    free_netdev(vnet_dev);

}

module_init(virt_net_init);

module_exit(virt_net_exit);

MODULE_AUTHOR("thisway.diy@163.com,17653039@qq.com");

MODULE_LICENSE("GPL");


Reference address:Tiny210 driver virtual network card driver

Previous article:Tiny210 IIC driver at24cxx access
Next article:USB device driver for Tiny210

Recommended ReadingLatest update time:2024-11-16 12:25

Using C8051F to drive PCF8563 clock chip
System functions Use C8051F to drive PCF8563 clock chip to display the clock. hardware design LCD12864 LCD circuit schematic diagram Using C8051F to drive PCF8563 clock chip circuit schematic diagram software design The following part is copied from TXT and copied to the web page. There are many spaces missi
[Microcontroller]
Using C8051F to drive PCF8563 clock chip
How to Reduce Output Ripple in LED Driver Design
  Usually, aluminum electrolytic capacitors are used for output capacitors to achieve large capacity. However, electrolytic capacitors are not very effective in suppressing high-frequency noise, and their ESR is relatively large, so a ceramic capacitor is connected in parallel next to it to make up for the shortcoming
[Power Management]
PCF8563 clock chip driver (STM8S/STM8L)
Simulate I2C to read the date and time in the clock chip PCF8563  #define SCLK_Clr() GPIO_ResetBits(GPIOD, GPIO_PIN_1) // Clock signal of SDA IIC interface #define SCLK_Set() GPIO_SetBits(GPIOD, GPIO_PIN_1) #define SDIN_Clr() GPIO_ResetBits(GPIOD, GPIO_PIN_2) //SCL IIC interface data signal #define SDIN_Set() GPIO_S
[Microcontroller]
Through complex combinations: M1 Mac mini can drive up to six displays
When Apple launched the new Mac mini with M1, it quietly changed the video output limit so that the Mac mini could drive two displays. But soon after the announcement, a YouTube video showed that it was possible to go far beyond Apple's limits. A video posted by netizen Ruslan Tulupov on Sunday shows that his M1 Mac
[Mobile phone portable]
Simulate 8(COM)*8(SEG) LCD driver on EM78Pxx
; /* = ... ; | ; | ;I. LCD scanning principle: | ;Forward scanning: COM on=V CC ,COM off=1/2VCC, corresponding to SEG=1, it outputs VSS, and 0, it outputs VCC. | ;Reverse scanning: COM on=VSS,COM off=1/2VCC, corresponding to SEG=1, it outputs VCC, and 0, it outputs VSS. | ; | ;II. Circuit connection: | ; All COM port
[Microcontroller]
STM32 peripheral driver---SysTick precise delay
Microcontroller: STM32F103  Library functions: V3.5  Description: Use the SysTick in the system kernel file core_cm3.c to achieve 1ms, 1us accurate delay, clock 72MHz, involving the registration of the interrupt service SysTick_Handler. delay.h #ifndef     __DELAY_H__ #define     __DELAY_H__ #include "stm32f10
[Microcontroller]
Texas Instruments (TI) introduces high-power multi-topology driver
    Texas Instruments (TI) announced a high-power multi-topology DC/DC LED driver with adjustable switching frequency and current sensing threshold, high design flexibility and low electromagnetic interference (EMI), suitable for automotive headlights, fog lights and general lighting applications. The TPS92690 is an N
[Power Management]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号