|
| 1 | +// Copyright 2019 Intel Corporation. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "github.com/intel-go/nff-go/flow" |
| 11 | + "github.com/intel-go/nff-go/packet" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +// You need to enable memory jumbo for maxPacketSegment more than 2014. |
| 16 | +// Explanation: internal packet buffers are 2048 bytes, first packet will have |
| 17 | +// ethernet and ipv4 headers = 14 + 2 = 34 bytes. |
| 18 | +// Buffer will overflows if maxPacketSegment > 2048 - 34 = 2014 bytes |
| 19 | + |
| 20 | +// Note: usage of 1484 < maxPacketSegment < 2014 without memory jumbo can work |
| 21 | +// on some NICs, but is treated as underfined behaviour |
| 22 | +// Explanation: in this situation packet will be fit in internal buffers |
| 23 | +// however packet will be more than 1518 bytes - Ethernet max non-jumbo frame |
| 24 | +const maxPacketSegment = 1484 |
| 25 | + |
| 26 | +// You need to enale chained jumbo for requiredPacketLength > maxPacketSegment |
| 27 | +// Explanation: if required length is more than segment length packet will consist of |
| 28 | +// several chained segments. You should enable chained jumbo to correctly handle them |
| 29 | + |
| 30 | +// Note: usage of maxPacketSegment > 2014 and also requiredPacketLength > maxPacketSegment |
| 31 | +// is forbidden. If you decicde to chain packets you shouldn't chain non-standart packets |
| 32 | +// Explanation: in this situation you will need to enable both |
| 33 | +// memory and chained jumbo and this is forbidden |
| 34 | +const requiredPacketLength = 7000 |
| 35 | + |
| 36 | +func main() { |
| 37 | + chained := flag.Bool("chained", false, "enable chained jumbo") |
| 38 | + memory := flag.Bool("memory", false, "enalbe memory jumbo") |
| 39 | + flag.Parse() |
| 40 | + |
| 41 | + // Initialize NFF-GO library at 10 available cores |
| 42 | + config := flow.Config{ |
| 43 | + ChainedJumbo: *chained, |
| 44 | + MemoryJumbo: *memory, |
| 45 | + } |
| 46 | + |
| 47 | + flow.CheckFatal(flow.SystemInit(&config)) |
| 48 | + |
| 49 | + generated := flow.SetGenerator(generate, nil) |
| 50 | + flow.SetHandler(generated, dump, nil) |
| 51 | + flow.SetSender(generated, 0) |
| 52 | + |
| 53 | + received, _ := flow.SetReceiver(0) |
| 54 | + flow.SetHandler(received, dump, nil) |
| 55 | + flow.SetStopper(received) |
| 56 | + flow.SystemStart() |
| 57 | +} |
| 58 | + |
| 59 | +func generate(pkt *packet.Packet, context flow.UserContext) { |
| 60 | + remain := requiredPacketLength |
| 61 | + c := 0 |
| 62 | + diff, remain := m(maxPacketSegment, remain) |
| 63 | + if packet.InitEmptyIPv4Packet(pkt, diff) == false { |
| 64 | + fmt.Printf("maxPacketSegment %d is more than 2014 bytes without enabling memory jumbo\n", maxPacketSegment) |
| 65 | + time.Sleep(5 * time.Second) |
| 66 | + return |
| 67 | + } |
| 68 | + pkt.Ether.DAddr = [6]uint8{0x00, 0x11, 0x22, 0x33, 0x44, 0x55} |
| 69 | + for i := 0; i < maxPacketSegment; i++ { |
| 70 | + (*(*[maxPacketSegment]byte)(pkt.Data))[i] = byte(c % 10) |
| 71 | + c++ |
| 72 | + } |
| 73 | + for remain != 0 { |
| 74 | + diff, remain = m(maxPacketSegment, remain) |
| 75 | + pkt = packet.InitNextPacket(diff, pkt) |
| 76 | + for i := 0; i < maxPacketSegment; i++ { |
| 77 | + (*(*[maxPacketSegment]byte)(pkt.Data))[i] = byte(c % 10) |
| 78 | + c++ |
| 79 | + } |
| 80 | + } |
| 81 | + time.Sleep(1 * time.Second) |
| 82 | +} |
| 83 | + |
| 84 | +func m(max int, remain int) (uint, int) { |
| 85 | + if remain > max { |
| 86 | + return uint(max), remain - max |
| 87 | + } else { |
| 88 | + return uint(remain), 0 |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func dump(currentPacket *packet.Packet, context flow.UserContext) { |
| 93 | + fmt.Println("NEW PACKET") |
| 94 | + fmt.Printf("BLOCK\n%x\nEND_BLOCK\n", currentPacket.GetRawPacketBytes()) |
| 95 | + for currentPacket.Next != nil { |
| 96 | + currentPacket = currentPacket.Next |
| 97 | + fmt.Printf("BLOCK\n%x\nEND_BLOCK\n", currentPacket.GetRawPacketBytes()) |
| 98 | + } |
| 99 | + fmt.Println("END PACKET") |
| 100 | +} |
0 commit comments