OpenCores
URL https://opencores.org/ocsvn/udp_ip_stack/udp_ip_stack/trunk

Subversion Repositories udp_ip_stack

[/] [udp_ip_stack/] [trunk/] [contrib/] [from_cern/] [IPv4_RX.vhd] - Blame information for rev 35

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 35 pjf
----------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer:            Peter Fall
4
-- 
5
-- Create Date:    16:20:42 06/01/2011 
6
-- Design Name: 
7
-- Module Name:    IPv4_RX - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--              handle simple IP RX
13
--              doesnt handle reassembly
14
--              checks and filters for IP protocol
15
--              checks and filters for IP addr
16
--              Handle IPv4 protocol
17
-- Dependencies: 
18
--
19
-- Revision: 
20
-- Revision 0.01 - File Created
21
-- Revision 0.02 - Improved error handling
22
-- Revision 0.03 - Added handling of broadcast address
23
-- Additional Comments: 
24
--
25
----------------------------------------------------------------------------------
26
library IEEE;
27
use IEEE.STD_LOGIC_1164.all;
28
use IEEE.NUMERIC_STD.all;
29
use work.axi.all;
30
use work.ipv4_types.all;
31
use work.arp_types.all;
32
 
33
entity IPv4_RX is
34
  port (
35
    -- IP Layer signals
36
    ip_rx             : out ipv4_rx_type;
37
    ip_rx_start       : out std_logic;  -- indicates receipt of ip frame.
38
    -- system signals
39
    clk               : in  std_logic;  -- same clock used to clock mac data and ip data
40
    reset             : in  std_logic;
41
    our_ip_address    : in  std_logic_vector (31 downto 0);
42
    rx_pkt_count      : out std_logic_vector(7 downto 0);   -- number of IP pkts received for us
43
    -- MAC layer RX signals
44
    mac_data_in       : in  std_logic_vector (7 downto 0);  -- ethernet frame (from dst mac addr through to last byte of frame)
45
    mac_data_in_valid : in  std_logic;  -- indicates data_in valid on clock
46
    mac_data_in_last  : in  std_logic   -- indicates last data in frame
47
    );
48
end IPv4_RX;
49
 
50
architecture Behavioral of IPv4_RX is
51
 
52
  type rx_state_type is (IDLE, ETH_HDR, IP_HDR, USER_DATA, WAIT_END, ERR);
53
 
54
  type rx_event_type is (NO_EVENT, DATA);
55
  type count_mode_type is (RST, INCR, HOLD);
56
  type settable_count_mode_type is (RST, INCR, SET_VAL, HOLD);
57
  type set_clr_type is (SET, CLR, HOLD);
58
 
59
 
60
  -- state variables
61
  signal rx_state         : rx_state_type;
62
  signal rx_count         : unsigned (15 downto 0);
63
  signal src_ip           : std_logic_vector (31 downto 0);  -- src IP captured from input
64
  signal dst_ip           : std_logic_vector (23 downto 0);  -- 1st 3 bytes of dst IP captured from input
65
  signal is_broadcast_reg : std_logic;
66
  signal protocol         : std_logic_vector (7 downto 0);   -- src protocol captured from input
67
  signal data_len         : std_logic_vector (15 downto 0);  -- src data length captured from input
68
  signal ip_rx_start_reg  : std_logic;  -- indicates start of user data
69
  signal hdr_valid_reg    : std_logic;  -- indicates that hdr data is valid
70
  signal frame_err_cnt    : unsigned (7 downto 0);  -- number of frame errors
71
  signal error_code_reg   : std_logic_vector (3 downto 0);
72
  signal rx_pkt_counter   : unsigned (7 downto 0);  -- number of rx frames received for us
73
 
74
  -- rx control signals
75
  signal next_rx_state     : rx_state_type;
76
  signal set_rx_state      : std_logic;
77
  signal rx_event          : rx_event_type;
78
  signal rx_count_mode     : settable_count_mode_type;
79
  signal set_dst_ip3       : std_logic;
