Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package vpagent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "io" |
| 21 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 22 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 23 | |
| 24 | "github.com/golang/protobuf/ptypes/empty" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | "google.golang.org/grpc" |
| 26 | ) |
| 27 | |
| 28 | func (vpa *VPAgent) receivePacketsIn(ctx context.Context) { |
| 29 | logger.Debug(ctx, "receive-packets-in-started") |
| 30 | // If we exit, assume disconnected |
| 31 | defer func() { |
| 32 | vpa.events <- vpaEventVolthaDisconnected |
| 33 | logger.Debug(ctx, "receive-packets-in-finished") |
| 34 | }() |
| 35 | if vpa.volthaClient == nil { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 36 | logger.Fatal(ctx, "no-voltha-connection") |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 37 | return |
| 38 | } |
| 39 | opt := grpc.EmptyCallOption{} |
| 40 | streamCtx, streamDone := context.WithCancel(context.Background()) |
| 41 | defer streamDone() |
| 42 | stream, err := vpa.volthaClient.Get().ReceivePacketsIn(streamCtx, &empty.Empty{}, opt) |
| 43 | if err != nil { |
| 44 | logger.Errorw(ctx, "Unable to establish Receive PacketIn Stream", |
| 45 | log.Fields{"error": err}) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | top: |
| 50 | |
| 51 | for { |
| 52 | select { |
| 53 | case <-ctx.Done(): |
| 54 | logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx}) |
| 55 | break top |
| 56 | default: |
| 57 | pkt, err := stream.Recv() |
| 58 | if err == io.EOF { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 59 | stream, err = vpa.volthaClient.Get().ReceivePacketsIn(streamCtx, &empty.Empty{}, opt) |
| 60 | if err != nil { |
| 61 | logger.Errorw(ctx, "Unable to establish Receive PacketIn Stream", |
| 62 | log.Fields{"error": err}) |
| 63 | return |
| 64 | } |
| 65 | continue |
| 66 | } |
| 67 | |
| 68 | if isConnCanceled(err) { |
| 69 | logger.Errorw(ctx, "error receiving packet", |
| 70 | log.Fields{"error": err}) |
| 71 | break top |
| 72 | } else if err != nil { |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 73 | logger.Warnw(ctx, "Ignoring unhandled error", log.Fields{"err": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 74 | continue |
| 75 | } |
| 76 | vpa.packetInChannel <- pkt |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (vpa *VPAgent) handlePacketsIn(ctx context.Context) { |
| 82 | logger.Debug(ctx, "handle-packets-in-started") |
| 83 | top: |
| 84 | for { |
| 85 | select { |
| 86 | case <-ctx.Done(): |
| 87 | logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx}) |
| 88 | break top |
| 89 | case packet := <-vpa.packetInChannel: |
| 90 | if vpc := vpa.getVPClient(packet.Id); vpc != nil { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 91 | vpc.PacketIn(ctx, packet) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | logger.Debug(ctx, "handle-packets-in-finished") |
| 96 | } |