Example #1
0
func run(ctx *cli.Context) {
	events, err := bpf.NewPerCpuEvents(&config)
	if err != nil {
		panic(err)
	}

	for {
		todo, err := events.Poll(-1)
		if err != nil {
			panic(err)
		}
		if todo > 0 {
			events.ReadAll(receiveEvent, lostEvent)
		}
	}

	//if err := events.CloseAll(); err != nil {
	//	panic(err)
	//}

}
Example #2
0
func run(ctx *cli.Context) {
	events, err := bpf.NewPerCpuEvents(&config)
	if err != nil {
		panic(err)
	}

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)
	go func() {
		for range signalChan {
			fmt.Printf("\nReceived an interrupt, stopping monitor...\n\n")

			lost, unknown := events.Stats()
			if lost != 0 || unknown != 0 {
				log.Warningf("%d events lost, %d unknonwn notifications", lost, unknown)
			}

			if err := events.CloseAll(); err != nil {
				panic(err)
			}

			os.Exit(0)
		}
	}()

	for {
		todo, err := events.Poll(5000)
		if err != nil && err != syscall.EINTR {
			panic(err)
		}
		if todo > 0 {
			if err := events.ReadAll(receiveEvent, lostEvent); err != nil {
				log.Warningf("Error received while reading from perf buffer: %s", err)
			}
		}
	}

}
Example #3
0
func (d *Daemon) EnableLearningTraffic() {
	startChan := make(chan bool, 1)
	stopChan1 := make(chan bool, 1)
	eventStopped := make(chan bool, 1)

	go func() {
		for {
			select {
			case lEP := <-d.endpointsLearningRegister:
				log.Debugf("Registering endpoint %+v", lEP)
				d.endpointsLearningMU.Lock()
				if lEP.Learn {
					if len(d.endpointsLearning) == 0 {
						startChan <- true
					}
					d.endpointsLearning[lEP.EndpointID] = lEP
				} else {
					delete(d.endpointsLearning, lEP.EndpointID)
					if len(d.endpointsLearning) == 0 {
						stopChan1 <- true
					}
				}
				d.endpointsLearningMU.Unlock()
			}
		}
	}()

	go func() {
		var events *bpf.PerCpuEvents
		var err error
		for {
			select {
			case <-startChan:
				log.Info("Starting monitoring traffic")
				events, err = bpf.NewPerCpuEvents(bpf.DefaultPerfEventConfig())
				if err != nil {
					log.Errorf("Error while starting monitor")
				}
				go func() {
					for {
						todo, err := events.Poll(5000)
						if err != nil {
							select {
							case <-eventStopped:
								log.Info("Monitor successfully stopped")
								return
							case <-time.Tick(time.Millisecond * 10):
								log.Error(err)
								return
							}
						}
						if todo > 0 {
							if err := events.ReadAll(d.receiveEvent, d.lostEvent); err != nil {
								log.Warningf("Error received while reading from perf buffer: %s", err)
							}
						}
					}
				}()
			case <-stopChan1:
				log.Info("All endpoints stopped learning traffic, stopping monitor...")
				events.CloseAll()
				eventStopped <- true
			}
		}
	}()

}