Skip to contentSkip to conversation

net/pcap: Generate unique MAC addresses for interfaces

PATCH Applied Cian Ferriter 2 participants 3 messages Sep 10, 2018 – Sep 21, 2018 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net/pcap: Generate unique MAC addresses for interfaces

The MAC addresses are generated in a similar manner as in the TAP PMD, where the address is based on the number of PCAP ports created.

This is useful for the purposes of debugging DPDK applications using PCAP devices instead of real devices where multiple devices should still have unique MAC addresses. This method was chosen over randomly assigning MAC addresses to make the creation of pcaps, specifically matching the destination ethernet address field to an interface, easier.

---
v1->v2:
* Add a "struct ether_addr" to "pmd_internals" and set MAC address in "pmd_init_internals()"
* Set the fixed part of the MAC address more explicitly.
* Add the static "iface_idx" globally.

 drivers/net/pcap/rte_eth_pcap.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/net/pcap/rte_eth_pcap.c +11 -5
index e8810a1..ec19d37 100644
@@ -39,6 +39,7 @@ static unsigned char tx_pcap_data[RTE_ETH_PCAP_SNAPLEN];
 static struct timeval start_time;
 static uint64_t start_cycles;
 static uint64_t hz;
+static uint8_t iface_idx;
 
 struct queue_stat {
 	volatile unsigned long pkts;
@@ -66,6 +67,7 @@ struct pcap_tx_queue {
 struct pmd_internals {
 	struct pcap_rx_queue rx_queue[RTE_PMD_PCAP_MAX_QUEUES];
 	struct pcap_tx_queue tx_queue[RTE_PMD_PCAP_MAX_QUEUES];
+	struct ether_addr eth_addr;
 	int if_index;
 	int single_iface;
 };
@@ -90,10 +92,6 @@ static const char *valid_arguments[] = {
 	NULL
 };
 
-static struct ether_addr eth_addr = {
-	.addr_bytes = { 0, 0, 0, 0x1, 0x2, 0x3 }
-};
-
 static struct rte_eth_link pmd_link = {
 		.link_speed = ETH_SPEED_NUM_10G,
 		.link_duplex = ETH_LINK_FULL_DUPLEX,
@@ -889,11 +887,19 @@ pmd_init_internals(struct rte_vdev_device *vdev,
 	 * - and point eth_dev structure to new eth_dev_data structure
 	 */
 	*internals = (*eth_dev)->data->dev_private;
+	/*
+	 * Interface MAC = 02:70:63:61:70:<iface_idx>
+	 * derived from: 'locally administered':'p':'c':'a':'p':'iface_idx'
+	 * where the middle 4 characters are converted to hex.
+	 */
+	(*internals)->eth_addr = (struct ether_addr) {
+		.addr_bytes = { 0x02, 0x70, 0x63, 0x61, 0x70, iface_idx++ }
+	};
 	data = (*eth_dev)->data;
 	data->nb_rx_queues = (uint16_t)nb_rx_queues;
 	data->nb_tx_queues = (uint16_t)nb_tx_queues;
 	data->dev_link = pmd_link;
-	data->mac_addrs = &eth_addr;
+	data->mac_addrs = &(*internals)->eth_addr;
 
 	/*
 	 * NOTE: we'll replace the data element, of originally allocated
Signature · 1 line
2.7.4
Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Cian Ferriter

The MAC addresses are generated in a similar manner as in the TAP PMD, where the address is based on the number of PCAP ports created.

This is useful for the purposes of debugging DPDK applications using PCAP devices instead of real devices where multiple devices should still have unique MAC addresses. This method was chosen over randomly assigning MAC addresses to make the creation of pcaps, specifically matching the destination ethernet address field to an interface, easier.

Applied
#2 lore raw
Quoted 10 lines · Ferruh Yigit
Quoted 9 lines · Cian Ferriter

The MAC addresses are generated in a similar manner as in the TAP PMD, where the address is based on the number of PCAP ports created.

This is useful for the purposes of debugging DPDK applications using PCAP devices instead of real devices where multiple devices should still have unique MAC addresses. This method was chosen over randomly assigning MAC addresses to make the creation of pcaps, specifically matching the destination ethernet address field to an interface, easier.

Quoted 3 lines
Quoted 1 line

Applied to dpdk-next-net/master, thanks.