1
0
mirror of https://github.com/corundum/corundum.git synced 2025-01-16 08:12:53 +08:00
corundum/rtl/arp.v

417 lines
16 KiB
Coq
Raw Normal View History

2014-11-20 22:55:28 -08:00
/*
2018-02-26 12:50:51 -08:00
Copyright (c) 2014-2018 Alex Forencich
2014-11-20 22:55:28 -08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Language: Verilog 2001
`timescale 1ns / 1ps
/*
* ARP block for IPv4, ethernet frame interface
*/
module arp #(
2018-06-18 13:59:58 -07:00
parameter CACHE_ADDR_WIDTH = 9,
2014-11-20 22:55:28 -08:00
parameter REQUEST_RETRY_COUNT = 4,
parameter REQUEST_RETRY_INTERVAL = 125000000*2,
parameter REQUEST_TIMEOUT = 125000000*30
)
(
input wire clk,
input wire rst,
/*
* Ethernet frame input
*/
input wire input_eth_hdr_valid,
output wire input_eth_hdr_ready,
input wire [47:0] input_eth_dest_mac,
input wire [47:0] input_eth_src_mac,
input wire [15:0] input_eth_type,
input wire [7:0] input_eth_payload_tdata,
input wire input_eth_payload_tvalid,
output wire input_eth_payload_tready,
input wire input_eth_payload_tlast,
input wire input_eth_payload_tuser,
/*
* Ethernet frame output
*/
output wire output_eth_hdr_valid,
input wire output_eth_hdr_ready,
output wire [47:0] output_eth_dest_mac,
output wire [47:0] output_eth_src_mac,
output wire [15:0] output_eth_type,
output wire [7:0] output_eth_payload_tdata,
output wire output_eth_payload_tvalid,
input wire output_eth_payload_tready,
output wire output_eth_payload_tlast,
output wire output_eth_payload_tuser,
/*
* ARP requests
*/
input wire arp_request_valid,
2018-06-18 13:59:58 -07:00
output wire arp_request_ready,
2014-11-20 22:55:28 -08:00
input wire [31:0] arp_request_ip,
output wire arp_response_valid,
2018-06-18 13:59:58 -07:00
input wire arp_response_ready,
2014-11-20 22:55:28 -08:00
output wire arp_response_error,
output wire [47:0] arp_response_mac,
/*
* Configuration
*/
input wire [47:0] local_mac,
input wire [31:0] local_ip,
input wire [31:0] gateway_ip,
input wire [31:0] subnet_mask,
input wire clear_cache
);
localparam [15:0]
ARP_OPER_ARP_REQUEST = 16'h0001,
ARP_OPER_ARP_REPLY = 16'h0002,
ARP_OPER_INARP_REQUEST = 16'h0008,
ARP_OPER_INARP_REPLY = 16'h0009;
wire incoming_frame_valid;
2018-06-18 13:59:58 -07:00
reg incoming_frame_ready;
2014-11-20 22:55:28 -08:00
wire [47:0] incoming_eth_dest_mac;
wire [47:0] incoming_eth_src_mac;
wire [15:0] incoming_eth_type;
wire [15:0] incoming_arp_htype;
wire [15:0] incoming_arp_ptype;
wire [7:0] incoming_arp_hlen;
wire [7:0] incoming_arp_plen;
wire [15:0] incoming_arp_oper;
wire [47:0] incoming_arp_sha;
wire [31:0] incoming_arp_spa;
wire [47:0] incoming_arp_tha;
wire [31:0] incoming_arp_tpa;
/*
* ARP frame processing
*/
arp_eth_rx
arp_eth_rx_inst (
.clk(clk),
.rst(rst),
// Ethernet frame input
.input_eth_hdr_valid(input_eth_hdr_valid),
.input_eth_hdr_ready(input_eth_hdr_ready),
.input_eth_dest_mac(input_eth_dest_mac),
.input_eth_src_mac(input_eth_src_mac),
.input_eth_type(input_eth_type),
.input_eth_payload_tdata(input_eth_payload_tdata),
.input_eth_payload_tvalid(input_eth_payload_tvalid),
.input_eth_payload_tready(input_eth_payload_tready),
.input_eth_payload_tlast(input_eth_payload_tlast),
.input_eth_payload_tuser(input_eth_payload_tuser),
// ARP frame output
.output_frame_valid(incoming_frame_valid),
.output_frame_ready(incoming_frame_ready),
.output_eth_dest_mac(incoming_eth_dest_mac),
.output_eth_src_mac(incoming_eth_src_mac),
.output_eth_type(incoming_eth_type),
.output_arp_htype(incoming_arp_htype),
.output_arp_ptype(incoming_arp_ptype),
.output_arp_hlen(incoming_arp_hlen),
.output_arp_plen(incoming_arp_plen),
.output_arp_oper(incoming_arp_oper),
.output_arp_sha(incoming_arp_sha),
.output_arp_spa(incoming_arp_spa),
.output_arp_tha(incoming_arp_tha),
.output_arp_tpa(incoming_arp_tpa),
// Status signals
.busy(),
.error_header_early_termination(),
.error_invalid_header()
);
2018-06-18 13:59:58 -07:00
reg outgoing_frame_valid_reg = 1'b0, outgoing_frame_valid_next;
wire outgoing_frame_ready;
reg [47:0] outgoing_eth_dest_mac_reg = 48'd0, outgoing_eth_dest_mac_next;
reg [15:0] outgoing_arp_oper_reg = 16'd0, outgoing_arp_oper_next;
reg [47:0] outgoing_arp_tha_reg = 48'd0, outgoing_arp_tha_next;
reg [31:0] outgoing_arp_tpa_reg = 32'd0, outgoing_arp_tpa_next;
2014-11-20 22:55:28 -08:00
arp_eth_tx
arp_eth_tx_inst (
.clk(clk),
.rst(rst),
// ARP frame input
.input_frame_valid(outgoing_frame_valid_reg),
.input_frame_ready(outgoing_frame_ready),
.input_eth_dest_mac(outgoing_eth_dest_mac_reg),
.input_eth_src_mac(local_mac),
.input_eth_type(16'h0806),
.input_arp_htype(16'h0001),
.input_arp_ptype(16'h0800),
.input_arp_oper(outgoing_arp_oper_reg),
.input_arp_sha(local_mac),
.input_arp_spa(local_ip),
.input_arp_tha(outgoing_arp_tha_reg),
.input_arp_tpa(outgoing_arp_tpa_reg),
// Ethernet frame output
.output_eth_hdr_valid(output_eth_hdr_valid),
.output_eth_hdr_ready(output_eth_hdr_ready),
.output_eth_dest_mac(output_eth_dest_mac),
.output_eth_src_mac(output_eth_src_mac),
.output_eth_type(output_eth_type),
.output_eth_payload_tdata(output_eth_payload_tdata),
.output_eth_payload_tvalid(output_eth_payload_tvalid),
.output_eth_payload_tready(output_eth_payload_tready),
.output_eth_payload_tlast(output_eth_payload_tlast),
.output_eth_payload_tuser(output_eth_payload_tuser),
// Status signals
.busy()
);
2018-06-18 13:59:58 -07:00
reg cache_query_request_valid_reg = 1'b0, cache_query_request_valid_next;
reg [31:0] cache_query_request_ip_reg = 32'd0, cache_query_request_ip_next;
2014-11-20 22:55:28 -08:00
wire cache_query_response_valid;
wire cache_query_response_error;
wire [47:0] cache_query_response_mac;
reg cache_write_request_valid_reg = 1'b0, cache_write_request_valid_next;
reg [31:0] cache_write_request_ip_reg = 32'd0, cache_write_request_ip_next;
reg [47:0] cache_write_request_mac_reg = 48'd0, cache_write_request_mac_next;
2014-11-20 22:55:28 -08:00
/*
* ARP cache
*/
arp_cache #(
.CACHE_ADDR_WIDTH(CACHE_ADDR_WIDTH)
)
arp_cache_inst (
.clk(clk),
.rst(rst),
// Query cache
2018-06-18 13:59:58 -07:00
.query_request_valid(cache_query_request_valid_reg),
.query_request_ready(),
.query_request_ip(cache_query_request_ip_reg),
2014-11-20 22:55:28 -08:00
.query_response_valid(cache_query_response_valid),
2018-06-18 13:59:58 -07:00
.query_response_ready(1'b1),
2014-11-20 22:55:28 -08:00
.query_response_error(cache_query_response_error),
.query_response_mac(cache_query_response_mac),
// Write cache
.write_request_valid(cache_write_request_valid_reg),
2018-06-18 13:59:58 -07:00
.write_request_ready(),
2014-11-20 22:55:28 -08:00
.write_request_ip(cache_write_request_ip_reg),
.write_request_mac(cache_write_request_mac_reg),
// Configuration
.clear_cache(clear_cache)
);
reg arp_request_operation_reg = 1'b0, arp_request_operation_next;
2014-11-20 22:55:28 -08:00
2018-06-18 13:59:58 -07:00
reg arp_request_ready_reg = 1'b0, arp_request_ready_next;
reg [31:0] arp_request_ip_reg = 32'd0, arp_request_ip_next;
2014-11-20 22:55:28 -08:00
2018-06-18 13:59:58 -07:00
reg arp_response_valid_reg = 1'b0, arp_response_valid_next;
reg arp_response_error_reg = 1'b0, arp_response_error_next;
2018-06-18 13:59:58 -07:00
reg [47:0] arp_response_mac_reg = 48'd0, arp_response_mac_next;
2014-11-20 22:55:28 -08:00
reg [5:0] arp_request_retry_cnt_reg = 6'd0, arp_request_retry_cnt_next;
reg [35:0] arp_request_timer_reg = 36'd0, arp_request_timer_next;
2014-11-20 22:55:28 -08:00
2018-06-18 13:59:58 -07:00
assign arp_request_ready = arp_request_ready_reg;
2014-11-20 22:55:28 -08:00
2018-06-18 13:59:58 -07:00
assign arp_response_valid = arp_response_valid_reg;
2014-11-20 22:55:28 -08:00
assign arp_response_error = arp_response_error_reg;
2018-06-18 13:59:58 -07:00
assign arp_response_mac = arp_response_mac_reg;
2014-11-20 22:55:28 -08:00
always @* begin
2018-06-18 13:59:58 -07:00
incoming_frame_ready = 1'b0;
2014-11-20 22:55:28 -08:00
outgoing_frame_valid_next = outgoing_frame_valid_reg & ~outgoing_frame_ready;
outgoing_eth_dest_mac_next = outgoing_eth_dest_mac_reg;
outgoing_arp_oper_next = outgoing_arp_oper_reg;
outgoing_arp_tha_next = outgoing_arp_tha_reg;
outgoing_arp_tpa_next = outgoing_arp_tpa_reg;
2018-06-18 13:59:58 -07:00
cache_query_request_valid_next = 1'b0;
cache_query_request_ip_next = cache_query_request_ip_reg;
cache_write_request_valid_next = 1'b0;
2018-06-18 13:59:58 -07:00
cache_write_request_mac_next = cache_write_request_mac_reg;
cache_write_request_ip_next = cache_write_request_ip_reg;
2014-11-20 22:55:28 -08:00
2018-06-18 13:59:58 -07:00
arp_request_ready_next = 1'b0;
2014-11-20 22:55:28 -08:00
arp_request_ip_next = arp_request_ip_reg;
arp_request_operation_next = arp_request_operation_reg;
arp_request_retry_cnt_next = arp_request_retry_cnt_reg;
arp_request_timer_next = arp_request_timer_reg;
2018-06-18 13:59:58 -07:00
arp_response_valid_next = arp_response_valid_reg & ~arp_response_ready;
arp_response_error_next = 1'b0;
2018-06-18 13:59:58 -07:00
arp_response_mac_next = 48'd0;
2014-11-20 22:55:28 -08:00
// manage incoming frames
2018-06-18 13:59:58 -07:00
incoming_frame_ready = outgoing_frame_ready;
if (incoming_frame_valid & incoming_frame_ready) begin
if (incoming_eth_type == 16'h0806 && incoming_arp_htype == 16'h0001 && incoming_arp_ptype == 16'h0800) begin
// store sender addresses in cache
cache_write_request_valid_next = 1'b1;
cache_write_request_ip_next = incoming_arp_spa;
cache_write_request_mac_next = incoming_arp_sha;
if (incoming_arp_oper == ARP_OPER_ARP_REQUEST) begin
// ARP request
if (incoming_arp_tpa == local_ip) begin
// send reply frame to valid incoming request
outgoing_frame_valid_next = 1'b1;
outgoing_eth_dest_mac_next = incoming_eth_src_mac;
outgoing_arp_oper_next = ARP_OPER_ARP_REPLY;
outgoing_arp_tha_next = incoming_arp_sha;
outgoing_arp_tpa_next = incoming_arp_spa;
end
end else if (incoming_arp_oper == ARP_OPER_INARP_REPLY) begin
// INARP request
if (incoming_arp_tha == local_mac) begin
// send reply frame to valid incoming request
outgoing_frame_valid_next = 1'b1;
outgoing_eth_dest_mac_next = incoming_eth_src_mac;
outgoing_arp_oper_next = ARP_OPER_INARP_REPLY;
outgoing_arp_tha_next = incoming_arp_sha;
outgoing_arp_tpa_next = incoming_arp_spa;
end
2014-11-20 22:55:28 -08:00
end
end
end
// manage ARP lookup requests
2018-06-18 13:59:58 -07:00
if (arp_request_operation_reg) begin
arp_request_ready_next = 1'b0;
cache_query_request_valid_next = 1'b1;
2014-11-20 22:55:28 -08:00
arp_request_timer_next = arp_request_timer_reg - 1;
// if we got a response, it will go in the cache, so when the query succeds, we're done
2018-06-18 13:59:58 -07:00
if (cache_query_response_valid & ~cache_query_response_error) begin
arp_request_operation_next = 1'b0;
2018-06-18 13:59:58 -07:00
cache_query_request_valid_next = 1'b0;
arp_response_valid_next = 1'b1;
arp_response_error_next = 1'b0;
arp_response_mac_next = cache_query_response_mac;
2014-11-20 22:55:28 -08:00
end
// timer timeout
if (arp_request_timer_reg == 0) begin
if (arp_request_retry_cnt_reg > 0) begin
// have more retries
// send ARP request frame
outgoing_frame_valid_next = 1'b1;
2018-06-18 13:59:58 -07:00
outgoing_eth_dest_mac_next = 48'hffffffffffff;
2014-11-20 22:55:28 -08:00
outgoing_arp_oper_next = ARP_OPER_ARP_REQUEST;
2018-06-18 13:59:58 -07:00
outgoing_arp_tha_next = 48'h000000000000;
2014-11-20 22:55:28 -08:00
outgoing_arp_tpa_next = arp_request_ip_reg;
arp_request_retry_cnt_next = arp_request_retry_cnt_reg - 1;
if (arp_request_retry_cnt_reg > 1) begin
arp_request_timer_next = REQUEST_RETRY_INTERVAL;
end else begin
arp_request_timer_next = REQUEST_TIMEOUT;
end
end else begin
// out of retries
arp_request_operation_next = 1'b0;
2018-06-18 13:59:58 -07:00
arp_response_valid_next = 1'b1;
arp_response_error_next = 1'b1;
2018-06-18 13:59:58 -07:00
cache_query_request_valid_next = 1'b0;
end
end
end else begin
arp_request_ready_next = ~arp_response_valid_next;
if (cache_query_request_valid_reg) begin
cache_query_request_valid_next = 1'b1;
if (cache_query_response_valid) begin
if (cache_query_response_error) begin
arp_request_operation_next = 1'b1;
// send ARP request frame
outgoing_frame_valid_next = 1'b1;
outgoing_eth_dest_mac_next = 48'hffffffffffff;
outgoing_arp_oper_next = ARP_OPER_ARP_REQUEST;
outgoing_arp_tha_next = 48'h000000000000;
outgoing_arp_tpa_next = arp_request_ip_reg;
arp_request_retry_cnt_next = REQUEST_RETRY_COUNT-1;
arp_request_timer_next = REQUEST_RETRY_INTERVAL;
end else begin
cache_query_request_valid_next = 1'b0;
arp_response_valid_next = 1'b1;
arp_response_error_next = 1'b0;
arp_response_mac_next = cache_query_response_mac;
end
end
end else if (arp_request_valid & arp_request_ready) begin
if (~(arp_request_ip | subnet_mask) == 0) begin
// broadcast address
// (all bits in request IP set where subnet mask is clear)
arp_response_valid_next = 1'b1;
arp_response_error_next = 1'b0;
arp_response_mac_next = 48'hffffffffffff;
end else if (((arp_request_ip ^ gateway_ip) & subnet_mask) == 0) begin
// within subnet, look up IP directly
// (no bits differ between request IP and gateway IP where subnet mask is set)
cache_query_request_valid_next = 1'b1;
cache_query_request_ip_next = arp_request_ip;
arp_request_ip_next = arp_request_ip;
end else begin
// outside of subnet, so look up gateway address
cache_query_request_valid_next = 1'b1;
cache_query_request_ip_next = gateway_ip;
arp_request_ip_next = gateway_ip;
2014-11-20 22:55:28 -08:00
end
end
end
end
2015-10-09 22:36:58 -07:00
always @(posedge clk) begin
2014-11-20 22:55:28 -08:00
if (rst) begin
outgoing_frame_valid_reg <= 1'b0;
2018-06-18 13:59:58 -07:00
cache_query_request_valid_reg <= 1'b0;
cache_write_request_valid_reg <= 1'b0;
2018-06-18 13:59:58 -07:00
arp_request_ready_reg <= 1'b0;
arp_request_operation_reg <= 1'b0;
arp_request_retry_cnt_reg <= 6'd0;
arp_request_timer_reg <= 36'd0;
2018-06-18 13:59:58 -07:00
arp_response_valid_reg <= 1'b0;
2014-11-20 22:55:28 -08:00
end else begin
outgoing_frame_valid_reg <= outgoing_frame_valid_next;
2018-06-18 13:59:58 -07:00
cache_query_request_valid_reg <= cache_query_request_valid_next;
2014-11-20 22:55:28 -08:00
cache_write_request_valid_reg <= cache_write_request_valid_next;
2018-06-18 13:59:58 -07:00
arp_request_ready_reg <= arp_request_ready_next;
2014-11-20 22:55:28 -08:00
arp_request_operation_reg <= arp_request_operation_next;
arp_request_retry_cnt_reg <= arp_request_retry_cnt_next;
arp_request_timer_reg <= arp_request_timer_next;
2018-06-18 13:59:58 -07:00
arp_response_valid_reg <= arp_response_valid_next;
2014-11-20 22:55:28 -08:00
end
2018-06-18 13:59:58 -07:00
cache_query_request_ip_reg <= cache_query_request_ip_next;
outgoing_eth_dest_mac_reg <= outgoing_eth_dest_mac_next;
outgoing_arp_oper_reg <= outgoing_arp_oper_next;
outgoing_arp_tha_reg <= outgoing_arp_tha_next;
outgoing_arp_tpa_reg <= outgoing_arp_tpa_next;
cache_write_request_mac_reg <= cache_write_request_mac_next;
cache_write_request_ip_reg <= cache_write_request_ip_next;
arp_request_ip_reg <= arp_request_ip_next;
2018-06-18 13:59:58 -07:00
arp_response_error_reg <= arp_response_error_next;
arp_response_mac_reg <= arp_response_mac_next;
2014-11-20 22:55:28 -08:00
end
endmodule