80
  signal set_dst_ip2       : std_logic;
81
  signal set_dst_ip1       : std_logic;
82
  signal set_ip3           : std_logic;
83
  signal set_ip2           : std_logic;
84
  signal set_ip1           : std_logic;
85
  signal set_ip0           : std_logic;
86
  signal set_protocol      : std_logic;
87
  signal set_len_H         : std_logic;
88
  signal set_len_L         : std_logic;
89
  signal set_ip_rx_start   : set_clr_type;
90
  signal set_hdr_valid     : set_clr_type;
91
  signal set_frame_err_cnt : count_mode_type;
92
  signal dataval           : std_logic_vector (7 downto 0);
93
  signal rx_count_val      : unsigned (15 downto 0);
94
  signal set_error_code    : std_logic;
95
  signal error_code_val    : std_logic_vector (3 downto 0);
96
  signal set_pkt_cnt       : count_mode_type;
97
  signal set_data_last     : std_logic;
98
  signal dst_ip_rx         : std_logic_vector (31 downto 0);
99
  signal set_is_broadcast  : set_clr_type;
100
 
101
 
102
-- IP datagram header format
103
--
104
--      0          4          8                      16      19             24                    31
105
--      --------------------------------------------------------------------------------------------
106
--      | Version  | *Header  |    Service Type      |        Total Length including header        |
107
--      |   (4)    |  Length  |     (ignored)        |                 (in bytes)                  |
108
--      --------------------------------------------------------------------------------------------
109
--      |           Identification                   | Flags |       Fragment Offset               |
110
--      |                                            |       |      (in 32 bit words)              |
111
--      --------------------------------------------------------------------------------------------
112
--      |    Time To Live     |       Protocol       |             Header Checksum                 |
113
--      |     (ignored)       |                      |                                             |
114
--      --------------------------------------------------------------------------------------------
115
--      |                                   Source IP Address                                      |
116
--      |                                                                                          |
117
--      --------------------------------------------------------------------------------------------
118
--      |                                 Destination IP Address                                   |
119
--      |                                                                                          |
120
--      --------------------------------------------------------------------------------------------
121
--      |                          Options (if any - ignored)               |       Padding        |
122
--      |                                                                   |      (if needed)     |
123
--      --------------------------------------------------------------------------------------------
124
--      |                                          Data                                            |
125
--      |                                                                                          |
126
--      --------------------------------------------------------------------------------------------
127
--      |                                          ....                                            |
128
--      |                                                                                          |
129
--      --------------------------------------------------------------------------------------------
130
--
131
-- * - in 32 bit words 
132
 
133
begin
134
 
135
  -----------------------------------------------------------------------
136
  -- combinatorial process to implement FSM and determine control signals
137
  -----------------------------------------------------------------------
138
 
139
  rx_combinatorial : process (
140
    -- input signals
141
    mac_data_in, mac_data_in_valid, mac_data_in_last, our_ip_address,
142
    -- state variables
143
    rx_state, rx_count, src_ip, dst_ip, protocol, data_len, ip_rx_start_reg, hdr_valid_reg,
144
    frame_err_cnt, error_code_reg, rx_pkt_counter, is_broadcast_reg,
145
    -- control signals
146
    next_rx_state, set_rx_state, rx_event, rx_count_mode,
147
    set_ip3, set_ip2, set_ip1, set_ip0, set_protocol, set_len_H, set_len_L,
148
    set_dst_ip3, set_dst_ip2, set_dst_ip1,
149
    set_ip_rx_start, set_hdr_valid, set_frame_err_cnt, dataval, rx_count_val,
150
    set_error_code, error_code_val, set_pkt_cnt, set_data_last, dst_ip_rx, set_is_broadcast
151
    )
152
  begin
153
    -- set output followers
154
    ip_rx_start                <= ip_rx_start_reg;
155
    ip_rx.hdr.is_valid         <= hdr_valid_reg;
156
    ip_rx.hdr.protocol         <= protocol;
