I have to write a Ethernet PHY loopback test in Linux. I've never done it before and had no clue where to start. After some researching, I came up with the plan for the test, and I wonder if anyone can help me to verify it and give me some pointers to my questions.

This is a external loopback setup. It connects the TX+/- to RX+/- pins for each internal PHY's on the Ethernet switch chip.

So the loopback high level packet path is as follow:

CPU->PHY0(chip)->Ethernet switch(chip)'s internal PHY->PHY0(chip)->CPU

For this test, I plan to

1. configure the internal PHYs on the Ethernet switch with specific MAC addresses,
2. pack the Ethernet test packet using CPU's MAC address as the source address and the
MAC address configured in step 1. as the destination address,
3. send and wait to receive the test packet,
4. compare sent and received data pattern.

Any suggestions? Also, I wonder if there is something available in Linux that I can use to generate the packets for step 2, and send the packets for step 3. Can pktgen be used?

Thank you very much.