linux内核中struct net_device的dev_addr和perm_addr有什么区别

时间:2015-05-20 02:09:45

标签: linux-device-driver ethernet

我正在阅读Linux(版本3.10)以太网驱动程序的源代码。内核网络中有一个重要的结构 net_device

在“ struct net_device ”中,有两个变量命名 dev_addr perm_addr ,表示NIC的mac地址。但是,某些供应商驱动程序同时设置 dev_addr perm_addr ,而某些设置 dev_addr

虽然在头文件中,但它被注释为:

package javaapplication1.pkg295;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new TestPane());

                DefaultListModel model = new DefaultListModel();
                model.addElement("Apple");
                model.addElement("Banana");
                model.addElement("Pear");
                model.addElement("Peach");
                model.addElement("Grap");
                JList list = new JList(model);
                list.setCellRenderer(new TransparentListCellRenderer());
                list.setOpaque(false);
                JScrollPane sp = new JScrollPane(list);
                sp.setOpaque(false);
                sp.getViewport().setOpaque(false);

                frame.add(sp);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TransparentListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            setForeground(Color.WHITE);
            setOpaque(isSelected);
            return this;
        }

    }

    public class TestPane extends JPanel {

        private BufferedImage bgImg;

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                bgImg = ImageIO.read(new File("C:\\Users\\shane\\Dropbox\\MegaTokyo\\Aqua\\200x200\\aria_manga_wallpaper-5541.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return bgImg == null ? new Dimension(200, 200) : new Dimension(bgImg.getWidth(), bgImg.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bgImg != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - bgImg.getWidth()) / 2;
                int y = (getHeight() - bgImg.getHeight()) / 2;
                g2d.drawImage(bgImg, x, y, this);
                g2d.dispose();
            }
        }

    }

}

我仍然无法区分它们。

感谢skrrgwasme,我也在内核文件net / core / dev.c中找到了reigster_netdev()函数:

unsigned char       perm_addr[MAX_ADDR_LEN]; /* permanent hw address */
/* Interface address info used in eth_type_trans() */
unsigned char       *dev_addr;  /* hw address, (before bcast
                       because most packets are
                       unicast) */

所以这个变量在NIC驱动程序中很少使用。

1 个答案:

答案 0 :(得分:1)

如果设备分配了永久MAC地址,则会填充perm_addr字段。这通常来自物理设备上的某些EEPROM或其他只读存储器。

dev_addr字段是当前分配的MAC地址,可以通过软件进行更改。