157
    ip_rx.hdr.data_length      <= data_len;
158
    ip_rx.hdr.src_ip_addr      <= src_ip;
159
    ip_rx.hdr.num_frame_errors <= std_logic_vector(frame_err_cnt);
160
    ip_rx.hdr.last_error_code  <= error_code_reg;
161
    ip_rx.hdr.is_broadcast     <= is_broadcast_reg;
162
    rx_pkt_count               <= std_logic_vector(rx_pkt_counter);
163
 
164
    -- transfer data upstream if in user data phase
165
    if rx_state = USER_DATA then
166
      ip_rx.data.data_in       <= mac_data_in;
167
      ip_rx.data.data_in_valid <= mac_data_in_valid;
168
      ip_rx.data.data_in_last  <= set_data_last;
169
    else
170
      ip_rx.data.data_in       <= (others => '0');
171
      ip_rx.data.data_in_valid <= '0';
172
      ip_rx.data.data_in_last  <= '0';
173
    end if;
174
 
175
    -- set signal defaults
176
    next_rx_state     <= IDLE;
177
    set_rx_state      <= '0';
178
    rx_event          <= NO_EVENT;
179
    rx_count_mode     <= HOLD;
180
    set_ip3           <= '0';
181
    set_ip2           <= '0';
182
    set_ip1           <= '0';
183
    set_ip0           <= '0';
184
    set_dst_ip3       <= '0';
185
    set_dst_ip2       <= '0';
186
    set_dst_ip1       <= '0';
187
    set_protocol      <= '0';
188
    set_len_H         <= '0';
189
    set_len_L         <= '0';
190
    set_ip_rx_start   <= HOLD;
191
    set_hdr_valid     <= HOLD;
192
    set_frame_err_cnt <= HOLD;
193
    rx_count_val      <= x"0000";
194
    set_error_code    <= '0';
195
    error_code_val    <= RX_EC_NONE;
196
    set_pkt_cnt       <= HOLD;
197
    dataval           <= (others => '0');
198
    set_data_last     <= '0';
199
    dst_ip_rx         <= (others => '0');
200
    set_is_broadcast  <= HOLD;
201
 
202
    -- determine event (if any)
203
    if mac_data_in_valid = '1' then
204
      rx_event <= DATA;
205
      dataval  <= mac_data_in;
206
    end if;
207
 
208
    -- RX FSM
209
    case rx_state is
210
      when IDLE =>
211
        rx_count_mode <= RST;
212
        case rx_event is
213
          when NO_EVENT =>              -- (nothing to do)
214
          when DATA =>
215
            rx_count_mode <= INCR;
216
            set_hdr_valid <= CLR;
217
            next_rx_state <= ETH_HDR;
218
            set_rx_state  <= '1';
219
        end case;
220
 
221
      when ETH_HDR =>
222
        case rx_event is
223
          when NO_EVENT =>                      -- (nothing to do)
224
          when DATA =>
225
            if rx_count = x"000d" then
226
              rx_count_mode <= RST;
227
              next_rx_state <= IP_HDR;
228
              set_rx_state  <= '1';
229
            else
230
              rx_count_mode <= INCR;
231
            end if;
232
                                                -- handle early frame termination
233
            if mac_data_in_last = '1' then
234
              error_code_val    <= RX_EC_ET_ETH;
235
              set_error_code    <= '1';
236
              set_frame_err_cnt <= INCR;
237
              set_ip_rx_start   <= CLR;
238
              set_data_last     <= '1';
239
              next_rx_state     <= IDLE;
240
              rx_count_mode     <= RST;
241
              set_rx_state      <= '1';
242
            else
243
              case rx_count is
244
                when x"000c" =>
245
                  if mac_data_in /= x"08" then  -- ignore pkts that are not type=IP
246
                    next_rx_state <= WAIT_END;
247
                    set_rx_state  <= '1';
248
                  end if;
249
 
250
                when x"000d" =>
251
                  if mac_data_in /= x"00" then  -- ignore pkts that are not type=IP
252
                    next_rx_state <= WAIT_END;
253
                    set_rx_state  <= '1';
254
                  end if;
255
 
256
                when others =>          -- ignore other bytes in eth header
257
              end case;
258
            end if;
259
        end case;
260
 
261
      when IP_HDR =>
262
        case rx_event is
263
          when NO_EVENT =>              -- (nothing to do)
264
          when DATA =>
265
            if rx_count = x"0013" then
266
              rx_count_val  <= x"0001";         -- start counter at 1
267
              rx_count_mode <= SET_VAL;
268
            else
269
              rx_count_mode <= INCR;
270
            end if;
271
                                        -- handle early frame termination
272
            if mac_data_in_last = '1' then
273
              error_code_val    <= RX_EC_ET_IP;
274
              set_error_code    <= '1';
275
              set_frame_err_cnt <= INCR;
276
              set_ip_rx_start   <= CLR;
277
              set_data_last     <= '1';
278
              next_rx_state     <= IDLE;
279
              rx_count_mode     <= RST;
280
              set_rx_state      <= '1';
281
            else
282
              case rx_count is
283
                when x"0000" =>
284
                  if mac_data_in /= x"45" then  -- ignore pkts that are not v4 with 5 header words
285
                    next_rx_state <= WAIT_END;
286
                    set_rx_state  <= '1';
287
                  end if;
288
 
289
                when x"0002" => set_len_H <= '1';
290
                when x"0003" => set_len_L <= '1';
291
 
292
                when x"0006" =>
293
                  if (mac_data_in(7) = '1') or (mac_data_in (4 downto 0) /= "00000") then
294
                                        -- ignore pkts that require reassembly (MF=1 or frag offst /= 0)
295
                    next_rx_state <= WAIT_END;
296
                    set_rx_state  <= '1';
297
                  end if;
298
 
299
                when x"0007" =>
300
                  if mac_data_in /= x"00" then  -- ignore pkts that require reassembly (frag offst /= 0)
301
                    next_rx_state <= WAIT_END;
302
                    set_rx_state  <= '1';
303
                  end if;
304
 
305
                when x"0009" => set_protocol <= '1';
306
 
307
                when x"000c" => set_ip3 <= '1';
308
                when x"000d" => set_ip2 <= '1';
309
                when x"000e" => set_ip1 <= '1';
310
                when x"000f" => set_ip0 <= '1';
311
 
312
                when x"0010" => set_dst_ip3 <= '1';
313
                  if ((mac_data_in /= our_ip_address(31 downto 24)) and
314
                      (mac_data_in /= IP_BC_ADDR(31 downto 24)))then  -- ignore pkts that are not addressed to us
315
                    next_rx_state <= WAIT_END;
316
                    set_rx_state  <= '1';
317
                  end if;
318
                when x"0011" => set_dst_ip2 <= '1';
319
                  if ((mac_data_in /= our_ip_address(23 downto 16)) and
320
                      (mac_data_in /= IP_BC_ADDR(23 downto 16)))then  -- ignore pkts that are not addressed to us
321
                    next_rx_state <= WAIT_END;
322
                    set_rx_state  <= '1';
323
                  end if;
324
                when x"0012" => set_dst_ip1 <= '1';
325
                  if ((mac_data_in /= our_ip_address(15 downto 8)) and
326
                      (mac_data_in /= IP_BC_ADDR(15 downto 8)))then  -- ignore pkts that are not addressed to us
327
                    next_rx_state <= WAIT_END;
328
                    set_rx_state  <= '1';
329
                  end if;
330
 
331
                when x"0013" =>
332
                  if ((mac_data_in /= our_ip_address(7 downto 0)) and
333
                      (mac_data_in /= IP_BC_ADDR(7 downto 0)))then  -- ignore pkts that are not addressed to us
334
                    next_rx_state <= WAIT_END;
335
                    set_rx_state  <= '1';
336
                  else
337
                    next_rx_state   <= USER_DATA;
338
                    set_pkt_cnt     <= INCR;                         -- count another pkt
339
                    set_rx_state    <= '1';
340
                    set_ip_rx_start <= SET;
341
                  end if;
342
 
343
                                        -- now have the dst IP addr
344
                  dst_ip_rx <= dst_ip & mac_data_in;
345
                  if dst_ip_rx = IP_BC_ADDR then
346
                    set_is_broadcast <= SET;
347
                  else
348
                    set_is_broadcast <= CLR;
349
                  end if;
350
                  set_hdr_valid <= SET;  -- header values are now valid, although the pkt may not be for us                                                                      
351
 
352
                  --if dst_ip_rx = our_ip_address or dst_ip_rx = IP_BC_ADDR then
353
                  --  next_rx_state   <= USER_DATA;
354
                  --  set_pkt_cnt     <= INCR;  -- count another pkt received
355
                  --  set_rx_state    <= '1';
356
                  --  set_ip_rx_start <= SET;
357
                  --else
358
                  --  next_rx_state <= WAIT_END;
359
                  --  set_rx_state  <= '1';
360
                  --end if;
361
 
362
                when others =>  -- ignore other bytes in ip header                                                                               
363
              end case;
364
            end if;
365
        end case;
366
 
367
      when USER_DATA =>
368
        case rx_event is
369
          when NO_EVENT =>              -- (nothing to do)
370
          when DATA =>
371
                                        -- note: data gets transfered upstream as part of "output followers" processing
372
            if rx_count = unsigned(data_len) then
373
              set_ip_rx_start <= CLR;
374
              rx_count_mode   <= RST;
375
              set_data_last   <= '1';
376
              if mac_data_in_last = '1' then
377
                next_rx_state   <= IDLE;
378
                rx_count_mode   <= RST;
379
                set_ip_rx_start <= CLR;
380
              else
381
                next_rx_state <= WAIT_END;
382
              end if;
383
              set_rx_state <= '1';
384
            else
385
              rx_count_mode <= INCR;
386
                                        -- check for early frame termination
387
              if mac_data_in_last = '1' then
388
                error_code_val    <= RX_EC_ET_USER;
389
                set_error_code    <= '1';
390
                set_frame_err_cnt <= INCR;
391
                set_ip_rx_start   <= CLR;
392
                next_rx_state     <= IDLE;
393
                rx_count_mode     <= RST;
394
                set_rx_state      <= '1';
395
              end if;
396
            end if;
397
        end case;
398
 
399
      when ERR =>
400
        set_frame_err_cnt <= INCR;
401
        set_ip_rx_start   <= CLR;
402
        if mac_data_in_last = '0' then
403
          set_data_last <= '1';
404
          next_rx_state <= WAIT_END;
405
          set_rx_state  <= '1';
406
        else
407
          next_rx_state <= IDLE;
408
          rx_count_mode <= RST;
409
          set_rx_state  <= '1';
410
        end if;
411
 
412
 
413
      when WAIT_END =>
414
        case rx_event is
415
          when NO_EVENT =>              -- (nothing to do)
416
          when DATA =>
417
            if mac_data_in_last = '1' then
418
              set_data_last   <= '1';
419
              next_rx_state   <= IDLE;
420
              rx_count_mode   <= RST;
421
              set_rx_state    <= '1';
422
              set_ip_rx_start <= CLR;
423
            end if;
424
        end case;
425
 
426
    end case;
427
 
428
  end process;
429
 
430
 
431
  -----------------------------------------------------------------------------
432
  -- sequential process to action control signals and change states and outputs
433
  -----------------------------------------------------------------------------
434
 
435
  rx_sequential : process (clk)--, reset)
436
  begin
437
    if rising_edge(clk) then
438
      if reset = '1' then
439
        -- reset state variables
440
        rx_state         <= IDLE;
441
        rx_count         <= x"0000";
442
        src_ip           <= (others => '0');
443
        dst_ip           <= (others => '0');
444
        protocol         <= (others => '0');
445
        data_len         <= (others => '0');
446
        ip_rx_start_reg  <= '0';
447
        hdr_valid_reg    <= '0';
448
        is_broadcast_reg <= '0';
449
        frame_err_cnt    <= (others => '0');
450
        error_code_reg   <= RX_EC_NONE;
451
        rx_pkt_counter   <= x"00";
452
 
453
      else
454
        -- Next rx_state processing
455
        if set_rx_state = '1' then
456
          rx_state <= next_rx_state;
457
        else
458
          rx_state <= rx_state;
459
        end if;
460
 
461
        -- rx_count processing
462
        case rx_count_mode is
463
          when RST     => rx_count <= x"0000";
464
          when INCR    => rx_count <= rx_count + 1;
465
          when SET_VAL => rx_count <= rx_count_val;
466
          when HOLD    => rx_count <= rx_count;
467
        end case;
468
 
469
        -- frame error count processing
470
        case set_frame_err_cnt is
471
          when RST  => frame_err_cnt <= x"00";
472
          when INCR => frame_err_cnt <= frame_err_cnt + 1;
473
          when HOLD => frame_err_cnt <= frame_err_cnt;
474
        end case;
475
 
476
        -- ip pkt processing
477
        case set_pkt_cnt is
478
          when RST  => rx_pkt_counter <= x"00";
479
          when INCR => rx_pkt_counter <= rx_pkt_counter + 1;
480
          when HOLD => rx_pkt_counter <= rx_pkt_counter;
481
        end case;
482
 
483
        -- source ip capture
484
        if (set_ip3 = '1') then src_ip(31 downto 24) <= dataval; end if;
485
        if (set_ip2 = '1') then src_ip(23 downto 16) <= dataval; end if;
486
        if (set_ip1 = '1') then src_ip(15 downto 8)  <= dataval; end if;
487
        if (set_ip0 = '1') then src_ip(7 downto 0)   <= dataval; end if;
488
 
489
        -- dst ip capture
490
        if (set_dst_ip3 = '1') then dst_ip(23 downto 16) <= dataval; end if;
491
        if (set_dst_ip2 = '1') then dst_ip(15 downto 8)  <= dataval; end if;
492
        if (set_dst_ip1 = '1') then dst_ip(7 downto 0)   <= dataval; end if;
493
 
494
        if (set_protocol = '1') then
495
          protocol <= dataval;
496
        else
497
          protocol <= protocol;
498
        end if;
499
 
500
        if (set_len_H = '1') then
501
          data_len (15 downto 8) <= dataval;
502
          data_len (7 downto 0)  <= x"00";
503
        elsif (set_len_L = '1') then
504
                                        -- compute data length, taking into account that we need to subtract the header length
505
          data_len <= std_logic_vector(unsigned(data_len(15 downto 8) & dataval) - 20);
506
        else
507
          data_len <= data_len;
508
        end if;
509
 
510
        case set_ip_rx_start is
511
          when SET  => ip_rx_start_reg <= '1';
512
          when CLR  => ip_rx_start_reg <= '0';
513
          when HOLD => ip_rx_start_reg <= ip_rx_start_reg;
514
        end case;
515
 
516
        case set_is_broadcast is
517
          when SET  => is_broadcast_reg <= '1';
518
          when CLR  => is_broadcast_reg <= '0';
519
          when HOLD => is_broadcast_reg <= is_broadcast_reg;
520
        end case;
521
 
522
        case set_hdr_valid is
523
          when SET  => hdr_valid_reg <= '1';
524
          when CLR  => hdr_valid_reg <= '0';
525
          when HOLD => hdr_valid_reg <= hdr_valid_reg;
526
        end case;
527
 
528
        -- set error code
529
        if set_error_code = '1' then
530
          error_code_reg <= error_code_val;
531
        else
532
          error_code_reg <= error_code_reg;
533
        end if;
534
      end if;
535
    end if;
536
  end process;
537
 
538
end Behavioral;
539
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.